Release Mail Tools 0.1.0

This commit is contained in:
2026-09-01 12:39:23 +02:00
commit 9bc0870404
63 changed files with 9850 additions and 0 deletions
+134
View File
@@ -0,0 +1,134 @@
import { expect, test, type Page } from "@playwright/test";
import { Buffer } from "node:buffer";
import { readFile } from "node:fs/promises";
const ORIGIN = "http://127.0.0.1:4195";
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 errors(page: Page) {
const values: string[] = [];
page.on("pageerror", (error) => values.push(error.message));
page.on("console", (message) => {
if (message.type() === "error") values.push(message.text());
});
return values;
}
test("runs from a nested path and keeps HTML preview local and inert", async ({
page,
}) => {
await page.setViewportSize({ width: 1800, height: 1000 });
const external = await watchLocalOnly(page);
const failures = errors(page);
await page.goto("/deep/nested/mail/");
await expect(page.getByRole("heading", { name: "Mail Tools" })).toBeVisible();
await expect(page.getByText(/Parsed 5 MIME parts/iu)).toBeVisible();
await page.getByRole("button", { name: "Bodies" }).click();
await page.getByLabel("Left body part").selectOption("1.2");
const frame = page.frameLocator(
'iframe[title="Sanitized HTML message body"]',
);
await expect(frame.getByText("Hello review team.")).toBeVisible();
expect(await page.locator("iframe").getAttribute("sandbox")).toBe("");
expect(await page.locator("iframe").getAttribute("srcdoc")).not.toContain(
"tracker.invalid",
);
expect(external).toEqual([]);
expect(
failures.filter(
(message) =>
!message.includes(
"Blocked script execution in 'about:srcdoc' because the document's frame is sandboxed",
),
),
).toEqual([]);
expect(
await page
.locator(".toolbox-shell__main")
.evaluate((node) => getComputedStyle(node).width),
).toBe("1440px");
});
test("opens an EML and downloads decoded attachment bytes", async ({
page,
}) => {
await page.goto("/deep/nested/mail/");
const source =
"From: Test <test@example.test>\r\nDate: Tue, 01 Sep 2026 10:00:00 +0000\r\nContent-Type: multipart/mixed; boundary=x\r\n\r\n--x\r\nContent-Type: application/octet-stream\r\nContent-Disposition: attachment; filename=proof.bin\r\nContent-Transfer-Encoding: base64\r\n\r\nAAEC/w==\r\n--x--";
await page.getByTestId("mail-file-input").setInputFiles({
name: "fixture.eml",
mimeType: "message/rfc822",
buffer: Buffer.from(source),
});
await expect(page.getByText(/Parsed 2 MIME parts/iu)).toBeVisible();
await page.getByRole("button", { name: "Attachments" }).click();
await expect(page.getByText("proof.bin", { exact: true })).toBeVisible();
const pending = page.waitForEvent("download");
await page.getByRole("button", { name: "Download" }).click();
const download = await pending;
expect(download.suggestedFilename()).toBe("proof.bin");
const saved = await download.path();
expect(saved).not.toBeNull();
expect(await readFile(saved!)).toEqual(Buffer.from([0, 1, 2, 255]));
});
test("retains the last model and creates canonical and redacted exports", async ({
page,
}) => {
await page.goto("/deep/nested/mail/");
await page.getByLabel("EML source").fill("not a header");
await page.getByRole("button", { name: "Inspect message" }).click();
await expect(page.getByRole("alert")).toContainText("last successful");
await page.getByRole("button", { name: "Export" }).click();
for (const [button, filename] of [
["Download canonical EML", "canonical.eml"],
["Download redacted EML", "redacted.eml"],
["Download report", "redaction-report.json"],
] as const) {
const pending = page.waitForEvent("download");
await page.getByRole("button", { name: button }).click();
expect((await pending).suggestedFilename()).toBe(filename);
}
});
test("integrates help, dark theme, PWA identity and hardened headers", async ({
page,
request,
}) => {
await page.goto("/deep/nested/mail/");
await page.getByRole("button", { name: "Help" }).click();
await expect(
page.getByRole("dialog", { name: "About Mail Tools" }),
).toContainText("opaque sandbox");
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",
);
const registration = await page.evaluate(async () =>
Boolean(await navigator.serviceWorker.ready),
);
expect(registration).toBe(true);
const index = await request.get("/deep/nested/mail/");
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/mail/toolbox-app.json");
await expect(manifest.json()).resolves.toMatchObject({
id: "de.add-ideas.mail-tools",
version: "0.1.0",
privacy: { processing: "local", telemetry: false },
});
});
+37
View File
@@ -0,0 +1,37 @@
import { render, screen } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import { describe, expect, it } from "vitest";
import { Workbench } from "../../src/components/Workbench";
describe("Mail workbench", () => {
it("shows parsed structure and retains it after an invalid edit", async () => {
const user = userEvent.setup();
render(<Workbench />);
expect(
screen.getByRole("heading", { name: "Mail Tools" }),
).toBeInTheDocument();
expect(screen.getByText(/Parsed 5 MIME parts/iu)).toBeInTheDocument();
await user.clear(screen.getByLabelText("EML source"));
await user.type(screen.getByLabelText("EML source"), "bad header");
await user.click(screen.getByRole("button", { name: "Inspect message" }));
expect(screen.getByRole("alert")).toHaveTextContent(/last successful/iu);
expect(
screen.getAllByText("multipart/mixed", { exact: false }),
).not.toHaveLength(0);
});
it("moves through body, attachment and diagnostic views", async () => {
const user = userEvent.setup();
render(<Workbench />);
await user.click(screen.getByRole("button", { name: "Bodies" }));
expect(
screen.getByRole("heading", { name: "Body-part comparison" }),
).toBeInTheDocument();
await user.click(screen.getByRole("button", { name: "Attachments" }));
expect(screen.getByText("notes.txt", { exact: true })).toBeInTheDocument();
await user.click(screen.getByRole("button", { name: "Diagnostics" }));
expect(
screen.getByText(/Authentication-Results claims/iu),
).toBeInTheDocument();
});
});
+94
View File
@@ -0,0 +1,94 @@
import { describe, expect, it } from "vitest";
import {
attachments,
decodeHeaderValue,
parseHeaders,
parseMessage,
walkParts,
} from "../../src/core/mime";
const MULTIPART = `From: =?UTF-8?Q?Ada_=E2=9C=93?= <ada@example.test>\r
Subject: folded\r
value\r
MIME-Version: 1.0\r
Content-Type: multipart/mixed; boundary="b"\r
\r
--b\r
Content-Type: text/plain; charset=utf-8\r
Content-Transfer-Encoding: quoted-printable\r
\r
hello=20world\r
--b\r
Content-Type: application/octet-stream\r
Content-Disposition: attachment; filename*=UTF-8''report%20one.txt\r
Content-Transfer-Encoding: base64\r
\r
YWJj\r
--b--`;
describe("MIME parser", () => {
it("unfolds fields and decodes RFC 2047 words", () => {
const headers = parseHeaders(
"Subject: =?UTF-8?Q?Hello_=E2=9C=93?=\r\n\tworld",
);
expect(headers[0]?.value).toBe("Hello ✓ world");
expect(decodeHeaderValue("=?ISO-8859-1?Q?Andr=E9?=")).toBe("André");
expect(decodeHeaderValue("=?UTF-8?Q?joined?= =?UTF-8?Q?_words?=")).toBe(
"joined words",
);
});
it("builds nested parts and decodes transfer encodings", () => {
const parsed = parseMessage(MULTIPART);
expect(walkParts(parsed.root).map((part) => part.mediaType)).toEqual([
"multipart/mixed",
"text/plain",
"application/octet-stream",
]);
expect(parsed.root.children[0]?.text).toBe("hello world");
expect([...parsed.root.children[1]!.bytes]).toEqual([97, 98, 99]);
expect(attachments(parsed.root)[0]).toMatchObject({
filename: "report one.txt",
size: 3,
});
});
it("parses nested message/rfc822 entities", () => {
const parsed = parseMessage(
"From: a@example.test\nContent-Type: message/rfc822\n\nFrom: b@example.test\nContent-Type: text/plain\n\nnested",
);
expect(parsed.root.children[0]?.text).toBe("nested");
});
it("decodes and exposes transferred message attachments", () => {
const nested = btoa(
"From: b@example.test\r\nContent-Type: text/plain\r\n\r\nnested",
);
const parsed = parseMessage(
`From: a@example.test\nContent-Type: message/rfc822; name="forwarded.eml"\nContent-Disposition: attachment; filename="forwarded.eml"\nContent-Transfer-Encoding: base64\n\n${nested}`,
);
expect(parsed.root.children[0]?.text).toBe("nested");
expect(attachments(parsed.root)[0]).toMatchObject({
filename: "forwarded.eml",
});
expect(attachments(parsed.root)[0]?.size).toBeGreaterThan(0);
});
it("fails closed for malformed and excessive structures", () => {
expect(() => parseMessage("Bad header\n\nbody")).toThrow(
/malformed header/iu,
);
expect(() =>
parseMessage("Content-Type: multipart/mixed; boundary=x\n\nnone"),
).toThrow(/boundary/iu);
expect(() =>
parseMessage("From: a@example.test\n\n12345", {
maxChars: 4,
maxHeaders: 2,
maxParts: 2,
maxDepth: 2,
maxDecodedBytes: 8,
}),
).toThrow(/limit/iu);
});
});
+60
View File
@@ -0,0 +1,60 @@
import { describe, expect, it } from "vitest";
import { compareBodies } from "../../src/core/compare";
import { diagnoseMessage } from "../../src/core/diagnostics";
import { canonicalMessage, redactMessage } from "../../src/core/export";
import { parseMessage } from "../../src/core/mime";
import { sanitizeMailHtml } from "../../src/core/sanitize";
describe("inert rendering and output", () => {
it("removes active and remote HTML while embedding a deny-all policy", () => {
const output = sanitizeMailHtml(
'<script>alert(1)</script><a href="https://bad.test">link</a><img src="https://bad.test/pixel"><p style="background:url(https://bad.test)">safe</p>',
);
expect(output).toContain("default-src 'none'");
expect(output).toContain("safe");
expect(output).not.toMatch(
/bad\.test|script|href=|src=|style="background/iu,
);
});
it("produces a bounded line comparison", () => {
expect(compareBodies("a\nb", "a\nc")).toMatchObject({
added: 1,
removed: 1,
equal: false,
});
expect(compareBodies("a\nb", "a\nb")).toMatchObject({ equal: true });
expect(compareBodies("a\nb\nc", "d\ne\nf", 2).truncated).toBe(true);
});
it("labels authentication fields as claims", () => {
const parsed = parseMessage(
"From: Ada <ada@example.test>\nDate: Tue, 01 Sep 2026 10:00:00 +0000\nAuthentication-Results: mx.test; spf=pass; dkim=fail\nDKIM-Signature: v=1; d=example.test; s=mail; a=rsa-sha256\n\nbody",
);
const diagnostics = diagnoseMessage(parsed);
expect(
diagnostics.some(
(item) => item.code === "auth.claim" && /spf, dkim/u.test(item.summary),
),
).toBe(true);
expect(
diagnostics.find((item) => item.code === "dkim.signature")?.detail,
).toMatch(/not cryptographically verified/iu);
});
it("normalizes canonical output and reports focused redaction", () => {
const parsed = parseMessage(
"From: a@example.test\nReceived: from private.example\nAuthentication-Results: mx; spf=pass\nSubject: hello\n\nline1\nline2",
);
expect(canonicalMessage(parsed)).toContain(
"From: a@example.test\r\nReceived:",
);
const redacted = redactMessage(parsed);
expect(redacted.output).not.toMatch(/Received|Authentication-Results/iu);
expect(redacted.output).toContain("Subject: hello\r\n\r\nline1\r\nline2");
expect(JSON.parse(redacted.report)).toMatchObject({
operation: "mail-header-redaction",
retainedHeaderCount: 2,
});
});
});