fix(sessions): reject session IDs with a trailing newline - #1187
Open
shoemoney wants to merge 1 commit into
Open
fix(sessions): reject session IDs with a trailing newline#1187shoemoney wants to merge 1 commit into
shoemoney wants to merge 1 commit into
Conversation
`_validate_uuid` documents itself as "Returns the string if it is a valid
UUID, else None", but `re.match` with a `$` anchor accepts one more string
than that: Python's `$` also matches immediately before a trailing newline.
_validate_uuid("3f2504e0-4f89-11d3-9a0c-0305e82c3301\n")
-> '3f2504e0-4f89-11d3-9a0c-0305e82c3301\n'
The newline is preserved in the returned value and flows into the nine call
sites, where it becomes part of a filename:
file_name = f"{uuid}.jsonl" # sessions.py:781
That path never exists, so `get_session_info` returns None and
`get_session_messages` / `list_subagents` return [] -- indistinguishable
from a session that genuinely is not there. A session ID read from a file
or captured from command output is the common way to acquire the trailing
byte.
Switch to `fullmatch`, which anchors both ends with no newline exemption,
and drop the now-redundant `^`/`$`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The bug
_validate_uuiddocuments a precise contract (src/claude_agent_sdk/_internal/sessions.py:70):It accepts one string that is not a valid UUID. Python's
$also matches immediately before a trailing newline, sore.match(r"^...$", s)succeeds on"<uuid>\n"— and the newline is preserved in the returned value:\r\n, leading whitespace and trailing junk are all correctly rejected — it is specifically the single trailing\nthat slips through, which is the one a caller is most likely to actually have.Why it matters
There are nine call sites. The validated value is used to build a filename (
sessions.py:781):3f2504e0-...-0305e82c3301\n.jsonlnever exists, so the failure is silent and typed as "not found":get_session_infoNoneget_session_messages[]list_subagents[]A caller cannot distinguish "you passed a malformed session ID" from "that session does not exist" — which is the distinction the validator exists to make. Acquiring the stray byte is ordinary: reading an ID out of a file, or capturing it from command output.
The fix
fullmatchanchors both ends with no newline exemption, so the redundant^/$come out with it. Two lines.Verification
Against the real imported function, before and after:
Full suite and lint:
Not included
I didn't add a regression test, to keep this to the two-line change — but
tests/test_sessions.pyis the obvious home for one and it's a three-line assert. Happy to push it here if you'd rather have the behaviour pinned.I also left the other three compiled patterns in this file alone.
_SKIP_FIRST_PROMPT_PATTERN.matchis a deliberate prefix match, and the other two are used withfindall/sub, so none of them have this issue.