Repository navigation
fix(security): charge the data-consumer command guard once per frame - #12858
Conversation
Design Review (Fable 5) — ✅ PASSDesign-level review of Design-Verdict: PASS Verify the four relocated bodies match their deleted originals byte-for-byte (modulo the stated alias-prefix edits) — the move rides on that equivalence. [DESIGN-REVIEWED] ac16ea2 |
GPT 5.6 Review — ✅ no blocking findingsGPT 5.6 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: |
First Principles Review (Fable 5) — ✅ PASSPremise-level review of First-Principles-Verdict: PASS Verify the four relocated bodies against base — the "byte-identical except 5 alias-prefix lines" claim needs a diff of old vs new, which only the base source can settle. What this change shipsInventory (6 items) — 6 justifiedIntent: stop
Evidence run: 5 production [FIRST-PRINCIPLES-REVIEWED] ac16ea2 |
Opus 4.8 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: |
Security Scope Review -- ✅ PASSWhich legitimate operations does Verdict basis: adjudicated, zero confirmed regressions. Security scope review -- adjudicated candidates
No candidate flipped from allowed to refused. Scope-Verdict: PASS TIGHTENING: The sensitive-command tier's data-consumer exemption had its command-level half ( ADJUDICATED: 17 candidates, 0 confirmed newly-refused handed to this review. The candidates probe the boundaries where the invariant could silently fail: self-name, kill-family, and ssh mentions as data-consumer arguments across all four memoized floors; a two-frame command and a nested PLATFORM COVERAGE: posix (11 rows) -- the diff's relocated code reads bash redirection grammar (heredocs, here-strings, [SCOPE-REVIEWED] ac16ea2 |
338b54c to
5fb3414
Compare
|
|
|
5fb3414 to
225ddbd
Compare
The self-protection floors walk one fixed argv per frame and ask _data_consumer_exempt about every token that passes a narrow trigger predicate. The command-level half of that guard reads only `tokens`, and one of its members sweeps the whole argv with _SCRIPT_EXECUTES_RE, so charging it per trigger token costs N x len(tokens). Hold the answer in a per-frame memo at each of the four call sites, computed lazily at the first trigger token. A command that reaches these floors and trips no trigger predicate still pays nothing, which an unconditional per-frame hoist would not preserve. With every production caller now supplying the verdict, `command_disqualified` becomes a required keyword argument and the self-computing fallback is gone. That branch had no remaining consumer, and requiring the argument means a caller cannot reintroduce the per-token sweep by omission: it is a TypeError, not a slow path. argv_floor.py sits at the package's per-module line cap, which leaves a fix no room, so the stdin-program helper cluster moves out to make it. `_stdin_redirect_carriers`, `_stdin_program_text` and `_python_reads_stdin` go to shell_normalizer beside the guard they serve, and `_has_self_importing_inline_program` goes to inline_payload, whose `_inline_payload_reaches_cli` it calls and which the normalizer cannot import back. Every body is byte-identical apart from dropping a sibling module alias that is redundant inside the module it named. The facade re-exports each name from its new owner, which the export-identity contract checks. Measured on a 1,600-trigger `echo pkill <name> ...` command: argv sweeps go from 350x/700x/1050x the argv length at three sizes to a constant 7x, and an 18KB command from 7.60s to 0.88s. Verdicts are unchanged: `tokens` is bound once per frame and never mutated, so the memo is a pure function of a fixed value. Closes #8972
GPT 5.6 Review —
|
225ddbd to
ac16ea2
Compare
cixuuz
left a comment
There was a problem hiding this comment.
Verified the fix does hoist _data_consumer_command_disqualified from per-trigger-token to per-frame via a lazy memo (7x constant calls regardless of trigger count, 0 calls when no trigger fires). All 5 review lanes (GPT, Opus, Design, First Principles, Security Scope) PASS/no-blocking at this exact head. Test suite includes structural call-count assertions and verdict-equivalence checks across all four affected self-protection floors.
bolichen97
left a comment
There was a problem hiding this comment.
Reviewed at ac16ea2 with Windows compatibility as the primary lens. The memoized _data_consumer_command_disqualified is a pure function of the frame's tokens, so the per-frame memo cannot change any refusal verdict; all six callers pass the now-required command_disqualified, so no TypeError path. The moved stdin/heredoc helpers are pure string processing; the only OS-conditional import (fcntl) is pre-existing and guarded. Local run on native Windows (Python 3.12.10, worktree src): test/test_denied_commands_security.py 1054 passed, 1 skipped (pre-existing test_windows_sweep_is_inert_off_windows, unrelated). Earlier First-Principles items 4/5 and the GPT namespace-patching finding are resolved at head.
…irodotdev#12858) The self-protection floors walk one fixed argv per frame and ask _data_consumer_exempt about every token that passes a narrow trigger predicate. The command-level half of that guard reads only `tokens`, and one of its members sweeps the whole argv with _SCRIPT_EXECUTES_RE, so charging it per trigger token costs N x len(tokens). Hold the answer in a per-frame memo at each of the four call sites, computed lazily at the first trigger token. A command that reaches these floors and trips no trigger predicate still pays nothing, which an unconditional per-frame hoist would not preserve. With every production caller now supplying the verdict, `command_disqualified` becomes a required keyword argument and the self-computing fallback is gone. That branch had no remaining consumer, and requiring the argument means a caller cannot reintroduce the per-token sweep by omission: it is a TypeError, not a slow path. argv_floor.py sits at the package's per-module line cap, which leaves a fix no room, so the stdin-program helper cluster moves out to make it. `_stdin_redirect_carriers`, `_stdin_program_text` and `_python_reads_stdin` go to shell_normalizer beside the guard they serve, and `_has_self_importing_inline_program` goes to inline_payload, whose `_inline_payload_reaches_cli` it calls and which the normalizer cannot import back. Every body is byte-identical apart from dropping a sibling module alias that is redundant inside the module it named. The facade re-exports each name from its new owner, which the export-identity contract checks. Measured on a 1,600-trigger `echo pkill <name> ...` command: argv sweeps go from 350x/700x/1050x the argv length at three sizes to a constant 7x, and an 18KB command from 7.60s to 0.88s. Verdicts are unchanged: `tokens` is bound once per frame and never mutated, so the memo is a pure function of a fixed value. Closes kirodotdev#8972
What is the problem?
is_deniedis quadratic in the number of tokens that trip a self-protection floor'strigger predicate.
Four floors in
src/kiro_crew/security/argv_floor.pywalk one fixed argv per frame andask
_data_consumer_exemptabout every token that passes a narrow trigger predicate:_is_credential_mint_is_self_program/_is_self_module_invocation_is_self_kill_is_kill_by_name_program_matches_self_subcommand_self_program_indexis notNone_is_ssh_to_self_ssh_family_verbis notNoneThe command-level half of that guard,
_data_consumer_command_disqualified, reads onlytokens. Its answer is therefore a property of the whole frame and is the same for everytoken in it, and one of its three members sweeps the entire argv with
_SCRIPT_EXECUTES_RE. Asking it once per trigger token costsN x len(tokens).The issue counts three call sites. There are four:
security.pywas one module when thereport was written and is now a package, and
_is_ssh_to_selfcarries the same shape. Thefifth caller of this signature, the payload walk in
security/__init__.py, already hoiststhe answer and is unchanged here.
Why this issue matters to the user
This floor runs for every command an agent proposes, so its cost lands on the interactive
path rather than in a batch job.
Measured against this branch's base, on a command carrying kill-family words as arguments
of a data consumer (
echo pkill <name> pkill <name> ...):is_deniedDoubling the trigger count roughly quadruples the time. Extrapolating the curve, a command
in the 25-30 KB range crosses a 25s-class watchdog. The command in that table is not
denied --
echomakes those words data, and the correct answer is "allowed" -- so whatthe user sees is a turn that stalls on a command the gate was always going to permit.
How our fix solves it
The chain from symptom to root cause:
is_deniedspends seconds on one command._SCRIPT_EXECUTES_RE, sweeping every token of the argv._data_consumer_command_disqualified.tokens, so within one frame their answer cannot change._data_consumer_exemptdefaults
command_disqualifiedtoNoneand self-computes.The fix is at step 5: each of the four frame loops now holds
disqualified: "bool | None" = Noneand passes it ascommand_disqualified. The framepays the guard once instead of once per trigger token.
The memo is computed lazily, at the first trigger token, which settles the tradeoff the
issue left open. These guards are reached only after a trigger predicate matches, so a
command that reaches these floors and trips no trigger predicate pays nothing today. An
unconditional per-frame hoist would charge every such command one argv sweep it currently
skips; the lazy memo charges it nothing while still collapsing
Nsweeps to one.test_a_command_with_no_trigger_token_pays_nothingpins that property.With all five production callers now supplying the verdict, the
Noneself-computing branchat step 5 has no remaining consumer, so
command_disqualifiedis a required keywordargument and that branch is deleted. This closes the defect rather than leaving its shape
available: a caller can no longer reintroduce the per-token sweep by omitting the argument,
because omitting it is a
TypeErrorinstead of a silent slow path.test_the_command_level_verdict_is_the_callers_to_supplypins that.The memo is declared inside the frame loop, so each frame computes its own.
tokensisrebound only by that loop and the list is never mutated in any of the four functions, so the
memoized value is a pure function of a fixed input and no verdict can move. The calls go
through
_shell_normalizer._data_consumer_command_disqualifiedrather than a direct import,matching the other cross-module helper calls in this file and keeping the facade's attribute
mirroring -- which is how tests patch this guard -- pointed at the object the owner holds.
Making room for it
security/argv_floor.pyis at the package's per-module line cap of 4,500 -- exactly atit, on this PR's original base and on today's main alike -- so a fix of any size trips
test_no_single_module_grows_back_into_a_monolith. That gate's own text rules out theeasy way out: "a cluster that reaches it is asking to be split, and RAISING the number is
not the fix."
So the stdin-program helper cluster moves out, which is the sanctioned remedy (the package
budget explicitly notes that "relocating a declaration between submodules moves nothing
across it"). It is also the cluster this fix sits in:
_has_self_importing_inline_programruns inside the first of the four frame loops,immediately before the trigger predicate.
_stdin_redirect_carriersshell_normalizer_stdin_program_textshell_normalizer_python_reads_stdinshell_normalizer_has_self_importing_inline_programinline_payload_inline_payload_reaches_cli;inline_payloadalready importsshell_normalizer, so the normalizer cannot import it backEvery moved body is byte-identical to its original except for one mechanical edit: inside
shell_normalizer,_shell_normalizer._normalize_operandand its two siblings are plainglobals, so the alias prefix is dropped on 5 lines. Both spellings resolve through the same
module namespace at call time, so this changes no verdict and leaves a facade patch -- which
mirrors onto that same owner -- visible exactly as before.
Result:
argv_floor.py4,132 lines (368 of headroom),shell_normalizer.py3,641,inline_payload.py928, package 24,034 of its 26,000 budget. The cap is untouched.What tests we did
test/test_denied_commands_security.pygainsTestDataConsumerGuardIsChargedPerFrameNotPerTriggerToken, a sibling of the existingpayload-axis class. Against base source, 8 of its cases fail; on this branch all pass.
trigger pairs -- exactly
7n, one per trigger token per reaching floor. This branchcharges 7 at every size.
_SCRIPT_EXECUTES_RE.searchcalls, against argv length: base runs350x / 700x / 1050x at three sizes, so the multiplier itself grows. This branch is a
constant 7x. Bounded at 10x, which base misses by 35x at the smallest size.
refused, compared against a run that discards the memo and recomputes at every call.
and nowhere outside it.
command_disqualifiedraises, and a suppliedTruewithdraws the exemption from every token.
Four files pass in full -- the two owning the changed code plus the two that police the
relocation:
test_denied_commands_security.py,test_argv_floor_inline_and_brace_scope.py,test_security_facade.pyandtest_security_gate_liveness.py, 1,261 passed / 1 skipped.The relocation reuses the existing export contract as its equivalence test rather than
adding a parallel one:
test_a_facade_name_is_the_owning_submodules_objectandtest_every_manifest_name_resolves_on_the_facadeare exactly the invariants a move mustpreserve, and they check identity against whichever submodule owns a name, so they hold
across the move without being edited. A script also compared each moved function's source
against its original and reported IDENTICAL for all four.
Separately, 51 curated commands (33 denied, 18 allowed) were run through base and this
branch and compared: zero verdict differences.
Three focused mutants were checked, and each is killed:
test_a_command_with_no_trigger_token_pays_nothingtest_the_memo_is_declared_inside_the_frame_loopcommand_disqualified=FalseGates run locally: comment-history (with selftest), brand, isort, flake8, black ratchet,
mypy (
Success: no issues found in 1694 source files), leaf-test-scope, testpaths-coverage.Pattern harvest
Rule candidate: semgrep
Pattern: a predicate whose result depends only on a whole collection, called from inside a
loop that iterates that same collection.
This is the third instance of one defect class in this file family. #8595 was the same shape
counted per nested payload, #8282 and #8338 were whole-argv re-walks charged per operator,
and this one is charged per trigger token. Each time the quantity in the exponent differed,
so each was found separately; the shape did not differ at all. A semgrep rule keyed on
"an argument to the call is the same name the enclosing
foriterates, and the callee'sparameters are only that name" would have flagged all three from the source alone, without
needing a measurement to discover the axis.
The structural half of this PR is the stronger form of that rule, applied to one guard:
making
command_disqualifiedrequired means this particular predicate can no longer becalled per token at all, so the pattern is unrepresentable here rather than merely
discouraged. Where a guard cannot take that treatment, the semgrep rule is the fallback.
Second, smaller candidate: the per-module line cap has no headroom warning.
argv_floor.pysat exactly at 4,500, so the gate could only fire once a change had already been written,
and the first PR to touch that file pays for a relocation it did not cause. A warning
threshold a few dozen lines under the cap would move that cost to whoever is approaching
it rather than whoever arrives after.
Any other suggestions on the work
One stated residual. A memo hoisted above the frame loop would apply one frame's
command-level answer to a later frame's argv, which is why the declaration sits inside the
loop. That mistake turned out not to be reachable behaviourally: each floor returns as
soon as a frame denies, so a frame whose guard answer differs from an earlier frame's is
visited only when the earlier frame did not deny, and no command was found that both
survives its first frame and disagrees with it. The nearest case found,
echo pkill <name>; sed 's/a/pkill -f <name>/e' f, has two frames answeringTruethenFalse-- the leak direction there withdraws an exemption rather than granting one, so theverdict is unchanged. Because the property is real but not observable, it is asserted on the
source rather than through a command, and this paragraph is the reason that test reads the
AST instead of calling
is_denied.The timing table above is evidence, not a test. An absolute wall-clock bound was written and
then removed: the 24KB repro lands near 14s on the host in the issue report and under 4s on
the host used here, so any bound tight enough to catch the quadratic on a slow runner passes
it on a fast one. The two count-based ratchets pin the same property without depending on
the machine.
Closes #8972