Skip to content

fix(security): bound how deep the ssh floor nests its readings - #37820

Merged
KiroCrewCaptain merged 1 commit into
kirodotdev:mainfrom
jeeshofone:fix/37792
Nov 16, 2026
Merged

KiroCrewCaptain merged 1 commit into
kirodotdev:mainfrom
jeeshofone:fix/37792

Conversation

@jeeshofone

@jeeshofone jeeshofone commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

Goal: A shell request with deeply nested double-quoted $( ) gets a permission answer instead of aborting the turn.

A request that nests double-quoted $( about 1,000 levels deep ('echo "$(' * 1000 + 'true; echo ssh' + ')"' * 1000, 10,014 characters, valid Bash) makes the ssh-to-self floor raise RecursionError. Nothing on the _resolve_permission path catches it, so the turn aborts before the permission request is answered.

Why it matters

One crafted command can abort any turn that runs the denied-command gate. The command never runs, but the turn is lost.

Not a goal

  • A budget on the total number of readings across sibling bodies (fan-out). That cost is the same on main and this PR does not change it.
  • Changing how the rm floor or other floors handle RecursionError.

What changed (motivation → approach → change)

_is_ssh_to_self reads each double-quoted $( ) body, bound function body and carried value with another _is_ssh_to_self call, and nothing bounds how many are open at once. A decorator, _bounded_ssh_reading, now counts the open readings in a context variable and refuses (returns True) once _MAX_SSH_READING_DEPTH (32) are open. The bound sits on the function every recursion path goes through, so no path can reset it, and an over-deep request is refused, never allowed and never a crash. Only text that names an ssh-family verb recurses at all (the verb probe returns False first), so a deep nest with no ssh verb is still allowed. A deterministic bound was chosen over a try/except RecursionError around the floor because the answer then does not depend on the interpreter's stack limit or how deep the caller already is. The facade manifest in argv_floor.py names the three new names, and a line-budget row records the +33 lines.

Backwards compatibility

Compatible: only commands that nest 32 or more ssh-naming readings deep get a different answer (a refusal instead of a crash or a read).
Removes nothing: the change adds a refusal for an input that crashed the gate.

Tests

TestSshFloorNestingDepth in test/test_denied_commands_security_ssh_self.py:

  • the issue's 1,000-level command is refused by _reaches_self_over_ssh with no exception, after exactly _MAX_SSH_READING_DEPTH (32) body readings (fails with RecursionError on origin/main);
  • is_denied refuses a 128-level nest as sandbox-escape-ssh-self with no exception;
  • the deepest open reading equals _MAX_SSH_READING_DEPTH on a 200-level nest, and the counter is back to 0 afterwards (a count, no wall clock);
  • a 3-level nest with ssh localhost id inside is still refused, and one without ssh is still allowed;
  • a 1,000-level nest with no ssh verb is still allowed.

The first version also sent the 1,000-level command through is_denied. It passed locally but timed out at 120 s on the 4-core CI runner. The cost is not in the ssh floor: _reaches_self_over_ssh answers in about 0.5 s locally. On main, _shell_payload_walk, _self_kill_span/_bare_kill_raw_bodies and _is_git_publish each make about 1,001 calls on that command and take about 20 s before the ssh floor is reached (20.7 s locally with the ssh floor stubbed out, 22.9 s on main before its RecursionError). That quadratic work is a separate defect, tracked by #37874 and fixed by #37875, so the gate row uses 128 levels (0.35 s locally) and the 1,000-level row asserts on the floor the issue names.

Proof: not provable -- other: prove.py reported INCONCLUSIVE (the mutated run errored at collection); reverting src/ to origin/main by hand makes the two new depth tests fail with RecursionError and AttributeError.

Related suites (denied_commands_security*, argv_floor*, facade_mirror*, facade_bindings, security_*, liveness_facade): 8,047 passed, 2 skipped. pre_push_gate passed. Flake-pattern ratchet passed. Semgrep (semgrep/ rules), black, flake8, isort and mypy are clean on the changed files.

Local reviews on this head: the Opus 5.5 lane found no findings after validation. Its two discovery candidates were dropped: (1) the bound refuses a 32nd reading without running that reading's own verb probe. That only happens when the 31 enclosing readings each name an ssh verb, and refusing there is the fail-closed direction. (2) Sibling fan-out cost is the same on main (see Not a goal). The GPT lane found nothing, but it did not run on the pinned gpt-6.1-sol model and could not run pytest, so CI's GPT review is the real pass.

Manual verification

Known limitation: a command refused at the depth bound gets the usual ssh-to-self refusal note, which tells the agent to wait and retry the same command.

N/A — unit coverage sufficient: the floor is a pure function of the command text.

Related Issues

Fixes #37792
Related: #35359, #37785 (both edit argv_floor_ssh.py; this diff keeps to the _is_ssh_to_self definition and the facade manifest)
Related: #37874 (quadratic work in the other floors on deep nesting), fixed by #37875

Sibling sweep

Swept: git grep -nE "_double_quoted_substitution_bodies(|RecursionError" -- src/kiro_crew/security/
Sibling: src/kiro_crew/security/deny_gate.py:892 -- not affected -- the rm floor already catches RecursionError and fails closed
Sibling: src/kiro_crew/security/inline_payload.py:216 -- not affected -- the payload parser already catches RecursionError

Pattern harvest

Rule candidate: review-prompt
Pattern: a security reader that recurses once per shell nesting level needs a deterministic depth bound that refuses past it

Checklist

  • At most two commits (one is the norm), with a Conventional Commits title
  • 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

@jeeshofone
jeeshofone requested review from a team and KiroCrewCaptain as code owners November 16, 2026 05:10
@github-actions github-actions Bot added fork Pull request from a fork (external contributor) readiness: checking Automated validation is still running labels Nov 16, 2026
@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Opus 5.5, fork) — ✅ PASS

Premise-level review of 984c4fbead1498c958ad56e9c4230d0ca7cc1057 via the fork AI-review pipeline — 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

The fix has a linked issue (#37792) and a test that fails on base. It bounds the depth inside the function that every recursion path calls, so it fixes the mechanism rather than a call site.

Notes

  • Item 1 — one consumer, generalized: _bounded_ssh_reading(read: "Callable[..., bool]") is a decorator factory with one use: grepping @_bounded_ssh_reading finds only _is_ssh_to_self. Subtraction: replace the factory with a depth check inlined in _is_ssh_to_self (or a wrapper written for that one function), and remove _bounded_ssh_reading from the argv_floor.py manifest.

What this change ships

Inventory (3 items) — 2 justified

Intent: a shell request with deeply nested $( ) gets a permission answer instead of aborting the turn. This is a FIX.

  1. Requests nested 32 or more ssh-naming readings deep are now refused instead of crashing the gate. — one consumer, generalized
  2. The facade manifest lists three new internal names. — justified
  3. A new line-budget record, ssh-self-reading-depth.txt. — justified

[FIRST-PRINCIPLES-REVIEWED] 984c4fb

Description read

[DESCRIPTION-READ] 8fc12020d9085d6b6e75a802fb07a15038c535d91268d978603b77df3c8a0bdb

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

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

UX Review (Opus 5.5, fork) — ⏭️ skipped

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

@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Opus 5.5, fork) — ✅ PASS

Design-level review of 984c4fbead1498c958ad56e9c4230d0ca7cc1057 via the fork AI-review pipeline — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS does not.

Design-Verdict: PASS

Nothing blocks: the depth bound sits on the one function every recursion path enters, and it fails closed.

Notes

  • The refusal at the bound gives the usual ssh-to-self note, which tells the agent to wait and retry the same command. That retry can never succeed, so the agent may loop. A refusal note specific to the depth bound would remove that cost.

[DESIGN-REVIEWED] 984c4fb

Description read

[DESCRIPTION-READ] 946de305591139a4b7dca9a6ead483bff093412393116a792df4af44c115d7f1

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

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

GPT 6.1 Review (fork) — ✅ no blocking findings

Reviewed 984c4fbead1498c958ad56e9c4230d0ca7cc1057 via the fork AI-review pipeline; updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] 984c4fb

@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Opus 5.5 Review (fork) — ✅ no blocking findings

Reviewed 984c4fbead1498c958ad56e9c4230d0ca7cc1057 via the fork AI-review pipeline; updated in place on each push.

Review details

No findings.

[OPUS-REVIEWED] 984c4fb

@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Security Scope Review (fork) — ✅ PASS

Which legitimate operations does 984c4fbead1498c958ad56e9c4230d0ca7cc1057 newly refuse? Model proposes, scripts/deny_diff.py decides — updated in place on each push. A script-confirmed regression blocks PR readiness.

adjudicated, zero confirmed regressions

Scope-Verdict: PASS

Reason: The change caps how deeply the ssh-to-self floor can nest its readings of _is_ssh_to_self at 32. That cap counts readings that are open at the same time, not all readings in a command. Sibling readings (repeated function calls, loop iterations, -e/proxycommand values) each go back to the same depth when they finish, so only commands nested more than 32 levels deep are newly refused. No ordinary operation nests that deep. The harness has not yet handed this review any confirmed rows, and the per-leg verdicts are its job.

TIGHTENING: _bounded_ssh_reading decorator on _is_ssh_to_self in src/kiro_crew/security/argv_floor_ssh.py -- refuses a crafted command nested deep enough (about 1,000 double-quoted $( ) levels) to raise RecursionError and abort the turn: if depth >= _MAX_SSH_READING_DEPTH: return True with _MAX_SSH_READING_DEPTH = 32.

ADJUDICATED: 16 candidates, 0 confirmed newly-refused

PLATFORM COVERAGE: posix (12 rows): plain ssh/scp/sftp/rsync to remote hosts, -e, ProxyCommand and GIT_SSH_COMMAND values that each open one nested reading, a 3-level quoted $( ) nest, a bound helper function called twice, and a fleet loop. Together they probe the shallow-nest and sibling-reading edges of the new counter. windows (4 rows): ssh.exe/scp.exe spellings, backslash local paths, and %USERPROFILE% in a cmd set GIT_SSH_COMMAND=. The diff makes no new assumption about paths, separators or executable names; the counter is a ContextVar that works the same on every platform. So these rows only confirm that the Windows spellings the floor already reads still reach it at depth 1. No assumed platform is left without a candidate.

[SCOPE-REVIEWED] 984c4fb

Security scope review -- adjudicated candidates

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

No candidate flipped from allowed to refused.

A double-quoted $( ) nested about 1,000 levels deep made _is_ssh_to_self and
_double_quoted_substitution_bodies recurse past Python's limit, so the
RecursionError aborted the turn before the permission request was answered.
_is_ssh_to_self now counts the readings open at once and refuses past 32.
Only text that names an ssh-family verb recurses, so a deep nest without one
is still allowed.

Fixes kirodotdev#37792
@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 16, 2026
@KiroCrewCaptain

Copy link
Copy Markdown
Collaborator

Code change approver recommendation: Approve

Why: the decider estimates an 8% chance that this change introduces a defect, within the 12% limit for approving on its own, and every check passes.

The PR puts a depth counter (a ContextVar) on _is_ssh_to_self. The decorator replaces the module global, so every recursive caller reaches the wrapper through it and no path can skip or reset the bound: the double-quoted $( ) bodies (line 1155), the bound function bodies (1010), the routing/proxycommand values (846) and the env/rsh/carried values (1325–1565). Once 32 readings are open the wrapper returns True, which is the floor's existing fail-closed direction. The depth goes back down in finally. The wrapper's signature (text_lower, *, own_line=False) matches every call site, including the map(...) ones. inspect.getsource still works through __wrapped__, so the cost test that parses the floor's AST keeps working. The test patch target _double_quoted_substitution_bodies is resolved through the module global, so the counting tests measure what they claim. The 1,000-level test fails on main with RecursionError and passes here. Text with no ssh verb still returns at the probe before any recursion, so deep nests without ssh stay allowed. The line-budget row (+33) matches the src delta (+30 in argv_floor_ssh.py, +3 in argv_floor.py), and argv_floor_ssh.py is 1,568 lines

An automated recommendation, not a review: a maintainer still decides. It approves on its own only when the estimated chance of a defect is at most 12% and the change has at most 400 changed lines. Decider v3c (217fe607) · commit 984c4fbead · 93 changed lines. If this recommendation is wrong, reply here: replies are how the approver is evaluated.

@KiroCrewCaptain
KiroCrewCaptain enabled auto-merge (squash) November 16, 2026 10:35

@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.

Approved. On head 984c4fb the review checked these things:

  • The ssh-self test file passes (382 tests), and the argv_floor, facade mirror, facade bindings and gate liveness suites pass (441 tests).
  • Probes show nesting depths 0 to 31 are read as before and depth 32 is refused.
  • With src reverted to the merge-base, 3 of the 5 new tests fail.
  • Mutating the bound check, the return True or the reset(token) call turns tests red.

Security: this change only tightens the ssh-to-self floor. Every recursion path calls the module-level _is_ssh_to_self, so the bound covers all of them. Past 32 open readings the floor refuses, and the ContextVar resets in finally. Nothing that main refuses is allowed now. The only new refusals are commands nested 32 or more levels deep that name an ssh verb, and the body lists those under Backwards compatibility.

Done well: the bound is a fixed depth rather than a caught RecursionError, so the result does not depend on the interpreter stack. The tests count calls instead of measuring time.

Optional:

  • Add an allowed row at the boundary, for example _nested_double_quoted(_MAX_SSH_READING_DEPTH - 1, "ssh far-host id") is False. Today a cap lowered to 2 still passes every test, yet it would refuse echo "$(echo "$(ssh far-host id)")".
  • The comment near argv_floor_ssh.py:1025 says only text that names an ssh verb recurses. In fact the refusal at the bound runs before the verb probe, so the refused reading itself may name no verb. It fails closed, so only the wording needs a fix.
  • After #37875 lands, share one constant for the two 32-level caps, or add a comment saying they are separate on purpose.

@KiroCrewCaptain
KiroCrewCaptain merged commit 7505181 into kirodotdev:main Nov 16, 2026
97 of 98 checks passed
@github-actions github-actions Bot removed the readiness: passed Eligible automated validation passed for the current revision label Nov 16, 2026
buluoray pushed a commit that referenced this pull request Nov 16, 2026
A double-quoted $( ) nested about 1,000 levels deep made _is_ssh_to_self and
_double_quoted_substitution_bodies recurse past Python's limit, so the
RecursionError aborted the turn before the permission request was answered.
_is_ssh_to_self now counts the readings open at once and refuses past 32.
Only text that names an ssh-family verb recurses, so a deep nest without one
is still allowed.

Fixes #37792

(cherry picked from commit 7505181)
bolichen97 pushed a commit that referenced this pull request Nov 16, 2026
…37964)

#37875 made the gate refuse any command nested past 32 substitutions
before a floor reads it, so the 128-level nest in #37820's test is now
refused by that cap and the test, which expected the ssh floor's rule,
is red on main. The test now checks the floor's bound at exactly 32
levels, which the cap lets through, and that 128 levels get the cap's
own refusal.

Closes #37963
buluoray added a commit that referenced this pull request Nov 19, 2026
…37964)

#37875 made the gate refuse any command nested past 32 substitutions
before a floor reads it, so the 128-level nest in #37820's test is now
refused by that cap and the test, which expected the ssh floor's rule,
is red on main. The test now checks the floor's bound at exactly 32
levels, which the cap lets through, and that 128 levels get the cap's
own refusal.

Closes #37963

(cherry picked from commit 529da1e)
buluoray added a commit that referenced this pull request Nov 19, 2026
…37964)

#37875 made the gate refuse any command nested past 32 substitutions
before a floor reads it, so the 128-level nest in #37820's test is now
refused by that cap and the test, which expected the ssh floor's rule,
is red on main. The test now checks the floor's bound at exactly 32
levels, which the cap lets through, and that 128 levels get the cap's
own refusal.

Closes #37963

(cherry picked from commit 529da1e)
buluoray added a commit that referenced this pull request Nov 19, 2026
…37964)

#37875 made the gate refuse any command nested past 32 substitutions
before a floor reads it, so the 128-level nest in #37820's test is now
refused by that cap and the test, which expected the ssh floor's rule,
is red on main. The test now checks the floor's bound at exactly 32
levels, which the cap lets through, and that 128 levels get the cap's
own refusal.

Closes #37963

(cherry picked from commit 529da1e)
buluoray added a commit that referenced this pull request Nov 19, 2026
…37964)

#37875 made the gate refuse any command nested past 32 substitutions
before a floor reads it, so the 128-level nest in #37820's test is now
refused by that cap and the test, which expected the ssh floor's rule,
is red on main. The test now checks the floor's bound at exactly 32
levels, which the cap lets through, and that 128 levels get the cap's
own refusal.

Closes #37963

(cherry picked from commit 529da1e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fork Pull request from a fork (external contributor)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

security: a deeply nested double-quoted $( ) crashes the ssh floor with RecursionError

2 participants