Skip to content

fix(pipeline): reject non-canonical header bytes instead of logging - #26

Open
cafebedouin wants to merge 2 commits into
mwaddip:mainfrom
cafebedouin:ergo-logic/header-roundtrip-reject
Open

cafebedouin wants to merge 2 commits into
mwaddip:mainfrom
cafebedouin:ergo-logic/header-roundtrip-reject

Conversation

@cafebedouin

@cafebedouin cafebedouin commented Sep 16, 2026 •

Copy link
Copy Markdown

Branch: ergo-logic/header-roundtrip-reject, stacked on #25 (declared section ids) (its commit is
included; merge that one first and this PR shows only its own commit — the two touch the same test
module and the same contract table). Files: src/pipeline.rs, facts/receive-path.md.

Why

Headers are the one receive sink where the label (the id) is recomputed from the parsed fields while
the bytes stored and served are the raw wire bytes. A header with trailing bytes parses to the same
id (same_id=true in the round-trip witness), the mismatch at process_headers is tracing::error!
only, and data.to_vec() is what put_header keeps and what the p2p serve hook and the API hand out.
facts/receive-path.md's invariant is met for sections (section_id_from_body::finish rejects
trailing bytes) and not for headers.

What

The round-trip mismatch now rejects the header with a PENALTY warn (or a debug line for local
ingest), before PoW, and a header that cannot be re-serialized is rejected too.

Tests

New: non_canonical_header_bytes_are_rejected (block 2666's canonical bytes plus four trailing
bytes: same id, not reported received, not buffered; the canonical bytes are accepted as the
control). Run: cargo test -p ergo-node-rust on the stacked branch: 83 passed (the two new tests included), 0 failed, 3 ignored (pre-existing ignores).

Does this penalize JVM peers?

No. The JVM never emits non-canonical header bytes: a header's bytes are its own re-serialization
(ergo v6.0.5, ergo-core/src/main/scala/org/ergoplatform/core/BytesSerializable.scala:12), its id is the hash of those bytes
(ergo-core/src/main/scala/org/ergoplatform/modifiers/history/header/Header.scala:62), the deserializer does not check for leftover input
(avldb/src/main/scala/org/ergoplatform/serialization/ErgoSerializer.scala:27-30), the received-modifier check compares the
declared id with that re-serialized hash (src/main/scala/org/ergoplatform/network/ErgoNodeViewSynchronizer.scala:819), and the
history store writes the re-serialization (src/main/scala/org/ergoplatform/nodeView/history/storage/HistoryStorage.scala:135). So the
JVM silently canonicalizes and never penalizes; every header it sends is canonical, and every header
this PR rejects would have been canonicalized by a JVM node. Rejecting is stricter than the reference.
The alternative that matches the JVM exactly is to keep the header and store reserialized instead
of data (one line at the store push) — your call; the invariant in facts/receive-path.md is met
either way, and the test here checks rejection.

Note for the maintainer

If a future header version legitimately carries unparsed_bytes, the round trip must preserve them
or this check will reject those headers. Header::scorex_serialize already writes unparsed_bytes
back out, but no test in the tree exercises a header with non-empty unparsed_bytes (every fixture
uses Box::new([]), and chain/src/cache.rs notes it is empty in every version accepted today), so
the preservation is by construction, not yet by test. A fixture with non-empty unparsed_bytes in
header_scorex_bytes_roundtrip would close that.

Changelog entry

Not in the patch (your repo instructions reserve CHANGELOG.md for the main session, and four PRs
inserting an Unreleased block at the same spot conflict with each other). Text, if useful:

#### Non-canonical header bytes are rejected, not stored

The pipeline recomputed a header's id from its parsed fields but stored and
served the raw wire bytes; a header with trailing bytes (or any
non-canonical encoding) parsed to the same id, the round-trip mismatch was
only logged, and the raw bytes were kept and served to peers and the API.
The mismatch now rejects the header with a penalty warn — the sections'
`finish()` rule applied to the header sink.

🤖 Generated with Claude Code

…section ids

bind_and_store_sections required only that a body hash to its delivered id
and that its header_id be a header the node holds; it never consulted the
header's roots. A body bound to its own label was therefore stored for any
known header, one row per distinct digest, under ids sync never queries —
fork-bound rows at height 0 that pruning cannot reach, and for best-chain
headers an unconditional height-index insert that let a poison body take
the honest body's index row. Load the header (best chain or stored fork)
and require (type_id, delivered_id) ∈ section_ids(header): the JVM's
bsCorrespondsToHeader membership test.

Test: body_under_its_own_id_for_a_known_header_is_dropped_unless_declared.
Contracts: facts/receive-path.md sections row, facts/sync.md request gate.
The header's id is recomputed from its parsed fields but the bytes stored
and served are the raw wire bytes. A header with trailing bytes parsed to
the same id, the round-trip mismatch was tracing::error! only, and the raw
bytes were kept, served to peers by the local-serve hook, and served by
the API — a label not recomputed from the bytes it labels, the one sink
facts/receive-path.md's invariant did not yet cover. The mismatch now
rejects the header with a PENALTY warn before PoW, and a header that
cannot be re-serialized is rejected too.

Test: non_canonical_header_bytes_are_rejected (block 2666's canonical bytes
plus four trailing bytes: same id, not received, not buffered; canonical
control accepted). Contract: facts/receive-path.md header row.
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.

1 participant