Skip to content

fix: refuse an auto-improvement session key ending in a newline - #34238

Merged
KiroCrewCaptain merged 1 commit into
mainfrom
fix/32712-session-key-newline
Nov 10, 2026
Merged

KiroCrewCaptain merged 1 commit into
mainfrom
fix/32712-session-key-newline

Conversation

@JLargent42

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: An Auto Improvement session key that ends in a newline is refused with 400 invalid_request on every platform, instead of failing with a 500 on Windows.

_validate_key in auto_improvement/backend/store.py applies ^[A-Za-z0-9][A-Za-z0-9._-]{0,249}$ with re.match. $ also matches just before a final \n, so pr-o-r-1\n (a %0A in /api/apps/auto-improvement/sessions/{key}) passes the gate. On Windows the resulting <key>.json name is refused with an OSError, which _handle_save_session and _handle_delete_session do 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

  • Changing the key's character set or its 250-character bound.
  • Mapping OSError to a 4xx in the session handlers.
  • Auditing other $-anchored patterns outside the session-record gate (see Sibling sweep).

What changed (motivation → approach → change)

  • Symptom: PUT/DELETE of .../sessions/pr-o-r-1%0A is a 500 on Windows.
  • Root cause: re.match with a $ anchor admits a trailing newline.
  • Change: _validate_key uses _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' existing ValueError branch then answers 400. The auto-improvement spec's key-validation paragraph states the fullmatch rule.

Backwards compatibility

Breaking: a session key ending in \n is now refused with 400 on POSIX, where it was accepted. Writer sweep on origin/main @ 73e3275: the only writer is the frontend sessionKey(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.json record 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_accepted
Reader: 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_redacted

Tests

  • test_dogfood_learnings_clone.py::TestALongRepoKeyCanStillPersist::test_a_trailing_newline_is_rejected: _validate_key refuses pr-o-r-1\n and 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 400 invalid_request for that key and write no record.
  • Unchanged and passing: the plain-key, 250-character and rejected-shape tests in TestALongRepoKeyCanStillPersist and TestSessions.

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 raises ValueError. Both touched test files pass in full (260 tests). local-gate.py shows 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_RE is already anchored with \Z
Sibling: 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_RE is a prefix check on a URL, not a file name gate

Pattern harvest

Rule candidate: semgrep
Pattern: a $-anchored regex applied with re.match to validate a value that becomes a file name; use fullmatch or \Z

Checklist

  • At most two commits (one is the norm), with a Conventional Commits title (feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable)
  • No secrets, credentials, or internal references in the diff

Contribution License Agreement

Contributions are accepted under the project's Apache-2.0 license; see the Licensing section of CONTRIBUTING.md.

_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
@JLargent42
JLargent42 requested review from a team and KiroCrewCaptain as code owners November 10, 2026 00:29
@JLargent42 JLargent42 self-assigned this Nov 10, 2026
@github-actions github-actions Bot added the readiness: checking Automated validation is still running label Nov 10, 2026
@github-actions

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5) — ⏭️ skipped

Revision 5eb8fbccc4aeccc7993b54a805af3860a2b43488 touches no user-facing surface (no changes under website/ or committed screenshots), so the UX review was skipped. Does not block merge.

@github-actions

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5) — ✅ PASS

Design-level review of 5eb8fbccc4aeccc7993b54a805af3860a2b43488 — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

Design-Verdict: PASS

Nothing blocks: this is a root-cause fix that uses the same fullmatch change #18781 already made for the artifact routes, and the only thing it takes away (a key ending in a newline) has no writer.

[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 .github/scripts/pr-description-capture.sh over the same inputs; if it differs, the description or that evidence changed after this verdict was formed and any finding drawn from either is unproven.

@github-actions

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5) — ✅ PASS

Premise-level review of 5eb8fbccc4aeccc7993b54a805af3860a2b43488 — why this exists and whether the shipped surface is the smallest honest version. Updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

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 ships

Inventory (4 items) — 4 justified

Intent: 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).

  1. A session key with a trailing newline is now refused with 400 invalid_request on PUT and DELETE, on every platform. — justified
  2. Breaking on POSIX: such a key was accepted there and now is not. The description declares it with a writer sweep. — justified
  3. The spec paragraph now states that keys are checked with fullmatch. — justified
  4. Two regression tests pin the refusal at the validator and at the real handlers. — justified

[FIRST-PRINCIPLES-REVIEWED] 5eb8fbc

Description read

[DESCRIPTION-READ] 68eebd563604d2344431df20d8026e576ab395db64b723aaebf0ece333475c09

That sha256 names the PR description this verdict read. Recompute it with .github/scripts/pr-description-capture.sh; if it differs, the description changed after this verdict was formed and any finding drawn from the description is unproven.

@github-actions

Copy link
Copy Markdown
Contributor

Opus 5.5 Review — ✅ no blocking findings

Reviewed 5eb8fbccc4aeccc7993b54a805af3860a2b43488 — this comment is updated in place on each push.

Review details

No findings.

[OPUS-REVIEWED] 5eb8fbc

Verdict parsed from the review's SHA-scoped output markers for commit 5eb8fbccc4aeccc7993b54a805af3860a2b43488.

False positive or not applicable? A repository writer can comment:
/ai-review override fable 5eb8fbccc4aeccc7993b54a805af3860a2b43488 false-positive: <reason, 20 to 500 characters>

@github-actions

Copy link
Copy Markdown
Contributor

GPT 6.1 Review — ✅ no blocking findings

GPT 6.1 completed its review of 5eb8fbccc4aeccc7993b54a805af3860a2b43488 and found no blocking issues.

This comment is updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] 5eb8fbc

False positive or not applicable? A repository writer can comment:
/ai-review override gpt 5eb8fbccc4aeccc7993b54a805af3860a2b43488 false-positive: <reason, 20 to 500 characters>

@github-actions github-actions Bot added readiness: passed Eligible automated validation passed for the current revision and removed readiness: checking Automated validation is still running labels Nov 10, 2026
@KiroCrewCaptain
KiroCrewCaptain enabled auto-merge (squash) November 10, 2026 01:40

@KiroCrewCaptain KiroCrewCaptain left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_config code, not invalid_request. A short PR comment could note this. No code change is needed.

@KiroCrewCaptain
KiroCrewCaptain merged commit 8a75ba3 into main Nov 10, 2026
98 checks passed
@KiroCrewCaptain
KiroCrewCaptain deleted the fix/32712-session-key-newline branch November 10, 2026 01:40
@github-actions github-actions Bot removed the readiness: passed Eligible automated validation passed for the current revision label Nov 10, 2026
KiroCrewCaptain added a commit to KiroCrewCaptain/kirocrew-app-auto-improvement that referenced this pull request Nov 13, 2026
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>
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.

auto-improvement: a session key ending in a newline passes _validate_key, so on Windows the session PUT and DELETE fail with 500 instead of 400

2 participants