From fb6964a74ea60751560a6d10366bfdad3dab4535 Mon Sep 17 00:00:00 2001 From: N0zoM1z0 <1705004398@qq.com> Date: Thu, 16 Jul 2026 22:28:21 +0800 Subject: [PATCH] fix(electron): keep missing unpacked ASAR entries unavailable --- README.md | 6 ++- docs/javascript-artifact-reconstruction.md | 29 +++++++----- src/application/ArtifactInventory.ts | 22 ++++++++- src/artifacts/AsarArtifactReader.ts | 20 +++++++++ .../javascriptArtifactReconstruction.test.ts | 45 ++++++++++++++++++- 5 files changed, 109 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 8633e3102..2a53c5f11 100644 --- a/README.md +++ b/README.md @@ -586,7 +586,11 @@ C++ toolchain. Compatible packaged binaries do not require this rebuild. ASAR inventory verifies Electron integrity metadata for both archive entries and `.asar.unpacked` companion files. Integrity failures identify the logical path, declared and calculated SHA-256 values, and whether the entry was -unpacked; REA does not silently accept the mismatched artifact. +unpacked; REA does not silently accept the mismatched artifact. If a supplied +ASAR declares unpacked companion bytes that are absent from the local artifact +set, REA keeps that occurrence as `unavailable` and continues analyzing the +embedded JavaScript instead of treating the missing native/resource bytes as +verified or absent. ## Security model diff --git a/docs/javascript-artifact-reconstruction.md b/docs/javascript-artifact-reconstruction.md index cddf20d42..2fa735721 100644 --- a/docs/javascript-artifact-reconstruction.md +++ b/docs/javascript-artifact-reconstruction.md @@ -52,6 +52,14 @@ SHA-256 digests, inventory IDs, ASAR container identity, and `.asar.unpacked` status. Direct ASAR inputs and filesystem-backed ASAR files nested beneath a directory are supported. +If an ASAR declares an unpacked companion entry but the corresponding +`.unpacked` file is absent from the operator-supplied artifact set, REA +keeps the ASAR occurrence with `hash_status: unavailable`, records an explicit +limitation, and does not create a content-addressed child artifact for those +missing bytes. This allows static JavaScript/Electron reconstruction to proceed +for the embedded files while preserving the missing native/resource bytes as an +unknown instead of silently treating them as absent or verified. + Selected bounded text is then parsed as inert data to recover: - `package.json` metadata and declared main or renderer entrypoints; @@ -153,13 +161,14 @@ knowable, `omitted_count` is `null` rather than a guessed value. ## Verification boundary The source-owned fixture covers an extracted directory, a direct ASAR with an -unpacked native add-on, and an ASAR nested beneath a directory. Tests also cover -deterministic reruns, parse-not-execute behavior, source-map approval, global -source-map limits, malformed structured data, invalid containers, traversal, -symlink escape, oversized text, cancellation, AST truncation, and repeated -content identities. A separate synthetic Electron fixture covers explicit safe -and unsafe preference values, preload and contextBridge surfaces, literal, -dynamic, paired, ambiguous, and unpaired IPC, validation candidates, utility -processes, and native binding requests. These fixtures establish parser and -artifact-reader claims; they do not replace the later operator-supplied -real-application benchmark. +unpacked native add-on, a direct ASAR whose unpacked native companion is +missing, and an ASAR nested beneath a directory. Tests also cover deterministic +reruns, parse-not-execute behavior, source-map approval, global source-map +limits, malformed structured data, invalid containers, traversal, symlink +escape, oversized text, cancellation, AST truncation, and repeated content +identities. A separate synthetic Electron fixture covers explicit safe and +unsafe preference values, preload and contextBridge surfaces, literal, dynamic, +paired, ambiguous, and unpaired IPC, validation candidates, utility processes, +and native binding requests. These fixtures establish parser and artifact-reader +claims; they do not replace the later operator-supplied real-application +benchmark. diff --git a/src/application/ArtifactInventory.ts b/src/application/ArtifactInventory.ts index c7f541ca8..ecd57dc79 100644 --- a/src/application/ArtifactInventory.ts +++ b/src/application/ArtifactInventory.ts @@ -375,6 +375,9 @@ const emptyScan = (): { readonly pendingContradictions: PendingIntegrityContradiction[]; } => ({ nodes: new Map(), occurrences: [], pendingContradictions: [] }); +const UNAVAILABLE_UNPACKED_LIMITATION = + "ASAR unpacked companion bytes were unavailable; no content hash or child artifact was produced."; + interface PendingIntegrityContradiction { readonly logicalPath: string; readonly declaredSha256: string; @@ -497,7 +500,16 @@ const scanReader = async ( logicalPath, parent?.occurrence_id ?? null, ); - const digested = await digestEntry(currentReader, entry, logicalPath); + let digested: + | { readonly node: ArtifactNode; readonly mismatched: boolean } + | undefined; + try { + digested = await digestEntry(currentReader, entry, logicalPath); + } catch (cause: unknown) { + if (!isUnavailableUnpackedEntry(cause, entry)) throw cause; + occurrence.hash_status = "unavailable"; + occurrence.limitations.push(UNAVAILABLE_UNPACKED_LIMITATION); + } if (digested !== undefined) { nodes.set(digested.node.artifact_id, digested.node); occurrence.artifact_id = digested.node.artifact_id; @@ -519,6 +531,14 @@ const scanReader = async ( return { nodes, occurrences, pendingContradictions }; }; +const isUnavailableUnpackedEntry = ( + cause: unknown, + entry: ArtifactEntry, +): boolean => + entry.unpacked && + cause instanceof ArtifactReaderFailure && + cause.reason === "unavailable"; + const classifyRoot = async ( path: string, directory: boolean, diff --git a/src/artifacts/AsarArtifactReader.ts b/src/artifacts/AsarArtifactReader.ts index 44394c350..6828cbd33 100644 --- a/src/artifacts/AsarArtifactReader.ts +++ b/src/artifacts/AsarArtifactReader.ts @@ -82,6 +82,20 @@ export class AsarArtifactReader implements ArtifactReader { Readable.from(extractFile(this.path, entry.adapterKey, false)), ); } catch (cause: unknown) { + if (entry.unpacked && isMissingFile(cause)) + return Promise.reject( + new ArtifactReaderFailure( + "unavailable", + `ASAR unpacked entry bytes are unavailable: ${entry.path}`, + { cause }, + { + logicalPath: entry.path, + declaredSha256: entry.declaredSha256, + calculatedSha256: null, + unpacked: true, + }, + ), + ); return Promise.reject( asarFailure(this.path, `read ${entry.path}`, cause), ); @@ -114,3 +128,9 @@ const asarFailure = ( `Malformed or unreadable ASAR during ${operation}: ${path}`, { cause }, ); + +const isMissingFile = (cause: unknown): boolean => + typeof cause === "object" && + cause !== null && + "code" in cause && + cause.code === "ENOENT"; diff --git a/tests/javascriptArtifactReconstruction.test.ts b/tests/javascriptArtifactReconstruction.test.ts index 31571cc2b..2dc3db90d 100644 --- a/tests/javascriptArtifactReconstruction.test.ts +++ b/tests/javascriptArtifactReconstruction.test.ts @@ -1,5 +1,5 @@ import { Readable } from "node:stream"; -import { mkdtemp, mkdir, symlink, writeFile } from "node:fs/promises"; +import { mkdtemp, mkdir, rm, symlink, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; @@ -171,6 +171,49 @@ describe("JavaScript artifact reconstruction", () => { ); }); + it("keeps ASAR analysis usable when unpacked native companion bytes are absent", async () => { + const root = await mkdtemp(join(tmpdir(), "rea-javascript-asar-missing-")); + const source = join(root, "source"); + await mkdir(source); + await writeJavaScriptArtifactFixture(source); + const archive = join(root, "app.asar"); + await createPackageWithOptions(source, archive, { unpack: "**/*.node" }); + await rm(join(`${archive}.unpacked`, "native", "addon.node")); + + const snapshot = await scanArtifactInventory(archive, { + maxEntries: 8_000, + maxTotalBytes: 512 * 1024 * 1024, + maxEntryBytes: 128 * 1024 * 1024, + maxCompressionRatio: 1_000, + maxDepth: 64, + maxPathBytes: 4_096, + }); + const missingNative = snapshot.occurrences.find( + ({ logical_path }) => logical_path === "native/addon.node", + ); + + expect(missingNative).toMatchObject({ + artifact_id: null, + hash_status: "unavailable", + limitations: expect.arrayContaining([ + "ASAR unpacked companion bytes were unavailable; no content hash or child artifact was produced.", + ]), + }); + + const result = await reconstructJavaScriptArtifact({ + input_path: archive, + format: "asar", + source_map_read_approved: true, + }); + + expect(result.statistics.parsed_javascript_files).toBeGreaterThan(0); + expect(result.graph.nodes).toEqual( + expect.arrayContaining([ + expect.objectContaining({ kind: "javascript-asset" }), + ]), + ); + }); + it("recurses into filesystem-backed ASAR containers without losing container-relative paths", async () => { const root = await mkdtemp(join(tmpdir(), "rea-javascript-nested-asar-")); const source = await fixtureDirectory();