Skip to content

[security] Verify input scripts against the scriptPubKey they commit to - #1047

Merged
newtonick merged 9 commits into
SeedSigner:devfrom
kdmukai:2026_09_psbt_input_scripts
Oct 29, 2026
Merged

newtonick merged 9 commits into
SeedSigner:devfrom
kdmukai:2026_09_psbt_input_scripts

Conversation

@kdmukai

@kdmukai kdmukai commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

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

Required scripts:
  * p2wsh: requires witness_script
  * p2sh: requires redeem_script
  * p2sh-p2wsh (nested segwit multisig): requires witness_script and redeem_script
    * (the redeem_script here can be derived from the witness_script, so only a buggy coordinator would omit it)

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:

  • redeem_script: only allowed on p2sh inputs
  • witness_script: only allowed on p2wsh and p2sh-p2wsh inputs

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

Read for Source
The checks, script type by script type _verify_input_scripts
Where the check runs among #995's amount checks, and why the per-input loop in _verify_input_amounts_and_scripts
What each error means, and its grade the three error classes
Every refused shape, including on another party's input TestPSBTParserInputScripts

The first commit's message gives the full argument in prose.


Measured on dev, without this PR

Two misleading results, both refused by this PR as PSBTExtraneousInputScriptError:

  • False change. A witness script added to a nested single sig input, a 1-of-2 of the user's
    key and an attacker's key, makes _get_policy read the wallet as that multisig. An output the
    attacker can spend alone then parses as the user's change (10,000 sats, no destinations).
  • False fee. A witness script added to a legacy p2sh input described by a witness_utxo
    alone 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

  • A consistent forgery of the scriptPubKey. The check binds each script to the scriptPubKey
    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), with S supplied, parses under S's policy and an output the
    attacker can spend parses as change. That holds with a witness_utxo alone and with a
    non_witness_utxo plus a rewritten txid. Funds stay safe: the signature commits to S
    (measured invalid for the real coin) or to the rewritten outpoint.
  • Nested segwit payjoin. The receiver's finalized input carries no scripts, so the psbt is
    refused as "Transaction Problem" (on dev, "Likely an Attack!"). Native segwit payjoins parse.
    The TODOs in _verify_input_scripts and _parse_inputs say what support would need.

Screenshots

Three new screens. Each ends the flow with "Discard transaction".

PSBTMissingInputScriptView (Warning)

PSBTMissingInputScriptView

PSBTInputScriptMismatchView (Dire Warning)

PSBTInputScriptMismatchView

PSBTExtraneousInputScriptView (Warning)

PSBTExtraneousInputScriptView

Testing

Mutations, full suite each time:

Mutation Tests that fail
Drop the missing-script refusals 3
Drop the mismatch refusals 3
Drop the extraneous-script refusals 2
Skip the p2sh-p2wsh inner layer 16
Remove the _verify_input_scripts call 7, every new test
Move the check after the rule for a witness_utxo alone 1
Move the check before the previous-transaction checks 3
Remove one except clause in PSBTOverviewView 1, that error's flow test

This pull request is categorized as a:

  • Other: security hardening

Checklist

I ran pytest locally

  • All tests passed before submitting the PR

I included screenshots of any new or modified screens

Should be part of the PR description above.

  • Yes

I added or updated tests

Any new or altered functionality should be covered in a unit test. Any new or updated sequences require FlowTests.

  • Yes

I tested this PR hands-on on the following platform(s):


I have reviewed these notes:

  • Keep your changes limited in scope.
  • If you uncover other issues or improvements along the way, ideally submit those as a separate PR.
  • The more complicated the PR, the harder it is to review, test, and merge.
  • We appreciate your efforts, but we're a small team of volunteers so PR review can be a very slow process.
  • Please only "@" mention a contributor if their input is truly needed to enable further progress.
  • I understand

Thank you! Please join our Devs' Telegram group to get more involved.

@newtonick newtonick added this to the 0.8.8 milestone Sep 25, 2026
@newtonick newtonick moved this to 0.8.8 Needs Review in @SeedSigner Development Board Sep 25, 2026
@kdmukai
kdmukai force-pushed the 2026_09_psbt_input_scripts branch 2 times, most recently from 7779520 to e92a972 Compare September 30, 2026 20:00
@kdmukai
kdmukai marked this pull request as ready for review October 3, 2026 18:47
@newtonick

newtonick commented Oct 7, 2026 •

Copy link
Copy Markdown
Collaborator

@kdmukai this PR currently includes 3 commits from PR #1046 that have already been merged. Can you rebase onto dev?

@newtonick

Copy link
Copy Markdown
Collaborator

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.

Comment thread src/seedsigner/models/psbt_parser.py Outdated
Comment on lines 360 to 361

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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."

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in 69bc9f4

@newtonick

Copy link
Copy Markdown
Collaborator

ACK e92a972. Reviewed and tested with device with testqrs.com

@kdmukai

kdmukai commented Oct 8, 2026

Copy link
Copy Markdown
Contributor Author

@kdmukai this PR currently includes 3 commits from PR #1046 that have already been merged. Can you rebase onto dev?

afaict from my end, it should be able to do a clean merge into current dev. The #1046 commits are already in dev's history, so the diff here should only show this PR's new changes.

@Chaitanya-Keyal Chaitanya-Keyal left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread tests/test_psbt_parser.py Outdated
Comment on lines +2447 to +2449
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)])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a copy of the same function in TestPSBTParserOutputOwnership:

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)])

It could be moved to PSBTParserOwnershipTestBase.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in 644699d

@Chaitanya-Keyal Chaitanya-Keyal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

re-ACK 69bc9f4

Comment thread src/seedsigner/models/psbt_parser.py Outdated
script_pubkey = inp.script_pubkey

# Verify the input's scripts and reject the psbt if verification fails
PSBTParser._verify_input_scripts(inp, script_pubkey)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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".

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e031426 fixes this ordering issue

@kdmukai

kdmukai commented Oct 22, 2026

Copy link
Copy Markdown
Contributor Author

Rebased to the latest in dev at ebc1e11.

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.

@Chaitanya-Keyal Chaitanya-Keyal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK dd3af65.

@newtonick

Copy link
Copy Markdown
Collaborator

@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!

@kdmukai

kdmukai commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor Author

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 _verify_input_amounts_and_scripts. For each input, the script check runs:

  • after the missing-utxo check and the checks on the input's previous transaction (output index in range, txid, and witness_utxo cross-check). The scriptPubKey is available by then. A forged previous transaction is still refused as an attack, even when the input's scripts are also wrong or missing.
  • before the check on inputs that carry only a witness_utxo, which reads the scripts to tell nested segwit from legacy p2sh.

testqrs scenarios. The #1047, #995, and #1041 scenarios, built from btc-datagen main at kdmukAI-bot/btc-datagen@7d7958c (the commit testqrs.com is deployed from) and parsed with PSBTParser, with each raised error mapped to its screen:

  • 717a0e9e (before the fix): 4 mismatches, each reaching "Likely an Attack!" through PSBTInputAmountVerificationError:
    • test-1047-missing-redeem-nested-singlesig
    • test-1047-missing-witness-nested-multisig
    • test-1047-payjoin-missing-redeem
    • test-1047-payjoin-finalized
  • bcd2610b (current head): all 26 match. Those four now reach "Transaction Problem" through PSBTMissingInputScriptError.

The testqrs.com blurbs for those four scenarios now give the attack screen as the outcome on a build without the fix.

Test. test__parse__checks_input_scripts_whoever_the_input_belongs_to now gives its foreign nested segwit input a witness_utxo alone, so its missing-redeem-script step takes the path above. The test fails on the previous order and passes on the current one.

test-995-legacy-outpoint-out-of-range. In that scenario the previous transaction is genuine and only the output index is impossible. #995 raises PSBTMissingInputUtxoError for it, which routes to "Transaction Problem" as a correctness problem. testqrs.com has expected "Transaction Problem" for this scenario since kdmukAI-bot/btc-datagen#4, deployed 2026-10-22.

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.
@kdmukai
kdmukai force-pushed the 2026_09_psbt_input_scripts branch from dd3af65 to bcd2610 Compare October 24, 2026 03:19
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.
@kdmukai

kdmukai commented Oct 26, 2026

Copy link
Copy Markdown
Contributor Author

Final revisions done (pending any further review comments). And tested via testqrs.com.

@Chaitanya-Keyal

Copy link
Copy Markdown
Contributor

re-ACK dd2a2be

@newtonick

Copy link
Copy Markdown
Collaborator

ACK, tested and reviewed

@newtonick
newtonick merged commit ada81cd into SeedSigner:dev Oct 29, 2026
2 checks passed
@newtonick newtonick moved this from 0.8.8 Needs Review to 0.8.8 Merged in @SeedSigner Development Board Oct 29, 2026
@jdlcdl

jdlcdl commented Nov 5, 2026

Copy link
Copy Markdown

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: 0.8.8 Merged

Development

Successfully merging this pull request may close these issues.

4 participants