fix(artifacts): take nested container families from bytes (#1553)

Inventory gave nested members a ZIP or ASAR family from their path
suffix. Byte classification replaced it only for ZIP magic, so text named
`.apk` or `.asar` stayed a container, and encrypted entries and symlinks
kept the suffix guess. A filesystem `.asar` without an ASAR header failed
the whole inventory as a malformed archive.

Nested ZIP families now follow ZIP magic and ASAR follows its header
pickle, shared with root classification. An unread member named like a
container has an unknown format and a limitation naming the suffix. ASAR
expansion requires the header.

The Android projection adds a limitation when no root AndroidManifest.xml
was observed, and its rejection names the observed root format.

Fixes #1491

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kaoru0822
2026-10-10 06:17:40 -07:00
committed by GitHub
co-authored by Claude Opus 5.5
parent c8fd2f152b
commit 4b985e5021
8 changed files with 263 additions and 67 deletions
@@ -154,9 +154,23 @@ describe("unexpanded ASAR inventory controls", () => {
"resources/encrypted.asar": "resources",
"resources/ordinary.asar": "resources",
});
expect(artifactOccurrenceAt(observed, "resources/app.asar")).toMatchObject({
artifact_kind: "container",
artifact_format: "asar",
});
expect(
artifactOccurrenceAt(observed, "resources/encrypted.asar").hash_status,
).toBe("unavailable");
artifactOccurrenceAt(observed, "resources/ordinary.asar"),
).toMatchObject({ artifact_kind: "resource", artifact_format: "file" });
expect(
artifactOccurrenceAt(observed, "resources/encrypted.asar"),
).toMatchObject({
artifact_kind: "unknown",
artifact_format: "unknown",
hash_status: "unavailable",
limitations: [
"The path suffix suggests ASAR, but these bytes were not read, so the container family is unknown.",
],
});
});
it("keeps ASAR-named directories as directory parents", async () => {
@@ -195,9 +209,30 @@ describe("unexpanded ASAR inventory controls", () => {
},
);
it("keeps malformed filesystem archives rejected", async () => {
const root = await createTestTempDirectory("rea-malformed-asar-");
it("records filesystem files without an ASAR header as ordinary bytes", async () => {
const root = await createTestTempDirectory("rea-ordinary-asar-");
await writeFile(join(root, "ordinary.asar"), "ordinary bytes");
const observed = await inventoryArtifact(root);
expect(artifactParentPaths(observed)).toEqual({
".": null,
"ordinary.asar": ".",
});
expect(artifactOccurrenceAt(observed, "ordinary.asar")).toMatchObject({
artifact_kind: "resource",
artifact_format: "file",
hash_status: "verified",
});
});
it("keeps filesystem archives with a malformed ASAR header rejected", async () => {
const root = await createTestTempDirectory("rea-malformed-asar-");
const json = Buffer.from("{not json");
const header = Buffer.alloc(16);
header.writeUInt32LE(4, 0);
header.writeUInt32LE(json.length + 12, 4);
header.writeUInt32LE(json.length + 8, 8);
header.writeUInt32LE(json.length, 12);
await writeFile(join(root, "broken.asar"), Buffer.concat([header, json]));
await expect(inventoryArtifact(root)).rejects.toMatchObject({
reason: "format",
});
+78 -1
View File
@@ -1,10 +1,18 @@
import { lstat, writeFile } from "node:fs/promises";
import { lstat, mkdir, symlink, writeFile } from "node:fs/promises";
import { join } from "node:path";
import {
TextReader,
Uint8ArrayReader,
Uint8ArrayWriter,
ZipWriter,
} from "@zip.js/zip.js";
import { describe, expect, it } from "vitest";
import { inventoryArtifact } from "../../../src/artifacts/inventory/ArtifactInventory.js";
import { classifyAndHashRoot } from "../../../src/artifacts/inventory/classify.js";
import { parseBinaryTarget } from "../../../src/application/BinaryTargetResolver.js";
import { artifactOccurrenceAt } from "../../fixtures/artifactEntryOrder.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
describe("ZIP signature detection", () => {
@@ -120,3 +128,72 @@ describe("ZIP signature detection", () => {
},
);
});
describe("nested container family", () => {
const plainZip = async (): Promise<Uint8Array> => {
const writer = new ZipWriter(new Uint8ArrayWriter());
await writer.add("readme.txt", new TextReader("hello\n"));
return writer.close();
};
it("takes ZIP families from member bytes, not member suffixes", async () => {
const root = await createTestTempDirectory("rea-nested-zip-family-");
const path = join(root, "outer.zip");
const writer = new ZipWriter(new Uint8ArrayWriter());
await writer.add("inner-text.apk", new TextReader("just some text\n"));
await writer.add("inner-real.apk", new Uint8ArrayReader(await plainZip()));
await writer.add("renamed.bin", new Uint8ArrayReader(await plainZip()));
await writer.add("locked.ipa", new TextReader("encrypted bytes"), {
password: "fixture-only",
});
await writeFile(path, await writer.close());
const observed = await inventoryArtifact(path);
expect(artifactOccurrenceAt(observed, "inner-text.apk")).toMatchObject({
artifact_kind: "resource",
artifact_format: "file",
limitations: [],
});
expect(artifactOccurrenceAt(observed, "inner-real.apk")).toMatchObject({
artifact_kind: "container",
artifact_format: "apk",
});
expect(artifactOccurrenceAt(observed, "renamed.bin")).toMatchObject({
artifact_kind: "container",
artifact_format: "zip",
});
expect(artifactOccurrenceAt(observed, "locked.ipa")).toMatchObject({
artifact_kind: "unknown",
artifact_format: "unknown",
hash_status: "unavailable",
limitations: [
"The path suffix suggests IPA, but these bytes were not read, so the container family is unknown.",
],
});
});
it.skipIf(process.platform === "win32")(
"does not name a container family for a symlink it did not follow",
async () => {
const root = await createTestTempDirectory("rea-nested-symlink-family-");
const directory = join(root, "application");
await mkdir(directory);
await writeFile(join(directory, "real.apk"), await plainZip());
await symlink("real.apk", join(directory, "link.apk"));
const observed = await inventoryArtifact(directory);
expect(artifactOccurrenceAt(observed, "link.apk")).toMatchObject({
entry_kind: "symlink",
artifact_kind: "unknown",
artifact_format: "unknown",
hash_status: "not-hashed",
limitations: [
"Symlink target was not followed or disclosed.",
"The path suffix suggests APK, but these bytes were not read, so the container family is unknown.",
],
});
expect(artifactOccurrenceAt(observed, "real.apk")).toMatchObject({
artifact_kind: "container",
artifact_format: "apk",
});
},
);
});
+38 -1
View File
@@ -119,7 +119,14 @@ describe("Android application projection", () => {
projectAndroidApplicationEvidence({ inventory_evidence: [inventory] }),
).toMatchObject({
ok: false,
error: { _tag: "AnalysisInputError" },
error: {
_tag: "AnalysisInputError",
issues: [
expect.objectContaining({
message: expect.stringContaining("root format is zip"),
}),
],
},
});
});
@@ -168,6 +175,36 @@ describe("Android application projection", () => {
});
});
describe("APK family without a root manifest", () => {
const missingManifest =
"No root AndroidManifest.xml occurrence was observed. The APK family comes from the .apk suffix of a ZIP archive, so these bytes may not be an APK.";
const projectApk = async (entries: Record<string, string>) => {
const root = await createTestTempDirectory("rea-android-manifest-");
const path = join(root, "Plain.apk");
const writer = new ZipWriter(new Uint8ArrayWriter());
for (const [entry, text] of Object.entries(entries))
await writer.add(entry, new TextReader(text));
await writeFile(path, await writer.close());
const inventory = parseEvidence(
await runProviderAnalysis(path, "inventory_artifact", {}),
);
return androidApplicationProjectionResultSchema.parse(
requireSuccessfulProjection(
projectAndroidApplicationEvidence({ inventory_evidence: [inventory] }),
).normalized_result,
);
};
it("says when the APK family rests only on the ZIP filename suffix", async () => {
expect((await projectApk({ "readme.txt": "hello" })).limitations).toContain(
missingManifest,
);
expect(
(await projectApk({ "AndroidManifest.xml": "manifest" })).limitations,
).not.toContain(missingManifest);
});
});
describe("case-distinct APK resource names", () => {
it("inventories and projects an APK whose resource names differ only in case", async () => {
const root = await createTestTempDirectory("rea-android-case-");