Skip to content

fix: reject non-canonical varint encodings - #25

Merged
prikshitchawla300 merged 1 commit into
masterfrom
prikshitchawla/cshld-2225-bip174-psbt-parser-advances-offset
Nov 11, 2026
Merged

prikshitchawla300 merged 1 commit into
masterfrom
prikshitchawla/cshld-2225-bip174-psbt-parser-advances-offset

Conversation

@prikshitchawla300

Copy link
Copy Markdown
Contributor

Summary

varuint.decode() accepted non-minimal CompactSize encodings (for example fd 03 00 for 3), while its callers advance by encodingLength(value). The two only agree for minimal encodings.

decode() now throws RangeError: Format Error: non-canonical varint encoding for fd / fe / ff-prefixed values that fit in a shorter encoding. This matches Bitcoin Core's ReadCompactSize.

Changes

  • ts_src/lib/converter/varint.ts: reject non-minimal encodings in decode().
  • Tests:
    • tests/varint.ts: non-minimal encodings for each prefix, including both sides of each boundary.
    • tests/fixtures/fromBuffer.ts: non-minimal key and value lengths through Psbt.fromBuffer.
    • tests/converters.ts (new): nested decoders (witnessUtxo, tapTree, tapBip32Derivation, proprietary keys).
  • src/: rebuilt output.

Notes for consumers

  • PSBTs with non-minimal length encodings are now rejected. Bitcoin Core already rejects these.
  • bitcoinjs-lib deep-imports this varint module, so it also gets the stricter decoder. Its callers advance by decode.bytes, which is still set correctly.
  • Consumers of @bitgo-forks/bip174 need a version bump to pick this up.

Testing

npm test (prettier, tslint, build, coverage) passes: 415/415.

varuint.decode() accepted non-minimal CompactSize encodings (e.g. fd 03 00
for 3), while callers advance by encodingLength(value). The two only agree
for minimal encodings.

decode() now throws "Format Error: non-canonical varint encoding" for
fd/fe/ff-prefixed values that fit in a shorter encoding, matching Bitcoin
Core's ReadCompactSize.

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Claude Code Review

This repository is configured for manual code reviews. Comment @claude review for a one-time review, or @claude review always to subscribe this PR to a review on every future push.

Tip: disable this comment in your organization's Code Review settings.

@linear-code

linear-code Bot commented Nov 11, 2026

Copy link
Copy Markdown

CSHLD-2225

@prikshitchawla300
prikshitchawla300 merged commit 3bda147 into master Nov 11, 2026
13 checks passed
@prikshitchawla300
prikshitchawla300 deleted the prikshitchawla/cshld-2225-bip174-psbt-parser-advances-offset branch November 11, 2026 08:05
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 3.1.0-master.5 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants