Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/modules/emailInbox/services/emailHtmlSanitizer.test.ts
T

112 lines
4.8 KiB
TypeScript

// @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(`<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", async () => {
const { html } = await sanitizeEmailHtml(`<a href="javascript:alert(1)">click</a>`);
expect(html).not.toContain("javascript:");
});
it("removes iframes, objects, forms and inputs (phishing surface)", async () => {
const { html } = await 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("removes auto-loading media elements", async () => {
const { html } = await sanitizeEmailHtml(
`<video autoplay poster="https://tracker.example/poster"><source src="https://tracker.example/video"></video><audio autoplay src="https://tracker.example/audio"></audio>`
);
expect(html).not.toContain("tracker.example");
expect(html).not.toMatch(/<(?:video|audio|source)/i);
});
it("strips SVG script payloads", async () => {
const { html } = await 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", async () => {
const { html, blockedImageCount } = await 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("blocks browser-normalized backslash, one-slash and relative tracking URLs", async () => {
const { html, blockedImageCount } = await sanitizeEmailHtml(
String.raw`<img src="https:\\tracker.example\pixel.png"><img src="http:/tracker.example/one.png"><img src="/open/pixel.png">`
);
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(`<img src="https://cdn.example/x.jpg">`, { 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(
`<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).not.toContain("style=");
expect(html).not.toContain("background=");
});
it("removes CSS tracking variants that evade url(...) regexes", async () => {
const { html } = await sanitizeEmailHtml(`
<style>@import "https://tracker.example/import.css"</style>
<div style="background: image-set('https://tracker.example/a' 1x); list-style: \\75rl(https://tracker.example/b)">x</div>
<span style="--payload: url(https://tracker.example/c); color: red">y</span>
`);
expect(html).not.toContain("tracker.example");
expect(html).not.toContain("<style");
expect(html).not.toContain("style=");
});
});
describe("sanitizeEmailHtml — link hardening", () => {
it("forces target=_blank + noopener on links", async () => {
const { html } = await 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("&lt;script&gt;");
expect(html).not.toContain("<script>");
expect(html).toContain("Zeile 2");
});
});