mirror of
https://github.com/morluto/rea.git
synced 2026-12-05 19:51:20 -05:00
a6cca9dc62
* feat(artifacts): decode UnityFS LZMA blocks Unity's default AssetBundle compression (type 1) left members as hash_status unavailable. Decode 5-byte Unity LZMA streams with the existing lzma-purejs dependency so inventory can hash and extract them. * fix(compression): read Unity LZMA dictionary via BinaryReader Architecture guards forbid raw Buffer numeric reads outside binaryContainer; match the UEFI LZMA path. * style(compression): format UnityFS LZMA helper * fix(compression): make UnityFS LZMA cancelable and end-marker safe Accept optional LZMA end markers without trailing junk, decode off-thread so timer aborts land, return one expected-sized buffer, and mark xinzexi unverified. * fix(compression): await Unity LZMA worker exit and transfer alone buffer Await worker.terminate() before settling abort/reply paths, and allocate the synthesized alone header with allocUnsafeSlow so the worker receives it without a second encoded-stream copy. * chore: retrigger CI after Greptile worker-exit follow-up * test(compression): clarify Unity LZMA cancellation case title Retriggers Actions after the worker-exit follow-up push did not receive a GitHub Actions check suite. * fix(unityfs): keep the directory for over-budget LZMA blocks and decode spec-valid streams UnityFS writers emit one LZMA block for a whole data stream, so the per-block decode budget refused the whole inventory of any LZMA bundle above 64 MiB, where before this change the directory was retained. An over-budget block now keeps the directory and only members inside it report an unavailable read naming the budget, for LZ4 and LZMA alike. The LZMA decoder window is bounded by the declared output instead of the declared dictionary, so a large dictionary on a small block decodes and no longer allocates a dictionary-sized window. Properties follow the LZMA specification (lc 0-8, lp 0-4, pb 0-4) rather than the LZMA2 lc + lp limit. Worker error events and exits without a reply stay runtime failures instead of being reported as a malformed block. Tests decode a liblzma-produced stream as a known answer, settle only after the worker exits on completion and cancellation, and share one encoder. * fix(artifacts): report Unity LZMA size mismatch as malformed Overflow past the bundle's declared length is a format error, not a decoder budget exhaustion. Declarations above maximumBytes still fail closed as a resource limit before decode starts. * fix(unityfs): name the encoded or decoded bound that refused a block A block refused by its encoded size reported its decoded size against the decoded budget. The block now records which bound refused it, and the member read names that bound, its declared size and the budget. --------- Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
376 lines
12 KiB
TypeScript
376 lines
12 KiB
TypeScript
import { collectEntries } from "../../tests/fixtures/artifactEntries.js";
|
|
import { buffer, text } from "node:stream/consumers";
|
|
|
|
import { describe, expect, it } from "vitest";
|
|
|
|
import { createTestTempFile } from "../../tests/fixtures/temporaryDirectory.js";
|
|
import {
|
|
buildUnityFs,
|
|
encodeUnityLzmaWithEndMarker,
|
|
type UnityFsMember,
|
|
} from "../../tests/fixtures/unityfs.js";
|
|
|
|
import type { ArtifactEntry } from "../resources/ArtifactReader.js";
|
|
import { UnityFsArtifactReader } from "./UnityFsArtifactReader.js";
|
|
|
|
const withReader = async <T>(
|
|
bytes: Buffer,
|
|
use: (reader: UnityFsArtifactReader) => Promise<T>,
|
|
): Promise<T> => {
|
|
const reader = new UnityFsArtifactReader(
|
|
await createTestTempFile("rea-unityfs-", "content.bundle", bytes),
|
|
);
|
|
try {
|
|
return await use(reader);
|
|
} finally {
|
|
await reader.close();
|
|
}
|
|
};
|
|
|
|
const members: readonly UnityFsMember[] = [
|
|
{ path: "CAB-b0b0e3d3930da9dc163f811e1cd9c0c2", data: "serialized" },
|
|
{ path: "main.resS", data: "resource bytes" },
|
|
{ path: "empty.sharedAssets", data: "" },
|
|
];
|
|
|
|
describe("UnityFsArtifactReader", () => {
|
|
it("preserves a directory node as a directory rather than declaring encryption", async () => {
|
|
await withReader(
|
|
buildUnityFs([
|
|
{ path: "assets", data: "", flags: 1 },
|
|
{ path: "assets/data.bin", data: "bytes", flags: 4 },
|
|
]),
|
|
async (reader) => {
|
|
const entries = await collectEntries(reader);
|
|
expect(entries).toEqual([
|
|
expect.objectContaining({
|
|
path: "assets",
|
|
kind: "directory",
|
|
encrypted: false,
|
|
}),
|
|
expect.objectContaining({
|
|
path: "assets/data.bin",
|
|
kind: "file",
|
|
encrypted: false,
|
|
}),
|
|
]);
|
|
expect(await text(await reader.open(entries[1]!))).toBe("bytes");
|
|
},
|
|
);
|
|
});
|
|
|
|
it("accepts consecutive dots within a safe filename", async () => {
|
|
await withReader(
|
|
buildUnityFs([{ path: "data..bin", data: "bytes" }]),
|
|
async (reader) => {
|
|
const [entry] = await collectEntries(reader);
|
|
expect(entry?.path).toBe("data..bin");
|
|
expect(await text(await reader.open(entry!))).toBe("bytes");
|
|
},
|
|
);
|
|
});
|
|
|
|
it("retains unknown directory facts instead of reporting zero blocks or offsets", async () => {
|
|
await withReader(
|
|
buildUnityFs(members, { flags: 0x1400 | 0x43 }),
|
|
async (reader) => {
|
|
await collectEntries(reader);
|
|
expect(reader.formatMetadata()).toMatchObject({
|
|
directory_status: "encrypted",
|
|
block_count: null,
|
|
data_start: null,
|
|
});
|
|
},
|
|
);
|
|
});
|
|
|
|
it("does not interpret a legacy encryption declaration as modern padding", async () => {
|
|
await withReader(
|
|
buildUnityFs(members, { engineVersion: "2019.4.20f1", flags: 0x243 }),
|
|
async (reader) => {
|
|
expect(await collectEntries(reader)).toEqual([]);
|
|
expect(reader.formatMetadata()).toMatchObject({
|
|
directory_status: "encrypted",
|
|
});
|
|
},
|
|
);
|
|
});
|
|
|
|
it("leaves ambiguous producer flag semantics unknown", async () => {
|
|
await withReader(
|
|
buildUnityFs(members, { engineVersion: "0.0.0", flags: 0x243 }),
|
|
async (reader) => {
|
|
expect(await collectEntries(reader)).toEqual([]);
|
|
expect(reader.formatMetadata()).toMatchObject({
|
|
directory_status: "unsupported-layout",
|
|
});
|
|
},
|
|
);
|
|
});
|
|
});
|
|
|
|
describe("UnityFS payloads and malformed declarations", () => {
|
|
it.each([
|
|
["lz4hc table and blocks", {}],
|
|
[
|
|
"uncompressed table and blocks",
|
|
{ blocksInfoCompression: 0, blockCompression: 0 },
|
|
],
|
|
["uncompressed blocks", { blockCompression: 0 }],
|
|
["blocks-info at the end", { blocksInfoAtEnd: true }],
|
|
["aligned header without data padding", { flags: 0x43 }],
|
|
["format 6 without padding alignment", { formatVersion: 6, flags: 3 }],
|
|
] as const)("lists and reads every member with %s", async (_, options) => {
|
|
await withReader(buildUnityFs(members, options), async (reader) => {
|
|
const entries = await collectEntries(reader);
|
|
expect(entries.map(({ path }) => path)).toEqual(
|
|
members.map(({ path }) => path),
|
|
);
|
|
expect(entries.every(({ byteOffset }) => byteOffset === null)).toBe(true);
|
|
expect(reader.formatMetadata()).toMatchObject({
|
|
kind: "unityfs",
|
|
format_version: "formatVersion" in options ? options.formatVersion : 8,
|
|
engine_version: "2021.3.32f1c1",
|
|
directory_status: "available",
|
|
});
|
|
const contents = await Promise.all(
|
|
entries.map(async (entry) => text(await reader.open(entry))),
|
|
);
|
|
expect(contents).toEqual(members.map(({ data }) => data));
|
|
});
|
|
});
|
|
|
|
it("reads a member spanning multiple compressed blocks", async () => {
|
|
const big = Buffer.alloc(131072 * 2 + 500, 0x61);
|
|
await withReader(
|
|
buildUnityFs([{ path: "big.resS", data: big }]),
|
|
async (reader) => {
|
|
const [entry] = await collectEntries(reader);
|
|
if (entry === undefined) throw new Error("expected one entry");
|
|
const stream = await reader.open(entry);
|
|
expect((await buffer(stream)).equals(big)).toBe(true);
|
|
},
|
|
);
|
|
});
|
|
|
|
it("preserves format and node metadata facts", async () => {
|
|
await withReader(buildUnityFs(members), async (reader) => {
|
|
const entries = await collectEntries(reader);
|
|
const metadata = reader.formatMetadata();
|
|
expect(metadata).toMatchObject({
|
|
signature: "UnityFS",
|
|
player_version: "5.x.x",
|
|
block_count: 1,
|
|
blocks_info_at_end: false,
|
|
});
|
|
expect(metadata?.data_hash).toMatch(/^[0-9a-f]{32}$/u);
|
|
expect(entries[0]?.formatMetadata).toMatchObject({
|
|
kind: "unityfs-entry",
|
|
index: 0,
|
|
source_path: members[0]?.path,
|
|
declared_offset: 0,
|
|
});
|
|
const first = members[0];
|
|
if (first === undefined || typeof first.data !== "string")
|
|
throw new Error("expected a string fixture member");
|
|
expect(entries[1]?.formatMetadata).toMatchObject({
|
|
declared_offset: first.data.length,
|
|
});
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("UnityFS unavailable and malformed directories", () => {
|
|
it("reports cancellation during unavailable-directory coverage", async () => {
|
|
await withReader(
|
|
buildUnityFs(members, { flags: 0x1400 | 0x43 }),
|
|
async (reader) => {
|
|
const controller = new AbortController();
|
|
const entries = reader
|
|
.entries(controller.signal, {
|
|
onCoverage: () => controller.abort(),
|
|
})
|
|
[Symbol.asyncIterator]();
|
|
await expect(entries.next()).rejects.toMatchObject({
|
|
reason: "cancelled",
|
|
});
|
|
},
|
|
);
|
|
});
|
|
|
|
it("reports an encrypted directory as unknown coverage without entries", async () => {
|
|
await withReader(
|
|
buildUnityFs(members, { flags: 0x1400 | 0x43 }),
|
|
async (reader) => {
|
|
const coverage: unknown[] = [];
|
|
const entries: ArtifactEntry[] = [];
|
|
for await (const entry of reader.entries(undefined, {
|
|
onCoverage: (value) => coverage.push(value),
|
|
}))
|
|
entries.push(entry);
|
|
expect(entries).toEqual([]);
|
|
expect(reader.formatMetadata()).toMatchObject({
|
|
directory_status: "encrypted",
|
|
});
|
|
expect(coverage).toEqual([
|
|
expect.objectContaining({ status: "unknown" }),
|
|
]);
|
|
},
|
|
);
|
|
});
|
|
|
|
it("inventories an LZMA blocks-info table", async () => {
|
|
await withReader(
|
|
buildUnityFs(members, { blocksInfoCompression: 1 }),
|
|
async (reader) => {
|
|
const entries = await collectEntries(reader);
|
|
expect(entries.map((entry) => entry.path)).toEqual(
|
|
members.map((member) => member.path),
|
|
);
|
|
expect(reader.formatMetadata()).toMatchObject({
|
|
directory_status: "available",
|
|
});
|
|
},
|
|
);
|
|
});
|
|
|
|
it("decodes an LZMA-compressed member block", async () => {
|
|
await withReader(
|
|
buildUnityFs(members, { blockCompression: 1 }),
|
|
async (reader) => {
|
|
const entries = await collectEntries(reader);
|
|
expect(entries).toHaveLength(members.length);
|
|
for (const [index, entry] of entries.entries()) {
|
|
const stream = await reader.open(entry);
|
|
expect(await text(stream)).toBe(
|
|
typeof members[index]!.data === "string"
|
|
? members[index]!.data
|
|
: members[index]!.data.toString("utf8"),
|
|
);
|
|
}
|
|
},
|
|
);
|
|
});
|
|
|
|
it("decodes an LZMA member block that includes an end marker", async () => {
|
|
await withReader(
|
|
buildUnityFs(members, {
|
|
blockCompression: 1,
|
|
encodeBlock: encodeUnityLzmaWithEndMarker,
|
|
}),
|
|
async (reader) => {
|
|
const entries = await collectEntries(reader);
|
|
expect(entries).toHaveLength(members.length);
|
|
for (const [index, entry] of entries.entries()) {
|
|
const stream = await reader.open(entry);
|
|
expect(await text(stream)).toBe(
|
|
typeof members[index]!.data === "string"
|
|
? members[index]!.data
|
|
: members[index]!.data.toString("utf8"),
|
|
);
|
|
}
|
|
},
|
|
);
|
|
});
|
|
|
|
it("refuses a malformed LZMA member block", async () => {
|
|
await withReader(
|
|
buildUnityFs([{ path: "broken", data: "payload" }], {
|
|
blockCompression: 1,
|
|
encodeBlock: () => Buffer.from([0x5d, 0, 0, 0, 1, 0, 0xff]),
|
|
}),
|
|
async (reader) => {
|
|
const [entry] = await collectEntries(reader);
|
|
if (entry === undefined) throw new Error("expected one entry");
|
|
await expect(buffer(await reader.open(entry))).rejects.toMatchObject({
|
|
reason: "malformed",
|
|
});
|
|
},
|
|
);
|
|
});
|
|
|
|
it("reports an LZHAM blocks-info table as unknown coverage", async () => {
|
|
await withReader(
|
|
buildUnityFs(members, { blocksInfoCompression: 4 }),
|
|
async (reader) => {
|
|
const entries: ArtifactEntry[] = [];
|
|
const coverage: unknown[] = [];
|
|
for await (const entry of reader.entries(undefined, {
|
|
onCoverage: (value) => coverage.push(value),
|
|
}))
|
|
entries.push(entry);
|
|
expect(entries).toEqual([]);
|
|
expect(reader.formatMetadata()).toMatchObject({
|
|
directory_status: "unsupported-compression",
|
|
});
|
|
expect(coverage).toEqual([
|
|
expect.objectContaining({ status: "unknown" }),
|
|
]);
|
|
},
|
|
);
|
|
});
|
|
|
|
it.each([
|
|
[
|
|
"format 9 with a different block row layout",
|
|
buildUnityFs(members, { formatVersion: 9 }),
|
|
"unavailable",
|
|
],
|
|
[
|
|
"an unsupported format version",
|
|
buildUnityFs(members, { formatVersion: 0x40 }),
|
|
"unavailable",
|
|
],
|
|
[
|
|
"a declared size beyond the file",
|
|
(() => {
|
|
const bytes = buildUnityFs(members);
|
|
bytes.writeBigUInt64BE(1n << 40n, 32);
|
|
return bytes;
|
|
})(),
|
|
"truncated",
|
|
],
|
|
[
|
|
"a node count that cannot fit",
|
|
buildUnityFs(members, { declaredNodeCount: 0xffff_ffff }),
|
|
"malformed",
|
|
],
|
|
["bytes without a bundle", Buffer.from("not a unity bundle"), "malformed"],
|
|
])("refuses %s", async (_, bytes, reason) => {
|
|
await withReader(bytes, async (reader) => {
|
|
await expect(collectEntries(reader)).rejects.toMatchObject({ reason });
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("UnityFsArtifactReader member streams", () => {
|
|
it("types cancellation during a member read instead of crashing", async () => {
|
|
const data = Buffer.alloc(4 * 1024 * 1024, 0x62);
|
|
await withReader(
|
|
buildUnityFs([{ path: "big.resS", data }]),
|
|
async (reader) => {
|
|
const [entry] = await collectEntries(reader);
|
|
if (entry === undefined) throw new Error("expected one entry");
|
|
const controller = new AbortController();
|
|
const stream = await reader.open(entry, controller.signal);
|
|
const chunks = stream[Symbol.asyncIterator]();
|
|
await chunks.next();
|
|
controller.abort();
|
|
await expect(chunks.next()).rejects.toMatchObject({
|
|
reason: "cancelled",
|
|
});
|
|
},
|
|
);
|
|
});
|
|
|
|
it("refuses an entry that did not come from this reader", async () => {
|
|
await withReader(buildUnityFs(members), async (reader) => {
|
|
const [entry] = await collectEntries(reader);
|
|
if (entry === undefined) throw new Error("expected one entry");
|
|
await expect(
|
|
reader.open({ ...entry, adapterKey: "unityfs-entry:99" }),
|
|
).rejects.toMatchObject({ reason: "integrity" });
|
|
});
|
|
});
|
|
});
|