Repository navigation
fix(rpc): verify fetched mempool txids before caching and document trust assumption - #2316
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #2316 +/- ##
==========================================
+ Coverage 78.84% 78.88% +0.03%
==========================================
Files 31 31
Lines 6060 6061 +1
Branches 288 289 +1
==========================================
+ Hits 4778 4781 +3
+ Misses 1203 1201 -2
Partials 79 79
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
The MSRV job is failing because of a transitive dependency Happy to open a tiny PR for the pin if that's preferred. |
2141610 to
3fba250
Compare
Dmenec
left a comment
There was a problem hiding this comment.
Approach ACK
I was thinking if the validation should retry (maybe 3 times at most) when failing so you don't throw the whole poll, but I think in this specific scenario it would be more annoying than useful.
Not sure if compute_txid cover the witness, so maybe a node could still serve a different witness for the same txid? If not, maybe worth a word in the doc so it doesn't read as the whole tx being verified.
Also, checking the code I noticed that next_block fetches blocks by hash but never checks the returned block actually hashes to what was requested:
bdk/crates/bitcoind_rpc/src/lib.rs
Lines 282 to 291 in 3fba250
I think in the same way we should check the block hash, merkle root and witness commitment. Checking only the hash isn't enough, since it only covers the header, so a node could serve the genuine header with forged txs. I saw your benchmarks in #2283 so, as much, it should be a few ms per block. Given the scope was narrowed there, maybe better as a follow-up?
Also, just a few nits I've seen through
| Ok(()) | ||
| } | ||
|
|
||
| //A fetched mempool tx whose body does not match the requested txid is rejected. |
There was a problem hiding this comment.
| //A fetched mempool tx whose body does not match the requested txid is rejected. | |
| // A fetched mempool tx whose body does not match the requested txid is rejected. |
| serde = "1" | ||
| serde_json = "1" |
There was a problem hiding this comment.
nit: these deps aren't needed, bitcoincore_rpc already re-exports both. In the test module this should be enough
use bitcoincore_rpc::jsonrpc::{serde, serde_json};- Add `tx.compute_txid() == txid` check to validate that fetched raw mempool transaction hashes against their requested txid before caching in Emitter::mempool_at. Returns `Error::UnexpectedStructure` for a mismatch. - Add a regression test for the above check. - Document that the crate assumes a trusted `bitcoind` connection, and that the txid check does not cover witness data. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3fba250 to
0d6fb41
Compare
Description
This PR adds a check in
Emitter::mempool_at: when a mempool transaction is fetched withget_raw_transaction, its computed txid must match the txid that was requested before it goes intomempool_snapshot. This stops an RPC response with the wrong transaction body from being emitted or cached under the requested txid. It also adds module-level documentation stating thatbdk_bitcoind_rpcassumes a connection to a trustedbitcoindnode.This addresses only the missing txid-validation part of #2282, following discussions on #2283.
Notes to the reviewers
Error::UnexpectedStructure, so it is non-breaking. A single mismatch fails the whole poll; callers retry on the next call.mempool_snapshotwere checked when they were inserted.mempool_at.next_blockdoes not verify that the returned block matches the requested hash, merkle root, or witness commitment.RpcApithat implements onlycall.serde/serde_jsoncome from thebitcoincore_rpc::jsonrpcre-exports, so no new dependencies are added.Changelog notice
Emitter::mempool/mempool_at: when a mempool transaction is fetched from the node, check that its computed txid matches the requested txid. A mismatch is rejected withError::UnexpectedStructure.bdk_bitcoind_rpcassumes a trustedbitcoindRPC connection.Checklists
All Submissions:
New Features:
Bugfixes: