Repository navigation
fix: refuse an auto-improvement session key ending in a newline - #34238
Conversation
_validate_key used re.match with a $-anchored pattern, and $ also matches just before a trailing newline, so a key like 'pr-o-r-1\n' (from %0A in the URL) passed the gate. On Windows the file name is refused with an OSError the handlers do not catch, so PUT and DELETE answered 500 instead of 400. Apply the pattern with fullmatch so the key is refused with 400 invalid_request on every platform. Fixes #32712
UX Review (Opus 5.5) — ⏭️ skippedRevision |
Design Review (Opus 5.5) — ✅ PASSDesign-level review of Design-Verdict: PASS Nothing blocks: this is a root-cause fix that uses the same [DESIGN-REVIEWED] 5eb8fbc Description read[DESCRIPTION-READ] 5bef77fb0fcc84b8f7201fae903330810c675aa69e22d63bb89cb9d5a21e11c9 That sha256 names everything this verdict read: the PR description, plus the 1 evidence file(s) the workflow collected from it and handed this review. It covers that evidence because the media strip replaces every attachment URL with the same placeholder, so an edit swapping one attachment for another would leave the description's captured text identical while what this review judged changed. Recompute it with |
First Principles Review (Opus 5.5) — ✅ PASSPremise-level review of First-Principles-Verdict: PASS Nothing blocks: this is a one-call fix at the mechanism that admits the bad key, with tests that fail on base. What this change shipsInventory (4 items) — 4 justifiedIntent: let a client that sends an Auto Improvement session key ending in a newline get a clean 400 rejection instead of a Windows 500 or a POSIX file name containing a newline. This is a FIX (#32712).
[FIRST-PRINCIPLES-REVIEWED] 5eb8fbc Description read[DESCRIPTION-READ] 68eebd563604d2344431df20d8026e576ab395db64b723aaebf0ece333475c09 That sha256 names the PR description this verdict read. Recompute it with |
Opus 5.5 Review — ✅ no blocking findingsReviewed Verdict parsed from the review's SHA-scoped output markers for commit False positive or not applicable? A repository writer can comment: |
GPT 6.1 Review — ✅ no blocking findingsGPT 6.1 completed its review of This comment is updated in place on each push. Review detailsNo findings. False positive or not applicable? A repository writer can comment: |
KiroCrewCaptain
left a comment
There was a problem hiding this comment.
Approving. On this head, the review confirmed that _validate_key in store.py now uses fullmatch. It rejects pr-o-r-1\n and a 250-character key followed by \n, it still accepts valid keys up to 250 characters, and the character set is unchanged. The one caller, session_path, feeds GET, PUT and DELETE, and each of those handlers already turns the ValueError into a 400. The review also found that the frontend sessionKey builder always ends a key in a hex digit, so no in-tree writer can produce a key the fix now refuses. Both new tests fail when the hunk is reverted, and the spec sentence in auto-improvement.md is updated in the same commit.
Done well: the fix is in the one shared validator, not in each handler. It follows the #18781 precedent and is pinned at both the validator and the real route handlers.
Optional:
- The GET route answers 400 with its existing
invalid_configcode, notinvalid_request. A short PR comment could note this. No code change is needed.
Ports, with their tests, the fixes the Kiro Crew wheel copy took after the GitLab change: - kirodotdev/KiroCrew#33556: on Windows the dependency check resolves git, gh and glab from PATH's absolute entries only. - kirodotdev/KiroCrew#33565: pinning git attributes refuses a Windows directory junction as it refuses a symlink. - kirodotdev/KiroCrew#34238: a session key ending in a newline is refused. - kirodotdev/KiroCrew#33510: member and runner-built sessions name this app as their owning_app, so their governance reads are judged under the same app as their tool calls. - kirodotdev/KiroCrew#35779: the suite's git reads no host global config, so a host core.hooksPath cannot refuse a fixture commit. The runner coverage docstring drops its stale location claim (kirodotdev/KiroCrew#35994). Co-authored-by: KiroCrewCaptain <312312717+KiroCrewCaptain@users.noreply.github.com>
Problem / Motivation
Goal: An Auto Improvement session key that ends in a newline is refused with 400
invalid_requeston every platform, instead of failing with a 500 on Windows._validate_keyinauto_improvement/backend/store.pyapplies^[A-Za-z0-9][A-Za-z0-9._-]{0,249}$withre.match.$also matches just before a final\n, sopr-o-r-1\n(a%0Ain/api/apps/auto-improvement/sessions/{key}) passes the gate. On Windows the resulting<key>.jsonname is refused with anOSError, which_handle_save_sessionand_handle_delete_sessiondo not catch, so PUT and DELETE answer 500.Why it matters
The gate promises that an unsafe key is rejected with a 400 before it reaches the file system. On Windows a client gets an unhandled 500 instead, and on POSIX a record is created under a file name containing a newline.
Not a goal
OSErrorto a 4xx in the session handlers.$-anchored patterns outside the session-record gate (see Sibling sweep).What changed (motivation → approach → change)
.../sessions/pr-o-r-1%0Ais a 500 on Windows.re.matchwith a$anchor admits a trailing newline._validate_keyuses_SAFE_KEY_RE.fullmatch(...), the fix fix(artifacts): anchor the origin session key at the true end of input #18781 used for the artifact routes. The handlers' existingValueErrorbranch then answers 400. The auto-improvement spec's key-validation paragraph states thefullmatchrule.Backwards compatibility
Breaking: a session key ending in
\nis now refused with 400 on POSIX, where it was accepted. Writer sweep on origin/main @ 73e3275: the only writer is the frontendsessionKey(kind, id, repo), whose sanitizer emits only[A-Za-z0-9._-]plus an FNV-1a hash, so it never produces a trailing newline.Reader: src/kiro_crew/apps/builtins/auto_improvement/backend/store.py:load_session -- app -- a stored
<key>\n.jsonrecord is unreachable by any key the frontend builds, so no reader loses a record it could find before;test_a_max_length_github_pr_key_is_acceptedReader: src/kiro_crew/apps/builtins/auto_improvement/backend/store.py:list_sessions -- app -- lists files by glob, not through
_validate_key, so its output is unchanged;test_listing_is_redactedTests
test_dogfood_learnings_clone.py::TestALongRepoKeyCanStillPersist::test_a_trailing_newline_is_rejected:_validate_keyrefusespr-o-r-1\nand a 250-character key plus\n.test_ai_backend_routes_coverage.py::TestSessions::test_a_key_with_a_trailing_newline_is_a_400: the real PUT and DELETE handlers answer 400invalid_requestfor that key and write no record.TestALongRepoKeyCanStillPersistandTestSessions.Proof: PROVEN src/kiro_crew/apps/builtins/auto_improvement/tests/test_ai_backend_routes_coverage.py::TestSessions::test_a_key_with_a_trailing_newline_is_a_400, src/kiro_crew/apps/builtins/auto_improvement/tests/test_dogfood_learnings_clone.py::TestALongRepoKeyCanStillPersist::test_a_trailing_newline_is_rejected (head 5eb8fbc)
Manual verification
Reproduced on main 73e3275:
store._validate_key('pr-o-r-1\n')returns the key. With the fix it raisesValueError. Both touched test files pass in full (260 tests).local-gate.pyshows 3 failures that also fail on unmodified main on this host (missing boto3, a managed essential-source check, a symlink-window test); none touch this code. Not run on Windows.Related Issues
Fixes #32712
Sibling sweep
Swept: git grep -n 're.compile(r"^' -- src/kiro_crew/apps/builtins/auto_improvement/backend/ and git grep -n '_RE.match(' -- src/kiro_crew/apps/builtins/auto_improvement/backend/
Sibling: src/kiro_crew/apps/builtins/auto_improvement/backend/store.py:49 -- changed here
Sibling: src/kiro_crew/apps/builtins/auto_improvement/backend/ledger_admin.py:96 -- not affected --
_FP_REis already anchored with\ZSibling: src/kiro_crew/apps/builtins/auto_improvement/backend/clone_setup.py:1099 -- not affected -- the branch name goes to git argv, never a file name, and git refuses a newline in a ref with an error the caller returns
Sibling: src/kiro_crew/apps/builtins/auto_improvement/backend/ledger_admin.py:85 -- not affected --
_PR_URL_REis a prefix check on a URL, not a file name gatePattern harvest
Rule candidate: semgrep
Pattern: a
$-anchored regex applied withre.matchto validate a value that becomes a file name; usefullmatchor\ZChecklist
feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)Contribution License Agreement
Contributions are accepted under the project's Apache-2.0 license; see the Licensing section of CONTRIBUTING.md.