mirror of
https://github.com/morluto/rea.git
synced 2026-11-22 14:35:45 +00:00
test(asar): consolidate header admission coverage (#12944)
This commit is contained in:
1 parent
8a971e192a
commit
4f472e001c
2 files changed
+110
-159
No files matched your search
@@ -11,46 +11,27 @@ import {
|
||||
|
||||
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],
|
||||
]);
|
||||
});
|
||||
const positionalHeader = (archive: Buffer, maxRead = archive.length) => {
|
||||
const read = vi.fn(
|
||||
async (
|
||||
target: Buffer,
|
||||
offset: number,
|
||||
length: number,
|
||||
position: number,
|
||||
) => {
|
||||
const bytesRead = Math.min(maxRead, length, archive.length - position);
|
||||
archive.copy(target, offset, position, position + bytesRead);
|
||||
return { bytesRead, buffer: target };
|
||||
},
|
||||
);
|
||||
return {
|
||||
read,
|
||||
handle: {
|
||||
read: read as unknown as FileHandle["read"],
|
||||
stat: async () => ({ isFile: () => true, size: archive.length }) as Stats,
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
it("recognizes JSON whitespace before the object token and finalizes file extents", () => {
|
||||
const archive = makeArchive();
|
||||
@@ -85,19 +66,7 @@ it("refuses a header that declares one member name twice", async () => {
|
||||
const archive = asarHeaderBytes(
|
||||
'{"files":{"a.js":{"size":10,"offset":"0"},"a.js":{"size":5,"offset":"10"}}}',
|
||||
);
|
||||
const handle = {
|
||||
stat: async () => ({ isFile: () => true, size: archive.length }) as Stats,
|
||||
read: async (
|
||||
target: Buffer,
|
||||
offset: number,
|
||||
length: number,
|
||||
position: number,
|
||||
) => {
|
||||
const bytesRead = Math.min(length, archive.length - position);
|
||||
archive.copy(target, offset, position, position + bytesRead);
|
||||
return { bytesRead, buffer: target };
|
||||
},
|
||||
} as unknown as Parameters<typeof admitAsarHeader>[0];
|
||||
const { handle } = positionalHeader(archive);
|
||||
const extents = await admitAsarHeader(handle);
|
||||
await expect(readAsarHeader(handle, extents)).rejects.toMatchObject({
|
||||
reason: "format",
|
||||
@@ -105,24 +74,31 @@ it("refuses a header that declares one member name twice", async () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("decodes admitted JSON across short reads without consuming padding", async () => {
|
||||
it("admits the fixed prefix and decodes JSON across short reads without consuming padding", async () => {
|
||||
const archive = asarHeaderBytes('{"files":{"�":{}}}', {
|
||||
padding: Buffer.from([0xff]),
|
||||
});
|
||||
const handle = {
|
||||
stat: async () => ({ isFile: () => true, size: archive.length }) as Stats,
|
||||
read: async (
|
||||
target: Buffer,
|
||||
offset: number,
|
||||
length: number,
|
||||
position: number,
|
||||
) => {
|
||||
const bytesRead = Math.min(2, length, archive.length - position);
|
||||
archive.copy(target, offset, position, position + bytesRead);
|
||||
return { bytesRead, buffer: target };
|
||||
},
|
||||
} as unknown as Parameters<typeof admitAsarHeader>[0];
|
||||
const { handle, read } = positionalHeader(archive, 3);
|
||||
const extents = await admitAsarHeader(handle);
|
||||
expect(extents).toEqual({
|
||||
archiveBytes: archive.length,
|
||||
headerBytes: archive.readUInt32LE(4),
|
||||
jsonBytes: archive.readUInt32LE(12),
|
||||
});
|
||||
expect(
|
||||
read.mock.calls.map(([, offset, length, position]) => [
|
||||
offset,
|
||||
length,
|
||||
position,
|
||||
]),
|
||||
).toEqual([
|
||||
[0, 16, 0],
|
||||
[3, 13, 3],
|
||||
[6, 10, 6],
|
||||
[9, 7, 9],
|
||||
[12, 4, 12],
|
||||
[15, 1, 15],
|
||||
]);
|
||||
await expect(readAsarHeader(handle, extents)).resolves.toEqual({
|
||||
files: { "�": {} },
|
||||
});
|
||||
|
||||
@@ -43,45 +43,33 @@ const admit = async (path: string, admission?: AsarHeaderAdmission) => {
|
||||
try {
|
||||
return await admitAsarHeader(handle, undefined, admission);
|
||||
} finally {
|
||||
// Admission must inspect only the fixed prefix, even for forged extents.
|
||||
const calls = [...read.mock.calls];
|
||||
const results = await Promise.all(
|
||||
read.mock.results.map(
|
||||
({ value }) => value as Promise<{ readonly bytesRead: number }>,
|
||||
),
|
||||
);
|
||||
read.mockRestore();
|
||||
await handle.close();
|
||||
expect(handle.fd).toBe(-1);
|
||||
expect(calls.length).toBeGreaterThan(0);
|
||||
let filled = 0;
|
||||
for (const [index, call] of calls.entries()) {
|
||||
const [buffer, offset, length, position]: readonly unknown[] =
|
||||
call as unknown as readonly unknown[];
|
||||
expect(buffer).toBeInstanceOf(Buffer);
|
||||
expect(offset).toBe(filled);
|
||||
expect(position).toBe(filled);
|
||||
expect(length).toBe(16 - filled);
|
||||
filled += results[index]!.bytesRead;
|
||||
for (const call of calls) {
|
||||
const [, , length, position] = call as unknown as [
|
||||
Buffer,
|
||||
number,
|
||||
number,
|
||||
number,
|
||||
];
|
||||
expect(position + length).toBeLessThanOrEqual(16);
|
||||
}
|
||||
expect(filled).toBeLessThanOrEqual(16);
|
||||
}
|
||||
};
|
||||
|
||||
it("admits a header produced by the upstream ASAR serializer", async () => {
|
||||
it("admits a real serialized header only when its complete extent exists", async () => {
|
||||
const { archive, original } = await createFixture();
|
||||
const headerBytes = original.readUInt32LE(4);
|
||||
const jsonBytes = original.readUInt32LE(12);
|
||||
const headerEnd = 8 + headerBytes;
|
||||
await expect(admit(archive)).resolves.toEqual({
|
||||
archiveBytes: original.length,
|
||||
headerBytes: original.readUInt32LE(4),
|
||||
jsonBytes: original.readUInt32LE(12),
|
||||
headerBytes,
|
||||
jsonBytes,
|
||||
});
|
||||
expect(Buffer.compare(await readFile(archive), original)).toBe(0);
|
||||
});
|
||||
|
||||
it("rejects every truncated prefix of a real serialized header", async () => {
|
||||
const { archive, original } = await createFixture();
|
||||
const headerEnd = 8 + original.readUInt32LE(4);
|
||||
for (let length = 0; length < headerEnd; length++) {
|
||||
expect((await readFile(archive)).equals(original)).toBe(true);
|
||||
for (const length of [0, 15, 16, headerEnd - 1]) {
|
||||
await writeFile(archive, original.subarray(0, length));
|
||||
await expect(
|
||||
admit(archive),
|
||||
@@ -90,10 +78,11 @@ it("rejects every truncated prefix of a real serialized header", async () => {
|
||||
reason: "format",
|
||||
});
|
||||
}
|
||||
// Member data is outside header admission; an intact header still passes.
|
||||
await writeFile(archive, original.subarray(0, headerEnd));
|
||||
await expect(admit(archive)).resolves.toMatchObject({
|
||||
await expect(admit(archive)).resolves.toEqual({
|
||||
archiveBytes: headerEnd,
|
||||
headerBytes,
|
||||
jsonBytes,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -120,59 +109,63 @@ it("rejects contradictory and unsigned-boundary extents before decoding", async
|
||||
}
|
||||
});
|
||||
|
||||
it("separates the header decode budget from container validity at its exact boundary", async () => {
|
||||
const root = await createTestTempDirectory("rea-asar-header-budget-");
|
||||
const archive = join(root, "budget.asar");
|
||||
for (const headerBytes of [
|
||||
DEFAULT_ASAR_MAX_HEADER_BYTES,
|
||||
DEFAULT_ASAR_MAX_HEADER_BYTES + 4,
|
||||
]) {
|
||||
const prefix = Buffer.alloc(16);
|
||||
prefix.writeUInt32LE(4, 0);
|
||||
prefix.writeUInt32LE(headerBytes, 4);
|
||||
prefix.writeUInt32LE(headerBytes - 4, 8);
|
||||
prefix.writeUInt32LE(2, 12);
|
||||
await writeFile(archive, prefix);
|
||||
// Supply the declared extent without allocating a header-sized JS buffer.
|
||||
// This tests extent admission only; the padding is not valid archive JSON.
|
||||
await truncate(archive, headerBytes + 8);
|
||||
if (headerBytes === DEFAULT_ASAR_MAX_HEADER_BYTES)
|
||||
await expect(admit(archive)).resolves.toEqual({
|
||||
archiveBytes: headerBytes + 8,
|
||||
headerBytes,
|
||||
jsonBytes: 2,
|
||||
});
|
||||
else
|
||||
await expect(admit(archive)).rejects.toMatchObject({
|
||||
reason: "limit",
|
||||
message: asarHeaderAdmissionRecovery(DEFAULT_ASAR_HEADER_ADMISSION),
|
||||
admission: {
|
||||
kind: "asar-header",
|
||||
maximumBytes: DEFAULT_ASAR_MAX_HEADER_BYTES,
|
||||
source: "default",
|
||||
},
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
const writeOversizedHeaderArchive = async (): Promise<string> => {
|
||||
const root = await createTestTempDirectory("rea-asar-header-recovery-");
|
||||
const archive = join(root, "app.asar");
|
||||
const headerBytes = DEFAULT_ASAR_MAX_HEADER_BYTES + 4;
|
||||
const json = Buffer.from("{}");
|
||||
const writeSparseHeaderArchive = async (
|
||||
archive: string,
|
||||
headerBytes: number,
|
||||
): Promise<void> => {
|
||||
const prefix = Buffer.alloc(16);
|
||||
prefix.writeUInt32LE(4, 0);
|
||||
prefix.writeUInt32LE(headerBytes, 4);
|
||||
prefix.writeUInt32LE(headerBytes - 4, 8);
|
||||
prefix.writeUInt32LE(json.length, 12);
|
||||
await writeFile(archive, Buffer.concat([prefix, json]));
|
||||
prefix.writeUInt32LE(2, 12);
|
||||
// Supply the extent without allocating a header-sized buffer.
|
||||
await writeFile(archive, Buffer.concat([prefix, Buffer.from("{}")]));
|
||||
await truncate(archive, headerBytes + 8);
|
||||
return archive;
|
||||
};
|
||||
|
||||
it("checks the exact default and selected header limits before reading JSON", async () => {
|
||||
const root = await createTestTempDirectory("rea-asar-header-budget-");
|
||||
const archive = join(root, "budget.asar");
|
||||
const raised = DEFAULT_ASAR_MAX_HEADER_BYTES + 4;
|
||||
const expected = (headerBytes: number) => ({
|
||||
archiveBytes: headerBytes + 8,
|
||||
headerBytes,
|
||||
jsonBytes: 2,
|
||||
});
|
||||
await writeSparseHeaderArchive(archive, DEFAULT_ASAR_MAX_HEADER_BYTES);
|
||||
await expect(admit(archive)).resolves.toEqual(
|
||||
expected(DEFAULT_ASAR_MAX_HEADER_BYTES),
|
||||
);
|
||||
await writeSparseHeaderArchive(archive, raised);
|
||||
await expect(admit(archive)).rejects.toMatchObject({
|
||||
reason: "limit",
|
||||
admission: {
|
||||
kind: "asar-header",
|
||||
maximumBytes: DEFAULT_ASAR_MAX_HEADER_BYTES,
|
||||
requiredBytes: raised,
|
||||
source: "default",
|
||||
},
|
||||
});
|
||||
await expect(
|
||||
admit(archive, { maximumBytes: raised - 1, source: "setting" }),
|
||||
).rejects.toMatchObject({
|
||||
reason: "limit",
|
||||
admission: {
|
||||
maximumBytes: raised - 1,
|
||||
requiredBytes: raised,
|
||||
source: "setting",
|
||||
},
|
||||
});
|
||||
await expect(
|
||||
admit(archive, { maximumBytes: raised, source: "setting" }),
|
||||
).resolves.toEqual(expected(raised));
|
||||
});
|
||||
|
||||
it("names the default header admission budget and its setting when source selection cannot bypass it", async () => {
|
||||
const recovery = asarHeaderAdmissionRecovery(DEFAULT_ASAR_HEADER_ADMISSION);
|
||||
const archive = await writeOversizedHeaderArchive();
|
||||
const root = await createTestTempDirectory("rea-asar-header-recovery-");
|
||||
const archive = join(root, "app.asar");
|
||||
await writeSparseHeaderArchive(archive, DEFAULT_ASAR_MAX_HEADER_BYTES + 4);
|
||||
let inventoryFailure: unknown;
|
||||
try {
|
||||
await inventoryArtifact(archive);
|
||||
@@ -262,24 +255,6 @@ afterEach(() => {
|
||||
vi.unstubAllEnvs();
|
||||
});
|
||||
|
||||
it("admits a header above the default when the selected admission raises the budget", async () => {
|
||||
const archive = await writeOversizedHeaderArchive();
|
||||
const headerBytes = DEFAULT_ASAR_MAX_HEADER_BYTES + 4;
|
||||
await expect(
|
||||
admit(archive, { maximumBytes: headerBytes - 1, source: "setting" }),
|
||||
).rejects.toMatchObject({
|
||||
reason: "limit",
|
||||
admission: { maximumBytes: headerBytes - 1, source: "setting" },
|
||||
});
|
||||
await expect(
|
||||
admit(archive, { maximumBytes: headerBytes, source: "setting" }),
|
||||
).resolves.toEqual({
|
||||
archiveBytes: headerBytes + 8,
|
||||
headerBytes,
|
||||
jsonBytes: 2,
|
||||
});
|
||||
});
|
||||
|
||||
it("applies the caller-selected environment, not the ambient one, to inventory and JavaScript analysis", async () => {
|
||||
const { archive, original } = await createFixture();
|
||||
const headerBytes = original.readUInt32LE(4);
|
||||
|
||||
Reference in new issue
Block a user