Skip to content

Harden cmd_save_position: validate session_id before path use (#538) - #541

Merged
fdaviddpt merged 2 commits into
mainfrom
fix/538
Sep 4, 2026
Merged

fdaviddpt merged 2 commits into
mainfrom
fix/538

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

What

cmd_save_position (pipeline/shell.py) built the position sidecar path
(position.<session_id>) and the evicted-sidecar removal path by
interpolating session_id directly into a filename, without ever calling
pipeline.extract._validate_session_id -- the same check find_session()
already runs before its own equivalent join.

Both current shell callers (scripts/post-tool-hook.sh,
scripts/session-end-hook.sh, scripts/save-session.sh) already filter the
id before Python ever sees it, so nothing reachable today was exploitable;
this is hardening, not a vulnerability fix, matching the issue's own
classification -- a future caller reaching cmd_save_position by another
route now inherits the same guarantee instead of nothing.

Change

  • cmd_save_position now calls _validate_session_id(session_id) at the
    top, matching find_session.
  • Each evicted_id in the sidecar-cleanup loop is also validated, inside
    the existing try/except so a poisoned legacy id (a store written before
    this fix, or hand-edited) degrades to "sidecar survives a little late" --
    exactly what the surrounding comment already documents for a failed
    unlink -- rather than raising an uncaught ValueError that would abort
    the save already in progress.

Self-review

Two reviewers (Explore, oss:auditor) were spawned against the first
commit and both independently found the same regression: the eviction-loop
validation, in its first form, sat outside the try/except, so a poisoned
legacy id would raise uncaught mid-save -- worse than doing nothing, since
none of the shell callers check the exit status of save-position. Fixed
in the second commit, with a regression test proven red against the
un-fixed shape and green against the fixed one.

Below the bar, not fixed here: _validate_session_id itself (unchanged
by this diff, in pipeline/extract.py) rejects /, \ and .. but not
:, so a colon-bearing session_id would reach position.<id> and, on
NTFS, could land as an Alternate Data Stream on position.<prefix> rather
than a distinct file. This is a pre-existing gap in a function this diff
only calls, shared with find_session and every other caller -- fixing it
is a decision about that function's own contract, out of this issue's blast
radius. Reported for filing as a separate issue rather than fixed here.

Tests

  • tests/test_save_position_validates_session_id_538.py (new): a hostile
    session_id (../../etc/evil, foo/bar, foo\bar, ..) passed
    directly to cmd_save_position must raise, paired with a positive
    control that a well-formed id still writes its sidecar -- red before the
    fix (4 failed, 1 passed), green after.
  • Same file: a poisoned id already present in the store's eviction slot
    must not abort the save -- red against the first-commit shape (1 failed),
    green after moving the validation inside the try.
  • Ran narrowed: pytest tests/test_save_position_validates_session_id_538.py tests/test_position_store.py tests/test_position_sidecar_353.py -- 57
    passed. Full test_command intentionally not run locally; CI is the
    merge gate.

Closes #538

pipeline/shell.py built the position sidecar path and the evicted-sidecar
removal path by interpolating session_id directly into a filename, without
calling pipeline.extract._validate_session_id -- the same check
find_session() already runs before its own equivalent join.

Both shell callers filter the id before Python ever sees it, so this is
hardening rather than a fix for a reachable exploit: a future caller
reaching cmd_save_position by another route now inherits the same
guarantee instead of nothing.

Add the check at the top of cmd_save_position, and on each evicted id in
the sidecar-cleanup loop, so the loop is independent of that argument.

Co-Authored-By: Max <noreply>
Both spawned reviewers found the same regression: the new
_validate_session_id(evicted_id) call sat outside the existing
try/except OSError block, so a legacy or hand-edited store holding an
already-invalid key in `sessions` would now raise ValueError out of
cmd_save_position instead of just being a best-effort skip -- aborting
the save's own trailing #450 quarantine bookkeeping, and doing so
silently, since none of the shell callers check the exit status.

Move the validation inside the existing try, catching ValueError
alongside OSError so a poisoned evicted id degrades to "sidecar
survives a little late" exactly as #353 already documents, rather
than crashing. Add a regression test (poisoned legacy eviction id,
still evicted, save still lands) proven red against the un-fixed
shape and green against this one.

Co-Authored-By: Max <noreply>
@fdaviddpt
fdaviddpt merged commit c0ad3a9 into main Sep 4, 2026
13 checks passed
@fdaviddpt
fdaviddpt deleted the fix/538 branch September 4, 2026 19:58
fdaviddpt added a commit that referenced this pull request Sep 4, 2026
On NTFS a colon in a filename is read as an Alternate Data Stream
separator (`filename:stream`), so a colon-bearing session_id joined
into `position.<session_id>` could silently land as an ADS on an
existing file rather than a distinct file of its own.

_validate_session_id() already rejected '/', '\' and '..'; this adds
':' to the same check, shared by find_session() and (since #538/#541)
cmd_save_position() and its eviction-loop cleanup.

No caller today supplies a colon-bearing session_id -- all current
callers pass Claude Code's own UUID-shaped ids -- so this is hardening
against a future caller, the same framing #538 used for the traversal
check.


Claude-Session: https://claude.ai/code/session_01Bv6BGZD2EdZ9EuJ7tPDTb7

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Harden cmd_save_position: validate session_id before it becomes a path component

1 participant