fix: sign the bytes that are actually written to disk - #1
Open
bryan-anthropic wants to merge 1 commit into
Open
Conversation
signMcpbFile computed the detached PKCS#7 signature over the pre-patch buffer and only afterwards grew the ZIP EOCD comment_length to account for the appended signature block. Because Buffer.from() copies, the two bytes at eocdOffset + 20 differ between the content that was signed and the content written to disk, so the messageDigest authenticated attribute never matches the file's pre-signature bytes. verifyMcpbFile reconstructs the signed content as everything before the signature header, which is the patched version, and any other standards-compliant verifier does the same. OpenSSL rejects such a file with PKCS7_signatureVerify:digest failure. Bundles signed before the comment_length patch was introduced verify cleanly, so this is a regression. The comment_length depends on the signature block length and the signature covers the patched bytes, so the two are mutually dependent. Patch the EOCD field first using the length of a trial signature, then re-sign, iterating until the block length stops changing. In practice this converges immediately because the content is detached and does not affect the DER encoding size. Adds a regression test that pulls the messageDigest authenticated attribute out of the signature and asserts it equals the SHA-256 of the on-disk pre-signature bytes. The existing tests could not catch this: they accept either "signed" or "unsigned" as a passing status.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
signMcpbFilecomputes the detached PKCS#7 signature over the pre-patch buffer, then grows the ZIP EOCDcomment_lengthafterwards:The two bytes at
eocdOffset + 20therefore differ between the content that was signed and the content on disk.verifyMcpbFilereconstructs the signed content as everything before the signature header — the patched version — and so does any other standards-compliant verifier.Reproduction
Packing a bundle with
mcpb pack, signing it, then verifying the detached signature with OpenSSL:The signature validates only against bytes that never reach disk. Every signed bundle is affected — the comment_length always grows by a nonzero amount.
This is a regression: at the parent of the commit that introduced the comment_length patch, the same reproduction gives
VERIFICATION SUCCESSFULagainst the on-disk bytes.Fix
The comment_length depends on the signature block length, and the signature covers the patched bytes, so the two are mutually dependent. This patches the EOCD field first using the length of a trial signature, then re-signs, iterating until the block length stops changing. It converges immediately in practice, because the content is detached and does not affect the DER encoding size. A bound guards against a length that never stabilizes, and an oversized block now raises a clear error instead of silently throwing from
writeUInt16LE.Tests
Adds a regression test that pulls the
messageDigestauthenticated attribute out of the signature and asserts it equals the SHA-256 of the on-disk pre-signature bytes. It fails on the current code with the expected digest mismatch and passes with the fix. Extraction uses node-forge's ASN.1 parser rather than shelling out to OpenSSL, so it runs on all CI platforms.The existing tests could not catch this — they accept
["self-signed", "unsigned"]as passing, and the tampering test asserts"unsigned", which holds trivially.Full suite: 126 passed.
tsc --noEmitand Prettier are clean.Note on
mcpb verifyUnrelated to this change and not addressed here:
verifyMcpbFilecalls node-forge'sp7.verify(), which is a stub that throwsPKCS#7 signature verification not yet implementedin both 1.3.3 and 1.4.0. It is caught and turned into{status: "unsigned"}, somcpb verifyreports "Extension is not signed" for every signed bundle and the digest comparison below it is unreachable. That is worth a separate fix.