test(asar): consolidate inventory boundary coverage (#12948)

This commit is contained in:
morluto authored and GitHub committed 2026-11-20 23:04:21 +00:00
1 parent 0b0b847afd
commit 916a033eb7
2 files changed
+63 -94

No files matched your search

@@ -88,46 +88,41 @@ describe("ASAR JSON signature boundaries", () => {
expect(await inventoryArtifact(application)).toEqual(inventory);
});
it.each([
{ label: "vertical tab", prefix: "\v", padding: undefined },
{ label: "form feed", prefix: "\f", padding: undefined },
{ label: "nonbreaking space", prefix: "\u00a0", padding: undefined },
{ label: "byte order mark", prefix: "\ufeff", padding: undefined },
{
label: "pickle padding outside jsonBytes",
prefix: " ",
padding: Buffer.from("{"),
},
])("requires an object within jsonBytes for $label", async (fixture) => {
it("classifies ZIP members as opaque when their ASAR JSON object starts outside its valid framing", async () => {
const root = await createTestTempDirectory("rea-asar-invalid-json-space-");
const json =
fixture.padding === undefined
? `${fixture.prefix}${JSON.stringify({
files: { "main.js": { size: 0, offset: "0" } },
})}`
: fixture.prefix;
const invalid = asarHeaderBytes(
json,
fixture.padding === undefined ? {} : { padding: fixture.padding },
);
const writer = new ZipWriter(new Uint8ArrayWriter());
await writer.add("resources/app.asar", new Uint8ArrayReader(invalid));
const outerPath = join(root, "opaque.zip");
const writer = new ZipWriter(new Uint8ArrayWriter());
const header = '{"files":{"main.js":{"size":0,"offset":"0"}}}';
const invalidHeaders = {
"vertical-tab": asarHeaderBytes(`\v${header}`),
"form-feed": asarHeaderBytes(`\f${header}`),
nonbreaking: asarHeaderBytes(`\u00a0${header}`),
bom: asarHeaderBytes(`\ufeff${header}`),
padding: asarHeaderBytes(" ", { padding: Buffer.from("{") }),
};
await writer.add("resources/", undefined, { directory: true });
const members = Object.entries(invalidHeaders);
for (const [name, bytes] of members)
await writer.add(`resources/${name}.asar`, new Uint8ArrayReader(bytes));
await writeFile(outerPath, await writer.close());
const inventory = await inventoryArtifact(outerPath);
expect(artifactOccurrenceAt(inventory, "resources/app.asar")).toMatchObject(
{
expect(artifactParentPaths(inventory)).toEqual({
".": null,
resources: ".",
...Object.fromEntries(
members.map(([name]) => [`resources/${name}.asar`, "resources"]),
),
});
for (const [name] of members)
expect(
artifactOccurrenceAt(inventory, `resources/${name}.asar`),
name,
).toMatchObject({
artifact_kind: "resource",
artifact_format: "file",
hash_status: "verified",
},
);
expect(
inventory.occurrences.some(({ logical_path }) =>
logical_path.startsWith("resources/app.asar/"),
),
).toBe(false);
});
});
});
@@ -4,7 +4,7 @@ import { Readable } from "node:stream";
import { buffer } from "node:stream/consumers";
import { createPackage, listPackage, uncache } from "@electron/asar";
import { describe, expect, it, onTestFinished } from "vitest";
import { describe, expect, it } from "vitest";
import { ArtifactResourceScope } from "../../../src/resources/ArtifactResourceScope.js";
import { inventoryArtifact } from "../../fixtures/artifactInventory.js";
@@ -114,30 +114,27 @@ describe("ASAR path boundary", () => {
});
describe("ASAR inventory freshness", () => {
it("preserves missing-container I/O as an I/O failure instead of malformed format", async () => {
const root = await createTestTempDirectory("rea-asar-missing-");
const reader = new AsarArtifactReader(join(root, "missing.asar"));
await expect(
reader.entries()[Symbol.asyncIterator]().next(),
).rejects.toMatchObject({
reason: "io",
message: expect.stringContaining("ENOENT"),
});
});
it("preserves an ASAR path replaced by a directory as a filesystem failure", async () => {
const fixture = await archiveFixture();
await rm(fixture.archive);
await mkdir(fixture.archive);
const reader = new AsarArtifactReader(fixture.archive);
await expect(
reader.entries()[Symbol.asyncIterator]().next(),
).rejects.toMatchObject({
reason: "io",
message: expect.stringContaining("EISDIR"),
});
it("preserves native I/O failures for missing and directory-replaced archives", async () => {
const { archive } = await archiveFixture();
const reader = new AsarArtifactReader(archive);
try {
await rm(archive);
await expect(
reader.entries()[Symbol.asyncIterator]().next(),
).rejects.toMatchObject({
reason: "io",
cause: { code: "ENOENT" },
});
await mkdir(archive);
await expect(
reader.entries()[Symbol.asyncIterator]().next(),
).rejects.toMatchObject({
reason: "io",
cause: { code: "EISDIR" },
});
} finally {
await reader.close();
}
});
it.each(["archive", "bundle"] as const)(
@@ -150,7 +147,6 @@ describe("ASAR inventory freshness", () => {
expect(
first.occurrences.map(({ logical_path }) => logical_path),
).toContain(`${prefix}old.js`);
expect(await inventoryArtifact(fixture[target])).toEqual(first);
await fixture.replace();
const second = await inventoryArtifact(fixture[target]);
@@ -163,7 +159,6 @@ describe("ASAR inventory freshness", () => {
);
expect(paths).toContain(`${prefix}new.js`);
expect(paths).not.toContain(`${prefix}old.js`);
expect(await inventoryArtifact(fixture[target])).toEqual(second);
} finally {
uncache(fixture.archive);
}
@@ -223,7 +218,6 @@ describe("ASAR captured-byte identity", () => {
const originalSize = (await lstat(fixture.archive)).size;
const directory = new DirectoryArtifactReader(fixture.bundle);
const resourceScope = new ArtifactResourceScope();
onTestFinished(() => resourceScope.close());
try {
await expect(
resourceScope.run(() =>
@@ -310,7 +304,6 @@ describe("ASAR captured-byte identity", () => {
},
});
const resourceScope = new ArtifactResourceScope();
onTestFinished(() => resourceScope.close());
try {
const failure: unknown = await resourceScope
.run(() =>
@@ -399,46 +392,27 @@ describe("ASAR reader lifecycle", () => {
}
});
it("releases the selected archive header on close after a cancelled read", async () => {
const fixture = await archiveFixture();
const reader = new AsarArtifactReader(fixture.archive);
const entries = reader.entries()[Symbol.asyncIterator]();
try {
const entry = await entries.next();
if (entry.done) throw new Error("Expected an ASAR file entry");
const controller = new AbortController();
controller.abort();
await expect(async () =>
reader.open(entry.value, controller.signal),
).rejects.toMatchObject({ reason: "cancelled" });
await entries.return?.();
await reader.close();
await fixture.replace();
expect(listPackage(fixture.archive, { isPack: false })).toEqual([
`${sep}new.js`,
]);
} finally {
await entries.return?.();
await reader.close();
uncache(fixture.archive);
}
});
it("refuses an entry inventoried before close as not produced by the reader", async () => {
it("retires entries on close and recaptures the replaced archive", async () => {
const fixture = await archiveFixture();
const reader = new AsarArtifactReader(fixture.archive);
try {
const entries = [];
for await (const entry of reader.entries()) entries.push(entry);
const [entry] = entries;
if (entry === undefined) throw new Error("Expected an ASAR file entry");
const original = [];
for await (const entry of reader.entries()) original.push(entry);
expect(original.map(({ path }) => path)).toEqual(["old.js"]);
await reader.close();
await expect(reader.open(entry)).rejects.toMatchObject({
name: "ArtifactReaderFailure",
await expect(reader.open(original[0]!)).rejects.toMatchObject({
reason: "integrity",
integrityCheck: "foreign-entry",
});
await fixture.replace();
const current = [];
for await (const entry of reader.entries())
current.push([
entry.path,
(await buffer(await reader.open(entry))).toString("utf8"),
]);
expect(current).toEqual([["new.js", CONTENT]]);
} finally {
await reader.close();
uncache(fixture.archive);