// @vitest-environment jsdom
import { describe, expect, it } from "vitest";
import { renderPlainTextAsHtml, sanitizeEmailHtml } from "./emailHtmlSanitizer.server";
// 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", async () => {
const { html } = await 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", async () => {
const { html, blockedImageCount } = await 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("blocks browser-normalized backslash, one-slash and relative tracking URLs", async () => {
const { html, blockedImageCount } = await sanitizeEmailHtml(
String.raw`

`
);
expect(blockedImageCount).toBe(3);
expect(html).not.toMatch(/\ssrc=/);
expect(html.match(/data-hg-blocked-src=/g)).toHaveLength(3);
});
it("keeps external images when the user opted in", async () => {
const { html, blockedImageCount } = await sanitizeEmailHtml(`
`, { allowExternalImages: true });
expect(blockedImageCount).toBe(0);
expect(html).toContain('src="https://cdn.example/x.jpg"');
});
it("removes all sender-controlled CSS and legacy background attributes", async () => {
const { html } = await sanitizeEmailHtml(
`x
y | `
);
expect(html).not.toContain("t.example");
expect(html).not.toContain("style=");
expect(html).not.toContain("background=");
});
it("removes CSS tracking variants that evade url(...) regexes", async () => {
const { html } = await sanitizeEmailHtml(`
x
y
`);
expect(html).not.toContain("tracker.example");
expect(html).not.toContain("