Skip to content

🐛 extractSignature: read DER length instead of stripping trailing 00 pairs (fixes #317) - #319

Open
razortrax wants to merge 1 commit into
vbuch:developfrom
razortrax:fix/extract-signature-trailing-zero-truncation
Open

🐛 extractSignature: read DER length instead of stripping trailing 00 pairs (fixes #317)#319
razortrax wants to merge 1 commit into
vbuch:developfrom
razortrax:fix/extract-signature-trailing-zero-truncation

Conversation

@razortrax

Copy link
Copy Markdown

Problem

extractSignature() strips the placeholder padding with:

.replace(/(?:00|>)+$/, '');

The regex cannot distinguish sign()'s zero-padding from a genuine signature whose last byte(s) happen to be 0x00. A PKCS#7 SignedData's final bytes are the raw RSA signature — effectively random — so ~1/256 signatures genuinely end in 0x00. When one does, the strip eats real DER bytes and downstream parsers throw Too few bytes to read ASN.1 value. on a perfectly valid signature.

Full analysis in #317. We hit this as an intermittent (~0.4%) verification failure in CI; reproduced 9 truncations in 1500 sign/extract cycles against @signpdf/utils 3.x.

Fix

DER is self-describing: read the total element length from the SEQUENCE header (short- and long-form lengths supported) and slice there — exactly the direction suggested in #317. If the blob does not parse as a DER SEQUENCE, fall back to the historical trailing-zero strip so non-DER edge cases keep their previous behavior.

Also, the slice-then-strip previously relied on the regex to remove the trailing >; the hex cleanup now strips only > and whitespace before decoding.

Tests

🤖 Generated with Claude Code

…pairs

The trailing-byte strip (replace(/(?:00|>)+$/)) cannot distinguish sign()'s
zero-padding from a genuine signature whose last byte(s) happen to be 0x00
(~1/256 chance per signature), so it intermittently truncated valid DER and
downstream parsers failed with "Too few bytes to read ASN.1 value."

Read the total element length from the DER SEQUENCE header instead; fall
back to the historical trailing-zero strip when the blob is not parseable
DER. Fixes vbuch#317.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

Copilot AI 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.

The job was not started because the account is locked due to a billing issue.

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.

2 participants