173 lines
6.2 KiB
TypeScript
173 lines
6.2 KiB
TypeScript
import { strToU8, zipSync } from "fflate";
|
|
import { describe, expect, it } from "vitest";
|
|
import { analyzePackage } from "../../src/package/analyze";
|
|
import { parseStaticXml } from "../../src/package/xml";
|
|
|
|
function archive(name: string, entries: Record<string, Uint8Array>): File {
|
|
const bytes = zipSync(entries, { level: 0 });
|
|
return new File([new Uint8Array(bytes).buffer], name, {
|
|
type: "application/zip",
|
|
});
|
|
}
|
|
|
|
describe("compound package analysis", () => {
|
|
it("detects OOXML and resolves local and external relationships", async () => {
|
|
const file = archive("sample.docx", {
|
|
"[Content_Types].xml": strToU8(
|
|
'<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"><Default Extension="xml" ContentType="application/xml"/><Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/></Types>',
|
|
),
|
|
"_rels/.rels": strToU8(
|
|
'<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="officeDocument" Target="word/document.xml"/><Relationship Id="rId2" Type="hyperlink" Target="https://example.test" TargetMode="External"/></Relationships>',
|
|
),
|
|
"word/document.xml": strToU8("<document/>"),
|
|
});
|
|
const result = await analyzePackage(file);
|
|
expect(result.kind).toBe("docx");
|
|
expect(result.relationships).toHaveLength(2);
|
|
expect(result.relationships[0]).toMatchObject({
|
|
mode: "internal",
|
|
resolvedPath: "word/document.xml",
|
|
exists: true,
|
|
});
|
|
expect(result.relationships[1]).toMatchObject({ mode: "external" });
|
|
expect(
|
|
result.diagnostics.some(
|
|
(item) => item.code === "MISSING_RELATIONSHIP_TARGET",
|
|
),
|
|
).toBe(false);
|
|
});
|
|
|
|
it("reports missing relationship targets and path collisions", async () => {
|
|
const file = archive("unsafe.docx", {
|
|
"[Content_Types].xml": strToU8("<Types/>"),
|
|
"_rels/.rels": strToU8(
|
|
'<Relationships><Relationship Id="x" Type="part" Target="missing.xml"/></Relationships>',
|
|
),
|
|
"Report.txt": strToU8("a"),
|
|
"report.TXT": strToU8("b"),
|
|
});
|
|
const result = await analyzePackage(file);
|
|
expect(
|
|
result.diagnostics.some(
|
|
(item) => item.code === "MISSING_RELATIONSHIP_TARGET",
|
|
),
|
|
).toBe(true);
|
|
expect(
|
|
result.diagnostics.filter((item) => item.code === "DUPLICATE_PATH"),
|
|
).toHaveLength(2);
|
|
});
|
|
|
|
it("detects EPUB metadata and missing manifest parts", async () => {
|
|
const file = archive("book.epub", {
|
|
mimetype: strToU8("application/epub+zip"),
|
|
"META-INF/container.xml": strToU8(
|
|
'<container><rootfiles><rootfile full-path="EPUB/package.opf"/></rootfiles></container>',
|
|
),
|
|
"EPUB/package.opf": strToU8(
|
|
'<package><metadata><title>Local book</title><creator>Ada</creator></metadata><manifest><item id="chapter" href="chapter.xhtml" media-type="application/xhtml+xml"/><item id="lost" href="lost.png" media-type="image/png"/></manifest><spine><itemref idref="chapter"/></spine></package>',
|
|
),
|
|
"EPUB/chapter.xhtml": strToU8("<html><body>hello</body></html>"),
|
|
});
|
|
const result = await analyzePackage(file);
|
|
expect(result.kind).toBe("epub");
|
|
expect(result.metadata).toContainEqual({
|
|
section: "EPUB",
|
|
name: "Title",
|
|
value: "Local book",
|
|
});
|
|
expect(result.diagnostics).toContainEqual(
|
|
expect.objectContaining({
|
|
code: "MISSING_MANIFEST_ITEM",
|
|
path: "EPUB/lost.png",
|
|
}),
|
|
);
|
|
});
|
|
|
|
it("adapts ODF and JAR manifests without executing their contents", async () => {
|
|
const odf = await analyzePackage(
|
|
archive("sheet.ods", {
|
|
mimetype: strToU8("application/vnd.oasis.opendocument.spreadsheet"),
|
|
"META-INF/manifest.xml": strToU8(
|
|
'<manifest><file-entry full-path="content.xml" media-type="text/xml"/><file-entry full-path="Pictures/missing.png" media-type="image/png"/></manifest>',
|
|
),
|
|
"content.xml": strToU8("<document-content/>"),
|
|
}),
|
|
);
|
|
expect(odf.kind).toBe("ods");
|
|
expect(odf.diagnostics).toContainEqual(
|
|
expect.objectContaining({
|
|
code: "MISSING_ODF_ENTRY",
|
|
path: "Pictures/missing.png",
|
|
}),
|
|
);
|
|
|
|
const jar = await analyzePackage(
|
|
archive("tool.jar", {
|
|
"META-INF/MANIFEST.MF": strToU8(
|
|
"Manifest-Version: 1.0\r\nMain-Class: example.Main\r\n",
|
|
),
|
|
"example/Main.class": new Uint8Array([0xca, 0xfe, 0xba, 0xbe]),
|
|
}),
|
|
);
|
|
expect(jar.kind).toBe("jar");
|
|
expect(jar.metadata).toContainEqual({
|
|
section: "JAR manifest",
|
|
name: "Main-Class",
|
|
value: "example.Main",
|
|
});
|
|
});
|
|
|
|
it("adapts APK and both WebExtension identities with bounded inventories", async () => {
|
|
const apk = await analyzePackage(
|
|
archive("app.apk", {
|
|
"AndroidManifest.xml": new Uint8Array([3, 0, 8, 0]),
|
|
"classes.dex": strToU8("dex\n035\0"),
|
|
"lib/arm64-v8a/liblocal.so": new Uint8Array([0x7f, 0x45, 0x4c, 0x46]),
|
|
"META-INF/LOCAL.RSA": new Uint8Array([1, 2, 3]),
|
|
}),
|
|
);
|
|
expect(apk.kind).toBe("apk");
|
|
expect(apk.signatures[0]?.format).toMatch(/APK\/JAR v1/iu);
|
|
|
|
const firefox = await analyzePackage(
|
|
archive("addon.xpi", {
|
|
"manifest.json": strToU8(
|
|
JSON.stringify({
|
|
manifest_version: 3,
|
|
name: "Local add-on",
|
|
version: "1.0.0",
|
|
browser_specific_settings: { gecko: { id: "local@example.test" } },
|
|
icons: { 48: "icon.png" },
|
|
}),
|
|
),
|
|
}),
|
|
);
|
|
expect(firefox.kind).toBe("firefox-extension");
|
|
expect(firefox.diagnostics).toContainEqual(
|
|
expect.objectContaining({
|
|
code: "MISSING_EXTENSION_ASSET",
|
|
path: "icon.png",
|
|
}),
|
|
);
|
|
|
|
const chrome = await analyzePackage(
|
|
archive("addon.zip", {
|
|
"manifest.json": strToU8(
|
|
JSON.stringify({
|
|
manifest_version: 3,
|
|
name: "Local extension",
|
|
version: "1.0.0",
|
|
}),
|
|
),
|
|
}),
|
|
);
|
|
expect(chrome.kind).toBe("chrome-extension");
|
|
});
|
|
|
|
it("rejects DTD/entity-bearing metadata", () => {
|
|
expect(() =>
|
|
parseStaticXml('<!DOCTYPE x [<!ENTITY y "boom">]><x>&y;</x>'),
|
|
).toThrow(/DTD and entity/iu);
|
|
});
|
|
});
|