Release Package Tools 0.1.0
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
import { expect, test, type Page } from "@playwright/test";
|
||||
import { Buffer } from "node:buffer";
|
||||
import { strToU8, zipSync } from "fflate";
|
||||
|
||||
const ORIGIN = "http://127.0.0.1:4201";
|
||||
async function watchLocalOnly(page: Page) {
|
||||
const external: string[] = [];
|
||||
await page.route("**/*", async (route) => {
|
||||
const url = new URL(route.request().url());
|
||||
if (url.origin !== ORIGIN) {
|
||||
external.push(url.href);
|
||||
await route.abort();
|
||||
} else await route.continue();
|
||||
});
|
||||
return external;
|
||||
}
|
||||
|
||||
function docx(chapter = "hello") {
|
||||
return zipSync({
|
||||
"[Content_Types].xml": strToU8(
|
||||
'<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><Relationship Id="rId1" Type="officeDocument" Target="word/document.xml"/></Relationships>',
|
||||
),
|
||||
"word/document.xml": strToU8(`<document>${chapter}</document>`),
|
||||
"word/media/pixel.png": new Uint8Array([137, 80, 78, 71]),
|
||||
});
|
||||
}
|
||||
|
||||
test("runs at a nested path, stays local, and uses the shared width", async ({
|
||||
page,
|
||||
}) => {
|
||||
await page.setViewportSize({ width: 1800, height: 1000 });
|
||||
const external = await watchLocalOnly(page);
|
||||
await page.goto("/deep/nested/package/");
|
||||
await expect(
|
||||
page.getByRole("heading", { name: "Package Tools" }),
|
||||
).toBeVisible();
|
||||
expect(external).toEqual([]);
|
||||
expect(
|
||||
await page
|
||||
.locator(".toolbox-shell__main")
|
||||
.evaluate((node) => getComputedStyle(node).width),
|
||||
).toBe("1440px");
|
||||
});
|
||||
|
||||
test("inspects OOXML, navigates relationships and downloads an entry", async ({
|
||||
page,
|
||||
}) => {
|
||||
await page.goto("/deep/nested/package/");
|
||||
await page.getByTestId("package-input").setInputFiles({
|
||||
name: "sample.docx",
|
||||
mimeType:
|
||||
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
||||
buffer: Buffer.from(docx()),
|
||||
});
|
||||
await expect(page.getByText("1 package inspected locally.")).toBeVisible();
|
||||
await expect(
|
||||
page.getByRole("heading", { name: "OOXML document (DOCX)" }),
|
||||
).toBeVisible();
|
||||
await page.getByRole("button", { name: "Relationships" }).click();
|
||||
await expect(
|
||||
page.getByRole("cell", { name: "word/document.xml" }),
|
||||
).toBeVisible();
|
||||
await page.getByRole("button", { name: "Package tree" }).click();
|
||||
await page.getByRole("button", { name: "document.xml 26 B" }).click();
|
||||
await expect(page.getByText(/<document>hello<\/document>/u)).toBeVisible();
|
||||
const download = page.waitForEvent("download");
|
||||
await page.getByRole("button", { name: "Download verified entry" }).click();
|
||||
expect((await download).suggestedFilename()).toBe("document.xml");
|
||||
});
|
||||
|
||||
test("compares two local package inventories", async ({ page }) => {
|
||||
await page.goto("/deep/nested/package/");
|
||||
await page.getByTestId("package-input").setInputFiles([
|
||||
{
|
||||
name: "left.docx",
|
||||
mimeType: "application/zip",
|
||||
buffer: Buffer.from(docx("old")),
|
||||
},
|
||||
{
|
||||
name: "right.docx",
|
||||
mimeType: "application/zip",
|
||||
buffer: Buffer.from(docx("new and larger")),
|
||||
},
|
||||
]);
|
||||
await expect(page.getByText("2 packages inspected locally.")).toBeVisible();
|
||||
await expect(
|
||||
page.getByRole("heading", { name: "Compare packages" }),
|
||||
).toBeVisible();
|
||||
await expect(
|
||||
page.getByRole("row", { name: /changed word\/document\.xml/u }),
|
||||
).toBeVisible();
|
||||
});
|
||||
|
||||
test("integrates help, theme, PWA identity and hardened headers", async ({
|
||||
page,
|
||||
request,
|
||||
}) => {
|
||||
await page.goto("/deep/nested/package/");
|
||||
await page.getByRole("button", { name: "Help" }).click();
|
||||
await expect(
|
||||
page.getByRole("dialog", { name: "About Package Tools" }),
|
||||
).toContainText("inventoried—not validated");
|
||||
await page.keyboard.press("Escape");
|
||||
await page.getByRole("button", { name: "Personalize" }).click();
|
||||
await page.getByRole("button", { name: "Dark" }).click();
|
||||
await expect(page.locator(".toolbox-shell").first()).toHaveAttribute(
|
||||
"data-toolbox-theme",
|
||||
"dark",
|
||||
);
|
||||
expect(
|
||||
await page.evaluate(async () =>
|
||||
Boolean(await navigator.serviceWorker.ready),
|
||||
),
|
||||
).toBe(true);
|
||||
const index = await request.get("/deep/nested/package/");
|
||||
expect(index.headers()["content-security-policy"]).toContain(
|
||||
"connect-src 'self'",
|
||||
);
|
||||
expect(await index.text()).not.toMatch(/\b(?:src|href)=["']\//u);
|
||||
const manifest = await request.get("/deep/nested/package/toolbox-app.json");
|
||||
await expect(manifest.json()).resolves.toMatchObject({
|
||||
id: "de.add-ideas.package-tools",
|
||||
version: "0.1.0",
|
||||
privacy: { processing: "local", telemetry: false },
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,172 @@
|
||||
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);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,51 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { compareInventories } from "../../src/package/compare";
|
||||
import type { ArchiveEntryRecord } from "../../src/archive/types";
|
||||
import type { PackageDocument } from "../../src/package/types";
|
||||
|
||||
const entry = (
|
||||
path: string,
|
||||
size: number,
|
||||
crc32: string,
|
||||
): ArchiveEntryRecord => ({
|
||||
id: path,
|
||||
sourceIndex: 0,
|
||||
rawPath: path,
|
||||
path,
|
||||
collisionKey: path.toLowerCase(),
|
||||
kind: "file",
|
||||
size,
|
||||
compressedSize: size,
|
||||
crc32,
|
||||
extractable: true,
|
||||
issues: [],
|
||||
});
|
||||
|
||||
function document(entries: ArchiveEntryRecord[]): PackageDocument {
|
||||
return { entries } as PackageDocument;
|
||||
}
|
||||
|
||||
describe("inventory comparison", () => {
|
||||
it("separates unchanged, changed and one-sided entries", () => {
|
||||
const result = compareInventories(
|
||||
document([
|
||||
entry("same", 1, "a"),
|
||||
entry("changed", 2, "b"),
|
||||
entry("left", 1, "c"),
|
||||
]),
|
||||
document([
|
||||
entry("same", 1, "a"),
|
||||
entry("changed", 3, "d"),
|
||||
entry("right", 1, "e"),
|
||||
]),
|
||||
);
|
||||
expect(
|
||||
Object.fromEntries(result.map((item) => [item.path, item.status])),
|
||||
).toEqual({
|
||||
changed: "changed",
|
||||
left: "only-left",
|
||||
right: "only-right",
|
||||
same: "same",
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user