Skip to content

fix(artifacts): explain ASAR PE signed-after-packing mismatches - #9024

Closed
BeqaKs wants to merge 5 commits into
morluto:mainfrom
BeqaKs:fix/asar-signed-after-packing-7212
Closed

BeqaKs wants to merge 5 commits into
morluto:mainfrom
BeqaKs:fix/asar-signed-after-packing-7212

Conversation

@BeqaKs

@BeqaKs BeqaKs commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #7212

When an unpacked native addon is Authenticode-signed after app.asar is packed, its companion bytes no longer match the ASAR header hash. Before this change, REA reported that case the same way as a swapped binary: an unexplained artifact_integrity_mismatch / "observed bytes are untrusted". REA now checks whether removing the Authenticode certificate table restores the declared SHA-256. If it does, REA adds a verifiable signing_explanation to the contradiction.

Problem and expected behavior

  • Trigger: an ASAR with an unpacked PE (*.node) whose companion was signed after packing. The file grows by exactly the certificate table, and the hash disagrees with integrity.hash.
  • Expected: stripping IMAGE_DIRECTORY_ENTRY_SECURITY (removing the table range, zeroing the security directory and PE checksum) reproduces the declared SHA-256. The record should say so, and the bytes should stay observed and untrusted.
  • A mismatch that stripping does not explain, such as a hand-modified signed addon or a trailer beyond the table, stays an ordinary unexplained contradiction.

Change and scope

  • src/native/pe/PeUnsignedDigest.ts (new) rebuilds the pre-signing PE bytes from the parsed PE headers, compares their SHA-256 with the claim, and reads signer subjects through the existing parsePeCertificates. This second read happens only for an unpacked SHA-256 PE entry that already mismatched, and is bounded to 64 MiB.
  • scanReader (the producer) attaches the explanation once, both to the strict-policy ArtifactReaderFailure details and to the pending contradiction.
  • signedAfterPackingExplanationSchema lives in domain/artifactIntegrity.ts and is reused by artifactGraph (SHA-256 contradiction variant only) and the error contract. It is no longer duplicated inline.
  • Consumers now read the parsed fact instead of the generic text: canonical contradiction limitations, the JavaScript application graph limitations, artifact inspection contradiction statements, and the strict-failure message.
  • Not included: Mach-O code-signature growth (Allow explicit evidence-preserving continuation after artifact integrity mismatches #112) and any signature validity attestation. The explanation is not a trust upgrade: hash_status stays mismatched, trust stays observed-untrusted, and equivalence is unchanged.

Contribution scope: not applicable (read-only fix within the existing artifact-integrity boundary).

Contract and boundary impact

  • Semantic owner and earliest changed stage: src/artifacts/inventory/scanReader.ts digest step. Consumers are scanCanonical, JavaScriptArtifactGraphBuilder, artifactInspection, and analysisErrorPresentation.

  • Contribution scope fit and linked maintainer decision: not applicable.

  • CLI and MCP/tool-catalog contract: additive optional signing_explanation on SHA-256 integrity_contradictions entries and on artifact_integrity_mismatch details. docs/error-contract.schema.json was regenerated.

  • Provider, bridge, target-format, or platform compatibility: PE Authenticode only.

  • Evidence, artifact, provenance, or reconstruction contract: additive. Declared and observed digests and the trust level are unchanged.

  • Target selection, authority, upstream behavior, and cleanup details: not applicable.

  • Generated metadata, package, or installation impact: none beyond the regenerated error schema.

  • Read existing files or captured data.

  • Launch or attach to a process.

  • Inject instrumentation or run scripts/code.

  • Change process memory, registers, files, or configuration.

  • Contact network services.

  • Recover protected code or work around protections.

  • Documentation or usability only; no target interaction.

  • Not sure yet; investigation or maintainer guidance needed.

Evidence and regression coverage

  • Tests added or updated:

    • src/native/pe/PeUnsignedDigest.test.ts: PE32 and PE32+ restore the digest. A content mutation, a missing security directory, and an extra appended trailer all stay unexplained.
    • tests/boundary/filesystem/asarSignedAfterPacking.test.ts: a real @electron/asar package whose unpacked addon is signed after packing. Covers the strict-failure projection details, record-and-continue contradiction and limitation, and the application limitation. An ordinary stripped-addon mismatch stays unexplained.
  • Base reproduction: on main, the same fixture produces only the generic contradiction with no signing_explanation, so the new assertions fail.

  • Remaining proof gaps: not run against the Razer App Engine sample from the issue. Only synthetic PE fixtures signed with the repo's PKCS#7 fixture were used.

  • Observed, derived, and inferred claims remain distinguishable.

  • Artifact identity, source provenance, and failed attempts remain preserved.

  • Unsupported, incomplete, unavailable, or uncertain outcomes remain visible.

Validation performed

  • npm run typecheck: passed
  • npm run test:focused -- src/native/pe/PeUnsignedDigest.test.ts tests/boundary/filesystem/asarSignedAfterPacking.test.ts tests/boundary/filesystem/javascriptIntegrityPolicy.test.ts tests/boundary/cli/javascriptIntegrityPolicyCli.test.ts tests/boundary/mcp/asarUnpackedExtractionMcp.test.ts tests/boundary/mcp/artifactMcp.test.ts: 36 passed
  • npm run test:focused -- tests/boundary/mcp/electronMcp.test.ts tests/boundary/mcp/javascriptNativeHandoffIdentity.test.ts src/artifacts/DfuSeArtifactReader.test.ts (with the new tests): 25 passed
  • npm run docs:check and generate-error-schema.mjs --check: passed
  • npm run verify:module-boundaries: passed
  • npm run check:fast: typecheck passed. Lint failed with 1 error in the untouched tests/acceptance/webSourceLocationCli.test.ts (max-lines-per-function), which is already on main.

Compatibility, safety, and release

  • Breaking changes or migration steps: none (additive optional field).
  • Real Hopper/Ghidra, browser, or OS coverage: not applicable.
  • Package or release metadata impact: none.
  • Security, privacy, process, or containment review: read-only. The explanation never upgrades trust, and real tampering stays fail-closed under the strict policy.

Review follow-up validation (caf9a6b)

  • The explanation now uses the same observed digest as the first read; expected optional read failures preserve the original mismatch and their reason. Cancellation and cleanup failures remain visible.
  • Certificate subjects are matched using encoded issuer and serial with exactly one match. signer_subjects_complete reports incomplete or ambiguous decoding; this does not verify a signature.
  • Independent unsigned PE fixtures cover PE32 and PE32+, while filesystem and MCP tests cover mismatches, read failures, cleanup, and the actual advertised MCP schema.
  • Executed: PE parser and digest tests (57 passed); focused artifact MCP and signed ASAR tests (21 passed); signed ASAR and extraction MCP tests (10 passed); npm run check:fast, npm run format:check, and npm run docs:check passed. The generated error schema also passed Ajv 2020-12 meta-schema validation. The earlier lint failure below describes the earlier run; the latest check:fast passed.
  • The signed PE fixtures are synthetic. The Razer sample and real signed applications were not verified.

Author readiness

Human owner: @BeqaKs

  • I reviewed the final diff and relevant existing code, understand the change and its edge cases, and take responsibility for the complete PR, including AI-assisted work.
  • I checked the evidence above, confirmed it supports the current change, and recorded meaningful validation gaps.
  • I declare this PR ready for initial maintainer review.

Before merge

  • This PR addresses a concrete problem or an agreed enhancement.
  • Material scope and authority questions are resolved, and any required maintainer scope decision covers the final diff.
  • The PR has one focused outcome and the title follows type(scope): outcome.
  • Related issue is linked, or the reason for not linking one is stated above.
  • Tests cover changed observable behavior and meaningful failure paths.
  • Owning docs, contracts, and generated metadata are updated where needed.
  • User-visible CLI/MCP output includes representative output.
  • I checked the final diff for secrets, unrelated cleanup, and unsupported claims.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ⚠️ Failed 2026-11-05T14:11:45.891156Z
We couldn’t complete this review. Comment @codex review to try again.
c6f5363 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex usage limits have been reached for code reviews. Please check with the admins of this repo to increase the limits by adding credits.
Repo admins can enable using credits for code reviews in their settings.

@BeqaKs

BeqaKs commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

Static checks are red only on the pre-existing main lint in tests/acceptance/webSourceLocationCli.test.ts (max-lines-per-function), which this PR does not touch. That gate is already being restored in #9023; once that lands, rebasing this branch should clear Static checks without further product changes.

@BeqaKs

BeqaKs commented Nov 5, 2026

Copy link
Copy Markdown
Contributor Author

Pushed a follow-up that applies the same webSourceLocationCli.test.ts helper extraction as #9023 so Static checks clear on this branch (check:fast passes locally). Product change for #7212 is unchanged.

@greptile-apps

greptile-apps Bot commented Nov 5, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 4/5 Tier: plus

[Medium impact] The change affects artifact-integrity reports for a narrow class of unpacked PE files.

The PR still needs to preserve the known mismatch when the optional companion read also fails to close.

Summary

Adds a checked explanation when removing a PE Authenticode certificate table restores an unpacked ASAR companion's declared SHA-256.

  • Unpacked PE mismatches now explain signing added after packing.
  • Source-location tests share setup helpers without changing what they check.

Source review only; tests were not run. Whether another PR supersedes this one could not be checked because GitHub access was unavailable.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Hash unpacked companion] --> B{SHA-256 mismatch?}
  B -->|Yes, PE within size limit| C[Read again and compare observed hash]
  C -->|Same bytes| D[Remove certificate table and clear signing fields]
  D --> E{Declared hash restored?}
  E -->|Yes| F[Attach explanation and signer coverage]
  E -->|No| G[Keep unexplained mismatch]
  C -->|Different bytes| G
  F --> H[Apply integrity policy; bytes remain untrusted]
  G --> H
Loading

Reviews (3) · Last reviewed commit: "fix(artifacts): preserve signed companio..." · Reviewed by Greptile

Comment thread src/artifacts/inventory/scanReader.ts Resolved
Comment thread src/artifacts/inventory/scanReader.ts Resolved
Comment thread src/native/pe/PeUnsignedDigest.ts Resolved
BeqaKs and others added 4 commits November 10, 2026 06:53
When an unpacked PE companion fails ASAR integrity only because Authenticode
was applied after packing, record a verifiable signing_explanation on the
SHA-256 contradiction and error details instead of an opaque untrusted
mismatch. Bytes stay observed-untrusted; any other mutation remains an
unexplained contradiction.

Fixes morluto#7212
decodeName now returns start/end for signer matching; keep the empty
RDNSequence distinction without requiring a span-free subject shape.
analyze_javascript_application on the signed-after-packing ASAR fixture
can exceed the default 30s boundary timeout on slower hosts.
@BeqaKs
BeqaKs force-pushed the fix/asar-signed-after-packing-7212 branch from caf9a6b to 5a0b3b3 Compare November 10, 2026 07:02
@BeqaKs BeqaKs closed this Nov 14, 2026
@morluto morluto added area: evidence Evidence, artifacts, and reconstruction bug Something isn't working labels Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: evidence Evidence, artifacts, and reconstruction bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Explain ASAR integrity mismatches caused by signing unpacked addons after packing

2 participants