fix(artifacts): bind ASAR interpretation to owned snapshots
Verify captured container bytes against their inventoried digest before listing or reading packed members. Isolate per-reader ASAR metadata, preserve original unpacked companions, and retain complete inventories when snapshot cleanup fails.
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
import { mkdir, rm, symlink, writeFile } from "node:fs/promises";
|
||||
import { lstat, mkdir, rm, symlink, writeFile } from "node:fs/promises";
|
||||
import { join, sep } from "node:path";
|
||||
import { Readable } from "node:stream";
|
||||
import { buffer } from "node:stream/consumers";
|
||||
|
||||
import { createPackage, listPackage, uncache } from "@electron/asar";
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import { scanCanonicalArtifactInventory } from "../../../src/artifacts/inventory/scanCanonical.js";
|
||||
import { DirectoryArtifactReader } from "../../../src/artifacts/DirectoryArtifactReader.js";
|
||||
import { inventoryArtifact } from "../../../src/artifacts/inventory/ArtifactInventory.js";
|
||||
import { AsarArtifactReader } from "../../../src/artifacts/AsarArtifactReader.js";
|
||||
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
@@ -165,7 +168,121 @@ describe("ASAR inventory freshness", () => {
|
||||
}
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe("ASAR captured-byte identity", () => {
|
||||
it("rejects same-size nested-container replacement between hashing and interpretation", async () => {
|
||||
const fixture = await archiveFixture();
|
||||
const originalSize = (await lstat(fixture.archive)).size;
|
||||
const directory = new DirectoryArtifactReader(fixture.bundle);
|
||||
await expect(
|
||||
scanCanonicalArtifactInventory(fixture.bundle, {}, async () => ({
|
||||
format: directory.format,
|
||||
entries: (signal) => directory.entries(signal),
|
||||
provenance: () => directory.provenance(),
|
||||
close: () => directory.close(),
|
||||
async open(entry, signal) {
|
||||
const source = await directory.open(entry, signal);
|
||||
return Readable.from(
|
||||
(async function* () {
|
||||
yield* source;
|
||||
await fixture.replace();
|
||||
expect((await lstat(fixture.archive)).size).toBe(originalSize);
|
||||
})(),
|
||||
);
|
||||
},
|
||||
})),
|
||||
).rejects.toMatchObject({
|
||||
reason: "integrity",
|
||||
message: expect.stringContaining("changed before interpretation"),
|
||||
});
|
||||
});
|
||||
|
||||
it("isolates interleaved readers from replacement headers and packed bytes", async () => {
|
||||
const fixture = await archiveFixture();
|
||||
await writeFile(join(fixture.source, "second.js"), "original metadata");
|
||||
await createPackage(fixture.source, fixture.archive);
|
||||
const first = new AsarArtifactReader(fixture.archive);
|
||||
const second = new AsarArtifactReader(fixture.archive);
|
||||
const left = first.entries()[Symbol.asyncIterator]();
|
||||
const right = second.entries()[Symbol.asyncIterator]();
|
||||
try {
|
||||
const initial = await left.next();
|
||||
if (initial.done) throw new Error("Expected first member");
|
||||
expect(initial.value.path).toBe("old.js");
|
||||
await fixture.replace();
|
||||
await writeFile(join(fixture.source, "second.js"), "replaced metadata");
|
||||
await createPackage(fixture.source, fixture.archive);
|
||||
const replacement = await right.next();
|
||||
if (replacement.done) throw new Error("Expected replacement member");
|
||||
expect(replacement.value.path).toBe("new.js");
|
||||
const originalMember = await left.next();
|
||||
const replacementMember = await right.next();
|
||||
if (originalMember.done || replacementMember.done)
|
||||
throw new Error("Expected second members");
|
||||
expect(originalMember.value.declaredSha256).not.toBe(
|
||||
replacementMember.value.declaredSha256,
|
||||
);
|
||||
expect(
|
||||
(await buffer(await first.open(originalMember.value))).toString(),
|
||||
).toBe("original metadata");
|
||||
await first.close();
|
||||
expect(
|
||||
(await buffer(await second.open(replacementMember.value))).toString(),
|
||||
).toBe("replaced metadata");
|
||||
} finally {
|
||||
await left.return?.();
|
||||
await right.return?.();
|
||||
await first.close();
|
||||
await second.close();
|
||||
}
|
||||
});
|
||||
|
||||
it("retains a completed inventory and snapshot ownership when cleanup fails", async () => {
|
||||
const fixture = await archiveFixture();
|
||||
const attempts: string[] = [];
|
||||
const reader = new AsarArtifactReader(
|
||||
fixture.archive,
|
||||
undefined,
|
||||
async (path) => {
|
||||
attempts.push(path);
|
||||
if (attempts.length === 1) throw new Error("snapshot removal denied");
|
||||
await rm(path, { recursive: true, force: true });
|
||||
},
|
||||
);
|
||||
try {
|
||||
const failure: unknown = await scanCanonicalArtifactInventory(
|
||||
fixture.archive,
|
||||
{},
|
||||
async () => reader,
|
||||
).catch((cause: unknown) => cause);
|
||||
expect(failure).toMatchObject({
|
||||
cleanup: { resources: [attempts[0]] },
|
||||
partialObservation: {
|
||||
kind: "artifact-inventory",
|
||||
inventory: {
|
||||
occurrences: expect.arrayContaining([
|
||||
expect.objectContaining({
|
||||
logical_path: "old.js",
|
||||
hash_status: "verified",
|
||||
}),
|
||||
]),
|
||||
},
|
||||
},
|
||||
});
|
||||
const root = attempts[0];
|
||||
if (root === undefined) throw new Error("Expected owned snapshot");
|
||||
expect((await lstat(root)).isDirectory()).toBe(true);
|
||||
await reader.close();
|
||||
expect(attempts).toEqual([root, root]);
|
||||
await expect(lstat(root)).rejects.toMatchObject({ code: "ENOENT" });
|
||||
} finally {
|
||||
await reader.close();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("ASAR reader lifecycle", () => {
|
||||
it("refreshes a header cached before the reader was created", async () => {
|
||||
const fixture = await archiveFixture();
|
||||
try {
|
||||
|
||||
@@ -69,11 +69,6 @@ describe("ASAR entry streaming", () => {
|
||||
await writeFile(join(source, "small.js"), "module.exports = 1;\n");
|
||||
await createPackageWithOptions(source, archive, {});
|
||||
|
||||
const identity = await lstat(archive);
|
||||
const probe = await openFile(archive, "r");
|
||||
expect(await matchingDescriptorCount(identity)).toBeGreaterThan(0);
|
||||
await probe.close();
|
||||
await waitForNoMatchingDescriptor(identity);
|
||||
const reader = new AsarArtifactReader(archive);
|
||||
|
||||
try {
|
||||
@@ -83,6 +78,12 @@ describe("ASAR entry streaming", () => {
|
||||
}
|
||||
const entry = entries.find(({ path }) => path === "small.js");
|
||||
if (entry === undefined) throw new Error("Expected packed ASAR member");
|
||||
const snapshot = await reader.openContainer();
|
||||
const identity = await lstat(snapshot.path);
|
||||
await buffer(snapshot);
|
||||
const probe = await openFile(snapshot.path, "r");
|
||||
expect(await matchingDescriptorCount(identity)).toBeGreaterThan(0);
|
||||
await probe.close();
|
||||
expect(await matchingDescriptorCount(identity)).toBe(0);
|
||||
const output = await reader.open(entry);
|
||||
expect(await matchingDescriptorCount(identity)).toBeGreaterThan(0);
|
||||
@@ -108,7 +109,6 @@ describe("ASAR entry streaming", () => {
|
||||
await writeFile(join(source, "small.js"), "module.exports = 1;\n");
|
||||
await createPackageWithOptions(source, archive, {});
|
||||
|
||||
const identity = await lstat(archive);
|
||||
const reader = new AsarArtifactReader(archive);
|
||||
|
||||
try {
|
||||
@@ -116,6 +116,9 @@ describe("ASAR entry streaming", () => {
|
||||
for await (const entry of reader.entries()) entries.push(entry);
|
||||
const entry = entries.find(({ path }) => path === "small.js");
|
||||
if (entry === undefined) throw new Error("Expected packed ASAR member");
|
||||
const snapshot = await reader.openContainer();
|
||||
const identity = await lstat(snapshot.path);
|
||||
await buffer(snapshot);
|
||||
const controller = new AbortController();
|
||||
const opening = reader.open(entry, controller.signal);
|
||||
controller.abort();
|
||||
@@ -127,7 +130,7 @@ describe("ASAR entry streaming", () => {
|
||||
},
|
||||
);
|
||||
|
||||
it("classifies a missing packed member container as an I/O failure", async () => {
|
||||
it("keeps captured packed bytes readable after the original container disappears", async () => {
|
||||
const root = await createTestTempDirectory("rea-asar-missing-member-");
|
||||
const source = join(root, "source");
|
||||
const archive = join(root, "fixture.asar");
|
||||
@@ -142,10 +145,9 @@ describe("ASAR entry streaming", () => {
|
||||
const entry = entries.find(({ path }) => path === "small.js");
|
||||
if (entry === undefined) throw new Error("Expected packed ASAR member");
|
||||
await rm(archive);
|
||||
await expect(reader.open(entry)).rejects.toMatchObject({
|
||||
reason: "io",
|
||||
cause: { code: "ENOENT" },
|
||||
});
|
||||
expect((await buffer(await reader.open(entry))).toString()).toBe(
|
||||
"module.exports = 1;\n",
|
||||
);
|
||||
} finally {
|
||||
await reader.close();
|
||||
}
|
||||
@@ -205,7 +207,7 @@ describe("ASAR entry producer identity", () => {
|
||||
["packed to unpacked", false],
|
||||
["unpacked to packed", true],
|
||||
] as const)(
|
||||
"rejects a retained %s entry after a new entries pass",
|
||||
"rejects a retained %s entry after closing and recapturing the archive",
|
||||
async (_transition, wasUnpacked) => {
|
||||
const root = await createTestTempDirectory("rea-asar-entry-refresh-");
|
||||
const source = join(root, "source");
|
||||
@@ -236,6 +238,7 @@ describe("ASAR entry producer identity", () => {
|
||||
archive,
|
||||
wasUnpacked ? {} : { unpack: "small.js" },
|
||||
);
|
||||
await reader.close();
|
||||
const currentEntries = [];
|
||||
for await (const entry of reader.entries()) currentEntries.push(entry);
|
||||
const current = currentEntries.find(({ path }) => path === "small.js");
|
||||
@@ -345,7 +348,7 @@ describe("ASAR read failures", () => {
|
||||
);
|
||||
|
||||
it.each(["missing", "replaced"] as const)(
|
||||
"validates the container before returning a zero-length member when it is %s",
|
||||
"reads a captured zero-length member when the original container is %s",
|
||||
async (change) => {
|
||||
const root = await createTestTempDirectory("rea-asar-empty-member-");
|
||||
const source = join(root, "source");
|
||||
@@ -363,11 +366,7 @@ describe("ASAR read failures", () => {
|
||||
if (change === "missing") await rm(archive);
|
||||
else await writeFile(archive, "replacement container");
|
||||
|
||||
await expect(reader.open(entry)).rejects.toMatchObject(
|
||||
change === "missing"
|
||||
? { reason: "io", cause: { code: "ENOENT" } }
|
||||
: { reason: "integrity" },
|
||||
);
|
||||
expect(await buffer(await reader.open(entry))).toHaveLength(0);
|
||||
} finally {
|
||||
await reader.close();
|
||||
}
|
||||
|
||||
@@ -122,7 +122,7 @@ describe("JavaScript artifact ingestion content identities", () => {
|
||||
}
|
||||
});
|
||||
|
||||
it("retains native filesystem errors for outer IO classification", async () => {
|
||||
it("retains filesystem error context in typed IO failures", async () => {
|
||||
const { archive } = await fixture();
|
||||
const snapshot = await scanArtifactInventory(archive);
|
||||
await rm(archive);
|
||||
@@ -130,7 +130,7 @@ describe("JavaScript artifact ingestion content identities", () => {
|
||||
try {
|
||||
await expect(
|
||||
readJavaScriptArtifactFiles(reader, snapshot),
|
||||
).rejects.toMatchObject({ code: "ENOENT" });
|
||||
).rejects.toMatchObject({ reason: "io", cause: { code: "ENOENT" } });
|
||||
} finally {
|
||||
await reader.close();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user