Repository navigation
fix(distribution): gate record keys so a control character cannot forge a row - #332
Conversation
…ge a row A record's own key list was the one payload `@tsv` emitted unescaped. Because `@tsv` keeps a raw newline inside the quotes it adds, a key carrying one split its logical row into two physical rows. Where the injected text continued with a recognized row kind and an empty record-name column, the forged row satisfied `load_manifest_rows`' column-count check and reached the `case`, which then indexed an associative array with the empty name — something Bash refuses outright, so the run died with `bad array subscript` under `set -euo pipefail` before `die` could name anything. `ckey`/`tkey` are now gated behind new `ckctl`/`tkctl` boolean rows, the same shape the `files`, `requires`, `managed`, and `locally-owned` payloads already use: the shell reads the boolean and reports `<record> keys may not contain control characters` before it reads any key payload. Gating at the point the rows are emitted makes the forged row impossible rather than defending each of the eighteen subscript writes. `load_manifest_rows` additionally rejects a row whose record-name column is empty. That is unreachable from manifest content once the gate holds, and stays as the structural backstop for any future row kind. A bare newline with no forged continuation previously surfaced as `internal error: malformed manifest row`; it now names the offending record like every other key violation. Closes #330 Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
|
Bugbot is not enabled for your account, so this pull request was not reviewed. Enable Bugbot in the Cursor dashboard to get automatic reviews on future PRs. |
|
Claude finished @kyle-sexton's task in 6m 23s —— View job Claude's review
No The fix's core design — gating One likely correctness gap — 🔴 Important
This calls A component or target map with a non-string key reaches exactly that: e.g. I wasn't able to execute Minor — test coverage asymmetry — 🟡 Nit The four new fixtures cover empty-name forgery for both components and targets, but the real-record-name-forgery and bare-newline cases are only exercised for components ( |
|
Claude has reviewed this PR 1 time. The lane skips further automatic reviews after 5; deleting this comment resets the count. |
…the pass The key gate introduced in the previous commit tested every key for control characters without first checking that the key is a string. `test` matches only strings, so an unquoted `7:`, `true:`, or `1.5:` key aborted the whole yq pass with `cannot match with !!int`, and the shell reported `could not read the component catalog` — naming neither the record nor the key. That is the same nameless-failure shape the gate exists to remove. The gate now mirrors `cfstr`: one row carries both a keys-are-strings boolean and a control-character boolean computed over `select(tag == "!!str")`, the key payload is gated on both, and the shell reports `<record> keys must be strings` before the control-character check. Filtering without that first check would instead hide a non-string key from `validate_record_keys` entirely. Renamed `ckctl`/`tkctl` to `ckstr`/`tkstr` to match the `cfstr`/`crstr`/`tmstr`/ `tlstr` naming the sibling boolean rows already use. Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
The pull_request synchronize event for 40e1e37 was lost, so no workflow runs exist for that commit. This empty commit re-fires the event.
…350) <!-- CURSOR_AGENT_PR_BODY_BEGIN --> ## Summary - Add merge-key detection so `!!merge` keys emit `merge keys are not supported` instead of the generic `keys must be strings`. - Pin the five remaining non-string key shapes in `distribution/sync-manifest.test.sh`: float, null, sequence complex key, mapping complex key, and merge key (integer/boolean were already pinned in #332). - Remove the vacuous `bad array subscript` assertion from the bare-newline fixture. ## Test plan - [x] `bash harness/shell/run-tests.sh distribution/sync-manifest.test.sh` (key-shape assertions) - [ ] CI green on merge ## Related Closes #333 <!-- CURSOR_AGENT_PR_BODY_END --> <div><a href="https://cursor.com/agents/bc-e95c74ba-a1a6-4270-b3db-3938d8c4f19e?cursor_ref=pr_footer&cursor_cta=open_in_web"><picture><source media="(prefers-color-scheme: dark)" srcset="https://cursor.com/assets/images/open-in-web-dark.png"><source media="(prefers-color-scheme: light)" srcset="https://cursor.com/assets/images/open-in-web-light.png"><img alt="Open in Web" width="114" height="28" src="https://cursor.com/assets/images/open-in-web-dark.png"></picture></a> <a href="https://cursor.com/background-agent?bcId=bc-e95c74ba-a1a6-4270-b3db-3938d8c4f19e&cursor_ref=pr_footer&cursor_cta=open_in_cursor"><picture><source media="(prefers-color-scheme: dark)" srcset="https://cursor.com/assets/images/open-in-cursor-dark.png"><source media="(prefers-color-scheme: light)" srcset="https://cursor.com/assets/images/open-in-cursor-light.png"><img alt="Open in Cursor" width="131" height="28" src="https://cursor.com/assets/images/open-in-cursor-dark.png"></picture></a> </div> Co-authored-by: Cursor Agent <[email protected]> Co-authored-by: Kyle Sexton <[email protected]>
Summary
A record's own key list was the one payload
@tsvemitted unescaped.@tsvwraps a string containing a control character in double quotes but keeps the
raw character inside, so a mapping key carrying a newline split its logical
row into two physical rows. Where the injected text continued with a recognized
row kind and an empty record-name column, the forged row satisfied
load_manifest_rows' column-count check, reached thecase, and indexed anassociative array with the empty name — which Bash refuses outright:
Under
set -euo pipefailthat fires beforediecan run, so the operator learnedneither the offending record nor the offending key.
The fix gates the rows rather than the subscripts
ckey/tkeyare now emitted only for records whose keys pass both akeys-are-strings check and a control-character check, carried on new
ckstr/tkstrrows. That mirrorscfstrexactly — the shape thefiles,requires,managed, andlocally-ownedpayloads already use — and the shellreads the two booleans in order, reporting
<record> keys must be stringsandthen
<record> keys may not contain control charactersbefore it reads any keypayload.
Both columns are needed.
testmatches only strings and aborts the entire yqpass on anything else, so a control-character check alone turns an unquoted
7:key into a nameless
could not read the component catalog. Filtering the controlcolumn through
select(tag == "!!str")stops the abort; the separate stringsboolean is what makes the resulting diagnostic accurate.
Without it, a non-string key would still be caught — the
ckeypayload uses anunfiltered
keys[], so7reachesvalidate_record_keysand is reported ascontains unknown key '7'. That message is misleading rather than absent: yqrenders the integer key as the string
7, so the manifest looks like it declaredan unknown string key when it actually declared a non-string one. The strings
column also keeps collection keys (
? [a, b]) out of ackeyrow, where asequence has no sensible rendering.
Gating at emission makes the forged row impossible, instead of defending each of
the eighteen
*["$name"]writes inload_manifest_rowsindividually. Sameargument as #329: the fix belongs where the hazard originates, not at every site
that could be hit by it.
Two yq behaviors that shaped it, both re-confirmed empirically here:
ckstrcolumns are bound after the!!mapselect, not on theunconditional
ctagrow. Akeys-derived column onctagwould evaluatekeysagainst non-map records and abort the whole pass — gotcha 2 from perf(distribution): validate the manifest in two yq passes, not 274 #320.allover an empty collect returnstrue, so a record declaring no keys(
consumer: {}) still emitsckstr true trueand still reachesvalidate_record_keysto be rejected for its missingfileskey. The existingcomponent is an empty mappingcase covers that and stays green.Structural backstop
load_manifest_rowsadditionally rejects any row whose record-name column isempty. With the gate in place that is unreachable from manifest content, so it
carries the
internal error:prefix consistent with the adjacent malformed-rowcheck, and exists so a future row kind cannot reintroduce the class.
What each diagnostic was before, and is now
Measured on both engines against the contract suite's own base manifest, so these
are observed strings rather than predictions:
COMPONENT_TAG["$name"]: bad array subscriptcomponent 'consumer' keys may not contain control charactersTARGET_TAG["$name"]: bad array subscripttarget 'beta/two' keys may not contain control charactersmanifest component pass reported 3 records, expected 2component 'consumer' keys may not contain control charactersmanifest target pass reported 3 records, expected 2target 'beta/two' keys may not contain control charactersinternal error: malformed manifest row 'plain"'component 'consumer' keys may not contain control characters7:component 'consumer' contains unknown key '7'component 'consumer' keys must be stringstrue:target 'beta/two' contains unknown key 'true'target 'beta/two' keys must be strings<<: *anchorcomponent 'consumer' contains unknown key '<<'component 'consumer' keys must be stringsManifest valid: 2 components, 2 targetsThe two crashes and the malformed-row internal error are genuine repairs. The
three non-string rows are changed, not repaired — the previous message was
already named, just misleading, because yq renders an integer key as the string
7andvalidate_record_keysthen reported it as an unknown string key ratherthan as a non-string one.
The merge key is the one row where the new message is arguably less specific:
<<carries tag!!merge, so it lands in the non-string bucket, and an authorwriting
<<: *defaultssees no non-string key in their source. Both enginesreject it either way — YAML merge keys have never been accepted by this schema —
so nothing changes behaviorally, but whether it deserves its own diagnostic is
tracked in #333 rather than papered over here.
The final row is the control that matters most for a gate like this: clean input
still validates, on both engines, identically.
The open hypothesis from #330: refuted
That issue's body flagged, explicitly as untested, that a forged row naming a
real record might not crash but instead overwrite that record's tag and inflate
record_count, potentially letting the fail-loud count assertion pass while agenuine row was lost. That does not happen. Measured against the pre-fix
engine, both real-record variants exit 1:
The structural reason is stronger than the counting one. Because
@tsvquote-wraps any value containing a control character, the carrier record
always keeps the opening
"and the text up to the newline as part of its ownkey — so the carrier unavoidably acquires a bogus key that
validate_record_keysrejects. A forged row cannot be injected without leaving that marker on the
record that carried it. The
record_countmismatch above is a second,independent catch layered on top: the forged row adds to the count while the
separately-read name list is unchanged, and it can never mask a shortfall because
every real record emits exactly one
ctag/ttagand none can be suppressed.So the pre-fix engine was already failing closed for this variant on two grounds;
only the empty-name variant crashed. Recorded here so the issue closes on a
resolved question rather than an open one.
Deliberately out of scope
That issue also observed that
distribution/validate-sync-manifest.mjsrejectsthese manifests but never runs against the live manifest in CI — referenced only
from the contract suite. Wiring it into the live gate is not in this PR: it
introduces a second source of truth for manifest validity and needs a decision
about which validator is authoritative on disagreement. Filed as #331 rather
than bundled here, so the observation survives #330 closing.
Test plan
Seven regression cases added to
distribution/sync-manifest.test.sh, coveringboth sides of every shape:
component key forges a row with an empty record nametarget key forges a row with an empty record namecomponent key forges a row naming a real recordtarget key forges a row naming a real recordcomponent key is not a stringtarget key is not a stringcomponent key with a bare newline is rejected, which additionally asserts theoutput contains neither
bad array subscriptnormalformed manifest row—the two pre-fix failure modes.
Every fixture was run against the pre-fix engine as well, to prove it reaches
the behavior it targets rather than passing vacuously; the before/after table
above is that measurement. A no-op
${var/pattern/replacement}substitution — thetrap found in an earlier PR here — would leave a valid manifest that exits 0 and
fails
invalid_case's nonzero assertion, and the unmodified-manifest control rowconfirms that baseline is genuinely rc=0.
The yq expressions were also exercised standalone (
yq eval -rover craftedmanifests) to confirm they emit
ckstr <record> false truefor a non-string key,ckstr <record> true falsefor a control-character key — both with theckeypayload suppressed —
true truefor a clean record and for an empty mapping, andno
ckstrrow at all for a non-map record.Suite — the
distributioncheck runsbash distribution/sync-manifest.test.shafter
npm ci --prefix distribution(so it does not self-skip): 228 assertionspass, 0 fail, with the sixteen new assertions landing as
[213]–[228]. Thelive 34-component / 8-target manifest still validates.
A local Windows run of the same suite against this exact head also completed
clean: exit 0, 213 assertions, the new ones at
[198]–[213]. The localtotal is lower because the suite skips executable-bit and mode-preserving-
filesystem cases behind
uname -s == Linux*gates, not because anything wasskipped or failed.
Known coverage gaps, filed as #333 rather than left implicit: the engine
rejects six non-string key shapes but only
7:andtrue:are pinned by tests(
1.5:,null:,? [a, b],? {p: q}, and<<were verified by hand againstthe engine). The bare-newline case's
assert_not_contains 'bad array subscript'is also vacuous for its own fixture — that input produced a malformed-row error
pre-fix, never a subscript error — so it stands as a forward-looking guard, not
as evidence.
Lint —
shellcheck --rcfile .shellcheckrcclean on both files.Related
Closes #330.
split out of sync-manifest.sh: a control-character mapping key forges a TSV row and crashes load_manifest_rows on an empty record name #330 rather than bundled here.
assert_sorted_uniqueempty-element fix; samefix-at-the-origin shape.
the
ckey/tkeycontrol-character gap as non-blocking at the time.