Repository navigation
fix(apps): prove the gateway peer before Code Review Sage secret sends - #23502
JLargent42 wants to merge 2 commits into
Conversation
UX Review (Opus 5.5) — ⏭️ skippedRevision |
First Principles Review (Opus 5.5) — ✅ PASSPremise-level review of First-Principles-Verdict: PASS Nothing blocks. Each item fixes a secret-exposure path that issue #26301 tracks, and tests run inside the real sandbox show the fix. No mechanism already on base does a TCP proof without reading What this change shipsInventory (9 items) — 9 justifiedIntent: keep Code Review Sage's internal secret from reaching a foreign process that holds the gateway's loopback port. This is a FIX.
[FIRST-PRINCIPLES-REVIEWED] fa84516 Description read[DESCRIPTION-READ] b38ba220cc1f0ac564868e5eaf2a8d393e6ef26cc7a28127c2e59f80f3ca2fff 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 blocks: the TCP-only challenge-response covers the one case the existing peer proof cannot reach inside the sandbox, and the message layout lives in one module that both sides import. Notes
[DESIGN-REVIEWED] fa84516 Description read[DESCRIPTION-READ] 22e4110eb3769ef6b342dbd43edd637c3feb8a3fb608eef8f2ca6fd45cc60770 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: |
8bf544e to
befd287
Compare
befd287 to
c65f09d
Compare
Sensitive change review
|
c65f09d to
2305236
Compare
2305236 to
1c5b82b
Compare
|
Addressed the First Principles premise block on The BLOCK's concern was an unverified availability premise: if the sealed mount namespace masked the gateway pidfile, the dashboard socket, or macOS (Seatbelt) is NOT covered: no CI lane builds a sandboxed backend on Seatbelt, so the live |
Sensitive change review
|
|
Follow-up to the GPT span So the sandboxed child can list the gateway's |
08f72b9 to
cc29fbc
Compare
Sensitive change review
|
|
Disposition for the Opus 5.5 block on
|
cc29fbc to
ff4431a
Compare
Sensitive change review
|
|
Disposition for GPT 6.1 span
|
iamwhatever
left a comment
There was a problem hiding this comment.
Requesting changes (issue required) at ff4431a. This PR also stays reserved for a human reviewer because it changes which peer receives the gateway's internal secret and edits CI workflows.
I found no defect in the code. Both Sage senders now prove the peer before the first byte (review_driver.py#L204-L213, L739-L762), a refusal sends nothing, and the standalone path keeps the old proxy- and redirect-disabled opener.
What blocks: the deferred work points at #23270, which was closed on 2026-10-27 as fixed by #23283. Two items remain without an open issue:
- The other sandboxed senders listed under Not a goal still use plain
loopback_urlopenon main (cron_trigger.py,mcp_computer.py,mcp_core_parts/gateway.py). - The gateway-side change that would let a TCP-only gateway be proven from the Linux sandbox. Until then
_TCP_ONLY_REMEDYrefuses Sage reviews on hosts whose data home is too long for a unix socket. Both the Design and First Principles lanes asked for this to be tracked.
Please open one issue per item (or reopen #23270 if it still describes them) and link it in the description in place of the closed #23270.
The red macOS shard 5 is test_probes_see_a_driver_declared_in_a_nested_repo, a known flake unrelated to this diff; the PR's own Seatbelt step passed.
— crew-pr-reviewer (r135)
|
@iamwhatever thanks for the review at
|
Sensitive change review
|
|
@iamwhatever on the second deferred item from your review (the TCP-only gateway that the Linux sandbox could not prove): it is now covered in this PR rather than tracked separately, at head
The Sensitive change review template for this head is posted above. |
The Code Review Sage backend sender wrote its X-Internal-Secret to the local gateway over a plain loopback opener, so a foreign process that held the port could receive it -- most acutely in the port probe, which dials candidate ports by design. Route both the credential-bearing API call and the probe through the gateway's connection-bound peer proof (_owner_verified_secret_urlopen), so the secret is written only after the connected peer is proven to be this install's recorded gateway; an unproven peer is refused and nothing is sent. The standalone path (no kiro_crew import) keeps its proxy/redirect-disabled opener. Run the same live in-sandbox proof under wrap_argv(mode="standard") on macOS, where the Sage backend runs under sandbox-exec. The case skips only where /usr/bin/sandbox-exec is absent, and fails if the wrap did not apply Seatbelt or the kernel does not report the child as sandboxed. Add a Linux in-sandbox case for a TCP-only gateway (loopback listener, no dashboard socket), the configuration the listener degrades to when the socket path exceeds the AF_UNIX limit. The proof's TCP arm does not reach MATCH from inside the Linux namespace sandbox: the child cannot read the gateway's /proc/<pid>/fd links, so the peer pid does not resolve. The case pins that fail-closed refusal and reports each input of the TCP verdict (fd-link read, resolved peer pid, recorded pid, owner uid) in the warnings summary. A refused Sage _api_request now keeps the proof's own reason, and adds the remedy (a data home short enough for the socket path) only when it runs in the Linux sandbox against a gateway with no dashboard socket. The cli spec's TCP-arm entry documents the limit. Move the proxy- and redirect-disabled loopback opener guard out of review_driver into sage_lib/loopback.py unchanged, so review_driver stays under the 2,000-line file cap; review_driver re-exports loopback_urlopen, and the fallback-guard test runs the guard from its new file. The sharded macOS run is pytest -q, which does not name passing tests, and a skip exits 0. Run test_sage_peer_proof_reaches_match_inside_seatbelt by node id with -v beside the existing peer-identity canary, and fail the step unless it reports exactly 1 passed. Pin the node id in test_macos_platform_tests_gate.py's by-name list.
Sensitive change review
|
|
GPT 6.1 finding on
The head is also rebased onto main |
Sensitive change review
|
Sensitive change review
|
|
Opus 5.5 finding on
Also in this head since |
…et sends When the gateway has no dashboard socket (its socket path is too long for AF_UNIX, so it listens on loopback TCP only), the sandboxed Sage backend could not prove its peer: the TCP arm resolves the peer pid through the gateway's /proc/<pid>/fd links, which the Linux namespace sandbox cannot read, so every review spawn and publish was refused. Prove a TCP-only gateway with a challenge-response on the connection the secret will travel over. Before any credential byte, the sender sends GET /api/health with a fresh 256-bit nonce in X-Gateway-Peer-Challenge. The gateway answers a direct-local TCP caller with X-Gateway-Peer-Proof: HMAC-SHA256 keyed with its internal secret over a domain tag, the nonce, and the connection's two endpoints as its own kernel reports them. The sender recomputes it with the secret it would send and its own view of the same connection and writes the request on that connection only on an exact match. A listener without the secret cannot answer; one that relays the challenge to the real gateway gets a proof over its own connection's endpoints, which do not match; the nonce rules out replay. Every other answer (non-200, a missing or wrong proof, Connection: close, non-Content-Length framing, oversized, extra bytes, or no answer within 5 s) refuses with a SEL denied event and nothing is sent. The proof reads no /proc entry and nothing in run/ beyond the secret the sender already reads. The message layout lives in kiro_crew.loopback_peer_challenge, shared by both sides; the sender is sage_lib/loopback.py's challenge_verified_urlopen; the gateway's answer is in api_health. With the dashboard socket present the sender keeps the socket peer-credential proof unchanged. The shorter-data-home remedy is removed, since the case it described now passes. Tests: a real aiohttp api_health gateway passes over TCP, on one connection; impostors (no proof, a guessed proof, a relay to the real gateway, a replayed proof, a port taken after the gateway exits) are refused and receive no secret; a restarted gateway on the same port passes only with its new secret; a slow responder and eleven malformed answers carrying a valid proof are refused. The in-sandbox file runs the Sage sender under the real Linux namespace sandbox and under Seatbelt against a TCP-only gateway (MATCH, the request arrives) and against an impostor (refused, nothing received).
Sensitive change review
|
|
First Principles BLOCK on
Facade guard: |
|
HOLD at fa84516 for a human reviewer. Thank you for opening #26301 and for the explanation. My earlier issue request is cleared: #26301 covers the other sandboxed senders, and the TCP-only proof is now built in this PR, so it needs no issue. I found no defect. The proof is an HMAC keyed with the internal secret over a domain tag, a fresh 256-bit nonce and the connection's kernel-reported endpoints, compared in constant time (loopback_peer_challenge.py). Why it is held: this PR changes which peer is trusted to receive the gateway's internal secret, and it adds a secret-keyed response to a network route. Our review policy reserves that for a maintainer. You also noted the owner paused the TCP-only path pending a scope review. A maintainer's sign-off clears this hold. — crew-pr-reviewer (r138) |
|
Thanks for this. One follow-up is needed before this can move. The deferred work points at #23270, which is closed, so two follow-ups have no open issue yet. Could you open one issue for each and link both in the description?
@iamwhatever, once those issues are linked, could you take another look? That is the point your change request asked for. A maintainer will still need to sign off on the security change itself after that. At fa84516 all 102 checks pass. |
bolichen97
left a comment
There was a problem hiding this comment.
Maintainer decision (security sign-off): not adopted. The maintainer's rule for security changes is that we do not raise security strictness, so the maintainer will not add this restriction: Code Review Sage senders proving the gateway peer (owner-verified socket or the new loopback challenge-response) before every secret-bearing send.
Reviewed at fa84516. As I read it, almost all of this PR is that new check: the _verified_urlopen path in review_driver.py, the new loopback_peer_challenge.py and the challenge reply in api_health, the peer-proof tests, and the two CI steps that run them. The existing proxy- and redirect-disabled loopback opener is already on main, so moving it to sage_lib/loopback.py on its own does not change behavior.
Please drop the peer-proof parts. If something that is not a tightening remains that you want to keep, push that alone and I will review it; otherwise please close the PR. Thank you for the careful work and the thorough sandbox tests.
|
Closing per the maintainer security sign-off (not adopted: we do not raise security strictness). Thanks @bolichen97 for the review. |
Problem / Motivation
Goal: bind the Code Review Sage backend's
X-Internal-Secretsends to a proven gateway peer, so the secret is never written to a foreign process that holds the loopback port.The Code Review Sage app backend sends an
X-Internal-Secretto the local gateway over a plain loopback opener. A foreign process that holds the loopback port at send time therefore receives the secret. The exposure is sharpest in the port probe (_probe), whose job is to dial candidate ports that may have something other than this install's gateway listening.#23283 added a connection-bound gateway-peer proof for the CLI internal-secret senders (
kiro_crew.cli_server_peer._owner_verified_secret_urlopen): the secret is written only after the kernel-reported peer on that very connection is proven to be this install's recorded gateway. This change routes the Code Review Sage senders through the same proof.Part of #26301 (the peer proof for the remaining sandboxed senders). Follow-up to #23270, closed by #23283.
Namespace posture
The Sage app backend is itself launched through the agent sandbox (
apps/backend.py→wrap_argv(cmd, mode="standard", …)). On Linux that isunshare(CLONE_NEWUSER)thenunshare(CLONE_NEWNS): a new user and mount namespace, with the host pid and network namespaces. On macOS it issandbox-execwith the Seatbelt profile.test/test_sage_sender_proves_peer_in_sandbox.pyplays the gateway (records the run marker for its own pid and serves the dashboard unix socket), runs the unmonkeypatched proof in a child confined by the realstandardnamespace sandbox, and asserts a liveMATCH. It runs in theBackend Tests (namespace sandbox)job, which assertsuserns_available()first.wrap_argv(mode="standard")runs by node id in its ownBackend Tests (macOS)step, which fails unless the log reports exactly1 passed.MATCHfrom inside the Linux namespace sandbox: the child can list the gateway's/proc/<pid>/fdbut cannot read its links, so the peer pid does not resolve. Sage therefore proves a TCP-only gateway with the challenge-response described below, which needs no/procentry and no new privilege.test_a_tcp_only_gateway_is_proven_inside_the_sandboxruns the Sage sender under the realstandardnamespace sandbox against the real/api/healthhandler over TCP and assertsMATCHand that the request arrived;test_an_impostor_on_a_tcp_only_port_is_refused_inside_the_sandboxasserts a listener without the proof is refused and receives no secret.test_a_tcp_only_gateway_is_proven_inside_seatbeltruns the same TCP-only case underwrap_argv(mode="standard").TCP-only gateway: the challenge-response
On the connected socket, before any credential byte, the sender sends
GET /api/healthwith a fresh 256-bit nonce inX-Gateway-Peer-Challengeand no credential. The gateway answers a direct-local TCP caller withX-Gateway-Peer-Proof= HMAC-SHA256 keyed with its internal secret over a domain tag, the nonce, its own local endpoint and the caller's endpoint, as its kernel reports them. The sender recomputes the HMAC with the secret it is about to send and its own view of the same connection (peer address, local address), compares in constant time, and writes the secret-bearing request on that same connection only on an exact match.Why another local process listening on the port cannot spoof it:
127.0.0.1:<port>, the sender's client port). For the two to match, the gateway would have to be the far end of the sender's own connection, which is what the proof establishes. The kernel allows only one connection per endpoint pair, and the gateway does not setSO_REUSEPORT, so no other process can share its listening endpoint.test_an_impostor_that_relays_the_challenge_to_the_real_gateway_is_refusedasserts the relay obtained a real proof and was still refused.test_a_proof_captured_from_an_earlier_answer_is_not_acceptedcovers this.Content-Length-framed HTTP/1.1 200 that keeps the connection open, with exactly one matching proof and nothing after it, within 5 s, is refused with a SELdeniedevent before the request is written.The proof's only inputs are the secret Sage already reads (
run/gateway-<port>.secret, readable inside the sandbox today) and the socket itself.What changed
review_driver.pyadds_verified_urlopen(req, port, timeout, *, operation). Whenkiro_crewis importable it sends a credential-bearing request through_owner_verified_secret_urlopenif the dashboard socket exists, and throughsage_lib/loopback.py'schallenge_verified_urlopenif it does not (TCP-only). On the standalone path (python3 sage_lib/review_driver.py), which runs outside an install and has no gateway to prove against, it falls back to the existing proxy/redirect-disabled opener._api_requestsends through the proof withoperation="sage_api"; an unproven peer raises_GatewayPeerRefused(not anOSError) and the call returns an error with nothing sent, carrying the proof's own reason._probesends through the proof withoperation="sage_probe"; a refused peer is a foreign listener, so the secret is not sent and the port reads as "not our gateway" (False)..github/workflows/platform-tests.ymlruns the Seatbelt case by node id with-v, and.github/workflows/ci.ymladds the in-sandbox file to the namespace-sandbox job..github/black-baseline.txtdrops a file this change formats.kiro_crew/loopback_peer_challenge.py(new) holds the challenge's message layout (header names, nonce form, endpoint text, HMAC), shared by the gateway and the sender so the two cannot drift.dashboard/handlers/core.py'sapi_healthadds the proof header when a direct-local TCP caller sends a well-formed challenge; its body and every other response are unchanged.docs/system-specs/modules/cli.mddocuments why the TCP arm does not reachMATCHinside the Linux sandbox and specifies the challenge-response.sage_lib/loopback.py(new) holds the proxy- and redirect-disabled loopback opener guard, moved out ofreview_driver.pyunchanged to keep it under the 2,000-line cap, and the TCP-only senderchallenge_verified_urlopen;review_driver.pyre-exportsloopback_urlopen, the SageREADME.mdlists the module, andtest/test_review_driver_loopback.pyruns the guard from its new file.No rule, config key, or public function is removed. The standalone fallback preserves today's behavior where no install is present.
Why it matters
A local secret written to whatever process currently holds a loopback port is a credential-exposure path. The proof binds the credential to a connection whose peer is confirmed to be this install's own gateway, closing the window where the gateway dies and a foreign listener binds the freed port between resolve and send.
Not a goal
Tests
test/test_sage_sender_proves_peer.py: with the dashboard socket present, both_api_requestand_proberun through the socket peer proof with the rightoperationand theX-Internal-Secretheader; a refusal sends nothing and keeps the proof's own reason; the probe maps a refusal to "not our gateway".test/test_sage_tcp_peer_challenge.py(real loopback sockets; the genuine gateway is the realapi_healthhandler served by aiohttp; every impostor records the bytes it received):_api_requestand_probereach it end to end;Connection: close, chunked, noContent-Length, duplicate proof header, trailing bytes, oversized head, header without a colon, close without answering), with a well-framed control that passes;test/test_sage_sender_proves_peer_in_sandbox.py: the live in-sandbox cases above (Linux unix socket MATCH, Linux TCP-only MATCH with the request delivered, Linux TCP-only impostor refused with nothing received, macOS Seatbelt unix-socket MATCH, macOS Seatbelt TCP-only MATCH).test/test_review_driver_loopback.pyandtest/test_macos_platform_tests_gate.pyupdated for the new sender and the pinned macOS step.The new tests fail on the previous head (ff4431a). With only the shared message module added so the file imports, 32 of the 38 cases in
test_sage_tcp_peer_challenge.pyandtest_sage_sender_proves_peer.pyfail;_api_requestto a TCP-only gateway returnsgateway peer not verified; request not sent: … could not be proven to be this install's gateway. Without it, the file fails at import. In the sandbox, ff4431a's own TCP-only case asserted the refusal thattest_a_tcp_only_gateway_is_proven_inside_the_sandboxnow asserts against.Manual verification
None beyond CI: each in-sandbox case runs in a lane that provides its sandbox and cannot skip there.
Related Issues
Part of #26301
Follow-up to #23270 (closed by #23283)
Pattern harvest
Rule candidate: review-prompt
Pattern: a credential-bearing loopback/
localhostsend that writes the secret before proving the connected peer is this install's own recorded gateway. The whole class was the subject of #23270 (the CLI senders were fixed in #23283; the Code Review Sage senders here; the remaining sandboxed senders tracked in #26301). A reviewer prompt that flags any newX-Internal-Secret-bearing send not routed through the connection-bound peer proof would stop the class at authoring time.Checklist