Skip to content

fix(security): charge the data-consumer command guard once per frame - #12858

Merged
bolichen97 merged 1 commit into
mainfrom
fix/deny-self-floor-linear-8972
Sep 22, 2026
Merged

bolichen97 merged 1 commit into
mainfrom
fix/deny-self-floor-linear-8972

Conversation

@chenmingwei23

@chenmingwei23 chenmingwei23 commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

What is the problem?

is_denied is quadratic in the number of tokens that trip a self-protection floor's
trigger predicate.

Four floors in src/kiro_crew/security/argv_floor.py walk one fixed argv per frame and
ask _data_consumer_exempt about every token that passes a narrow trigger predicate:

floor 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_index is not None
_is_ssh_to_self _ssh_family_verb is not None

The command-level half of that guard, _data_consumer_command_disqualified, reads only
tokens. Its answer is therefore a property of the whole frame and is the same for every
token in it, and one of its three members sweeps the entire argv with
_SCRIPT_EXECUTES_RE. Asking it once per trigger token costs N x len(tokens).

The issue counts three call sites. There are four: security.py was one module when the
report was written and is now a package, and _is_ssh_to_self carries the same shape. The
fifth caller of this signature, the payload walk in security/__init__.py, already hoists
the 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> ...):

trigger tokens command bytes seconds in is_denied
800 6 KB 0.94
1,600 12 KB 3.54
2,400 18 KB 7.60

Doubling 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
-- echo makes those words data, and the correct answer is "allowed" -- so what
the 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:

  1. The turn stalls because is_denied spends seconds on one command.
  2. It spends them inside _SCRIPT_EXECUTES_RE, sweeping every token of the argv.
  3. That sweep is one of three guards inside _data_consumer_command_disqualified.
  4. Those guards read only tokens, so within one frame their answer cannot change.
  5. The four callers ask anyway, once per trigger token, because _data_consumer_exempt
    defaults command_disqualified to None and self-computes.

The fix is at step 5: each of the four frame loops now holds
disqualified: "bool | None" = None and passes it as command_disqualified. The frame
pays 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 N sweeps to one.
test_a_command_with_no_trigger_token_pays_nothing pins that property.

With all five production callers now supplying the verdict, the None self-computing branch
at step 5 has no remaining consumer, so command_disqualified is a required keyword
argument 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 TypeError instead of a silent slow path.
test_the_command_level_verdict_is_the_callers_to_supply pins that.

The memo is declared inside the frame loop, so each frame computes its own. tokens is
rebound 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_disqualified rather 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.py is at the package's per-module line cap of 4,500 -- exactly at
it, 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 the
easy 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_program runs inside the first of the four frame loops,
immediately before the trigger predicate.

function new owner why there
_stdin_redirect_carriers shell_normalizer argv/token inspection, beside the guard
_stdin_program_text shell_normalizer same, and calls the one above
_python_reads_stdin shell_normalizer same
_has_self_importing_inline_program inline_payload calls _inline_payload_reaches_cli; inline_payload already imports shell_normalizer, so the normalizer cannot import it back

Every moved body is byte-identical to its original except for one mechanical edit: inside
shell_normalizer, _shell_normalizer._normalize_operand and its two siblings are plain
globals, 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.py 4,132 lines (368 of headroom), shell_normalizer.py 3,641,
inline_payload.py 928, package 24,034 of its 26,000 budget. The cap is untouched.

What tests we did

test/test_denied_commands_security.py gains
TestDataConsumerGuardIsChargedPerFrameNotPerTriggerToken, a sibling of the existing
payload-axis class. Against base source, 8 of its cases fail; on this branch all pass.

  • Guard-call ratchet. Base charges 700 / 1,400 / 2,100 guard calls at 100 / 200 / 300
    trigger pairs -- exactly 7n, one per trigger token per reaching floor. This branch
    charges 7 at every size.
  • Argv-sweep ratchet. _SCRIPT_EXECUTES_RE.search calls, against argv length: base runs
    350x / 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.
  • Laziness. A command with no trigger token charges zero sweeps.
  • Verdict equivalence. Eight commands spanning all four trigger predicates, exempt and
    refused, compared against a run that discards the memo and recomputes at every call.
  • Scope. An AST assertion, per floor, that the memo is declared inside the frame loop
    and nowhere outside it.
  • Required argument. Omitting command_disqualified raises, and a supplied True
    withdraws the exemption from every token.
  • Mentions beside real invocations stay denied.

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.py and test_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_object and
test_every_manifest_name_resolves_on_the_facade are exactly the invariants a move must
preserve, 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:

mutant killed by
compute the memo eagerly per frame test_a_command_with_no_trigger_token_pays_nothing
hoist the memo above the frame loop test_the_memo_is_declared_inside_the_frame_loop
pass command_disqualified=False 6 cases, including pre-existing exemption-refusal tests

Gates 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 for iterates, and the callee's
parameters 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_disqualified required means this particular predicate can no longer be
called 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.py
sat 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 answering True then
False -- the leak direction there withdraws an exemption rather than granting one, so the
verdict 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

@chenmingwei23
chenmingwei23 requested review from a team and Zedmor September 22, 2026 16:55
@github-actions github-actions Bot added the readiness: checking Automated validation is still running label Sep 22, 2026
@github-actions

github-actions Bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Fable 5) — ✅ PASS

Design-level review of ac16ea2e00681ded95a27797e77c5e02ae7fc4f9 — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

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

@github-actions

github-actions Bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

GPT 5.6 Review — ✅ no blocking findings

GPT 5.6 completed its review of ac16ea2e00681ded95a27797e77c5e02ae7fc4f9 and found no blocking issues.

This comment is updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] ac16ea2

False positive or not applicable? A repository writer can comment:
/ai-review override gpt ac16ea2e00681ded95a27797e77c5e02ae7fc4f9: <one-sentence reason>

@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention and removed readiness: checking Automated validation is still running labels Sep 22, 2026
@github-actions

github-actions Bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Fable 5) — ✅ PASS

Premise-level review of ac16ea2e00681ded95a27797e77c5e02ae7fc4f9 — 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/CONCERNS are advisory.

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 ships

Inventory (6 items) — 6 justified

Intent: stop is_denied stalling for seconds on long commands whose trigger words are data — a FIX.

  1. Long commands with many trigger words no longer stall the command gate (per-frame lazy memo in four floors) — justified
  2. command_disqualified becomes required; the self-computing None branch is deleted, so omission is a TypeError — justified
  3. Three stdin helpers move to shell_normalizer.py, one to inline_payload.py — rides along; mandated by the 4,500-line cap in test_security_gate_liveness.py:66, whose own text forbids raising it and sanctions relocation
  4. Facade re-export blocks follow the moves; exported names unchanged — rides along; mechanical half of item 3
  5. New test class pins guard-call and sweep counts, laziness, memo scope, verdict equivalence — justified
  6. Old None-branch-equivalence test rewritten to pin the required argument — justified; its premise ("call sites this change does not touch") is what the fix removes, declared in the description

Evidence run: 5 production _data_consumer_exempt( call sites in src/ — 4 frame loops fixed here, security/__init__.py:1404 already hoisting — so the deleted None branch had 0 remaining consumers. _data_consumer_command_disqualified grep: those same 5 plus the facade export, no unfixed sibling. No leftover Iterator/_heredoc_marker-family uses in argv_floor.py after the move (0 matches). Item 2 is the cause-level close: the quadratic shape is now unrepresentable, not merely avoided. Provenance is checkable — ratchet tests the PR adds fail on base by the description's own counts (7n vs 7). No default, capability, or verdict changes; lens 9 does not trigger.

[FIRST-PRINCIPLES-REVIEWED] ac16ea2

@github-actions

github-actions Bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Opus 4.8 Review — ✅ no blocking findings

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

Review details

No findings.

[OPUS-REVIEWED] ac16ea2

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

False positive or not applicable? A repository writer can comment:
/ai-review override fable ac16ea2e00681ded95a27797e77c5e02ae7fc4f9: <one-sentence reason>

@github-actions

github-actions Bot commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Security Scope Review -- ✅ PASS

Which legitimate operations does ac16ea2e00681ded95a27797e77c5e02ae7fc4f9 newly refuse? Updated in place on each push. A script-confirmed regression blocks; the model proposes candidates and never decides a refusal.

Verdict basis: adjudicated, zero confirmed regressions.

Security scope review -- adjudicated candidates

  • macos-latest (platform posix): 14 classified, 0 newly refused, 14 still allowed.
  • ubuntu-latest (platform posix): 14 classified, 0 newly refused, 14 still allowed.
  • windows-latest (platform windows): 5 classified, 0 newly refused, 5 still allowed.

No candidate flipped from allowed to refused.

Scope-Verdict: PASS

TIGHTENING: The sensitive-command tier's data-consumer exemption had its command-level half (_data_consumer_command_disqualified) hoisted into a lazy per-frame memo in all four self-protection floors, and _data_consumer_exempt's command_disqualified parameter made required ("the parameter is REQUIRED so that a caller cannot fall back to computing them per token -- the whole-argv sweep inside them is what makes that shape quadratic in the token count") -- the one threat it refuses is the cost-blowup denial of service on the gate itself: "a 24KB command carrying 1,600 kill-family words as arguments of a data consumer took ~14s, which crosses a 25s-class watchdog around 2,200 such words." The accompanying relocation of _python_reads_stdin, _stdin_program_text, _stdin_redirect_carriers, and _has_self_importing_inline_program from argv_floor.py into shell_normalizer.py/inline_payload.py is a verbatim move (only namespace qualifiers change), so no match surface widened; the intended invariant is verdict-identity, and every internal call site -- the four frame loops plus the payload walk in the package body, which already passed the keyword -- supplies the now-required argument.

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 bash -c payload frame, where a memo hoisted one scope too far would carry one frame's command-level verdict into the next; the non-executing awk '{print $1}' form sitting exactly on the _SCRIPT_EXECUTES_RE guard the memo now charges once per frame; and the relocated stdin-python detector's designed-allowed shapes -- a benign heredoc, a pipe into python3 - and into python3 -c, and the docstring's own isort src/kiro_crew/… && python3 - <<'PY' neighbor-path case that a regression back to frame-wide scanning would re-refuse.

PLATFORM COVERAGE: posix (11 rows) -- the diff's relocated code reads bash redirection grammar (heredocs, here-strings, </| glue) and the repo's posix paths, so the heredoc, pipe, ~-path, and repo-relative-path rows exercise those assumptions directly. windows (3 rows) -- the moved detector matches interpreter names via _PYTHON_PROGRAM_RE, which admits python.exe/py, so the type … | python.exe -c row probes that spelling, and the backslash-path pytest and findstr rows probe the other host's separators and search tool against the memoized floors. any (2 rows) -- the platform-neutral mention shapes (echo kirocrew restart, echo ssh localhost) for the two floors not otherwise covered by a path-carrying row. No platform assumption in the diff is left unprobed: it introduces no new environment-variable or drive-letter handling. The per-leg verdicts are the deterministic fold's to report, after this review is captured.

[SCOPE-REVIEWED] ac16ea2

@chenmingwei23
chenmingwei23 force-pushed the fix/deny-self-floor-linear-8972 branch from 338b54c to 5fb3414 Compare September 22, 2026 17:18
@github-actions github-actions Bot added readiness: checking Automated validation is still running and removed readiness: action required A blocking check or review needs attention labels Sep 22, 2026
@chenmingwei23

Copy link
Copy Markdown
Contributor Author
  • Item 5 -- zero consumers of the command_disqualified=None fallback: fixed in 5fb3414.

span=6af636d632a9 disposition=fixed. The finding is correct and I could name no
production caller of the None branch: all five (4 in argv_floor.py, 1 in
security/__init__.py) supply the verdict after this diff, so the branch was dead.
Taken the first of the two routes the item offers: the fallback branch is deleted.
command_disqualified is now a required keyword-only bool in _data_consumer_exempt,
and the if command_disqualified is None recomputation lines are gone.
This is the stronger outcome for the defect the PR fixes, not just tidiness: with the
fallback present, a future caller reintroduces the quadratic by OMITTING one keyword
argument, silently and with no test able to see it. Required, that same omission is a
TypeError at the call site.
Pinned by test_the_command_level_verdict_is_the_callers_to_supply, which asserts the
omission raises and that a supplied True withdraws the exemption from every token.
1,239 tests pass across the two owning files; mypy clean over 1,694 files.

@chenmingwei23

Copy link
Copy Markdown
Contributor Author
  • Item 4 -- a comment describing a caller set this diff empties: fixed in 5fb3414.

span=666a693271c0 disposition=fixed. Correct, and the same root cause as the other item,
so it is resolved by the same change rather than by rewording.
The sentence "Callers that ask about a SINGLE token pass no precomputed value" described
a set with no members left. With the fallback branch deleted there is no such caller
class to describe, so the sentence is gone rather than corrected -- a corrected version
would still have been documenting an unreachable path.
The replacement comment in _data_consumer_exempt states only what holds: the verdict
arrives already computed, every caller iterates one fixed argv, and the parameter is
required so the per-token shape is not reachable.
The test that rested on the same premise is rewritten with it. It asserted the two
branches agree; there is one branch now, so it asserts the contract that replaced them.
Comment History Gate passes on the added lines.

@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention and removed readiness: checking Automated validation is still running labels Sep 22, 2026
@chenmingwei23

Copy link
Copy Markdown
Contributor Author
  • Verdict-equivalence ratchet patches the wrong namespace: fixed in 225ddbd.

span=5759bfc46afe disposition=fixed. Correct, and confirmed by measurement before
changing anything: patching security._data_consumer_exempt and then calling
is_denied("echo pkill <name>") observes ZERO calls, while patching
argv_floor._data_consumer_exempt observes 7. The four frame loops bind the name as
their own module global, and the facade mirrors an attribute write onto one owning
submodule only -- the normalizer -- so the test was comparing the real function with
itself and would have passed whatever the memo did.
Fixed by patching both caller namespaces, argv_floor and the security package body
(the payload walk), since each holds its own binding.
Added an assert calls["n"] > 0, so a wrong or partial patch target is now a RED test
rather than a vacuous green. That assertion immediately earned its place: it rejected
awk 'system(...)', whose kill is denied by another tier and never consults the guard.
That case is replaced by two two-frame commands that build the memo more than once per
call, and the omission is explained in the test.
1,241 tests pass across the two owning files; mypy clean; gates green.

@chenmingwei23
chenmingwei23 force-pushed the fix/deny-self-floor-linear-8972 branch from 5fb3414 to 225ddbd Compare September 22, 2026 17:43
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Sep 22, 2026
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
@github-actions

Copy link
Copy Markdown
Contributor

GPT 5.6 Review — ⚠️ review incomplete

GPT 5.6 did not produce a complete verdict for 225ddbd68b10096824cec42b8b63f39e8740a6fc; inspect the workflow logs and re-run it.

This comment is updated in place on each push.

See the GPT 5.6 Review job logs; this commit has no completed GPT verdict.

False positive or not applicable? A repository writer can comment:
/ai-review override gpt 225ddbd68b10096824cec42b8b63f39e8740a6fc: <one-sentence reason>

@chenmingwei23
chenmingwei23 force-pushed the fix/deny-self-floor-linear-8972 branch from 225ddbd to ac16ea2 Compare September 22, 2026 18:36
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: passed Eligible automated validation passed for the current revision and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Sep 22, 2026

@cixuuz cixuuz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 bolichen97 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@bolichen97
bolichen97 merged commit 47203cb into main Sep 22, 2026
98 checks passed
@bolichen97
bolichen97 deleted the fix/deny-self-floor-linear-8972 branch September 22, 2026 23:04
@github-actions github-actions Bot removed the readiness: passed Eligible automated validation passed for the current revision label Sep 22, 2026
wknight1 pushed a commit to wknight1/KiroCrew that referenced this pull request Oct 5, 2026
…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
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.

security: is_denied is quadratic in self-floor trigger token count at three _data_consumer_exempt call sites

3 participants