Repository navigation
calls: bound type-less reply decode/render against deep nesting (CWE-674) - #108
Merged
Merged
Conversation
…674) The type-less reply paths decoded and rendered attacker-controlled Candid reply bytes on the ordinary worker thread (~2 MiB stack) with no depth guard, unlike the DID-typed path which already runs its decode + Display inside on_deep_stack (a 64 MiB stack). candid's decoder, IDLArgs/IDLValue Display, the OQL tree walk (extract_oql/cell_scalar), AND the decoded tree's recursive Drop all recurse once per nesting level with no bound. The MCP client freely picks the target canister (validated only as a well-formed Principal), so a reply that is a recursive `opt` chain (`type t = opt t`: a few KB of `opt` tags on the wire) decodes to a tree deep enough to overflow the worker stack, an uncatchable process abort that drops every concurrent session. Fix: run the whole decode-render-drop of each type-less path on the guarded deep stack, exactly as the DID path does, so the deep tree is created, walked, rendered, and dropped there and only the finished String/OqlResult crosses back: - decode_reply: wrap the type-less fallback (from_bytes + to_string). The DID path's on_deep_stack has already returned, so this does not nest. - decode_text_reply: wrap the whole decode + match + render. - parse_execute_reply: wrap the whole body (decode via DID or type-less fallback, then extract_oql / to_string) in one on_deep_stack, and make decode_args_with_did decode in place (it no longer takes its own on_deep_stack, so there is no nesting; its sole caller now provides the deep stack). This also moves the previously-unguarded extract_oql/cell_scalar walk and the IDLArgs Drop onto the deep stack. - management::format_status: guard the canister_status raw-Candid fallback the same way. Defense in depth: that reply is the management canister's own bounded record, not the target's arbitrary output, but no type-less reply decode should run unbounded on the worker stack. on_deep_stack returns None only if the parse thread can't be spawned; each caller then degrades to an explanatory string, never a parse on the small stack. Adds a regression test that a deeply-nested type-less reply decodes on the deep stack across all three calls.rs paths. Co-Authored-By: Claude Opus 4.8 <[email protected]> Claude-Session: https://claude.ai/code/session_014T9N8USDfNK5yzznPGg7Ym
There was a problem hiding this comment.
Pull request overview
This PR mitigates a stack-exhaustion DoS risk (CWE-674) by ensuring type-less Candid reply decoding (including subsequent tree walk, rendering, and recursive Drop) runs on the existing guarded “deep stack” thread rather than on the ~2 MiB worker stack, aligning behavior with the already-guarded DID-typed decode path.
Changes:
- Wrap type-less reply decode/render paths (
decode_reply,decode_text_reply, andparse_execute_reply) inon_deep_stackso deep recursion can’t abort the process. - Refactor
decode_args_with_didto decode “in place” (no internalon_deep_stack) to avoid nested deep-stack usage and permit exhaustion. - Add a regression test covering deeply-nested typeless replies, and apply the same defense-in-depth guard to management
canister_statusraw-Candid fallback rendering.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/calls.rs |
Moves typeless decode + OQL walk + render + drop onto on_deep_stack, removes nested guarding from decode_args_with_did, and adds a regression test for deep nesting. |
src/management.rs |
Guards the raw-Candid canister_status fallback decode/render with on_deep_stack for defense in depth. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
aterga
marked this pull request as ready for review
July 30, 2026 14:21
sea-snake
approved these changes
Jul 30, 2026
aterga
pushed a commit
that referenced
this pull request
Aug 3, 2026
Post-audit merges (#87-#115) that changed user-visible or documented behavior, folded into the README: - II's consent screen requires an explicit access-level choice (Questions only vs Actions & questions); it is not a read-only-by-default opt-out. - Name the connect binding cookie correctly: mcp_connect, not sid, with Secure conditional on an https PUBLIC_URL. - Document the official pages (/privacy-policy, /support, /terms) and the OpenAI domain-verification endpoint with its env-gated 404 behavior (#109, #112, #115). - /version now reports the served II pairings via the instances array (#94). - CWE-674: note the post-parse depth bounds on interface alias chains (#87) and the type-less reply decode/render path (#108). - Companion-control note: percent-encoded slashes/dots in redirect paths are rejected (#107). - Deploy: describe the reference native deployment pipeline (staging tracks main, release-* tags ship production with the binary attached) (#81, #93). Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01TGhwpdTbUqQbR5AiiN8Ljw
aterga
added a commit
that referenced
this pull request
Aug 18, 2026
…ICPBB-438) (#132) Every canister-reply decode used candid's default `DecoderConfig` (no `decoding_quota`), so nothing bounded the number of decoded elements. A zero-byte element type lets a ~13-byte reply declare tens of millions of elements (`vec null` × 4e7, and far more via nesting); decoding it drives a multi-gigabyte allocation and an UNCATCHABLE `handle_alloc_error`/OOM abort that drops every concurrent user's session. `on_deep_stack` recovers panics, not aborts, so its CWE-674 depth guard does not catch this element-count axis (the reply is depth-1). Route all five reply-decode sites (`decode_reply`, the typed `decode_bytes_with_did`, `parse_execute_reply`, `decode_text_reply`, and `from_bytes_with_types`) through a shared `DecoderConfig` carrying a decoding and skipping quota. candid charges the quota per decoded value, so the bound is absolute (nesting/padding can't raise it) and an over-budget reply returns the ordinary "reply is not decodable" error instead of aborting. The quota is sized so a rejected bomb's transient allocation stays in the tens of MB while realistic replies are untouched. Completes the untrusted-reply hardening line of #37/#82/#87/#108 (which bounded the depth axis) along the element-count axis. Co-authored-by: Claude Opus 5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes a MEDIUM stack-exhaustion DoS (CWE-674): the type-less reply-decode paths decoded and rendered attacker-controlled Candid reply bytes on the ordinary worker thread (~2 MiB stack) with no depth guard, unlike the DID-typed path which already runs its decode +
Displayinsideon_deep_stack(a 64 MiB stack).The MCP client freely picks the target canister (
tools.rsvalidates only that it's a well-formedPrincipal, no allowlist), so the reply bytes are fully attacker-controlled. candid's decoder,IDLArgs/IDLValueDisplay, the OQL tree walk (extract_oql/cell_scalar), and the decoded tree's recursiveDropall recurse once per nesting level with no bound. A recursiveoptchain (type t = opt t→ a few KB ofopttags on the wire) decodes to a tree deep enough to overflow the worker stack — an uncatchable process abort that drops every concurrent session.Fix
Run the whole decode → walk → render → drop of each type-less path on the guarded deep stack, exactly as the DID path does, so the deep tree is created, walked, rendered, and dropped there and only the finished
String/OqlResultcrosses back:decode_reply(calls.rs:702): wrap the type-less fallback. The DID path'son_deep_stackhas already returned, so this does not nest.decode_text_reply: wrap the whole decode + match + render.parse_execute_reply: wrap the whole body (DID or type-less decode, thenextract_oql/to_string) in oneon_deep_stack, and makedecode_args_with_diddecode in place (it no longer takes its ownon_deep_stack, so there's no nesting — the sole caller now provides the deep stack). This also moves the previously-unguardedextract_oql/cell_scalarwalk and theIDLArgsDroponto the deep stack.management::format_status: guard thecanister_statusraw-Candid fallback too. Defense in depth — that reply is the management canister's own bounded record, not the target's arbitrary output, but no type-less reply decode should run unbounded on the worker stack.Notes:
IDLValuerecurses onDrop, so the value must never cross back to the shallow stack. Each path now creates and drops the tree entirely insideon_deep_stack(the existing DID path already did this by returning only theString).on_deep_stackholds a bounded parse permit and must not nest; verifieddecode_args_with_didno longer calls it.on_deep_stackreturnsNoneonly if the parse thread can't be spawned; each caller then degrades to an explanatory string, never a parse on the small stack.Testing
cargo test— all pass (151 + 8 + 1, 0 failed).typeless_reply_paths_bound_deep_nesting: builds a deeply-nestedoptreply and assertsdecode_reply,decode_text_reply, andparse_execute_replyall render it on the deep stack. Depth is kept modest (candid's decode/Displayis superlinear in depth, so CI stays fast); it exercises the guarded path end-to-end rather than trying to trigger an abort (which is uncatchable and can't be asserted on).Checklist
🤖 Generated with Claude Code
Generated by Claude Code