Files
rea/src/artifacts/AsarHeader.test.ts
morluto e8da7e1edd fix(artifacts): share bounded ASAR signature observations
Parse Pickle extents once at the ASAR boundary and carry finalized signature
facts through root and nested classification. Scan JSON whitespace within its
declared extent and decode budget without retaining the whitespace prefix.
Keep prefix storage bounded independently of stream chunk ownership.

Fill fixed ASAR prefixes and DMG trailers across short reads, preserve
cancellation through admission and stream EOF, and retain existing cleanup
ownership. Cover public CLI/MCP selection, root/nested inventory, malformed
JSON whitespace and padding, limits, native/XML consumers and descriptor faults.

Validation: 24 focused files / 170 tests; full check; docs check; test discovery.
Official inert ASAR fixtures and controlled descriptor faults were exercised;
real Electron execution, Windows and natural OS short-read frequency remain
unverified. Equivalent compact CLI tasks returned identical results and reply
token estimates; repeated timings do not establish a speedup.

Fixes #3574
Fixes #5188
2026-10-19 14:32:48 +08:00

81 lines
2.3 KiB
TypeScript

import type { Stats } from "node:fs";
import { expect, it, vi } from "vitest";
import {
admitAsarHeader,
createAsarHeaderSignatureProbe,
} from "./AsarHeader.js";
import { asarHeaderBytes } from "../../tests/fixtures/asarHeader.js";
const makeArchive = (): Buffer => asarHeaderBytes(' \t\r\n{"files":{}} ');
it("fills the fixed Pickle prefix across controlled short reads", async () => {
const archive = makeArchive();
const calls: Array<readonly [number, number, number]> = [];
const handle = {
stat: vi.fn(
async () =>
({
isFile: () => true,
size: archive.length,
}) as Stats,
),
read: vi.fn(
async (
target: Buffer,
offset: number,
length: number,
position: number,
) => {
calls.push([offset, length, position]);
const bytesRead = Math.min(3, length, archive.length - position);
archive.copy(target, offset, position, position + bytesRead);
return { bytesRead, buffer: target };
},
),
} as unknown as Parameters<typeof admitAsarHeader>[0];
await expect(admitAsarHeader(handle)).resolves.toEqual({
archiveBytes: archive.length,
headerBytes: archive.readUInt32LE(4),
jsonBytes: archive.readUInt32LE(12),
});
expect(calls).toEqual([
[0, 16, 0],
[3, 13, 3],
[6, 10, 6],
[9, 7, 9],
[12, 4, 12],
[15, 1, 15],
]);
});
it("recognizes JSON whitespace before the object token and finalizes file extents", () => {
const archive = makeArchive();
const probe = createAsarHeaderSignatureProbe();
probe.write(archive.subarray(0, 7));
expect(probe.observation.state).toBe("pending");
probe.write(archive.subarray(7, 21));
expect(probe.observation.state).toBe("matched");
const result = probe.finish(archive.length);
expect(result).toMatchObject({
state: "matched",
extents: {
archiveBytes: archive.length,
headerBytes: archive.readUInt32LE(4),
jsonBytes: archive.readUInt32LE(12),
},
signatureOffset: 20,
});
});
it("rejects a signature whose declared header exceeds the observed archive", () => {
const archive = makeArchive();
const probe = createAsarHeaderSignatureProbe();
probe.write(archive);
expect(probe.finish(archive.length - 1)).toMatchObject({
state: "rejected",
reason: "archive-too-short",
});
});