Skip to content

Harden cmd_save_position: validate session_id before it becomes a path component #538

Closed

Description

@fdaviddpt

What

pipeline/shell.py:379 builds the position sidecar path by interpolating the
session id directly:

sidecar = os.path.join(sidecar_dir, f"position.{session_id}")

and :398 removes evicted sidecars the same way. Neither calls
_validate_session_id (pipeline/extract.py:251), which every other
session-id-to-path derivation in the package goes through — find_session
(extract.py:289) validates before joining.

Why this is hardening and not a vulnerability

I could not construct an exploit, and this should not be filed or fixed as a
security issue:

  • Both callers filter the id before Python ever sees it.
    scripts/post-tool-hook.sh:625, :679 and scripts/session-end-hook.sh:132
    reject '', ., .. and anything matching *[!A-Za-z0-9._-]*.
    scripts/save-session.sh:197 independently re-validates against
    ^[a-f0-9][a-f0-9-]*$.
  • The mandatory position. prefix means even a hostile value cannot produce an
    absolute path or a leading ../. Traversal would additionally require an
    existing position.<something> directory to walk through.

So the guarantee is real, but it lives entirely in the shell callers. A future
caller reaching cmd_save_position by another route — a test helper, a new
hook, a different host's adapter — inherits nothing. The validation is one
line and the invariant stops depending on who calls.

Proposed change

Call _validate_session_id(session_id) at the top of cmd_save_position,
matching find_session. Evicted ids come from the store's own keys, so they
are covered transitively once entries can only be written with a validated id;
validating them at :398 as well is cheap and makes the loop independent of
that argument.

Test

The bar in CLAUDE.md applies: the test must fail before the change. A test
passing a session_id containing / or .. directly to cmd_save_position
and asserting it raises will pass today only if the write happens to fail for
an unrelated reason — assert on the exception, and pair it with a positive
control that a well-formed id still writes its sidecar, so the case cannot
pass by nothing happening.

Provenance

Found during a repository-wide security audit. The audit's headline result was
that no finding cleared the exploitability bar; this is one of two
defense-in-depth notes it produced. The other is the config() jq key
interpolation in scripts/log.sh:294.

Activity

  1. added
    enhancement
    priority:low
    on Sep 4, 2026
  2. self-assigned this
    on Sep 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementpriority:low

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions