Skip to content

fix(core): key CX-receipt spent-marker off the signed header, not MerkleProof - #8

Open
Frozen wants to merge 1 commit into
devfrom
fix/cx-receipt-replay-spent-key
Open

fix(core): key CX-receipt spent-marker off the signed header, not MerkleProof#8
Frozen wants to merge 1 commit into
devfrom
fix/cx-receipt-replay-spent-key

Conversation

@Frozen

@Frozen Frozen commented Aug 12, 2026

Copy link
Copy Markdown
Owner

Summary

Independent of #6 (quorum-mask fix). Closes a replay path against genuine, validly-signed CX-receipt proofs, distinct from the forged-signature bug.

  • `IsSpent` / `WriteCXReceiptsProofSpent` only derived the double-spend key from the signed `Header` (`ShardID`/`Number`) once `IsCXMerkleProofReplayFixEpoch` is active for the proof's claimed epoch. For any proof whose `Header.Epoch()` precedes that fork epoch, the key fell back to `CXMerkleProof.ShardID`/`BlockNum` — fields `ValidateCXReceiptsProof` never binds to the `Header` for those epochs.
  • The `Header` (and its signature) can't be altered without invalidating `VerifyHeaderSignature`, but `MerkleProof.ShardID`/`BlockNum` are otherwise unauthenticated for pre-fix-epoch proofs.
  • Net effect: a genuine, already-applied incoming receipt could be resubmitted with a mutated `MerkleProof` identity (different claimed `ShardID`/`BlockNum`, same real `Header`+signature+`Receipts`). Each resubmission hashes to a different "unspent" key → `ApplyIncomingReceipt` credits the destination again with no corresponding new source debit → net-new ONE, same mechanism as ordinary inflation but via replay of a real receipt instead of a forged one.

Fix

Always derive the spent-marker key from `Header.ShardID()`/`Header.Number()`, dropping the epoch gate.

Why this is safe without a new fork-epoch: this only changes internal bookkeeping (which key records "already applied"), not `ValidateCXReceiptsProof`'s acceptance criteria — no new rejection path is introduced, so it can't cause a resyncing node to reject an already-finalized historical block. For every honestly-generated proof, `MerkleProof.ShardID`/`BlockNum` are naturally populated from the same source block as `Header`, so the on-disk key is byte-identical to what's already stored — no migration needed. Only a proof with a deliberately mismatched `MerkleProof` identity now resolves to the (correct) `Header`-derived key instead of an attacker-chosen one.

Test plan

  • New test `TestIsSpentIgnoresMutatedMerkleProofIdentity`: writes a spent-marker for a genuine proof, then confirms a "replay" with the same `Header` but mutated `MerkleProof.ShardID`/`BlockNum` is still detected as spent.
  • Existing `TestWriteCXReceiptsProofSpentUsesMerkleProofIdentity` / `TestWriteCXReceiptsProofSpentWithKey` (rawdb-level, unchanged low-level functions) still pass.
  • `go build ./core/...`, `go vet ./core/...`

🤖 Generated with Claude Code

…kleProof

IsSpent and WriteCXReceiptsProofSpent only derived the spent-marker key from
the signed block.Header (ShardID/Number) from IsCXMerkleProofReplayFixEpoch
onward. For any proof whose Header.Epoch() precedes that fork epoch, the key
fell back to CXMerkleProof.ShardID/BlockNum -- fields that ValidateCXReceiptsProof
never binds to the Header for those epochs. Since the Header (and therefore its
signature) can't be altered without invalidating VerifyHeaderSignature, but
MerkleProof.ShardID/BlockNum are otherwise unauthenticated, a genuine,
already-applied CXReceiptsProof could be resubmitted with a mutated
MerkleProof identity: each resubmission hashed to a different, "unspent"
key, letting ApplyIncomingReceipt credit the destination again with no
corresponding source debit.

Always derive the spent-marker key from the Header. This doesn't change what
ValidateCXReceiptsProof accepts, and is a no-op for honestly generated
proofs -- MerkleProof.ShardID/BlockNum are naturally populated from the same
source block as the Header, so the on-disk key is unchanged for real
historical data; only a proof with a deliberately mismatched MerkleProof
identity resolves to a different (correct) key than before.

Add TestIsSpentIgnoresMutatedMerkleProofIdentity covering the replay case.

@greptile-apps greptile-apps Bot 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.

Your trial has ended. Reactivate Greptile to resume code reviews.

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