mirror of
https://github.com/bitcoin/bitcoin
synced 2026-12-04 15:08:38 +00:00
wallet: harden external signer psbt processing, revamp mock #36114
No reviewers
Labels
No labels
Android
Backport
Block storage
Brainstorming
Bug
Build system
CI failed
Consensus
Data corruption
Descriptors
Docs
DrahtBot Guix build requested
Feature
Fuzzing
GUI
Good First Review
IPC
Insufficient Review
Kernel
Linux/Unix
Mempool
Mining
Needs Backport (31.x)
Needs Backport (32.x)
Needs Benchmark
Needs Conceptual Review
Needs backport (30.x)
Needs rebase
Needs release note
Net
P2P
PSBT
Privacy
Private Broadcast
Questions and Help
RPC/REST/ZMQ
Refactoring
Resource usage
Review club
Scripts and tools
Settings
TX fees and policy
Tests
Tracking Issue
UTXO Db and Indexes
Up for grabs
Upstream
Utils/log/libs
Validation
Waiting for author
Wallet
Windows
good first issue
interfaces
macOS
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
bitcoin/bitcoin!36114
Loading…
Reference in a new issue
No description provided.
Delete branch "2026/08/external-signer-mock"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
The current external signer functional test mock can only echo a prepared PSBT. This gets in the way of testing more complicated scenarios, i.e. misbehaving signers and (MuSig2) multisig.
GetUnsignedTxid(): unlikeGetUniqueID(), this commits to PSBTv2 input sequences, which BIP 370 lets Updaters and Combiners change.The remaining commits prepare the test helpers and enforce IWYU for
src/external_signer.cpp.This PR absorbs the scenarios from bitcoin/bitcoin#35358, but is better able to test them thanks to the new mock signer implementation.
This PR does not intend to fully harden against a malicious external signer, and can't protect against a malicious HWI (equivalent) process running as the same user.
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.
External sites
Reviews
See the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste
<!--meta-tag:bot-skip-->into the comment that the bot should ignore.Conflicts
Reviewers, this pull request conflicts with the following ones:
src/and treat them as errors by hebasto)If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.
LLM Linter (✨ experimental)
Possible places where comparison-specific test macros should replace generic comparisons:
test/functional/mocks/signer.py:assert len(signed.i) > 1→ Preferassert_greater_than(len(signed.i), 1).2026-12-01 18:09:13
cc @bigspider and other hardware wallet folks, any other hardware wallet shenanigans we should check against (perhaps in a followup)?
Approved flame
MarkUp
🐙 This pull request conflicts with the target branch and needs rebase.
Rebased after #36113, still based on #36076.
Concept ACK
I made the new mock return a finalized response and
FindUnsafeSighashTypedoesn't catch it. The fields it reads are the onesPSBTInput::Serializewrites only when the input isn't finalized, and BIP 174 has a finalizer clear them, so the signatures arrive infinal_script_sigandfinal_script_witnessalone.With
SIGHASH_NONEthesendcomes back complete, both signatures in the transaction end in0x02, andtestmempoolacceptallows it.The commit message says the change rejects a response containing signatures made with an unsafe sighash type, and a finalized one does. The different-transaction check still rejects a finalized response that changes an output.
Is skipping the finalized case deliberate, or worth covering?
nit:
Mergeonly returns false for two reasons,GetUniqueIDand the version comparison, since neitherPSBTInput::MergenorPSBTOutput::Mergehas a failure path.I returned the same transaction as a v0 PSBT and
sendfailed with this message.GetUniqueIDis what stops it, since it zeroesnSequenceonly from version 2 on, and before this commit that response was accepted.Worth a separate message, or does the signer have to return the version it was given?
nit: this transaction has an ECDSA and a taproot input, and
FindUnsafeSighashTypecollects from both before it scans, so either signature alone triggers the rejection. Disabling them_tap_key_sigcollection leaveswallet_signer.pygreen, and disabling thepartial_sigsone does too. Only removing both makessighash_none_hiddenfail.A single-input transaction of each type would make each branch carry its own case. Worth adding?
Rebased after https://github.com/bitcoin/bitcoin/pull/36076 and addressed @jeanpablojp's feedback.
It now rejects finalized PSBTs, which avoids complicating
FindUnsafeSighashType. HWI doesn't finalize.I simplified test setup by using
create_outpoints.Also enforced IWYU.
Added a test case.
I added a constraint that the version must match.
Earlier concerns neither republished nor resolved: 1 major finding · 1 minor finding · 1 suggestion. See the Run archive linked from the Review report for their assessments.
🟠 Minor · Finalized-input check rejects inputs already finalized in the request
When a signer echoes a pre-finalized foreign input, mixed-wallet PSBT signing now fails with
EXTERNAL_SIGNER_FAILED; the in-tree mock preserves these fields, but HWI behavior is unconfirmed.Location:
src/external_signer.cpp:178(head).🔵 Suggestion · Unsafe-sighash rejection attributes request data to the signer
A signer that honors an existing
NONEorSINGLEdeclaration can prompt for approval before Core rejects the echoed request data as signer misbehavior.Location:
src/external_signer.cpp:185(head).🔵 Suggestion · Signer tests miss the Taproot script-path sighash check
Removing the
m_tap_script_sigsscan would leave the new external-signer tests passing because their fixtures produce only ECDSA and key-path Schnorr signatures.Location:
src/external_signer.cpp:111(head).🔵 Suggestion · The
signtxcontract omits the accepted sighash typesA same-version, unfinalized signer response can satisfy the documented requirements yet be rejected for using
SIGHASH_NONEorSIGHASH_SINGLE.Location:
src/external_signer.cpp:183(head).🔵 Suggestion · External-signer compatibility changes lack a user-facing release note
The new response checks can reject previously accepted signer tools with only a generic RPC error; the number of affected deployed signers is unknown.
Location:
doc/external-signer.md:153(head).Review coverage was incomplete.
Reviewed:
70f13577ba61· Review report · Previous reportspermalink
🐙 This pull request conflicts with the target branch and needs rebase.
Trivial rebase after #36440.
tACK
26f84e2fdbI built the head and compared its signer path with two Combiners: BIP174's, as
combinepsbtruns it, and btclib-wallet'scombine. The new checks stop what a Combiner lets through.A replaying mock returned responses built from the wallet's real request, and
wallet_signer.pypasses. The signer path and both Combiners reject a removed output, a reduced value and a changed script. Only the signer path rejects NONE and SINGLE signatures, aPSBT_IN_SIGHASH_TYPEof NONE, and one or all inputs finalized. ALL|ANYONECANPAY passes and gives the honest txid. A changed v2 sequence or a corrupted signature leaves the wallet's transaction as it was and returns an incomplete PSBT. I can share the probe and its output.🐙 This pull request conflicts with the target branch and needs rebase.
Rebased after #36624.
@fametrano I expanded the tests based on your comment. If anything is still missing, feel free to suggest additional test coverage (or publish your probe in a gist, linking to that).
@Sjors the new tests cover the cases in my tACK except two: a changed v2 input sequence and a corrupted signature. In both the wallet's transaction stays as it was and
walletprocesspsbtreturns an incomplete PSBT, which a test could pin.The probe and its output: https://gist.github.com/fametrano/e808f936324f16573f74e5f0f3aadf02
I re-ran it on the new head and
wallet_signer.pypasses. tACKdc30c41f9bThanks, added two more cases.
tACK
42298793aaThe two new cases are the ones I suggested, and each fails when its protection is removed. Copying the signer's sequence into the merged PSBT fails the sequence case. Skipping signature verification fails the corrupted-signature case, for ECDSA and Schnorr. The wallet verifies in two places,
ProduceSignatureandCWallet::FillPSBT, so both had to go.wallet_signer.pypasses, and my probe gives the same outcomes as in the gist.Concept ACK, left an inline suggestion
Was following an issue and imo it feels a bit stringent to REJECT this. Is leaning towards a warning possible, and let the signer determine how to handle it? So that the function
FindUnsafeSighashTypeis only used to detect the unsafe sighash for the sake of emitting a warningAdded tests work as expected
Introduced a few changes to the test/functional/mocks/signer.py, and they were successfully caught
if mode == "psbt_v0"psbt.o[0].map[PSBT_OUT_SCRIPT] = bytes([0x51])branchif mode in ("sighash_none", "sighash_none_hidden"):branchelif mode in ("finalized", "partially_finalized"):branchelif mode == "strip":branchVery few users are going to see that warning, especially in the GUI. We could add a warning mechanism to the signer flow (currently it either succeeds or errors), but can you explain what a use case would be for an unsafe sighash type?
An enum class could probably work instead of a boolean. No strong opinion on this, looks great as is
Collaboration between two parties, each with their inputs and outputs (saying this loosely though and only for SINGLE). If security outweighs flexibility, we can go with this. One that I haven't found any use case is NONE. Again looking great as is, no strong opinion
🐙 This pull request conflicts with the target branch and needs rebase.
Trivial rebase after #36807.
Concept ACK
aee9e7a4da26cb47319095f4820b4849672fa3aeBuilt on macOS arm64 and tested an edge case around PSBTv2 input sequences in
psbtx.Merge(*signer_psbtx)(src/external_signer.cpp:181):In
test_invalid_signer_signatures(test/functional/wallet_signer.py:319),walletcreatefundedpsbtpopulatesPSBT_IN_SEQUENCE, soPSBTInput::Mergepreserves the original sequence. However,PSBT_IN_SEQUENCEis optional in BIP 370 — when omitted (sequence == std::nullopt),GetUnsignedTx()(src/psbt.cpp:134) defaults toCTxIn::SEQUENCE_FINAL(0xffffffff).If a caller passes a v2 PSBT with an omitted
PSBT_IN_SEQUENCEtowalletprocesspsbtand the signer injects e.g.PSBT_IN_SEQUENCE = 0xfffffffdand signs that modified transaction:GetUniqueID()zeroesnSequencefor v2, sopsbtx.Merge(*signer_psbtx)returnstrue.PSBTInput::Merge(src/psbt.cpp:464) copiesinput.sequenceintopsbtxbecausesequence == std::nullopt.walletprocesspsbtreturnscomplete=Truedirectly for alegacyinput, and forbech32mres["psbt"]retains the injected0xfffffffd+ valid signature sofinalizepsbtreturnscomplete=True(testmempoolacceptallowed).Would it make sense to snapshot
psbtx.GetUnsignedTx()beforeMergeand verify the unsigned tx hash did not change afterwards? That still permits thestripmode (wheresigner_psbtxomitsPSBT_IN_SEQUENCE) while catching sequence injection whenPSBT_IN_SEQUENCEwas omitted on the original input.(Disclosure per AI policy: used an LLM while auditing
PSBTInput::Mergecallers, then builtaee9e7alocally and verified the regtest case manually.)@johnny9 that's quite a lot of back-pings :-)
Added a check based on @enessubass's feedback: https://github.com/bitcoin/bitcoin/pull/36114#issuecomment-7142498603
I ended up adding a
GetUnsignedTxid()helper topsbt.hfor this.ACK
70f13577baGetUnsignedTxid()cleanly closes the omitted-PSBT_IN_SEQUENCEcase. Reviewed all 9 commits and manually tested a couple of extra regtest cases not in the test suite: an omittedPSBT_IN_SEQUENCEwhere the signer explicitly fills0xffffffff(SEQUENCE_FINAL, accepted since the unsigned txid does not change, whereas144CSV is rejected), and a 65-bytePSBT_IN_TAP_SCRIPT_SIGcarrying a hiddenSIGHASH_NONE/SINGLE|ANYONECANPAYbyte.(AI policy disclosure: used an LLM to help script the local regtest checks; reviewed the commits directly.)