Skip to content

Anchor session-id validation and bound REMEMBER_BRANCH_CMD output to one line (#500, #501) - #520

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

fdaviddpt merged 2 commits into
mainfrom
fix/500

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

Closes #500
Closes #501

#500: option-shaped session_id reaches REMEMBER_BRANCH_CMD's argv[1]

scripts/save-session.sh's session-id validator (^[a-f0-9-]+$) admitted a leading hyphen, so an option-shaped id (-e, --, -adef) could reach $REMEMBER_BRANCH_CMD "$SESSION_ID"'s argv[1] and be misread as a flag by whatever argument parser the operator's own resolver uses.

Fix chosen: anchor the validator, not add a -- separator at the call site. scripts/save-session.sh:197 is the single choke point every caller (CLI, session-end-hook.sh's STDIN_SESSION_ID, session-start-hook.sh's recovery path) passes through before Step 3's branch resolution -- confirmed by the self-review's Explore pass, which grepped every caller and found none bypasses this gate. Anchoring there is cheaper than fixing each caller, and unlike inserting -- before $SESSION_ID, it does not silently shift the documented argv[1] contract for every existing resolver (a -- separator would move the session id to argv[2], breaking any resolver written against the README's current $REMEMBER_BRANCH_CMD "$SESSION_ID" documentation). Regex: ^[a-f0-9-]+$ -> ^[a-f0-9][a-f0-9-]*$.

#501: REMEMBER_BRANCH_CMD's stdout substituted into the summarizer prompt unbounded

REMEMBER_BRANCH_CMD's stdout becomes $BRANCH and is substituted verbatim into pipeline/prompts.py's {{BRANCH}}. $(...) only strips a trailing newline, so a resolver printing more than one line would write arbitrary content at column 0 of the prompt.

Fix: reject the result outright on any embedded newline, falling through to the git branch --show-current lookup and logging a WARNING -- the same fall-through-and-log shape #481 already uses for a non-zero exit or empty stdout, rather than silently truncating to the first line (a truncation a reader of the log could not tell apart from the resolver only ever having meant to print one line).

Self-review caught a gap in the first pass: the guard only matched an embedded LF, not a lone CR with no LF (printf "part1\rpart2"), which a naive terminal reads as "return to column 0 mid-line" the same way an embedded LF does. Widened to catch both (*$'\n'*|*$'\r'*)). The regression test for this (test_branch_cmd_carriage_return_falls_through_to_git) captures raw subprocess bytes rather than text-mode output, because Python's subprocess.run(..., text=True) silently rewrites a lone \r to \n on read -- a naive version of the test would have passed whether or not the fix actually caught \r.

Tests

All four new negative cases (three option-shaped-id parametrizations for #500, one multi-line-resolver case for #501) plus the CR follow-up were watched red against the pre-fix code before their respective fixes landed, and green after. Positive controls pair each: an ordinary hyphenated session id is still accepted, and an ordinary single-line resolver output is still used and still silent. Targeted run (tests/test_save_session_branch_override.py plus five adjacent files touching session-id handling, prompt substitution and save-session gating): 120 passed. Full repo test_command not re-run locally, per this repo's own CLAUDE.md and the loop's #765 guidance -- CI is the merge gate.

Self-review

Spawned Explore and oss:auditor against the diff. Explore returned NO FINDINGS. oss:auditor returned two findings:

Docs / changelog

README.md's REMEMBER_BRANCH_CMD row documents both the anchored SESSION_ID contract and the single-line stdout expectation. Two changelog fragments (changelog.d/500.fixed.md, changelog.d/501.fixed.md).

…t to one line (#500, #501)

#500: save-session.sh's SESSION_ID validator (`^[a-f0-9-]+$`) admitted a
leading hyphen, so an option-shaped id ("-e", "--", "-adef") could reach
REMEMBER_BRANCH_CMD's argv[1] and be read as a flag by whatever argument
parser the operator's own resolver happens to use. Anchored the regex to
`^[a-f0-9][a-f0-9-]*$` -- the single choke point every caller (CLI,
session-end-hook.sh, session-start-hook.sh's recovery path) passes through
before Step 3 -- rather than fixing each caller individually or adding a
`--` separator at the call site (which would silently shift the documented
argv[1] contract for every existing resolver).

#501: REMEMBER_BRANCH_CMD's stdout became $BRANCH and was substituted
verbatim into the summarizer prompt with no newline bound of its own --
$(...) only strips trailing newlines, so a multi-line resolver result would
write arbitrary content at column 0 of the prompt. A multi-line result is
now refused outright, falling through to the git-branch lookup and logging
a WARNING the same way a non-zero exit or empty stdout already does,
matching the repo's #481 convention that a configured-but-failing resolver
reads as a reported fault rather than silent truncation.

Both fixes are TDD: tests/test_save_session_branch_override.py gained an
option-shaped-id case (parametrized -adef/--/-e) and a positive control
(an ordinary hyphenated id still accepted) for #500, and a multi-line
resolver case plus its positive control (a normal single-line resolver
still used, still silent) for #501 -- all four new negative cases were
watched red against the pre-fix code before the fix landed.

README.md's REMEMBER_BRANCH_CMD row now documents both: SESSION_ID is
guaranteed hex-leading before the resolver ever sees it, and the
resolver's own stdout is expected to be a single line.

Closes #500
Closes #501

Co-Authored-By: Max <noreply>
Self-review finding (oss:auditor, class C): the multi-line rejection landed
in the previous commit only matched an embedded LF (`case ... in *$'\n'*)`).
A resolver emitting a lone CR with no LF (`printf "part1\rpart2"`) is not
multi-line by that check, so $(...)  -- which only strips a trailing LF --
would still let it through to $BRANCH unbounded. A bare CR can reposition a
naive terminal's cursor to column 0 mid-line the same way an embedded LF
can, which is the exact concern #501 names, just via a different control
character. Widened the case pattern to `*$'\n'*|*$'\r'*)`.

New test (test_branch_cmd_carriage_return_falls_through_to_git) is written
to actually observe the CR: subprocess's text=True path silently rewrites a
lone '\r' to '\n' on read (Python's universal-newline translation), which
would have made a naive version of this test pass whether or not the fix
caught '\r' at all -- the test captures raw bytes and decodes without that
translation instead. Watched red against the pre-fix case pattern, green
after the widen.

README.md and the #501 changelog fragment updated to mention both control
characters.

Co-Authored-By: Max <noreply>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant