Skip to content

Cap OQL table densification to bound reply-driven memory (ICPBB-384/385) - #136

Merged
aterga merged 3 commits into
mainfrom
security/oql-table-cap
Aug 18, 2026
Merged

aterga merged 3 commits into
mainfrom
security/oql-table-cap

Conversation

@aterga

@aterga aterga commented Aug 18, 2026

Copy link
Copy Markdown
Collaborator

What

rows_to_table (in src/calls.rs) materializes a dense Vec<Vec<String>> sized columns × rows from a canister's OQL reply. Both dimensions were attacker-controllable with no cap:

  • columns come from the first row's cell names, and
  • every subsequent row is densified (aligned by name) to that column set.

A compact reply whose first row declares tens of thousands of columns and that then carries thousands of (even empty) rows expands to hundreds of MB of owned Strings out of an ~80 KB wire payload — a memory-amplification OOM (ICPBB-384, and its duplicate ICPBB-385).

The Candid decode quota added in #132 does not catch this: the decoded tree stays small; the blow-up is entirely in our own alignment loop, downstream of the decoder.

Fix

Cap the materialized table at MAX_OQL_COLUMNS (256) × MAX_OQL_ROWS (1000):

  • extra first-row columns are dropped;
  • rows past the cap 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 pages with offset rather than treating a truncated table as the whole result set.

Real OQL pages are far smaller than these bounds, so well-behaved replies are unaffected.

Tests

  • New rows_to_table_caps_wide_and_tall_replies: a 300-column / 1100-row reply is densified to exactly 256 × 1000, every row aligned to the capped column set, and truncated is set.
  • Existing OQL tests unchanged (176 lib tests pass; clippy clean).

Notes

MVP with no stability commitment (see https://mcp.internetcomputer.org/terms). This is defense-in-depth against a malicious/compromised target canister; it is not a confidentiality or integrity issue.

🤖 Generated with Claude Code

`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]>
@aterga
aterga requested review from a team and a balanced review from Copilot August 18, 2026 11:15

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Bounds OQL table densification to mitigate reply-driven memory amplification.

Changes:

  • Caps tables at 256 columns and 1,000 rows.
  • Propagates truncation through result parsing.
  • Adds coverage for oversized replies.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/calls.rs Resolved
Comment thread src/calls.rs Resolved
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]>
Copilot AI review requested due to automatic review settings August 18, 2026 11:32

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Suppressed comments (1)

src/calls.rs:1311

  • This behavior materially contradicts the PR description: it says extra first-row columns are dropped, truncated is folded into has_more, and a 300-column reply yields a 256-column table, but this branch refuses the entire result as TooManyColumns. Please either implement the documented capped-table behavior or update the PR description and test claims to describe the refusal/error contract.
            if pairs.len() > MAX_OQL_COLUMNS {
                return Some(TableOutcome::TooWide { column_count: pairs.len() });

Comment thread src/calls.rs Resolved
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]>
Copilot AI review requested due to automatic review settings August 18, 2026 11:48

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

Suppressed comments (3)

src/calls.rs:1335

  • named counts named cells, while the width check counts distinct names. Since duplicate names are explicitly deduplicated, a row with 257 distinct names plus duplicates is reported by tools.rs as having a much larger exact column count. Avoid presenting this value as the actual column count; either compute a truthful bounded/lower-bound result or have the tool report only that the 256-column limit was exceeded.
                // recover it. `named` is the declared cell count (== column count
                // for a well-formed reply with no duplicate names).
                return Some(TableOutcome::TooWide { column_count: named });

src/calls.rs:832

  • The referenced decode quota is not present in this checkout: parse_execute_reply still uses default IDLArgs::from_bytes / from_bytes_with_types at lines 1117 and 1199, and there is no DecoderConfig in src. Rebase on #132 before claiming this layer exists, or revise the comment so it accurately describes the current code.
/// hundreds of MB out of an ~80 KB reply (ICPBB-384/385). The decode quota
/// (#132) does not catch this: the decoded tree stays small; the blow-up is in
/// OUR alignment loop, not candid's decoder. Real OQL pages are far smaller; a

src/calls.rs:826

  • This comment and the PR description say width overflow is truncated and folded into has_more, but the implementation instead refuses the whole result as TooManyColumns; the described 300×1100 → 256×1000 test is also not present. Update the PR description and this comment to match the non-pageable error contract, or restore the documented behavior.

This issue also appears in the following locations of the same file:

  • line 830
  • line 1333
/// Widest and tallest table [`rows_to_table`] will materialize from a reply
/// before it signals truncation (folded into `has_more`). Their product bounds

@aterga
aterga merged commit 9917052 into main Aug 18, 2026
8 checks passed
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.

3 participants