Skip to content

fix(security): an ml.sh.read range that cuts a GitHub token is scrubbed by its shape, text and bytes alike - #2227

Open
wanderdev0 wants to merge 6 commits into
mainfrom
fix/sh-read-token-range
Open

wanderdev0 wants to merge 6 commits into
mainfrom
fix/sh-read-token-range

Conversation

@wanderdev0

@wanderdev0 wanderdev0 commented Dec 1, 2026 •

Copy link
Copy Markdown
Owner

ml.sh.read widened a range by the longest registered form and range-scrubbed registered values only; the GitHub token's shape rule ran later, in forModel, on the cut slice. The rule needs the prefix and 36 body characters in one text, so a range that started or ended inside an unregistered ghp_/github_pat_ token handed back that part of it unscrubbed, and the bytewise branch (binary ranges) never ran the shape rule at all. e4's rows in tests/redteam-sh-read-token-range.test.mjs (d173c02) pin it: six failed on the branch tip, all seven pass here.

The fix

  • margin() (src/secret-registry.ts) is never less than GITHUB_TOKEN_MAX (266, now exported from src/secret-scrub.ts), so a token the range cuts is whole in the window.

  • scrubRange matches GITHUB_TOKEN by shape over the whole window, first (as scrubSecrets does), dropping a registered hit that overlaps a token. A token that overlaps the slice becomes [a GitHub token] once, its outside part dropped, the same rule as a registered form. Token hits are returned as tokens, apart from the named counts, because a token has no registered name.

  • shRead (src/sw/sw-shell-call.ts) adds tokens to scrubbed, so a range changed only by a token says it was touched. The bytewise branch's early return of the raw bytes when nothing is registered is gone: with the 266 floor the margin is never 0.

  • A cut head (scrubbedHead, the first 64 KiB an ml.sh call returns) held back only a tail that begins a registered form, so a head cut inside an unregistered token ended in ghp_ and up to 35 of its 36 body characters, which the shape never matches. tokenTail (src/secret-registry.ts, beside heldTail) holds back a tail of gh?_ or github_pat_ and a body short of the shape's minimum; scrubbedHead holds back the larger of the two. The most held back is ghp_ and 35 characters, 39 bytes. heldTail and forLive are unchanged. The rows came first, in 308a1de: a cut head ending in ghp_ or github_pat_ and 30 body characters, and a whole output ending in ghp_abc, which the model reads as printed with complete: true (nothing follows it, so it is no token's front).

One existing assertion changes because the rule did: tests/secret-registry.test.mjs "margin: …" asserted margin([]) === 0; it now asserts 266 for no forms, 266 when every form is shorter, and the longest form when one is longer than 266.

This was the smaller of the two candidate fixes. Wiring src/secret-scrub-range.ts instead would need an async source adapter over the host read and its separate config-secret path.

What a binary range now costs and reads as

Every binary range is widened by 266 bytes a side and scrubbed, where before, with nothing registered, it was returned as read. Measured on this laptop (scrubRange + latin1 + bytes back, median of 200), the default 64 KiB range adds 2.8 ms (p90 3.1 ms) and a 4 KiB range adds 0.18 ms. A binary range holding a token-shaped run of bytes reads [a GitHub token] in their place, with scrubbed: 1 and length still the raw range's byte count. A range with no match is the bytes as read, with no scrubbed. Both are pinned by a new row in tests/secret-scrub-crossings.test.mjs ("a binary read range holding an unregistered GitHub token…"), since e4's binary rows cover the two cuts only.

Docs

  • docs/spec/SECRET_HANDLES.md: the token row names the cut read range and points at the new limit. "A cut never lets a prefix through" gives the 266 floor, the shape match, and a head cut before a token's body (39 bytes at most). scrubbed: <n> counts tokens too. Known limits gains: forLive applies the registered forms only, so an unregistered GitHub token shows in a live output's stream until the step settles. The settled step, what is stored and what the model reads carry [a GitHub token]. Ruling 39's shape detector is where it is repaired, not this PR.
  • docs/dev/site-access.md:43: a host with a port is always a scrubbed form; a name without one is skipped when it is dotless or loopback (the wording of fix(security): a step's In render crosses the stored scrub at the START, the approval card and the DONE #2223's fix).

src/secret-scrub-range.ts and its two test files are left as they are: no source file imports it, and whether it has a caller coming is the session-store lane's to answer.

Tests: npm test (full), e4's seven rows, check-doc-links.

Drafted by Claude Code · work: small-fixes · session f98b97c7 · claude-opus-5-5

🤖 Generated with Claude Code

wanderdev0 and others added 3 commits December 1, 2026 19:02
…ts body

Seven rows on `ml.sh.read`. Six fail on this branch:
1. a text range that starts inside an unregistered `ghp_` token returns its body (`forModel` needs the prefix and 36 body characters in the same text);
2. a text range that ends inside returns the token's front, prefix included;
3. and 4. the same two cuts on the bytewise path, which never reaches `forModel`;
5. the same start-cut on a `github_pat_` token;
6. two ranges that split a token leave neither half scrubbed.

The seventh pins what holds: a range holding the whole (line-delimited) token still reads `[a GitHub token]`.

A refusal counts as an answer that keeps the token, so the rows assert the property over both shapes of answer. Fixtures put the token on its own line, because the shape rule anchors on `\b`.

Co-Authored-By: Claude Code <noreply@anthropic.com>
…ed by its shape, text and bytes alike

The range scrub matched registered forms only and widened by the longest
of them, so a range cut inside an unregistered ghp_/github_pat_ token
handed back its part, and a binary range never met the shape rule.
margin() now floors at the longest token (266), scrubRange matches the
token's shape over the window, and a token counts in `scrubbed`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@wanderdev0 wanderdev0 added @docs Documentation and specs only, no behaviour change @security Trust boundaries: consent, site access, privileged fetches, what a page or model reaches labels Dec 1, 2026
wanderdev0 and others added 2 commits December 1, 2026 19:50
…gistered GitHub token's body

Fails on 4a1bbff: scrubbedHead holds back the start of a registered form only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ed GitHub token's body

tokenTail, beside heldTail: a tail of gh?_ (or github_pat_) and a body short of the shape's minimum, 39 characters at
most. scrubbedHead holds back the larger of the two; heldTail and forLive are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@wanderdev0

Copy link
Copy Markdown
Owner Author

Verdict at 326a0dffacf21af25ea615429a8a5376f91f10a1: no change asked.

Failing first: at d173c02 (built fresh) six of seven rows in tests/redteam-sh-read-token-range.test.mjs fail, row 5 (the whole-token pin) passes; at 308a1de the new head row in tests/secret-scrub-crossings.test.mjs fails; at 326a0df the three files (crossings, secret-registry, redteam-sh-read-token-range) pass, 71 of 71.

Margin: margin floors at 266 (src/secret-registry.ts), and SH_READ_MAX - 2 * m stays positive with SH_READ_MAX at 524288. At offset 0 lo clamps to 0 with nothing before it; at the end of output the window is shorter and the token ends inside it; a token is at most 266 characters, so one that overlaps the slice lies whole in a window widened by 266 on each side; a window edge reads as \b, which only over-scrubs. The removed early return ran only when m was 0, which margin no longer returns, and the answer was { $bytes } either way.

Head cut, probed with an 82-character github_pat_ token and a 36-character ghp_ token against the fake host, then ml.sh.read at the head's end and 3 bytes before it. github_pat_ cut after 21 body characters: the head holds the front back (tokenTail), both ranges read [a GitHub token] and more after it. Cut after 22, 23, 50 and 81: the shape matches at the head's end, so the head ends [a GitHub token]… [first 65536 of … bytes] and both ranges read [a GitHub token] and more after it. ghp_ cut after 35: held back, both ranges read the marker. Cut after exactly 36: the head ends in the marker; the range at the head's end reads and more after it with no scrubbed, the range 3 bytes before reads the marker. No four-character run of either body shows in any head or range. An output that is only ghp_ and 10 characters is whole, so nothing is held: it reads as printed, complete: true.

Not asked: the held tail is dropped from both the head and the next read. With an output of 65527 a, a space and ghp_abcd xyz rest, the head ends a … [first 65536 of 65546 bytes] and ml.sh.read at 65536 returns xyz rest, so ghp_abcd reaches the script from neither, while the note claims 65536 bytes. heldTail already did this for a registered form's front; tokenTail makes it happen at any gh?_ word on the cut. Name the kept byte count in the note, so a read from there starts at the held tail.

Drafted by Claude Code · work: reviews-1 · session 5a179699 · claude-opus-5-5

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

@docs Documentation and specs only, no behaviour change @security Trust boundaries: consent, site access, privileged fetches, what a page or model reaches

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant