Measure the change window from the smallest input index
What changed, and why it matters
This commit fixes a security check in SeedSigner, a small hardware-wallet-like device used to sign Bitcoin transactions. Previously, when deciding whether a change output's address was suspiciously far from the user's known addresses, the software measured from the largest input address index. A malicious transaction coordinator could add one extra input at a very high address index to widen the allowed window and sneak change into a distant, possibly attacker-controlled address without triggering a warning. The fix measures from the smallest input address index instead, so added high-index inputs can only narrow the window, not expand it. The commit also handles the edge case where all matching inputs use hardened (hidden) indices by measuring from zero.
Treat this as a security fix and include it in the next release. Users who build or install SeedSigner from source should update to a version containing this commit. Review any custom forks or downstream builds to ensure they apply the same change-output window logic. No immediate external incident response is indicated beyond normal patching.
Security signals we found
Fixes a window-widening attack against change-output verification
Change-output address index check now uses smallest input index instead of largest
Adds hardened-input edge-case handling (window measured from 0)
Includes updated and new unit tests for the corrected behavior
Commit message explicitly describes malicious-coordinator scenario
Evidence from the diff
In src/seedsigner/models/psbt_parser.py, _is_change_address_index_within_window() previously computed the largest non-hardened input address index sharing the change output’s base path and allowed change address indices up to largest + CHANGE_ADDRESS_INDEX_WINDOW (10,000). A malicious PSBT coordinator could include an extra input at a very high non-hardened index to raise largest_input_address_index and thereby relax the distant-change warning. The patch changes the logic to use the smallest non-hardened input address index, and falls back to 0 if every matching input is hardened. This makes the check strictly tighter when extra inputs are added. Tests are updated and a new test verifies the all-hardened-input case.
Changed components
src/seedsigner/models/psbt_parser.pytests/test_psbt_parser.pyInspect captured patch +57 / −10
### src/seedsigner/models/psbt_parser.py
@@ -311,7 +311,7 @@ class PSBTParser():
HIGH_FEES_WARNING_THRESHOLD = 25
# The maximum difference allowed between a change output's address index and the
- # largest address index of the relevant inputs being spent.
+ # smallest address index of the relevant inputs being spent.
CHANGE_ADDRESS_INDEX_WINDOW = 10_000
# The non-fatal findings that can be recorded in self.warnings
@@ -1883,21 +1883,32 @@ def _is_wallet_change_derivation_path(self, change_derivation_path: List[int]) -
def _is_change_address_index_within_window(self, change_derivation_path: List[int]) -> bool:
"""
Determines whether a change output's address index is within
- CHANGE_ADDRESS_INDEX_WINDOW of the largest non-hardened address index among the
+ CHANGE_ADDRESS_INDEX_WINDOW of the smallest non-hardened address index among the
inputs that match the output's base path.
+ A malicious coordinator might attempt to add an input that would skew our
+ accounting of the address indices being spent. This would alter our range for the
+ change indices we would flag. But by constraining our rule to calculate the index
+ window from the smallest input index, we protect against deceptive large indices
+ skewing our safety check.
+
Expects a path that already passed the change path rules.
"""
base_path = change_derivation_path[:-2]
address_index = change_derivation_path[-1]
- largest_input_address_index = 0
+ smallest_input_address_index = None
for derivation_path in self._get_verified_input_derivation_paths_matching_base_path(base_path):
input_address_index = derivation_path.derivation[-1]
- if input_address_index < bip32.HARDENED_INDEX and input_address_index > largest_input_address_index:
- largest_input_address_index = input_address_index
+ if input_address_index < bip32.HARDENED_INDEX and (smallest_input_address_index is None or input_address_index < smallest_input_address_index):
+ smallest_input_address_index = input_address_index
+
+ if smallest_input_address_index is None:
+ # Edge case: every matching input is at a hardened address index. So
+ # measure from 0, which gives the narrowest window.
+ smallest_input_address_index = 0
- return address_index <= largest_input_address_index + PSBTParser.CHANGE_ADDRESS_INDEX_WINDOW
+ return address_index <= smallest_input_address_index + PSBTParser.CHANGE_ADDRESS_INDEX_WINDOW
def _evaluate_change_derivation_paths(self):
### tests/test_psbt_parser.py
@@ -4012,24 +4012,24 @@ def test__parse__flags_each_distant_change_output_and_warns_once(self):
assert psbt_parser.warnings == [PSBTParser.WARNING__CHANGE_ADDRESS_INDEX_BEYOND_WINDOW]
- def test__parse__measures_the_window_from_the_largest_input_index_for_the_base_path(self):
+ def test__parse__measures_the_window_from_the_smallest_input_index_for_the_base_path(self):
"""
- The change address index window is measured from the largest "address_index"
+ The change address index window is measured from the smallest "address_index"
among our inputs that share the change output's base path, regardless of the
order the psbt lists the inputs in.
"""
# This fixture spends inputs at [base path]/1/6 and [base path]/1/0, in that
# order. Verify that input order doesn't matter when determining the window.
fixture_input_address_indices = [6, 0]
- last_index_within_window = max(fixture_input_address_indices) + PSBTParser.CHANGE_ADDRESS_INDEX_WINDOW
+ last_index_within_window = min(fixture_input_address_indices) + PSBTParser.CHANGE_ADDRESS_INDEX_WINDOW
for input_address_indices in (fixture_input_address_indices, list(reversed(fixture_input_address_indices))):
psbt = PSBT.parse(a2b_base64(PSBTTestData.SINGLE_SIG_NATIVE_SEGWIT_2_INPUTS))
if input_address_indices != fixture_input_address_indices:
psbt.inputs.reverse()
assert [list(inp.bip32_derivations.values())[0].derivation[-1] for inp in psbt.inputs] == input_address_indices
- # Within the window, when measured from the largest input index
+ # Within the window, when measured from the smallest input index
repoint_change(psbt, f"m/84h/1h/0h/1/{last_index_within_window}", seed=PSBTTestData.two_input_seed)
psbt_parser = PSBTParser(psbt, PSBTTestData.two_input_seed, network=SettingsConstants.REGTEST)
assert psbt_parser.change_data[0]["address_index_beyond_window"] is False
@@ -4085,6 +4085,42 @@ def test__parse__leaves_hardened_input_indices_out_of_the_window(self):
assert psbt_parser.change_data[0]["address_index_beyond_window"] is True
+ def test__parse__measures_the_window_from_index_0_when_every_input_address_index_is_hardened(self):
+ """
+ The change address index window is measured from index 0 when all of our inputs
+ at the change's base path have a hardened "address_index".
+
+ The psbt spends one of our utxos, at the hardened "address_index" 5h. Two outcomes
+ are tested:
+ * Change at the last index in the window should be within the window.
+ * Change one index past the window should be flagged.
+ """
+ # Add one of our utxos at a hardened "address_index", then remove the fixture's
+ # input so that the hardened one is our only input
+ fixture_input_derivation_path = self._input_derivation_path()
+ fixture_base_path = "/".join(fixture_input_derivation_path.split("/")[:-2])
+ hardened_input_derivation_path = f"{fixture_base_path}/0/5h"
+
+ psbt = self._psbt_with_change()
+ self._add_own_input(psbt, hardened_input_derivation_path)
+ del psbt.inputs[0]
+
+ # Scenario 1: change at the last index in the window
+ last_index_in_window = PSBTParser.CHANGE_ADDRESS_INDEX_WINDOW
+ repoint_change(psbt, self._sibling_path(fixture_input_derivation_path, branch=1, address_index=last_index_in_window))
+ psbt_parser = self._parse(psbt)
+
+ # The hardened input is our only input
+ assert self._verified_input_derivation_paths(psbt_parser) == [
+ bip32.parse_path(hardened_input_derivation_path),
+ ]
+ assert psbt_parser.change_data[0]["address_index_beyond_window"] is False
+
+ # Scenario 2: change one index past the window
+ repoint_change(psbt, self._sibling_path(fixture_input_derivation_path, branch=1, address_index=last_index_in_window + 1))
+ assert self._parse(psbt).change_data[0]["address_index_beyond_window"] is True
+
+
def test__parse__measures_each_base_path_window_from_its_own_inputs(self):
"""
Each base path is viewed as a separate wallet, so a change output going back toWhy this scored 60/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.