Skip to content

fix(offload): keep the sweep ask-dump file name inside its directory - #951

Merged
amiddavid merged 1 commit into
mainfrom
fix/askdump-filename
Nov 8, 2026
Merged

amiddavid merged 1 commit into
mainfrom
fix/askdump-filename

Conversation

@amiddavid

Copy link
Copy Markdown
Collaborator

Summary

What we found

  • The id comes from a client header. session.Scoped already allows only letters, digits and . _ : -. So no real id can leave the directory today.
  • The dump code did not check this itself. It relied on every caller.

What changed

  • askDumpSafe turns any other character into _.
  • The code skips the write if the name is not one local path element.

Behavior change: none for any id that session.Scoped allows. The name is the same. Only an id outside that set (not reachable today) now gets _ in the name.

How it was tested

  • New test TestAskDumpFileStaysInsideTheDirectory. It fails before the fix (file escape-001.json lands in the parent directory) and passes after.
  • go vet and go test -race ./components/offload/ pass on the box.

Data dependence

No change. The dump already holds conversation text, off by default. This PR changes only the file name.

🤖 Generated with Claude Code

CodeQL go/path-injection flagged the dump file name, which is built from
the session id. session.Scoped already limits real ids to a safe set.
The dump now cleans the name itself and checks it is one local element.

Assisted-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: DAVID AMID <[email protected]>

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 2 (glm, user of @amiddavid) GLM rits/zai-org/glm-5-3 via Claude Code 2.1.319 offload-review skill, @amiddavid's session fresh 2 of 2 1 of 2 df92f1cddbf7 approve

Verdict: approve

Findings

None. I could not break the fix. Details of what I tried are under "What I checked".

Non-blocking

  • sweep_askdump.go:146 — the filepath.IsLocal / filepath.Base guard can never fire after askDumpSafe: the name always starts with ask-, holds no separator, and so is always one local element. The code already says "defence in depth", so this is stated intent, not a defect. Keep it.
  • sweep_askdump_test.go:180 — the test asserts containment and count, not the mangled name itself. It would pass a sanitize that, say, hashed the whole id. The in-repo convention (see the gate test at line 51) prefers asserting the property, so this is fine as is.
  • sweep_askdump.go:113 — the comment "for every real id this returns the id unchanged" is correct today: safeExplicit allows exactly [a-zA-Z0-9._:-], the derived hash is hex, and tenant ids come from newID() (hex). It rests on the tenant half staying hex; the code stays safe even if it does not.

What I checked

  • Read the full diff (2 files, +44/−1) and both files in full.
  • The sanitize is airtight for path escape. askDumpSafe keeps only [a-zA-Z0-9._:-]. The name is ask-<safe>-<seq>.json: it always starts with ask-, never equals . or .., and holds no / or \. After the map, filepath.IsLocal is always true, so the write stays inside dir. Invalid UTF-8 bytes map through strings.Map to RuneError, which the allow-list rejects, so they become _.
  • The charset claim in the comment is true. session.safeExplicit (session/session.go:42) allows exactly letters, digits and . _ : -, rejects all-dots ids, and caps length at 128. Scoped output is that id, or a 16-char hex hash, optionally prefixed tenant:; tenant ids come from newID() (hex). So no real proxy id changes.
  • No overwrite from collapsed ids. Two hostile ids can sanitize to the same prefix, but sweepAskDumpSeq is a process-global atomic, so <seq> differs per dump and file names never collide.
  • The test catches the named mutation. With the raw session x/../../escape, the joined path parent/dump/ask-x/../../escape-001.json cannot resolve (ask-x does not exist), os.WriteFile fails, and the dump dir holds 0 files — the len(in) != 1 assertion at sweep_askdump_test.go:190 fails. So reverting the fix fails the test.
  • Fail-open holds. Every new failure path (unsafe name, over-long name → ENAMETOOLONG) logs one warning and returns; the adjudication is untouched, matching the file's stated contract.
  • Both call sites (extract_sweep.go:635, 817) go through the one writeSweepAskDump, so the guard covers every exit.
  • Repo rules. No data-dependence change: the dump stores the same bytes as before, only the file name changed, so docs/data-dependence.md needs no edit. No behavior change for users: real ids sanitize to themselves, and the dump is an operator opt-in bench instrument. No other session-derived file names exist in the repo (grepped).

@amiddavid amiddavid left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewer Kind Started by Context Review Round Head Verdict
Reviewer 1 (glm, user of @amiddavid) GLM rits/zai-org/glm-5-3 via Claude Code 2.1.319 offload-review skill, @amiddavid's session fresh 1 of 2 1 of 2 df92f1cddbf7 approve

Verdict: approve

Findings

None. The fix is correct and I could not break it.

Non-blocking

  • components/offload/sweep_askdump_test.go:191 — the test does not assert the written file name. A sanitizer that mangles real ids (drops an allowed character) would still pass it. Add one line: the file must be ask-x_.._.._escape-001.json. That also pins the identity property — for every id session.Scoped allows, the name is unchanged.
  • components/offload/sweep_askdump.go:146 — the filepath.IsLocal / Base guard can never fire while askDumpSafe runs: the sanitized name holds no separator. The comment says it is defence in depth, so this is fine. It guards a future edit that removes the sanitizer.
  • Pre-existing, not this PR: CG_SWEEP_ASK_DUMP is missing from the env-switch table in docs/data-dependence.md (the row for CONTEXT_GURU_DUMP / CONTEXT_GURU_CAPTURE, line 342). The ask-dump writes full conversation text to disk and belongs in that list. It came in with #508. Give it its own issue, per the repo rule that a main defect gets its own PR.

What I checked

  • The full diff: askDumpSafe, the name construction, the new guard, and the new test.
  • The charset claim. session.safeExplicit (session/session.go:42) allows letters, digits and . _ : -, max 128 bytes. askDumpSafe keeps exactly that set. So no reachable id changes its file name.
  • The tenant prefix. Scoped only trims spaces from the tenant, but the hosted proxy passes tn.ID, which newID makes from hex (tenant/tenant.go:1496). No separator can enter there.
  • Both hosts. The proxy and the bifrost adapter both resolve the session through Scoped / Resolve. The dump no longer depends on that, which is the point of the fix.
  • Fail open. The unsafe-name path logs one warning and skips the write. The dump is a diagnostic; skipping it cannot fail a request. The write error path is unchanged and still fail-open.
  • The test's mutation claim. With d.Session raw, the name is ask-x/../../escape-001.json. filepath.Join cleans that to <parent>/escape-001.json, so a file lands in the parent and the test fails. With the fix, one file lands inside the dump directory. Both assertions hold.
  • Behavior change. None for any id Scoped can produce. The dump stays off by default. No human-permission rule is triggered.
  • Data dependence. The PR statement is right: the dump already holds conversation text; only the file name changed. docs/data-dependence.md needs no change from this PR.
  • Unicode and length. strings.Map turns an invalid byte or a multi-byte rune into one _, so no separator can be synthesized. A reachable id is at most ~150 bytes, under the 255-byte file-name limit. A longer id fails WriteFile, which warns and returns.

@amiddavid
amiddavid merged commit 73f27be into main Nov 8, 2026
11 of 12 checks passed
@github-project-automation github-project-automation Bot moved this from New/ToDo to Done in Rossoctl Issue Prioritization Nov 8, 2026
amiddavid added a commit that referenced this pull request Nov 8, 2026
Round 1 of #960, non-blocking points:
- The CodeQL check run must come from GitHub's code-scanning app
  (github-advanced-security), so a job renamed to CodeQL cannot pass
  the check with no scan behind it.
- A passing check run can still carry an alert. The script now also
  reads the open code-scanning alerts on the PR's merge ref, and a
  failed read stops it with an error.
- The messages say what to do: the PR must target main to be scanned,
  and a run still in progress means wait.

Tested with a stub gh: a clean PR is READY; an open alert gives NOT
READY with the alert; a failed alert read gives an ERROR. On real
PRs: #951 and #960 pass the scan check; #943 fails it.

Assisted-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: DAVID AMID <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant