What changed, and why it matters
This commit adds a new test to check that the Bitcoin wire protocol v2 message parser rejects messages containing extra, unexpected trailing bytes. It does not change production code; it only strengthens test coverage for a parsing safety check that already existed for v1 messages.
No immediate action required. Treat as routine test-coverage improvement. If auditing, verify that ReadV2MessageN already rejects trailing bytes in production and that this test now exercises that path.
Security signals we found
Trailing-byte rejection is a defensive parsing invariant that can prevent protocol desynchronization or message-boundary confusion.
The test mirrors an existing v1 test (TestReadMessageTrailingBytes), suggesting the v2 code path may have lacked equivalent coverage.
No fix or behavior change is present; the commit only adds regression coverage.
Evidence from the diff
The diff adds TestReadV2MessageTrailingBytes in wire/message_test.go. It constructs a minimal v2 inv message payload with one unconsumed trailing byte (0xaa), calls ReadV2MessageN, and asserts that a *MessageError is returned. A blank line is also removed from an unrelated test table. No wire package implementation code is modified.
Changed components
wire/message_test.goReadV2MessageN parsing path (tested, not modified)Inspect captured patch +22 / −1
diff --git a/wire/message_test.go b/wire/message_test.go
index 278d8c2..dba4ac3 100644
--- a/wire/message_test.go
+++ b/wire/message_test.go
@@ -347,7 +347,6 @@ func TestReadMessageWireErrors(t *testing.T) {
ErrUnknownMessage,
24,
},
-
}
t.Logf("Running %d tests", len(tests))
@@ -428,6 +427,28 @@ func TestReadMessageTrailingBytes(t *testing.T) {
}
}
+// TestReadV2MessageTrailingBytes verifies that a v2 message with unconsumed
+// trailing bytes after BtcDecode is rejected with a MessageError.
+func TestReadV2MessageTrailingBytes(t *testing.T) {
+ payload := []byte{
+ v2Messages[CmdInv],
+ 0x00, // zero inventory vectors
+ 0xaa, // trailing data not consumed by MsgInv.BtcDecode
+ }
+
+ _, _, err := ReadV2MessageN(
+ payload, ProtocolVersion, BaseEncoding,
+ )
+ if err == nil {
+ t.Fatal("expected error for v2 message with trailing bytes")
+ }
+
+ var msgErr *MessageError
+ if !errors.As(err, &msgErr) {
+ t.Fatalf("expected MessageError, got: %T (%v)", err, err)
+ }
+}
+
// TestWriteMessageWireErrors performs negative tests against wire encoding from
// concrete messages to confirm error paths work correctly.
func TestWriteMessageWireErrors(t *testing.T) {
Why this scored 27/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.