Repository navigation
Bound Candid reply decoding with a quota (ICPBB-438) - #132
Merged
Merged
Conversation
…ICPBB-438) 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]>
There was a problem hiding this comment.
Pull request overview
Adds Candid decoding quotas to prevent allocation-amplification attacks from malicious canister replies.
Changes:
- Introduces shared decoding and skipping quotas.
- Applies quotas across typed and type-less reply decoders.
- Adds a regression test for compact
vec nullallocation bombs.
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
MRmarioruci
approved these changes
Aug 18, 2026
This was referenced Aug 18, 2026
aterga
added a commit
that referenced
this pull request
Aug 18, 2026
Second review round on rows_to_table (r3803651957): the width cap only ran on the first row, but every row still copied ALL of its cells into an intermediate `Vec<(String, String)>` (rendering each value) before alignment. A reply with a narrow first row and a later row carrying tens of thousands of junk cells could therefore still force a large transient allocation — bounded only implicitly by the #132 decode quota, a cross-module invariant that is easy to break. Make the bound explicit and local. The first row establishes the distinct column set (refused if it exceeds MAX_OQL_COLUMNS, detected with a plain counter so no full-width Vec is built). Every row — including later, attacker-widened ones — is then aligned by rendering ONLY the cells whose name is a known column, via a `col_pos` lookup. Intermediate allocation is now O(columns) per row regardless of how many junk cells a row carries; unknown cells cost a hashmap lookup and allocate nothing. Also adds exact-boundary tests (256 accepted / 257 refused columns; 1000 materialized / 1001 truncated rows) and a wide-later-row test that asserts a 5,000-junk-cell second row aligns to the 1-column set without widening. Co-Authored-By: Claude Opus 4.8 <[email protected]>
aterga
added a commit
that referenced
this pull request
Aug 18, 2026
…85) (#136) * Cap OQL table densification to bound reply-driven memory (ICPBB-384/385) `rows_to_table` builds a dense `Vec<Vec<String>>` sized `columns × rows`, where the column set is taken from the first row's cell names and every subsequent row is aligned to it. Both dimensions came straight from an attacker-controllable canister reply with no cap: a compact reply whose first row declares tens of thousands of columns and that then carries thousands of (even empty) rows densifies to hundreds of MB of owned `String`s out of an ~80 KB wire payload — a memory-amplification OOM. The decode quota added in #132 does not catch this: the decoded Candid tree stays small; the blow-up is in our own alignment loop, not candid's decoder. Cap the materialized table at MAX_OQL_COLUMNS (256) × MAX_OQL_ROWS (1000). Excess first-row columns are dropped and excess rows are not materialized; either case sets a `truncated` flag that `extract_oql` folds into `has_more`, so the agent sees the page is incomplete and can page with offset rather than treating a truncated table as the full result set. Real OQL pages are far smaller than these bounds. Co-Authored-By: Claude Opus 4.8 <[email protected]> * Refuse over-wide OQL tables instead of conflating them with has_more Addresses the PR review: the first cut folded BOTH row and column truncation into `has_more`. But `has_more` contractually means "more rows remain — page with a higher `offset`" (calls.rs:294), and the dropped columns are chosen by the query's `select`, not by `offset`. So a final page wider than the column cap would report `has_more = true` at every offset — trapping the agent in an infinite paging loop while still silently losing the extra columns. Separate the two truncation reasons: * Row truncation (> MAX_OQL_ROWS) stays pageable and still raises `has_more` — a higher `offset` genuinely returns those rows. * A first row wider than MAX_OQL_COLUMNS is now REFUSED (`OqlResult::TooManyColumns`), and the tool tells the agent to narrow its `select` rather than to page. Refusing at the first row also bounds the dense `cols × rows` allocation before any row is materialized, so the ICPBB-384/385 memory cap is unchanged. `rows_to_table` returns a `TableOutcome` that carries the two reasons apart; `extract_oql` maps them to the right `OqlResult`. Tests split into refuse-wide and page-tall cases, each asserting the internal outcome and the public mapping (a wide reply must NOT become a has_more table; a tall reply must raise has_more even over the canister's own hasMore = false). The rewrite also drops the earlier fixture whose row count was off by one. Co-Authored-By: Claude Opus 4.8 <[email protected]> * Bound per-row OQL alignment allocation, not just the final table Second review round on rows_to_table (r3803651957): the width cap only ran on the first row, but every row still copied ALL of its cells into an intermediate `Vec<(String, String)>` (rendering each value) before alignment. A reply with a narrow first row and a later row carrying tens of thousands of junk cells could therefore still force a large transient allocation — bounded only implicitly by the #132 decode quota, a cross-module invariant that is easy to break. Make the bound explicit and local. The first row establishes the distinct column set (refused if it exceeds MAX_OQL_COLUMNS, detected with a plain counter so no full-width Vec is built). Every row — including later, attacker-widened ones — is then aligned by rendering ONLY the cells whose name is a known column, via a `col_pos` lookup. Intermediate allocation is now O(columns) per row regardless of how many junk cells a row carries; unknown cells cost a hashmap lookup and allocate nothing. Also adds exact-boundary tests (256 accepted / 257 refused columns; 1000 materialized / 1001 truncated rows) and a wide-later-row test that asserts a 5,000-junk-cell second row aligns to the 1-column set without widening. Co-Authored-By: Claude Opus 4.8 <[email protected]> --------- Co-authored-by: Claude Opus 4.8 <[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.
Problem (ICPBB-438)
Every canister-reply decode in
src/calls.rsused candid's defaultDecoderConfig(decoding_quota: None), so nothing bounded the number of decoded elements. A zero-byte element type makes this a ~2M× amplifier:A ~13-byte reply can declare tens of millions of
nullelements (and far more via nesting). Decoding it drives a multi-gigabyte allocation →handle_alloc_error/OOM abort, which drops every concurrent user's in-memory session/token/grant. It's uncatchable:on_deep_stackrecovers panics (resume_unwind), and its CWE-674 depth guard doesn't apply because the reply is depth-1. Any authenticated user with a canister they control can hold the service down with one 13-byte reply per restart. It's the element-count axis of the same class #37/#82/#87/#108 already hardened along the depth axis.grep -rn "DecoderConfig|decoding_quota|skipping_quota" src/→ no matches, confirming the gap.Fix
A shared
reply_decoder_config()builds acandid::DecoderConfigwith a decoding and skipping quota, applied at all five decode sites:decode_reply(type-less), the typeddecode_bytes_with_did,parse_execute_reply,decode_text_reply, andfrom_bytes_with_types.nullisn't an exact primitive, sovec nulltakes the generic seq path, whereCompound::next_element_seedchargesadd_cost(3)per element before decoding it and there's noVec::with_capacity(len)pre-sizing — so the quota trips after ~quota/3elements, bounding a rejected bomb's transient allocation to ~tens of MB.REPLY_DECODING_QUOTA = 3_000_000) leaves realistic replies untouched (a reply with >~1M decoded values would render to unusable megabytes of text anyway); tunable.Test
decode_reply_rejects_a_vec_null_bomb_instead_of_abortingbuilds the 13-bytevec null × 40Mbomb and asserts the type-less decode returns the error path rather than aborting. Full lib suite green (176 tests); no new clippy warnings.Note
This is the fix
#37/#82/#87/#108would have made along this axis; those were proactive in-house hardening of the same "untrusted canister reply aborts the process" class.