Repository navigation
fix(artifacts): anchor the origin session key at the true end of input - #18781
Conversation
UX Review (Opus 5.5) — ⏭️ skippedRevision |
First Principles Review (Opus 5.5) — ✅ PASSPremise-level review of First-Principles-Verdict: PASS The fix only covers Not justified as shipped
What this change shipsInventory (4 items) — 3 justifiedIntent: an artifact saved with a stray trailing newline in its origin session key should be attributed to no session, not stored under a key nobody can select. This is a FIX (#18727, reproduced on main, with a test that fails on base).
[FIRST-PRINCIPLES-REVIEWED] 9370f21 Description read[DESCRIPTION-READ] afa7d25dd497580ffb026d4c104c0adae50a8ee1b254bc25de96c961957d902b That sha256 names the PR description this verdict read. Recompute it with |
Design Review (Opus 5.5) — ✅ PASSDesign-level review of Design-Verdict: PASS Nothing to check. [DESIGN-REVIEWED] 9370f21 Description read[DESCRIPTION-READ] 3ace57da46ca944c1b1a4ffbfbbdaceab7ba75834c8ebbf8e885be55c5e46215 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 |
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: |
|
CI note for head aca11ef: the red checks on this head are inherited from main, not caused by this change.
This PR changes 4 files (src/kiro_crew/dashboard/handlers/artifacts.py, test/test_artifacts_handlers.py, test/test_regex_anchor_contract.py, docs/system-specs/modules/artifacts.md) and touches neither the website nor the index. main is red on the same cause at this PR's base d3f462f: Build run https://github.com/kirodotdev/KiroCrew/actions/runs/38036318549 (Build Wheel and all Build Desktop jobs fail on the same gen-ui-index message) and CI run https://github.com/kirodotdev/KiroCrew/actions/runs/38036318449 (Frontend Tests (1), E2E (stub ACP backend, offline) and Coverage Gate fail there too). No rerun: the failure is deterministic until main carries the regenerated index. The index is not regenerated in this PR. This branch will be rebased once main carries the fix (#18753 regenerates the index). |
The dashboard artifact create and conversation-log materialize routes validate a client-supplied origin_session_key with a `$`-anchored pattern applied through `.match`. Python's `$` also matches immediately before a final newline, so "dashboard:abc\n" passed the validator and was stored unchanged; the store compares session keys exactly, so the artifact never matched the real session again in the session filter or the Source column. Anchor the pattern at `\Z`, the true end of input, the same shape PR #12892 used for the two artifact tag validators. The one sibling in the same handler, the publish-provider name gate, is matched raw with `.match` too and gets the same anchor. The grammar itself and the store are unchanged: a key with a trailing newline now collapses to the no-origin value exactly as every other value outside the grammar does. Tests: the red case `_clean_origin_session_key("dashboard:abc\n") == ""` plus valid keys, embedded newlines and non-string values in test_artifacts_handlers.py; both handler patterns registered in the trailing-newline contract test. Fixes #18727
aca11ef to
9370f21
Compare
iamwhatever
left a comment
There was a problem hiding this comment.
This fixes _clean_origin_session_key accepting "dashboard:abc\n": under .match, Python's $ also matches before a final newline. Such a key was stored as-is and never matched its real session again. The root cause is fixed: _SESSION_KEY_RE and its sibling _ARTIFACT_PROVIDER_RE in src/kiro_crew/dashboard/handlers/artifacts.py:126,222 now end in \Z, the same shape #12892 used for the tag validators. Character set and length cap are unchanged. Behaviour changes only for values ending in a newline, which now collapse to "" (no origin) or get the existing 400 "provider must match" error. The ?session= read filter deliberately keeps the raw value, so nothing widens there. The tests prove the fix. TestCleanOriginSessionKey's trailing-newline cases and the two new test_regex_anchor_contract entries fail on main and pass here, and the accept cases pin the grammar. All checks are green on 9370f21, every AI lane passes, and there are no reviews or change requests. Small, correct and safe. — crew-pr-reviewer
Problem / Motivation
Goal: An artifact saved with an origin session key that carries a trailing newline is attributed to no session, like any other value outside the key grammar, instead of being stored under a key no session has.
The dashboard's artifact create route (
POST /api/artifacts) and the conversation-log materialize route (POST /api/artifacts/materialize) accept anorigin_session_keysuch as"dashboard:abc\n"and store it unchanged. The validator's docstring says the value must match the permitted grammar, and the grammar has no newline in it. The artifact store compares session keys exactly, so an artifact stored this way never shows up in the?session=filter or under the right Source entry for the realdashboard:abcsession.Reproduced on
mainwith the real helper:Why it matters
Any client that sends a key with a stray trailing newline (a copied value, a script that reads a line from a file) gets an artifact that exists but belongs to a session nobody can select. The in-session Artifacts tab does not list it, and the Source column cannot resolve it. The same gap was closed for artifact tags in #12892 (reported as #10900); this validator was left on the old anchor.
Not a goal
?session=and?touched_by=read filters handle a validation miss (they keep the raw value by design, so a miss returns an empty list rather than another session's artifacts).$shape and that sweep is a separate change.^[a-z0-9-]{1,32}$. The characters and length it names are exactly what the code enforces; only the anchor spelling differs, and a Python-specific\Zwould read worse to a user than it helps.What changed (motivation → approach → change)
Symptom: a key with one trailing newline passes
_clean_origin_session_keyand is stored as-is.Root cause:
_SESSION_KEY_REwas^[A-Za-z0-9:_.\-]{1,128}$and was applied with.match. In Python,$matches at the end of the string and also immediately before a final newline, so"dashboard:abc\n"satisfies the pattern.Change: the pattern now ends in
\Z, which matches only at the true end of the input. This is the same shape #12892 applied to the two tag validators. The one sibling in the same handler file, the publish-provider name gate_ARTIFACT_PROVIDER_RE(^[a-z0-9-]{1,32}$, also applied raw with.match), gets the same anchor. Nothing else in the handler, the grammar or the store changes.src/kiro_crew/dashboard/handlers/artifacts.py_SESSION_KEY_REand_ARTIFACT_PROVIDER_REanchored at\Z; comments state whytest/test_artifacts_handlers.pyTestCleanOriginSessionKey: the trailing-newline cases, valid keys, other newline placements, non-string and over-long valuestest/test_regex_anchor_contract.pydocs/system-specs/modules/artifacts.mdBehaviour after the change: a key outside the grammar, trailing newline included, collapses to
""(no originating session) on the two write routes, exactly as a non-string, an empty string, an over-long key or an illegal character does today. A provider name with a trailing newline is now rejected at the regex gate with the existingprovider must matcherror instead of reaching the provider lookup; that gate answers 400 before any audit record is written, as it does for every other malformed provider name onmain.Backwards compatibility
Compatible: every value the grammar describes still passes; the only inputs that change outcome are ones ending in a newline, which no client sends on purpose and which the grammar never admitted. The three origin-key writers on
main(the dashboard client'screateArtifactandmaterializecalls,WidgetFrame's save, and the MCPartifact_savetool, which passes the resolved session key) all send a bare slot key with no newline.Removes nothing: the validator tightens by one character position; no route, field, kind or function is removed or renamed.
Tests
test/test_artifacts_handlers.py::TestCleanOriginSessionKeytest_trailing_newline_collapses_to_no_origin:_clean_origin_session_key("dashboard:abc\n") == ""and the same with a 128-character key plus newline. Red onmain(AssertionError: assert 'dashboard:abc\n' == ''), green with the fix.test_key_inside_the_grammar_is_returned_unchanged:chat-2,dashboard:chat-2,cron:foo, a Slackts, a 128-character key.test_any_other_newline_placement_still_collapses: embedded, leading and double trailing newline.test_values_outside_the_grammar_still_collapse:None, an int, a list,"", 129 characters, a space, markup.test/test_regex_anchor_contract.py:artifact-origin-session-keyandartifact-publish-providerentries. Bothtest_valid_input_plus_newline_is_rejectedcases were red onmainand are green with the fix; the accept cases pass on both.Gates run locally on the branch: black gate, isort, flake8, mypy, subprocess-encoding gate, comment-history gate, docs lint, and
scripts/local-gate.py(the change-related backend and frontend sets).Manual verification
N/A, unit coverage sufficient: the change is a one-token anchor swap on two compiled patterns, and the unit tests exercise the exact helper the two HTTP routes call with the exact value from the report.
Screenshots / video
No user-visible UI change: a backend validator anchor, no frontend file touched.
Related Issues
Fixes #18727
Same defect class as #10900, fixed for tags in #12892.
Pattern harvest
Rule candidate: semgrep
Pattern: a
re.compilepattern ending in$(withoutre.MULTILINE) applied with.matchor.searchto client-supplied input;$also matches before a trailing newline, so the validator admitsvalid + "\n". Anchor with\Zor usefullmatch.test/test_regex_anchor_contract.pyalready enforces this structurally for the packages it registers; the dashboard handlers package is not yet one of them.Checklist
feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)Contribution License Agreement
Contributed under the project's Apache-2.0 license, per the Licensing section of CONTRIBUTING.md.