Skip to content

Commit 06d9a88

Browse files
committed
Merge remote-tracking branch 'origin/main' into docs/include-module-folders-behavior
# Conflicts: # CHANGELOG.md
2 parents d4bec77 + 61c8e89 commit 06d9a88

7 files changed

Lines changed: 281 additions & 81 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,27 @@
22

33
## 2.13.1
44

5-
### Docs
6-
5+
### Documentation
6+
7+
- New [API token permissions](docs/troubleshooting.md#api-token-permissions) guide:
8+
the scopes `socketcli` needs, what happens without each one, and the API calls
9+
behind them.
10+
- Add `diff-scans:create` and `diff-scans:list` to CI tokens. Without them, scan
11+
comparisons fall back to a less resilient path with only a warning.
12+
- Documented how the baseline scan for a comparison is chosen.
13+
- Listed all four accepted token environment variables in priority order.
714
- `--include-module-folders` help and docs now say what the flag does: every
815
supported manifest under `node_modules`, `bower_components` or `jspm_packages`,
916
nested folders included, is added to the scan, and Socket resolves the
1017
dependencies those files declare. It does not list installed packages. The troubleshooting guide covers scanning
1118
packages installed outside the lockfile with a CycloneDX SBOM.
1219

20+
### Changed: bump the Socket Python SDK to 3.6.3
21+
22+
- Bumped the pinned Socket Python SDK (`socketdev`) from `3.6.2` to `3.6.3`.
23+
The release fixes `settings.integration_events()`, which the CLI does not
24+
call, so CLI behavior is unchanged.
25+
1326
## 2.13.0
1427

1528
### Upgrade notes

‎README.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ pip install socketsecurity
1818
export SOCKET_SECURITY_API_TOKEN="<token>"
1919
```
2020

21+
The token needs more than scan-creation access. Most missing scopes fail the run, and
22+
a token without the `diff-scans` scopes falls back with only a warning. See
23+
[`docs/troubleshooting.md#api-token-permissions`](https://github.com/SocketDev/socket-python-cli/blob/main/docs/troubleshooting.md#api-token-permissions).
24+
2125
### 3) Run a basic scan
2226

2327
```bash

‎docs/ci-cd.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -661,3 +661,12 @@ Prebuilt examples in this repo:
661661
the console table, including native Buildkite job logs, and in GitHub/GitLab
662662
security comments when that SCM adapter is configured. Findings without a known
663663
patched release leave the console cell blank and omit the comment field.
664+
- A token missing the `diff-scans:create` and `diff-scans:list` scopes does not fail
665+
the build. It logs
666+
`Diff scan comparison failed with APIAccessDenied(Insufficient permissions)` and falls
667+
back to a less resilient comparison path. This affects every run that compares
668+
scans: PR/MR events, and runs without an SCM integration. An SCM-integrated branch
669+
push creates a full scan and never compares. Tokens created from the nine-scope
670+
list in the CI/CD token setup guide lack both scopes; see
671+
[`troubleshooting.md#api-token-permissions`](troubleshooting.md#api-token-permissions)
672+
for the full scope list and what each missing scope does.

‎docs/cli-reference.md‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,22 @@ If you don't want to provide the Socket API Token every time then you can use th
232232
|:------------|:---------|:--------|:----------------------------------------------------------------------------------|
233233
| `--api-token` | False | | Socket Security API token (can also be set via SOCKET_SECURITY_API_TOKEN env var) |
234234
235+
The token is read from the first of these that is set, then from `--api-token`:
236+
237+
1. `SOCKET_SECURITY_API_KEY`
238+
2. `SOCKET_SECURITY_API_TOKEN`
239+
3. `SOCKET_API_KEY`
240+
4. `SOCKET_API_TOKEN`
241+
242+
This repository's examples use `SOCKET_SECURITY_API_TOKEN`, and the CI/CD guides on
243+
docs.socket.dev use `SOCKET_SECURITY_API_KEY`. Either works. If both are set,
244+
`SOCKET_SECURITY_API_KEY` wins.
245+
246+
> **Token permissions:** the CLI needs more than scan-creation access. Most missing
247+
> scopes fail the run, and a token without the `diff-scans` scopes falls back with only
248+
> a warning. See [`troubleshooting.md#api-token-permissions`](troubleshooting.md#api-token-permissions)
249+
> for the scopes to grant, the API calls a run makes, and which flags add to them.
250+
235251
#### Repository
236252
| Parameter | Required | Default | Description |
237253
|:-----------------|:---------|:--------|:------------------------------------------------------------------------------------------------------------------|
@@ -930,7 +946,7 @@ When `--enable-gitlab-security` (or `--enable-json` / `--enable-sarif`) is used
930946
### Requirements
931947
932948
- **GitLab Version**: GitLab 12.0 or later (for Security Dashboard support)
933-
- **Socket API Token**: Set via `$SOCKET_API_TOKEN` environment variable or `--api-token` parameter
949+
- **Socket API Token**: Set via `SOCKET_SECURITY_API_TOKEN` environment variable or `--api-token` parameter (see [Authentication](#authentication) for the other accepted variable names)
934950
- **CI/CD Artifacts**: Reports must be uploaded as `dependency_scanning` artifacts
935951
936952
### Troubleshooting

‎docs/troubleshooting.md‎

Lines changed: 179 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,39 +28,197 @@ Both failures follow `--disable-blocking` and `--exit-code-on-api-error`. Before
2828
`repo:list` ended in the `repo:create` message, and the run exited `2` whatever
2929
either flag said.
3030

31-
## API token scopes for scan comparisons
32-
33-
PR/MR runs compare the new scan against the repository's head scan. That comparison
34-
first uses the diff-scans endpoints, which require an organization token with these
35-
scopes in addition to whatever the scan itself needs:
36-
37-
- `diff-scans:create`
38-
- `diff-scans:list`
39-
- `full-scans:list`
40-
41-
`full-scans:list` also covers the read of the new full scan that `--strict-blocking`,
42-
`--enable-gitlab-security` and `--legal-format fossa` use to get alerts on unchanged
43-
packages.
44-
45-
If the token is missing them the scan still succeeds, so this is easy to miss. The only
46-
signal is a warning, after which the CLI falls back to the older streaming comparison:
31+
## API token permissions
32+
33+
The CLI authenticates with a single Socket organization API token. Most missing scopes
34+
fail the run with an API error. The two `diff-scans` scopes are the exception: without
35+
them the run falls back to an older comparison and only logs a warning.
36+
37+
### Scopes to grant
38+
39+
For a least-privilege CI token, grant these:
40+
41+
| Scope | Needed for | Without it |
42+
|:---|:---|:---|
43+
| `full-scans:create` | Creating the scan, on every run. `--reach` also locates the analysis engine, uploads manifests and records the reachability scan under it | The run fails |
44+
| `full-scans:list` | Finding the baseline scan and comparing against it, on every run that compares scans. Also fetching scan contents for `--enable-json`, `--enable-sarif`, `--enable-gitlab-security` and `--generate-license` on a full scan | Those runs fail |
45+
| `diff-scans:create` | Creating the comparison | The run [falls back](#apiaccessdenied-on-the-scan-comparison) with a warning |
46+
| `diff-scans:list` | Polling for the comparison, and finding an existing one | The same fallback |
47+
| `repo:list` | Looking up the repository's default branch, on runs that compare scans without `--base-scan-id` or `--base-commit-sha` | Those runs fail; see [Repository lookup or creation fails](#repository-lookup-or-creation-fails) |
48+
| `repo:create` | Creating the repository when that lookup finds none | The first comparison for a repository Socket has not seen fails |
49+
| `packages:list` | `--generate-license` only | Those runs fail |
50+
51+
A failed run exits with the API-error code: `3` by default, the value of
52+
`--exit-code-on-api-error` when set, or `0` with `--disable-blocking`.
53+
54+
Scopes are grouped by their prefix, and granting a group grants every scope in it:
55+
`full-scans` covers `full-scans:create`, `full-scans:list` and `full-scans:delete`. The
56+
dashboard's token dialog lists each scope under its group. A token with the groups
57+
`full-scans`, `diff-scans`, `repo` and `packages` therefore works too; it just holds
58+
more than this CLI uses.
59+
60+
The scopes each endpoint checks are listed in the
61+
[API reference](https://docs.socket.dev/reference). `GET organizations` and the
62+
telemetry event posted when someone replies to a Socket comment with an ignore command
63+
check no scope, so any valid token for the organization works. `GET report/supported`
64+
needs no token at all.
65+
66+
**Tokens restricted to specific repositories** can scan those repositories but cannot
67+
set up a new one. The API rejects them on `POST orgs/{org}/repos`, and
68+
`POST orgs/{org}/full-scans` does not create a missing repository for them, whatever
69+
scopes they hold. They are also rejected on `POST orgs/{org}/purl`, so
70+
`--generate-license` needs a token with organization-wide repository access.
71+
72+
### Tokens with the nine-scope CI/CD list
73+
74+
The [CI/CD token setup guide](https://docs.socket.dev/docs/create-socket-api-key-for-cicd)
75+
was written before the diff-scans endpoints existed. Tokens created from its
76+
nine-scope list hold exactly these:
77+
78+
`repo:list`, `repo:create`, `repo:update`, `security-policy:read`,
79+
`triage:alerts-list`, `triage:alerts-update`, `full-scans:list`, `full-scans:create`,
80+
`packages:list`
81+
82+
**That list has neither `diff-scans:create` nor `diff-scans:list`**, and every run
83+
that compares scans uses both. A token with only those nine scopes always falls back
84+
to the older comparison path; see
85+
[the next section](#apiaccessdenied-on-the-scan-comparison). Add the two scopes.
86+
87+
The list also has four scopes this CLI never uses: `repo:update`,
88+
`security-policy:read`, `triage:alerts-list` and `triage:alerts-update`. A token used
89+
only by `socketcli` can drop them.
90+
91+
### API calls by run type
92+
93+
Every run:
94+
95+
| API call | Scope | Purpose |
96+
|:---|:---|:---|
97+
| `GET organizations` | none | Resolve the org ID and slug from the token |
98+
| `POST python-cli-runs` | `full-scans:create` | Register the run. When log upload is on (`--upload-logs`, or enabled for the organization), `POST python-cli-runs/{id}/logs` and `POST python-cli-runs/{id}/finalize` follow under the same scope. A failure in any of the three is ignored |
99+
| `GET report/supported` | none | Fetch supported manifest patterns |
100+
| `POST orgs/{org}/full-scans` | `full-scans:create` | Create the new scan. With an organization-wide token, the server creates the repository if it does not exist yet |
101+
102+
Comment events with `--scm github` or `--scm gitlab` create no scan.
103+
104+
Runs that compare scans (see [which runs compare](#apiaccessdenied-on-the-scan-comparison)) add:
105+
106+
| API call | Scope | Purpose |
107+
|:---|:---|:---|
108+
| `GET orgs/{org}/repos/{repo}` | `repo:list` | Look up the repository's default branch. Skipped with `--base-scan-id` or `--base-commit-sha` |
109+
| `POST orgs/{org}/repos` | `repo:create` | Create the repository, **only attempted when the lookup above finds none** |
110+
| `GET orgs/{org}/full-scans` | `full-scans:list` | Find the baseline: the newest matching scan on the default branch, or with `--base-commit-sha` the scan for that commit or a scanned ancestor. Skipped with `--base-scan-id` |
111+
| `POST orgs/{org}/full-scans` | `full-scans:create` | Create a temporary empty baseline when no scan matches |
112+
| `POST orgs/{org}/diff-scans/from-ids` | `diff-scans:create` and `full-scans:list` | Create the comparison (preferred path) |
113+
| `GET orgs/{org}/diff-scans` | `diff-scans:list` | Find the existing comparison when creation returns 409 |
114+
| `GET orgs/{org}/diff-scans/{id}` | `diff-scans:list` | Poll for the computed comparison |
115+
| `GET orgs/{org}/full-scans/diff` | `full-scans:list` | Streaming comparison (fallback path). The API marks this endpoint as deprecated |
116+
117+
Flags add:
118+
119+
| Flag | API call | Scope |
120+
|:---|:---|:---|
121+
| `--reach` | `GET organizations` (plan check) | none |
122+
| `--reach` | `POST orgs/{org}/coana-cli/artifacts` (locate the analysis engine download, only when it is not cached) | `full-scans:create` or `fixes:list` |
123+
| `--reach` | `POST orgs/{org}/upload-manifest-files`, `POST tier1-reachability-scan/finalize` | `full-scans:create` |
124+
| `--generate-license` | `POST orgs/{org}/purl` | `packages:list` |
125+
| `--enable-json`, `--enable-sarif`, `--enable-gitlab-security` or `--generate-license` on a full scan | `GET orgs/{org}/full-scans/{id}` (scan contents) | `full-scans:list` |
126+
| `--strict-blocking`, `--enable-gitlab-security` or `--legal-format fossa` on a run that compares scans | `GET orgs/{org}/full-scans/{id}` (alerts on unchanged packages) | `full-scans:list` |
127+
128+
`--reach` also runs the analysis engine with the same token. The engine reports to the
129+
`tier1-reachability-scan/*` endpoints, which check `full-scans:create`.
130+
131+
## `APIAccessDenied` on the scan comparison
132+
133+
Any run that compares scans first tries the diff-scans endpoints. A token without the
134+
`diff-scans:create` and `diff-scans:list` scopes logs a warning and continues on the
135+
older path:
47136

48137
```
49138
Diff scan comparison failed with APIAccessDenied(Insufficient permissions), falling back to the streaming scan comparison
50139
```
51140

52-
Grant the scopes to use the diff-scans path. It polls with short, bounded requests
53-
rather than holding one connection open while the backend computes, which is what lets
54-
large comparisons survive network idle timeouts — notably Azure NAT gateways, which
55-
reap idle connections after four minutes and surface as an intermittent
56-
`ConnectionResetError`.
141+
The scan still succeeds and the diff results are the same, so this is easy to miss.
142+
143+
**Which runs compare scans:**
144+
145+
| Run | Compares? |
146+
|:---|:---|
147+
| Pull request or merge request run with `--scm github` / `--scm gitlab` | Yes |
148+
| Comment event with `--scm github` / `--scm gitlab` | No. It creates no scan |
149+
| Any other event with `--scm github` / `--scm gitlab` (branch and default-branch pushes) | No. It creates a full scan |
150+
| No SCM integration (`--scm api`, the default) | Yes |
151+
152+
In the rows that compare, the CLI first looks for a supported manifest among the
153+
commit's changed files, or among `--files` when that is given. If it finds none, or
154+
the directory is not a git repository and `--files` is not given, it creates a full
155+
scan instead. `--ignore-commit-files` in a git repository and `--enable-diff` outside
156+
one skip that check, so those runs always compare.
157+
158+
Before 2.9.0 an SCM-integrated branch push also compared, so a pipeline scanning only
159+
pushes (`--pr-number 0 --default-branch`) hit this too. If you are diagnosing an older
160+
run, that is why the warning can appear in a log with no pull request in sight.
161+
162+
**Which scope is missing.** The fallback calls `GET orgs/{org}/full-scans/diff`, which
163+
checks `full-scans:list`. When the fallback succeeds, the missing scope is
164+
`diff-scans:create`, `diff-scans:list`, or both. A token without `full-scans:list`
165+
does not get that far: the CLI lists full scans to pick the baseline before it compares
166+
anything, so the run fails first with `Failed to resolve the matching head scan for
167+
repo ...` and the API-error exit code. With `--base-scan-id` that lookup is skipped,
168+
and the run fails at the fallback instead. A fallback that fails another way, such as a
169+
timeout or a dropped connection, says nothing about scopes.
170+
171+
**Why the diff-scans path is preferred.** It polls with short, bounded requests rather
172+
than holding one connection open while the backend computes, which is what lets large
173+
comparisons survive network idle timeouts — notably Azure NAT gateways, which reap idle
174+
connections after four minutes and surface as an intermittent `ConnectionResetError`.
175+
Falling back costs resilience, not correctness.
57176

58177
The two paths can take noticeably different amounts of time on the same repository,
59178
because cached diff-scan responses always embed per-package license details while the
60179
streaming comparison requests a lean payload. On a large dependency tree, compare the
61180
`Diff scan comparison ready in ...` timing against the `Diff Report Gathered in ...`
62181
total before assuming either path is at fault.
63182

183+
## Baseline scans that do not appear in the dashboard
184+
185+
A run that compares scans picks its baseline from the repository's default branch: the
186+
newest scan there with the same scan type (`socket`, or `socket_tier1` with `--reach`)
187+
and, with `--workspace`, the same workspace. `--base-scan-id` and `--base-commit-sha`
188+
override that choice. When no scan matches, the CLI creates a **temporary empty
189+
baseline** and compares to that:
190+
191+
```
192+
No previous scan found - creating empty baseline scan
193+
Baseline selected: source=empty scan_id="<baseline>"
194+
Comparing scans - Head scan ID: <baseline>, New scan ID: <real scan>
195+
```
196+
197+
The baseline is created as a temporary scan and is deliberately not set as the
198+
repository head or default branch, so it does not show up as a repository scan in the
199+
dashboard. Looking it up by ID will come up empty. This is expected — the scan to look
200+
at is the "New scan ID".
201+
202+
Because everything is new relative to an empty baseline, that comparison reports
203+
every package as added and zero as unchanged. This happens on a repository's first
204+
comparison, and also the first time it is compared under a different scan type or
205+
workspace, such as the first `--reach` run on a repository that only has regular
206+
scans. A new branch compares against the default branch's newest matching scan, not an
207+
empty baseline.
208+
209+
## Reachability scan IDs are not full-scan IDs
210+
211+
With `--reach`, the CLI logs an extra identifier before the normal scan flow starts:
212+
213+
```
214+
Reachability scan ID: <tier-1 reachability scan ID>
215+
```
216+
217+
That value is `tier1ReachabilityScanId`, read out of `.socket.facts.json`. It
218+
identifies the reachability analysis run, not a Socket full scan, and will not resolve
219+
in the dashboard's scan views. The full scan to look at is the "New scan ID" reported
220+
by the comparison step that follows.
221+
64222
## Common gotchas
65223

66224
- In diff scope, `--strict-blocking` uses a stricter alert set (`new + unchanged`) for blocking checks and diff-based output selection.

‎pyproject.toml‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ dependencies = [
1616
"gitpython==3.2.1",
1717
"packaging==26.3",
1818
"python-dotenv==1.2.4",
19-
"socketdev==3.6.2",
19+
"socketdev==3.6.3",
2020
"beautifulsoup4==4.15.0",
2121
"markdown==3.11.3",
2222
"brotli==1.2.1; platform_python_implementation == 'CPython'",
@@ -47,13 +47,13 @@ test = [
4747
"pytest==9.2.0",
4848
"pytest-cov==7.1.0",
4949
"pytest-mock==3.17.0",
50-
"pytest-asyncio==1.4.1.post1",
50+
"pytest-asyncio==1.5.0",
5151
"pytest-watch==4.2.0"
5252
]
5353
dev = [
54-
"ruff==0.17.4",
54+
"ruff==0.17.5",
5555
"twine==7.0.0", # for building
56-
"uv==0.13.7", # for dependency management
56+
"uv==0.13.8", # for dependency management
5757
"pre-commit==4.7.0",
5858
"hatch==1.18.2"
5959
]

0 commit comments

Comments
 (0)