// @vitest-environment jsdom
import { describe, expect, it } from "vitest";
import { renderPlainTextAsHtml, sanitizeEmailHtml } from "./emailHtmlSanitizer";
// VA-P23 — the XSS fixtures for the email body renderer (M09 Block F). Email
// HTML is untrusted third-party input; every vector here MUST stay neutralized.
describe("sanitizeEmailHtml — XSS vectors", () => {
it("strips script tags and inline event handlers", () => {
const { html } = sanitizeEmailHtml(`
Hi
`);
expect(html).not.toContain("`);
expect(html).not.toContain("script");
});
});
describe("sanitizeEmailHtml — external images (opt-in)", () => {
it("blocks external images by default and counts them", () => {
const { html, blockedImageCount } = sanitizeEmailHtml(
`

`,
);
expect(blockedImageCount).toBe(2);
// No live `src` may point at the network (`data-hg-blocked-src` is inert).
expect(html).not.toMatch(/\ssrc="(https?:)?\/\//);
expect(html).toContain('data-hg-blocked-src="https://tracker.example/pixel.png"');
expect(html).toContain('src="cid:inline-1"');
});
it("keeps external images when the user opted in", () => {
const { html, blockedImageCount } = sanitizeEmailHtml(`
`, { allowExternalImages: true });
expect(blockedImageCount).toBe(0);
expect(html).toContain('src="https://cdn.example/x.jpg"');
});
it("strips css url(...) tracking (style + background attr)", () => {
const { html } = sanitizeEmailHtml(`x
y | `);
expect(html).not.toContain("t.example");
expect(html).toContain("color:red");
});
});
describe("sanitizeEmailHtml — link hardening", () => {
it("forces target=_blank + noopener on links", () => {
const { html } = sanitizeEmailHtml(`Angebot`);
expect(html).toContain('target="_blank"');
expect(html).toContain("noopener");
});
});
describe("renderPlainTextAsHtml", () => {
it("escapes html in plain-text bodies", () => {
const html = renderPlainTextAsHtml(`Hallo \nZeile 2`);
expect(html).toContain("<script>");
expect(html).not.toContain("