Merge bitcoin/bitcoin#36456: Test: Cover malformed and truncated tx encodings
What changed, and why it matters
This commit only adds new test cases to Bitcoin Core. It does not change any production code, so it cannot directly introduce or fix a security vulnerability in running software. The tests verify that malformed and truncated transaction data is rejected properly, which helps ensure existing protections keep working.
No action required beyond normal review. Treat as a test-coverage improvement. If the tests fail, investigate the underlying deserialization code, but the diff itself is not a vulnerability patch.
Security signals we found
Adds regression-style tests for malformed transaction deserialization
Covers truncated CompactSize and witness transaction prefixes
Verifies rejection of unknown witness flag values
Evidence from the diff
The commit adds two unit tests. One checks that CompactSize integer prefixes announcing more bytes than are present throw an ‘end of data’ exception. The other builds a valid witness transaction, then confirms every strict prefix of its serialization fails to deserialize with ‘end of data’, and that a witness flag value other than 0x01 (specifically 0x02) is rejected as unknown optional data. These are purely test-coverage additions; no deserialization logic is modified.
Changed components
src/test/serialize_tests.cppsrc/test/transaction_tests.cppInspect captured patch +35 / −0
### src/test/serialize_tests.cpp
@@ -242,6 +242,15 @@ BOOST_AUTO_TEST_CASE(noncanonical)
BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, isCanonicalException);
}
+BOOST_AUTO_TEST_CASE(truncated_compact_size)
+{
+ // A CompactSize prefix followed by fewer bytes than it announces fails to read.
+ for (const char* hex : {"fd", "fd01", "fe010000", "ff01000000000000"}) {
+ DataStream ss{ParseHex(hex)};
+ BOOST_CHECK_EXCEPTION(ReadCompactSize(ss), std::ios_base::failure, HasReason{"end of data"});
+ }
+}
+
BOOST_AUTO_TEST_CASE(string_view)
{
const std::string_view sv{"hello, world"};
### src/test/transaction_tests.cpp
@@ -1394,4 +1394,30 @@ BOOST_AUTO_TEST_CASE(spends_witness_prog)
}
}
+BOOST_AUTO_TEST_CASE(tx_malformed_serialization)
+{
+ // A transaction with one input, one output and a one-element witness.
+ CMutableTransaction mtx;
+ mtx.vin.resize(1);
+ mtx.vin[0].scriptWitness.stack = {{0xaa}};
+ mtx.vout.emplace_back(1, CScript{} << OP_TRUE);
+ DataStream ser;
+ ser << TX_WITH_WITNESS(mtx);
+
+ CMutableTransaction tx;
+ DataStream{std::span{ser}} >> TX_WITH_WITNESS(tx);
+ BOOST_CHECK(CTransaction{tx}.HasWitness());
+
+ // Every strict prefix ends inside some field, such as a CompactSize, an output
+ // script, a witness element or the locktime, and fails to deserialize.
+ for (size_t len{0}; len < ser.size(); ++len) {
+ BOOST_CHECK_EXCEPTION(DataStream{std::span{ser}.first(len)} >> TX_WITH_WITNESS(tx), std::ios_base::failure, HasReason{"end of data"});
+ }
+
+ // A flag other than 1 is unknown, including one without the witness bit.
+ DataStream bad_flag;
+ bad_flag << mtx.version << uint8_t{0x00} << uint8_t{0x02} << mtx.vin << mtx.vout << mtx.vin[0].scriptWitness.stack << mtx.nLockTime;
+ BOOST_CHECK_EXCEPTION(bad_flag >> TX_WITH_WITNESS(tx), std::ios_base::failure, HasReason{"Unknown transaction optional data"});
+}
+
BOOST_AUTO_TEST_SUITE_END()Why this scored 12/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.