Repository navigation
[security] Verify input scripts against the scriptPubKey they commit to - #1047
Conversation
7779520 to
e92a972
Compare
|
Since the PR description doesn't mention it, I'll mention it: testqrs.com has been updated with this PR's test scenarios (listed under "PR #1047: input scripts"). It can be used to exercise these new checks. |
There was a problem hiding this comment.
nit: the _parse_inputs docstring could mention the new script check step, e.g. "Totals the input amounts, verifies each input's scripts, and determines the wallet policy."
|
ACK e92a972. Reviewed and tested with device with testqrs.com |
There was a problem hiding this comment.
ACK e92a972. Reviewed the 4 new commits and tested on device with testqrs.com.
On the duplicate commits: the #1046 commits have the same SHAs, so on merging, dev only picks up the 4 new ones. But the PR view hasn't caught up: GitHub still has the base pinned at cfaf443 (dev before #1046 merged), so Commits shows 7 and Files changed shows +660/−28 including #1046's diff. Against current dev it's 4 commits, +512/−13. GitHub only refreshes that on a push to the branch, so any new push would clear it.
One nit inline.
| def _foreign_multisig_script(self) -> script.Script: | ||
| """A 2-of-3 built entirely from someone else's keys.""" | ||
| return script.multisig(2, [foreign_public_key(f"m/48h/1h/0h/2h/0/{i}") for i in range(3)]) |
There was a problem hiding this comment.
This is a copy of the same function in TestPSBTParserOutputOwnership:
seedsigner/tests/test_psbt_parser.py
Lines 1389 to 1391 in e92a972
It could be moved to PSBTParserOwnershipTestBase.
| script_pubkey = inp.script_pubkey | ||
|
|
||
| # Verify the input's scripts and reject the psbt if verification fails | ||
| PSBTParser._verify_input_scripts(inp, script_pubkey) |
There was a problem hiding this comment.
I was just going through this PR again and now that #995 is merged, this should run before _verify_input_amounts(), which uses these scripts to decide legacy vs segwit.
Merged with dev locally, 4 of this PR's testqrs.com scenarios show "Likely an Attack!" instead of "Transaction Problem" (nested segwit inputs missing a script). This check needs the scriptPubKey, so it has to go after the missing-utxo and outpoint checks.
Also, unrelated to this PR: testqrs.com expects the attack screen for test-995-legacy-outpoint-out-of-range, but #995 now shows "Transaction Problem".
There was a problem hiding this comment.
Also, unrelated to this PR: testqrs.com expects the attack screen for test-995-legacy-outpoint-out-of-range, but #995 now shows "Transaction Problem".
Can confirm. This comment made me realize I forgot to create a follow up PR to btc-datagen for #995. So I did that just now: kdmukAI-bot/btc-datagen#4
69bc9f4 to
ff72286
Compare
|
Rebased to the latest in I'll need to do my own review pass of this PR's changes to make sure that everything in this PR still makes optimal sense against the latest changes. |
|
@kdmukai sorry for all the rebasing and conflict resolution on this one. I needed to start merging and just accept there would be conflicts. Too many stacked PRs touching psbt_parser. This one is next in the queue ideally to be reviewed and merged. Thanks! |
|
Fixes per @Chaitanya-Keyal's comment in 85833cc, followed by a comment-only rewrap in bcd2610. Summary of changes written by Claude: Order. The input script check now runs inside #995's per-input loop, which is renamed
testqrs scenarios. The #1047, #995, and #1041 scenarios, built from btc-datagen
The testqrs.com blurbs for those four scenarios now give the attack screen as the outcome on a build without the fix. Test.
|
BIP-174 asks a signer to hash an input's redeem or witness script against the scriptPubKey being spent. We read the wallet policy (m-of-n, cosigner keys) out of those scripts and never checked them, so a coordinator's own script decided which outputs we displayed as the user's change. _verify_input_scripts runs on every input before the policy is read, and each refusal has its own screen: - A missing script raises PSBTMissingInputScriptError, graded as a correctness problem. - A script that hashes to the wrong value raises PSBTInputScriptMismatchError, graded as an attack. It is the same contradiction between a supplied script and its scriptPubKey that PSBTOutputOwnershipContradictionError grades as an attack on outputs. - An extraneous script (a witness script on a legacy multisig input, a redeem script on a native segwit input) raises PSBTExtraneousInputScriptError, left ungraded. It is refused on every input type: there is no hash to check it against, and _get_policy decides a p2sh input is nested segwit from the mere presence of a witness script. Measured across the four p2sh and p2wsh fixtures: no signature over a wrong script was valid against the real coin, because embit builds the digest from the first supplied script (witness, then redeem, then scriptPubKey). Funds were never movable this way; what the check closes is the display.
The _verify_input_scripts docstring said BIP-174 requires the psbt to supply an input's redeem or witness script. BIP-174 allows a psbt to be passed between roles before every script is known, with each Updater adding the scripts it knows. A signer still needs an input's script to produce a valid signature for that input. BIP-174's signer rules check a script once it is provided. The mismatch and extraneous checks enforce those rules. The docstring now also groups the checks by the inputs they cover. The missing and mismatch checks run only on p2sh and p2wsh inputs. The extraneous script check runs on every input. Docstring-only change.
A payjoin receiver's input arrives finalized (BIP-78), and finalizing clears its redeem and witness script fields (BIP-174). So a nested segwit payjoin is refused as missing its input script; before input script verification it was refused as "Mixed inputs". Native segwit payjoins parse either way. Two TODOs mark what payjoins and other collaborative spends would need: - recover a finalized input's script from its final scriptSig or witness - read the wallet policy only from the user's own inputs The every-input test's docstrings now describe its fixture by the rule it tests rather than as a payjoin input. It is an unfinalized input that carries its redeem script, built to check that every input's scripts are verified whoever the psbt says the input belongs to. Comment-only change.
The comment above the _get_policy call in _parse_inputs said the input's scripts could now be used "safely". The check before it binds each script to the scriptPubKey the psbt supplies, and nothing in the parse authenticates that scriptPubKey yet, so the word claims more than the check establishes. The shared test base class described itself as the base for the two ownership test classes. The input script tests are a third subclass, and not an ownership test class. Comment-only change.
TestPSBTParserInputScripts carried a copy of _foreign_multisig_script identical to the one in TestPSBTParserOutputOwnership. Both classes now inherit it from PSBTParserOwnershipTestBase. The base class docstring listed the fixtures the class body defines. Adding this helper would have meant extending that list. It now names the tests the fixtures support: how PSBTParser verifies a psbt's claims about its inputs and outputs. That covers the input script tests too, which check an input's scripts whoever owns it. Test-only change.
The docstring now lists the method's two steps in the order they run: verifying each input's scripts and determining the wallet policy. Docstring-only change.
To decide whether an input's sighash commits its amount, the amount check reads the input's script type, and telling nested segwit from legacy p2sh reads the input's redeem and witness scripts. Those scripts were verified only later, in _parse_inputs. So a witness-only nested segwit input missing one of its scripts read as legacy p2sh and was refused with PSBTInputAmountVerificationError, graded as an attack, instead of PSBTMissingInputScriptError. The script check now runs inside the amount check's loop: after the checks on an input's previous transaction, so a forged one is still refused as an attack, and before the check that reads the scripts. The method is renamed _verify_input_amounts_and_scripts to match, and the per-type utxo requirements move to the comment on UNCOMMITTED_AMOUNT_SCRIPT_TYPES. The foreign nested segwit input in the every-input test now carries a witness_utxo alone, so its missing-redeem-script case takes the witness-only path. The test fails on the previous order.
The docstring and inline comments in _verify_input_amounts_and_scripts ran to 104 columns. They are now wrapped at 90, with no wording changes apart from a closing period on the two comments that now span two lines. Comment-only change.
dd3af65 to
bcd2610
Compare
parse()'s docstring is a high-level overview of the steps. _verify_input_amounts_and_scripts and _verify_input_scripts each end their docstring with a list of every error they raise, and name the errors only there.
|
Final revisions done (pending any further review comments). And tested via testqrs.com. |
|
re-ACK dd2a2be |
|
ACK, tested and reviewed |
|
Regarding proposed embit pr152 I have tested seedsigner as of Nov 1 (last merge pr1047), using pr152 atop embit 0.8.2, instead of embit 0.8.0. All tests for this pr at testqrs dot com still behave exactly as expected. |
Description
This PR increases the scrutiny and verification applied to INPUTS' scripts during the psbt parse.
Minor note: most script types require at most a single script to be provided (e.g. p2wsh only requires a witness script; some require no script at all), but multisig nested segwit (p2sh-p2wsh) requires both a redeem and witness script. So I will intermix singular "script" and plural "scripts" at various points here.
An input's script is supplied by the coordinator and then the parser reads the wallet policy out of that script. That policy decides which outputs are shown as change, so we must verify the input scripts before relying on them.
Note: there's no actual loss of funds risk associated with incorrect or malicious input scripts. If a bad input script tricks our UI into showing an output as our change, that same bad script will result in an invalid signature that cannot spend the input. So no real harm done. The added rigor in this PR catches scripts that contradict their input's scriptPubKey and raises an error, preventing the user from wasting any time on such a psbt.
Input script verification is straightforward: either the supplied scripts match what the input's scriptPubKey commits to or they don't.
This PR adds that input-side script verification as well as some input script correctness sanity checks.
Three types of issues are checked:
Required scripts must be included in the psbt
If any are missing, raise
PSBTMissingInputScriptError. This is considered a buggy psbt and not an attack.Verify that the input's scriptPubKey actually commits to the supplied script(s)
Rebuilds the input's scriptPubKey from the supplied scripts. If there is any disagreement, it means that the psbt has misrepresented details about the spending wallet, perhaps in an attempt to make a malicious output appear as if it was our change.
Raises
PSBTInputScriptMismatchError. Considered an attack (even though as noted above, there is no loss of funds risk due to this deception).No extraneous scripts are allowed:
The psbt adds an extraneous script that is nonsensical to include for the input's
script type:
If any extraneous scripts are present, raise
PSBTExtraneousInputScriptError. This could be due to a buggy coordinator or perhaps a malicious coordinator, but we don't need to make that determination; either way we reject the psbt.More precise description below by Claude, primarily intended to be consumed by other AIs
Where to read
_verify_input_scripts_verify_input_amounts_and_scriptsTestPSBTParserInputScriptsThe first commit's message gives the full argument in prose.
Measured on
dev, without this PRTwo misleading results, both refused by this PR as
PSBTExtraneousInputScriptError:key and an attacker's key, makes
_get_policyread the wallet as that multisig. An output theattacker can spend alone then parses as the user's change (10,000 sats, no destinations).
witness_utxoalone makes [security] Verify PSBT input amounts before displaying the fee #995's amount check read the input as segwit and accept its amount as stated.
Raising that amount by 100,000 sats raised the parsed fee by 100,000 sats.
Neither moves funds. Across the four p2sh and p2wsh fixtures, embit's signature for each
tampered input this PR refuses is invalid against the real coin, the false-fee psbt's included.
The exceptions are two inert shapes, a wrong or missing redeem script on p2sh-p2wsh and an extra
redeem script on p2wsh, which leave the signature valid and are refused anyway. Measured on embit
0.8.0, the pinned version.
Not covered
the psbt supplies, and nothing in the parse authenticates that scriptPubKey. [security] Verify PSBT input amounts before displaying the fee #995's txid check
does not either, since the txid is also a field of the psbt. Measured at this PR's head: an
input forged to
p2wsh(S), withSsupplied, parses underS's policy and an output theattacker can spend parses as change. That holds with a
witness_utxoalone and with anon_witness_utxoplus a rewritten txid. Funds stay safe: the signature commits toS(measured invalid for the real coin) or to the rewritten outpoint.
refused as "Transaction Problem" (on
dev, "Likely an Attack!"). Native segwit payjoins parse.The TODOs in
_verify_input_scriptsand_parse_inputssay what support would need.Screenshots
Three new screens. Each ends the flow with "Discard transaction".
PSBTMissingInputScriptView(Warning)PSBTInputScriptMismatchView(Dire Warning)PSBTExtraneousInputScriptView(Warning)Testing
pytest: 332 pass, with 4 new parser tests and 3 new flow tests. The screenshot generatorpasses in all 22 locales.
expected screen.
Mutations, full suite each time:
_verify_input_scriptscallwitness_utxoaloneexceptclause inPSBTOverviewViewThis pull request is categorized as a:
Checklist
I ran
pytestlocallyI included screenshots of any new or modified screens
Should be part of the PR description above.
I added or updated tests
Any new or altered functionality should be covered in a unit test. Any new or updated sequences require FlowTests.
I tested this PR hands-on on the following platform(s):
I have reviewed these notes:
Thank you! Please join our Devs' Telegram group to get more involved.