79 lines
3.3 KiB
TypeScript
79 lines
3.3 KiB
TypeScript
// @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(`<p>Hi</p><script>alert(1)</script><img src="x" onerror="alert(2)">`);
|
|
expect(html).not.toContain("<script");
|
|
expect(html).not.toContain("onerror");
|
|
expect(html).toContain("<p>Hi</p>");
|
|
});
|
|
|
|
it("neutralizes javascript: URLs", () => {
|
|
const { html } = sanitizeEmailHtml(`<a href="javascript:alert(1)">click</a>`);
|
|
expect(html).not.toContain("javascript:");
|
|
});
|
|
|
|
it("removes iframes, objects, forms and inputs (phishing surface)", () => {
|
|
const { html } = sanitizeEmailHtml(
|
|
`<iframe src="https://evil.example"></iframe><object data="x"></object><form action="https://evil.example"><input name="password"></form>`,
|
|
);
|
|
expect(html).not.toContain("<iframe");
|
|
expect(html).not.toContain("<object");
|
|
expect(html).not.toContain("<form");
|
|
expect(html).not.toContain("<input");
|
|
});
|
|
|
|
it("strips SVG script payloads", () => {
|
|
const { html } = sanitizeEmailHtml(`<svg><script>alert(1)</script></svg>`);
|
|
expect(html).not.toContain("script");
|
|
});
|
|
});
|
|
|
|
describe("sanitizeEmailHtml — external images (opt-in)", () => {
|
|
it("blocks external images by default and counts them", () => {
|
|
const { html, blockedImageCount } = sanitizeEmailHtml(
|
|
`<img src="https://tracker.example/pixel.png"><img src="//cdn.example/x.jpg"><img src="cid:inline-1">`,
|
|
);
|
|
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(`<img src="https://cdn.example/x.jpg">`, { 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(`<div style="background-image:url('https://t.example/p.gif');color:red">x</div><td background="https://t.example/bg.gif">y</td>`);
|
|
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(`<a href="https://kunde.de/angebot">Angebot</a>`);
|
|
expect(html).toContain('target="_blank"');
|
|
expect(html).toContain("noopener");
|
|
});
|
|
});
|
|
|
|
describe("renderPlainTextAsHtml", () => {
|
|
it("escapes html in plain-text bodies", () => {
|
|
const html = renderPlainTextAsHtml(`Hallo <script>alert(1)</script>\nZeile 2`);
|
|
expect(html).toContain("<script>");
|
|
expect(html).not.toContain("<script>");
|
|
expect(html).toContain("Zeile 2");
|
|
});
|
|
});
|