p2p: test for calculate v1 checksum on original payload
What changed, and why it matters
This commit only adds a new unit test. It does not change any production code. The test checks that a malformed Bitcoin P2P v1 message with a non-standard boolean encoding and a bad checksum is correctly rejected. Because no actual code behavior is changed, this commit by itself does not fix or introduce a security issue.
No action needed for this commit alone. If reviewing the surrounding codebase, verify that the production decoder already validates the v1 checksum against the original raw payload bytes before deserialization, as the test assumes. Consider whether additional canonical-encoding tests are warranted.
Security signals we found
Fuzzer-derived malformed input
Checksum validation boundary
Non-canonical boolean encoding
Evidence from the diff
The diff adds a single test in p2p/src/message.rs named v1_message_rejects_invalid_checksum_with_noncanonical_encoding. The test feeds a fuzzer-derived SendCmpct payload where a boolean is encoded as 0x0c instead of the canonical 0x01, and the checksum is invalid against the raw payload but would be valid if the decoder re-encoded the payload. It asserts that decoding fails. No implementation code is modified, so this is a regression/behavior test rather than a patch.
Changed components
p2p/src/message.rs (test module only)Inspect captured patch +19 / −0
diff --git a/p2p/src/message.rs b/p2p/src/message.rs
index 7c7e57ed..b767869c 100644
--- a/p2p/src/message.rs
+++ b/p2p/src/message.rs
@@ -2951,4 +2951,23 @@ mod test {
let encoded = encoding::drain_to_vec(&mut encoder);
assert_eq!(encoded, expected_bytes);
}
+
+ #[test]
+ #[rustfmt::skip] // Keep readable byte layout with comments.
+ fn v1_message_rejects_invalid_checksum_with_noncanonical_encoding() {
+ // Derived from a fuzzer crash case, a SendCmpct message with a non-canonical
+ // boolean encoding (`0x0c` instead of `0x01`) and an invalid checksum.
+ // The decoder should validate the checksum against the raw bytes,
+ // not against re-encoded bytes, so it must reject this message.
+ let malformed_v1_message = [
+ 217, 173, 255, 0, // Network magic.
+ 115, 101, 110, 100, 99, 109, 112, 99, 116, 0, 0, 0, // `sendcmpct\0\0\0`
+ 9, 0, 0, 0, // Length is 9 bytes.
+ 23, 43, 230, 232, // Invalid checksum against payload, but valid against re-encoded payload.
+ 12, 12, 12, 218, 12, 14, 12, 226, 0, // Payload with non-canonical bool `0x0c`.
+ ];
+
+ encoding::decode_from_slice::<V1NetworkMessage>(&malformed_v1_message)
+ .expect_err("Message with invalid payload checksum should be rejected");
+ }
}
Why this scored 11/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.