btcutil: test byte constructors reject trailing data
What changed, and why it matters
This commit only adds new unit tests. It does not change any production code. The tests verify that two helper functions already reject input that contains extra bytes after a valid Bitcoin block or transaction. Because no code behavior is changed, there is no direct security fix here.
No action required; this is a test-only commit. If the tests fail, investigate whether NewBlockFromBytes/NewTxFromBytes properly enforce no-trailing-data deserialization.
Security signals we found
Tests assert strict deserialization rejects trailing bytes
No production code changes
Evidence from the diff
The diff adds two test cases in btcutil/block_test.go and btcutil/tx_test.go: TestNewBlockFromBytesRejectsTrailingData and TestNewTxFromBytesRejectsTrailingData. Each serializes a known block/transaction, appends a single trailing 0x00 byte, and asserts that NewBlockFromBytes/NewTxFromBytes return an error. No implementation code is modified.
Changed components
btcutil/block_test.gobtcutil/tx_test.goInspect captured patch +33 / −0
diff --git a/btcutil/block_test.go b/btcutil/block_test.go
index e5ae983..b626990 100644
--- a/btcutil/block_test.go
+++ b/btcutil/block_test.go
@@ -200,6 +200,23 @@ func TestNewBlockFromBytes(t *testing.T) {
}
}
+// TestNewBlockFromBytesRejectsTrailingData verifies that NewBlockFromBytes
+// rejects bytes after the serialized block.
+func TestNewBlockFromBytesRejectsTrailingData(t *testing.T) {
+ var block100000Buf bytes.Buffer
+ err := Block100000.Serialize(&block100000Buf)
+ if err != nil {
+ t.Errorf("Serialize: %v", err)
+ }
+
+ _, err = btcutil.NewBlockFromBytes(
+ append(block100000Buf.Bytes(), 0x00),
+ )
+ if err == nil {
+ t.Fatal("expected error for block with trailing data")
+ }
+}
+
// TestNewBlockFromBlockAndBytes tests creation of a Block from a MsgBlock and
// raw bytes.
func TestNewBlockFromBlockAndBytes(t *testing.T) {
diff --git a/btcutil/tx_test.go b/btcutil/tx_test.go
index 52447ae..1a051bc 100644
--- a/btcutil/tx_test.go
+++ b/btcutil/tx_test.go
@@ -76,6 +76,22 @@ func TestNewTxFromBytes(t *testing.T) {
}
}
+// TestNewTxFromBytesRejectsTrailingData verifies that NewTxFromBytes rejects
+// bytes after the serialized transaction.
+func TestNewTxFromBytesRejectsTrailingData(t *testing.T) {
+ testTx := Block100000.Transactions[0]
+ var testTxBuf bytes.Buffer
+ err := testTx.Serialize(&testTxBuf)
+ if err != nil {
+ t.Errorf("Serialize: %v", err)
+ }
+
+ _, err = btcutil.NewTxFromBytes(append(testTxBuf.Bytes(), 0x00))
+ if err == nil {
+ t.Fatal("expected error for transaction with trailing data")
+ }
+}
+
// TestTxErrors tests the error paths for the Tx API.
func TestTxErrors(t *testing.T) {
// Serialize the test transaction.
Why this scored 15/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.