Skip to content

feat(https_outcalls): only accept canonically encoded canister HTTP payloads - #11751

Merged
eichhorl merged 2 commits into
masterfrom
eichhorl/canister-http-canonical-payload
Oct 5, 2026
Merged

eichhorl merged 2 commits into
masterfrom
eichhorl/canister-http-canonical-payload

Conversation

@eichhorl

@eichhorl eichhorl commented Oct 5, 2026 •

Copy link
Copy Markdown
Contributor

This makes canonical encoding a validity rule of the canister HTTP payload: validate_payload now rejects a payload unless it is byte for byte what prost encodes its messages to.

Why

The block hash covers the raw bytes of the canister HTTP payload. This becomes a problem once nodes re-encode payloads they did not build. The upcoming hashes-in-blocks for canister HTTP responses does exactly that: a relay strips the response contents from a block, and the receiver puts them back and re-encodes. With a non-canonical payload, every honest relay would hand out a copy that no peer can validate.

How

parse::canonical_bytes_to_payload decodes the payload one length-delimited message at a time, re-encodes each message into a reused buffer and compares it with the bytes it was decoded from. validate_payload uses it after the size check and before any state lookup or signature verification. Batch delivery (into_messages) keeps the lenient bytes_to_payload, since it only ever sees validated payloads.

Cost

  • It costs one encode and compare per validated non-empty payload. Measured with the same per-message approach on a release build, that is about 0.1–0.3 ms for most payload shapes up to 2 MB, and about 1.1 ms at the 500-response cap with 27 signatures each. That is roughly 5–7% of validating such a payload.

Testing

  • Unit tests for each non-canonical form: an unknown field, a non-minimal length prefix, a repeated field, fields out of tag order, and a bool of 2. The last two have the same length as the canonical encoding, which is why comparing lengths would not be enough.
  • A validation test: a payload that validates is rejected once one field it does not know is appended to its messages, although it decodes to the same payload.

🤖 Generated with Claude Code

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟢 Approval recommended

The validation is narrowly scoped, dependency metadata is consistent, and canonical and malformed encodings are adequately tested.

Review effort: Balanced
Findings: None

What changed in this PR

Ensures canister HTTP consensus payloads use the exact canonical Prost encoding required for stable block hashes.

Changes:

  • Re-encodes decoded messages to detect non-canonical payload bytes.
  • Applies canonical decoding during payload validation.
  • Adds unit/integration coverage and Prost dependencies.
File Description
rs/​https_outcalls/​consensus/​src/​payload_builder/​parse.rs Implements canonical decoding and tests.
rs/​https_outcalls/​consensus/​src/​payload_builder.rs Enforces canonical encoding during validation.
rs/​https_outcalls/​consensus/​src/​payload_builder/​tests.rs Tests end-to-end rejection.
rs/​https_outcalls/​consensus/​Cargo.toml Adds Prost dependency.
rs/​https_outcalls/​consensus/​BUILD.bazel Adds matching Bazel dependency.
Cargo.lock Records the dependency update.

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

@eichhorl
eichhorl marked this pull request as ready for review October 5, 2026 08:51
@eichhorl
eichhorl requested a review from a team as a code owner October 5, 2026 08:51
@zeropath-ai

zeropath-ai Bot commented Oct 5, 2026

Copy link
Copy Markdown

✅ No security or compliance issues detected. Reviewed everything up to 4728c2c.

Security Overview
Detected Code Changes
Change Type Relevant files
Enhancement ► rs/https_outcalls/consensus/BUILD.bazel
    Add prost as dependency in BUILD.bazel
Enhancement ► rs/https_outcalls/consensus/Cargo.toml
    Include prost as workspace dependency
Enhancement ► rs/https_outcalls/consensus/src/payload_builder.rs
    Use canonical_bytes_to_payload for decoding payloads
Enhancement ► rs/https_outcalls/consensus/src/payload_builder/parse.rs
    Add canonical bytes-to-payload path and canonical message parsing with strict encoding checks
Enhancement ► rs/https_outcalls/consensus/src/payload_builder/parse.rs
    Introduce slice_to_canonical_messages to enforce canonical encoding during parsing
Enhancement ► rs/https_outcalls/consensus/src/payload_builder/parse.rs
    Add tests for canonical vs non-canonical encodings and undecodable payloads
Enhancement ► rs/https_outcalls/consensus/src/payload_builder/tests.rs
    Update tests imports and extend test coverage for canonical encoding behavior
Enhancement ► rs/https_outcalls/consensus/src/payload_builder/tests.rs
    Add test validating non-canonical encoding is rejected in payload validation

eichhorl added a commit that referenced this pull request Oct 5, 2026
A receiver puts a stripped payload back together by encoding what it decodes,
which reproduces the bytes that the block hash covers only if they are prost's
own encoding of the payload's messages. Stripping a payload that prost decodes
but would encode differently, e.g. one with an unknown field, would hand every
peer a block that it has to reject. #11751 makes payload validation reject
such payloads, so every block that an honest node strips is canonically
encoded; the docs now state that stripping relies on this.

The threshold applies to exactly the quantity that a response's metadata
reports as its `content_size`: a reject's code counts one byte, as in
`CanisterHttpReject::count_bytes`, rather than four. Declaring a stripped
response costs 36 bytes, not 34, and the feature flag's doc names the
threshold.

The benchmark case of 500 one-byte responses measured a block with nothing
worth stripping; it now uses the smallest response that is stripped. Every
case asserts that the block declares as many responses as it has worth
stripping, and that assembling it succeeds.

New tests cover the identical responses of a flexible outcall, which are
declared and fetched once, rejects on either side of the threshold, and the
content size the threshold applies to. The canister HTTP round trips through
the assembler compare the whole block, and the size test checks the exact
cost of each declaration. The payload fixture with one of every kind of
message moved to the test utilities, so that these tests share it.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@eichhorl
eichhorl added this pull request to the merge queue Oct 5, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Oct 5, 2026
@eichhorl
eichhorl added this pull request to the merge queue Oct 5, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Oct 5, 2026
@eichhorl
eichhorl added this pull request to the merge queue Oct 5, 2026
Merged via the queue into master with commit fca9b57 Oct 5, 2026
42 checks passed
@eichhorl
eichhorl deleted the eichhorl/canister-http-canonical-payload branch October 5, 2026 15:28
AleDema pushed a commit to AleDema/ic that referenced this pull request Oct 18, 2026
…y#11719)

**This is the PR that turns hashes in blocks on for canister HTTP
responses.** A block
proposal now goes out without the contents of the responses it delivers,
naming them by
the content hash it already carries alongside each of them. Receivers
have been able to
serve (dfinity#11717) and reassemble (dfinity#11718) those contents since the previous
two PRs, so
this is where the bandwidth saving starts.

An outcall response can be up to 2 MB and the block used to carry it to
every peer, on
top of the copy the replicas already gossip to each other. A payload
with nothing to
strip is left untouched byte for byte, so a block without an outcall
response never pays
for being re-encoded.

Declaring a stripped content costs 36 bytes in the stripped proposal,
its 32-byte hash
plus framing, and only contents of at least 64 bytes are stripped, each
of which saves
at least 70 bytes for every slot it is taken out of. So **stripping a
response never
makes the proposal that goes out bigger** than the stripped proposal
that every data
block already goes out as. Break-even is at about 30 bytes of content,
and the threshold
deliberately stays close to it: the declared hash and the content it
replaces both go to
every peer, so the comparison is the same whatever the subnet size, and
with up to 500
responses per block each byte of slack is multiplied by five hundred —
at a 1 KiB
threshold a block could have carried half a megabyte of unstripped
content to every
peer. It also keeps the replica's own terse rejects in the block, which
save little or
nothing when stripped.

A response is stripped whatever kind of outcall it answers, because in
each case the
replicas that signed for it are already holding its content — see the
module docs of
`canister_http.rs`.

The assembler benchmark gains the canister HTTP dimension, parameterized
by the number
of signatures a response carries, since that is what dominates the cost
of stripping and
reassembling. At the 500-response cap with a 40-node quorum, stripping a
block costs
**5.4 ms** and reassembling one **9.8 ms**. That case uses 1 KiB
responses and the
10-byte node ids of the test utilities; with real, 29-byte node ids, 500
responses with
27 signatures each only fit into the 2 MiB payload limit with bodies of
about 800 bytes,
but the signatures that dominate the cost stay the same.

> Earlier revisions of this description quoted 3.4 ms and 6.4 ms for
those cases. Two
> benchmark cases exceeded `MAX_CANISTER_HTTP_PAYLOAD_SIZE`, and
`iterator_to_bytes`
> silently drops what does not fit, so they measured roughly 300
responses under a label
> claiming 500. The cases are now sized to fit and
`fake_canister_http_payload` panics
> rather than let that happen again.

### Worth a reviewer's attention

For a fully replicated outcall, a node whose own adapter has not
produced the response
yet when the block arrives used to get the bytes in the block and now
does a round trip
for them. It is bounded — at least `n - f` peers hold the content, and
it is no worse on
bandwidth — but it is a new dependency of block assembly on a peer fetch
in the common
outcall mode, not just the non-replicated one.

A receiver puts the payload back together by encoding what it decodes,
which only
reproduces the bytes the block hash covers because payload validation
accepts nothing
but canonically encoded canister HTTP payloads (dfinity#11751).

### Stack

1. dfinity#11716 — protobuf definitions (merged)
2. dfinity#11717 — serve canister HTTP responses out of a block (merged)
3. dfinity#11718 — reassemble blocks whose canister HTTP responses were
stripped (merged)
4. **this PR — strip canister HTTP responses from block proposals**

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants