Skip to content

fix(p2ms): isAcceptableSignature always returned true due to operator precedence#2332

Open
Grizouforever wants to merge 1 commit intobitcoinjs:masterfrom
Grizouforever:fix/p2ms-isacceptablesignature-always-true
Open

fix(p2ms): isAcceptableSignature always returned true due to operator precedence#2332
Grizouforever wants to merge 1 commit intobitcoinjs:masterfrom
Grizouforever:fix/p2ms-isacceptablesignature-always-true

Conversation

@Grizouforever
Copy link
Copy Markdown

Summary

In ts_src/payments/p2ms.ts, the isAcceptableSignature function contains a logic bug caused by JS operator precedence:

// Before (buggy)
(opts!.allowIncomplete && (x as number) === OPS.OP_0) !== undefined

The !== undefined check is applied to the result of the && expression, which is always a boolean (true or false). Since neither true nor false is undefined, this condition always evaluates to true — meaning isAcceptableSignature accepted any input as a valid signature, completely bypassing P2MS signature validation.

Fix: Coerce the expression to a proper boolean with !!:

// After (fixed)
!!(opts!.allowIncomplete && (x as number) === OPS.OP_0)

Impact

Without this fix, P2MS inputs with invalid/garbage signatures pass validation silently. The function was intended to only allow OP_0 placeholders when allowIncomplete is set.

Tests

All 2660 existing tests pass, including the p2ms-specific tests that assert "throws Input has invalid signature(s)". No test changes were needed — the existing fixtures already encode the correct expected behavior.

p2ms
  ✔ throws Input has invalid signature(s)
  ✔ p2ms, m derives from ["output"]
  ...
  ✔ throws Input has invalid signature
  ✔ throws Input has invalid signature
  ✔ throws Input has invalid signature
  ✔ throws Witness has invalid signature

2660 passing (11s)

… precedence

The expression `(opts!.allowIncomplete && ...) !== undefined` always
evaluates to `true` because any boolean value (including `false`) is
not `undefined`. This caused `isAcceptableSignature` to accept any
input as a valid signature, bypassing P2MS signature validation entirely.

Fix by wrapping the expression with `!!` to coerce it to a proper boolean.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant