Skip to content

Commit 61c8e89

Browse files
leliaclaude
andauthored
Improve documentation for requisite API token permissions (#353)
* Document the API token permissions a run actually needs The only scope guidance in the repo covered the diff-scans comparison path, described it as PR/MR-only, and listed `full-scans:list` alongside the two `diff-scans:*` scopes. The comparison also runs on plain pushes, and the streaming fallback is itself a full-scans read, so a reader hitting the `APIAccessDenied` warning was pointed at the wrong permission and told the condition did not apply to them. Replace it with the full picture: what every run calls, what diff-producing runs add, and what each flag adds. Scope names are only published for the diff-scans path, so the rest are described by endpoint rather than guessed at. Also document three outcomes that look like failures and are not: the temporary empty baseline on a first scan, the reachability scan ID being a `tier1ReachabilityScanId` rather than a full scan ID, and a repository permission gap exiting 2 — a code the exit table documents as a keyboard interrupt, and which bypasses `--disable-blocking` and `--exit-code-on-api-error`. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * Name the token scopes and flag the two the setup guide omits The permission tables described endpoints but deferred on scope names, since nothing in this repo or the SDK publishes them. The public CI/CD token setup guide does: nine scopes, which the docs now name and map to the calls they cover. That guide omits `diff-scans:create` and `diff-scans:list`. Every diff-producing run tries the diff-scans endpoints first, so a token provisioned exactly as documented always fails that call and falls back to the legacy streaming comparison, with a warning as the only signal. Anyone following the documented setup hits this. The converse is also worth stating: `socketcli` makes no triage or security-policy calls, so three of the nine scopes the guide lists are not exercised by this CLI. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * Correct which runs compare, after the 2.9.0 flow change now sends only a "diff" event type to create_new_diff and everything else, default-branch pushes included, to create_full_scan_with_report_url. The permission docs asserted the opposite -- that plain pushes also reach the diff-scans path -- which was true when they were written against 2.8.1 and is not true now. Replace the claim with a table of which runs compare, and keep the old behavior as a note, since the warning still shows up in logs from pre-2.9.0 runs with no pull request in sight. Re-verified the rest against 2.9.7: SDK call surface, the four sys.exit(2) sites, the temporary empty baseline, and the reachability scan ID are all unchanged. * Map each API call to the scope the API checks The scope tables were inferred from scope names. The API reference publishes the scope each endpoint checks, so the tables now use those and the open question about unpublished scopes is resolved: GET organizations, GET report/supported and the ignore-comment telemetry post check none. Rechecking the call surface against 2.12.0 also changed the guidance: - repo:update is never exercised. POST full-scans checks only full-scans:create, including when it sets the repository head. - --sbom-file makes no API call; the CycloneDX export it was mapped to has no caller. License-metadata and full-scan metadata reads are likewise unreached outside tests. - diff-scans/from-ids checks full-scans:list as well as diff-scans:create. - --reach's manifest upload and tier-1 finalize check full-scans:create, as do the Coana CLI's reachability calls. - The exit-2 section now quotes the repo:create hint the CLI logs on a 403. * Describe which runs compare without pinning it to a release * Scope the repository lookup to diff-producing runs Only a diff-producing run that takes its baseline from the repository's head scan looks the repository up, and so only that run can create it or exit 2. A full-scan run uploads straight to full-scans, which creates a missing repository for an organization-wide token. A failed streaming fallback also only points at full-scans:list when it fails with Insufficient permissions. * Correct the supported-patterns failure and the empty-baseline trigger A failed GET report/supported raises from the SDK and stops the run; the bundled pattern list only covers a non-error response with no body. An empty baseline is created when the repository has no head scan, not on every new branch, since a branch compares against the repository's head scan. * Bump to 2.12.2 * Correct how missing scopes behave, and how the baseline is chosen A missing scope was described as mostly degrading with a warning. Only the two diff-scans scopes do that. The others fail the run: - full-scans:list is needed before any comparison. scan_type is always set, so the baseline is the newest matching scan listed from the default branch, not the repository's head-scan pointer, and a denied listing exits with the API-error code. The same listing explains when an empty baseline is created. - The scope table now says what happens without each scope. Calls the tables missed: POST python-cli-runs (and its logs and finalize calls) on every run, and POST coana-cli/artifacts under --reach, which accepts full-scans:create or fixes:list. GET report/supported needs no token, and the streaming fallback endpoint is deprecated in the API. Also covered: scope groups, tokens restricted to specific repositories, comment events, and the flags that force a comparison. The README exit-code table is left alone. The repository-failure section keeps the exit-2 details under "2.12.x and earlier", so it stays true when the exit-code contract changes. The setup-guide paragraphs describe tokens made from its nine-scope list instead of the guide's current contents, which are about to change. * Bump the pinned Socket Python SDK to 3.6.3 socketdev 3.6.3 fixes settings.integration_events(), which requested a path the API does not route. The CLI does not call it, so nothing changes at runtime; the pin moves so the CLI ships the current SDK. --------- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 4250b5c commit 61c8e89

8 files changed

Lines changed: 235 additions & 30 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,23 @@
11
# Changelog
22

3+
## 2.13.1
4+
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.
14+
15+
### Changed: bump the Socket Python SDK to 3.6.3
16+
17+
- Bumped the pinned Socket Python SDK (`socketdev`) from `3.6.2` to `3.6.3`.
18+
The release fixes `settings.integration_events()`, which the CLI does not
19+
call, so CLI behavior is unchanged.
20+
321
## 2.13.0
422

523
### 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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ build-backend = "hatchling.build"
66

77
[project]
88
name = "socketsecurity"
9-
version = "2.13.0"
9+
version = "2.13.1"
1010
requires-python = ">= 3.11"
1111
license = {"file" = "LICENSE"}
1212
dependencies = [
@@ -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'",

‎socketsecurity/__init__.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
__author__ = 'socket.dev'
2-
__version__ = '2.13.0'
2+
__version__ = '2.13.1'
33
USER_AGENT_NAME = 'socket-python-cli'
44
USER_AGENT = f'{USER_AGENT_NAME}/{__version__}'

‎uv.lock‎

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)