Skip to content

Commit 3463f09

Browse files
anandgupta42claude
andcommitted
fix: ledger redaction — preserve write paths and close a curl -u credential leak introduced by #1117
`SessionCompaction.redactLedgerDetail` called `Telemetry.maskString` before its own curl-credential redaction. #1117 added a filesystem-path masking pass to `maskString` that collapses any 2+ segment path — including `/usr/bin/curl` — to the literal `<path>` before the curl-context lookback ran. Two consequences: - Every write path in the ledger collapsed to `<path>`, so the ledger could no longer report which file was written (4 failing tests). - With the `curl` token gone, the curl-context lookback found nothing, so a path-qualified curl invocation's space-separated `-u user password` (non-colon-shaped, so not otherwise flagged as credential-shaped) passed through `redactLedgerDetail` unredacted — a real credential leaking into ledger text that is later persisted into a model prompt across compaction. Fix: - `Telemetry.maskString` gains an opt-out for its path-masking pass (`maskPaths`, default `true`, unchanged for every existing caller). Every other mask (api keys, bearer tokens, emails, internal hosts, quote collapsing) still applies regardless. - `redactLedgerDetail` calls `maskString(value, { maskPaths: false })`, restoring write-path fidelity and, because the `curl` token survives, restoring the curl-context lookback. - Belt-and-suspenders: `redactLedgerDetail`'s curl-context detection is also derived independently from the pre-mask raw value (correlated ordinally against the masked-string matches), so it no longer depends solely on `maskPaths:false` — a future masking rule that happens to eat the command name can't quietly reopen this leak. Adds an explicit adversarial test reproducing the leak (`/usr/bin/curl -u alice hunter2 ...` and the `curl.exe` path-qualified variant); confirmed it fails against pre-fix code via `git stash` and passes after. #1117's own telemetry path-masking is untouched for its other callers (mcp/index.ts, sql-execute.ts, tool.ts, prompt.ts, register.ts, dispatcher.ts, registry.ts, warehouse-add.ts, project-scan.ts) — confirmed via test/telemetry/mask-file-paths.test.ts still green. Fixes broken main: origin/main's TypeScript CI job has been red since ~2026-09-04 07:28Z on these 5 tests, blocking all PRs. The leak itself is main-only — not present in v0.10.0 or any released version. Co-Authored-By: Claude Opus 4.8 <[email protected]> Claude-Session: https://claude.ai/code/session_01VqnuBDGkh1ZT65Ti7e6DHZ
1 parent 9361e0b commit 3463f09

3 files changed

Lines changed: 88 additions & 12 deletions

File tree

‎packages/opencode/src/altimate/telemetry/index.ts‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1558,14 +1558,20 @@ export namespace Telemetry {
15581558
// Each match replaces with a fixed redaction so length-based fingerprinting
15591559
// can't reconstruct the original token.
15601560

1561-
export function maskString(s: string): string {
1561+
// altimate_change start — maskPaths opt-out for callers that redact on their
1562+
// own terms (see compaction.ts::redactLedgerDetail). Default true keeps
1563+
// every existing caller's behavior byte-for-byte unchanged; only a caller
1564+
// that explicitly passes maskPaths:false skips the #1117 path-masking pass,
1565+
// and every other rule (api keys, bearer tokens, emails, internal hosts,
1566+
// quote collapsing) still applies.
1567+
export function maskString(s: string, opts?: { maskPaths?: boolean }): string {
15621568
// Consumers truncate masked output to <= 2000 chars; masking beyond 8 KB
15631569
// buys nothing, and unbounded input is what turns any super-linear rule
15641570
// into a stall. Input is cut FIRST so every rule — the linear credential/
15651571
// quote passes included — does bounded work, and the cut must never fail
15661572
// a rule open across the boundary. No floor gates any of this: a floor is
15671573
// a leak past the floor.
1568-
if (s.length <= PM_CAP) return pmMask(s)
1574+
if (s.length <= PM_CAP) return pmMask(s, opts)
15691575
// the head ends at whitespace of any kind, so no token straddles it (a
15701576
// head with no whitespace at all is one token: nothing is emitted)
15711577
const ws = s.slice(0, PM_CAP).search(/\s\S*$/)
@@ -1583,17 +1589,18 @@ export namespace Telemetry {
15831589
// masked the same way with and without the continuation, cut back to
15841590
// whitespace. Whatever the continuation changes is dropped, never
15851591
// emitted half-proven.
1586-
const alone = pmMask(head)
1587-
const seen = pmMask(s.slice(0, at + PM_LOOKAHEAD))
1592+
const alone = pmMask(head, opts)
1593+
const seen = pmMask(s.slice(0, at + PM_LOOKAHEAD), opts)
15881594
let n = 0
15891595
while (n < alone.length && alone[n] === seen[n]) n++
15901596
if (n === alone.length) return alone
15911597
const back = alone.slice(0, n).search(/\s\S*$/)
15921598
return back >= 0 ? alone.slice(0, back).trimEnd() : ""
15931599
}
1600+
// altimate_change end
15941601

15951602
// the masking chain proper, on bounded input (see maskString)
1596-
function pmMask(s: string): string {
1603+
function pmMask(s: string, opts?: { maskPaths?: boolean }): string {
15971604
let out = s
15981605
// ANSI CSI sequences (colored subprocess stderr) would otherwise split
15991606
// tokens so neither credential nor path rules can see them
@@ -1604,7 +1611,13 @@ export namespace Telemetry {
16041611
.replace(/"(?:[^"\\]|\\.)*"/g, "?")
16051612
// Fast path: a string with no separator cannot contain a path — skip the
16061613
// whole path stack (most telemetry strings carry no path at all).
1607-
if (out.includes("/") || out.includes("\\") || /(?<![A-Za-z0-9])[A-Z]:[^\s:\\\/]{1,255}\.[A-Za-z]/.test(out)) {
1614+
// altimate_change — maskPaths:false (see maskString) skips this whole
1615+
// pass; every other rule in pmMask still runs.
1616+
const maskPaths = opts?.maskPaths ?? true
1617+
if (
1618+
maskPaths &&
1619+
(out.includes("/") || out.includes("\\") || /(?<![A-Za-z0-9])[A-Z]:[^\s:\\\/]{1,255}\.[A-Za-z]/.test(out))
1620+
) {
16081621
out = out
16091622
// altimate_change start — mask filesystem paths in error text
16101623
// Six masking rules (cloud URIs, Windows home, Windows/UNC incl. .\ and

‎packages/opencode/src/session/compaction.ts‎

Lines changed: 50 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -667,17 +667,57 @@ export namespace SessionCompaction {
667667
return SENSITIVE_NAME.test(name)
668668
}
669669

670+
// altimate_change — the flag+value regex used both to redact `-u`/`--user`
671+
// below and (via redactLedgerDetail's raw-value precomputation) to locate
672+
// the same occurrences in the pre-mask text. Shared so the two passes stay
673+
// in lockstep by construction instead of by copy-pasted duplication.
674+
const USER_FLAG_RE =
675+
/(^|\s)(--user|-u)(?:(=|\s+)("[^"]*"|'[^']*'|[^\s,;]+)|([^\s,;]+))(?:(\s+)("[^"]*"|'[^']*'|[^\s,;]+))?/gi
676+
677+
function isCurlContext(segment: string): boolean {
678+
// Windows invokes curl as `curl.exe`, and either platform may reach it
679+
// through a path such as /usr/bin/curl or a Windows System32 path.
680+
// Missing those spellings left the `-u` VALUE unredacted.
681+
return /(?:^|[\s/\\])curl(?:\.exe)?(?=\s|$)/i.test(segment)
682+
}
683+
670684
export function redactLedgerDetail(value: string): string {
671685
const sensitiveName = SENSITIVE_NAME
672-
let masked = Telemetry.maskString(value)
686+
// altimate_change — keep the filesystem-path masking pass OFF here. Left
687+
// on, it collapses a path-qualified command like `/usr/bin/curl` down to
688+
// `<path>` before the curlContext lookback below ever runs, which both
689+
// destroys the write paths this ledger exists to report AND — the actual
690+
// security bug — erases the `curl` token the lookback needs, so a
691+
// path-qualified `curl -u user password` credential slips through
692+
// unredacted. Every other telemetry mask (api keys, bearer tokens,
693+
// emails, internal hosts, quote collapsing) still applies.
694+
let masked = Telemetry.maskString(value, { maskPaths: false })
695+
696+
// altimate_change start — belt-and-suspenders curl detection. Derive
697+
// curlContext for each `-u`/`--user` occurrence from the RAW, pre-mask
698+
// `value` as well as from `masked`. maskPaths:false above is what keeps
699+
// the `curl` token intact today; this makes detection structurally
700+
// independent of that single flag, so a future masking rule that happens
701+
// to eat the command-name token can't quietly reopen this leak. The two
702+
// occurrence lists are correlated by ordinal position (same regex, same
703+
// match order); if a prior mask ever changes how many times the pattern
704+
// matches, this safely falls back to the masked-only signal — no worse
705+
// than before this change.
706+
const rawCurlByOrdinal = [...value.matchAll(USER_FLAG_RE)].map((m) =>
707+
isCurlContext(shellSegmentBefore(value, m.index + m[1].length)),
708+
)
709+
const maskedOccurrenceCount = [...masked.matchAll(USER_FLAG_RE)].length
710+
const ordinalsAligned = maskedOccurrenceCount === rawCurlByOrdinal.length
711+
let ordinal = 0
712+
// altimate_change end
673713

674714
// `-u` is also a benign flag for commands such as `git push -u` and
675715
// `python -u`. Redact it as authentication only in the current curl shell
676716
// segment, or when the value itself has a user:password shape. Long
677717
// `--user` follows the same rule so task literals are not discarded merely
678718
// because an unrelated CLI chose that option name.
679719
masked = masked.replace(
680-
/(^|\s)(--user|-u)(?:(=|\s+)("[^"]*"|'[^']*'|[^\s,;]+)|([^\s,;]+))(?:(\s+)("[^"]*"|'[^']*'|[^\s,;]+))?/gi,
720+
USER_FLAG_RE,
681721
(
682722
match,
683723
lead: string,
@@ -690,13 +730,17 @@ export namespace SessionCompaction {
690730
offset: number,
691731
whole: string,
692732
) => {
733+
// altimate_change — capture and advance the ordinal before any early
734+
// return so it always tracks this callback's position in `masked`'s
735+
// match sequence, matching how rawCurlByOrdinal was built.
736+
const currentOrdinal = ordinal++
693737
// Attached values are valid only for short `-u` (`-ualice:pass`).
694738
if (flag.toLowerCase() === "--user" && separator === undefined) return match
695739
const rawValue = (separatedValue ?? attachedValue ?? "").replace(/^["']|["']$/g, "")
696-
// Windows invokes curl as `curl.exe`, and either platform may reach it
697-
// through a path such as /usr/bin/curl or a Windows System32 path.
698-
// Missing those spellings left the `-u` VALUE unredacted.
699-
const curlContext = /(?:^|[\s/\\])curl(?:\.exe)?(?=\s|$)/i.test(shellSegmentBefore(whole, offset + lead.length))
740+
const maskedCurlContext = isCurlContext(shellSegmentBefore(whole, offset + lead.length))
741+
// altimate_change — OR in the raw-value signal (see block above).
742+
const curlContext =
743+
maskedCurlContext || (ordinalsAligned && (rawCurlByOrdinal[currentOrdinal] ?? false))
700744
// Outside a curl context a colon-shaped value is treated as
701745
// user:password. The ONE exemption is an explicitly recognized
702746
// all-numeric UID:GID pair (`docker run --user 1000:1000`), which is a

‎packages/opencode/test/session/compaction-ledger.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,25 @@ describe("SessionCompaction.renderLedger", () => {
439439
expect(SessionCompaction.redactLedgerDetail("curlywurly -u alice script.py")).toBe("curlywurly -u alice script.py")
440440
})
441441

442+
test("does not leak credentials via a path-qualified curl and the space-separated -u idiom (regression, #1117)", () => {
443+
// #1117 added a filesystem-path masking rule to Telemetry.maskString.
444+
// redactLedgerDetail used to call maskString BEFORE its own curl-context
445+
// lookback, so `/usr/bin/curl` collapsed to `<path>` first. The `curl`
446+
// token was gone by the time the lookback ran: curlContext came back
447+
// false, `-u alice hunter2` (space-separated, non-colon-shaped) isn't
448+
// credentialShaped either, and the credential passed through untouched
449+
// into ledger text that later gets persisted into a model prompt across
450+
// compaction. This is the exact adversarial shape of that leak.
451+
for (const command of [
452+
"/usr/bin/curl -u alice hunter2 https://example.com",
453+
"/usr/local/bin/curl.exe -u alice hunter2 https://example.com",
454+
]) {
455+
const detail = SessionCompaction.redactLedgerDetail(command)
456+
expect(detail).not.toContain("alice")
457+
expect(detail).not.toContain("hunter2")
458+
}
459+
})
460+
442461
test("keeps non-credential colon-shaped values outside a curl context", () => {
443462
// `1000:1000` has no alphabetic character before the colon, so it is a
444463
// UID:GID pair rather than user:password and must survive in the ledger.

0 commit comments

Comments
 (0)