84 lines
3.6 KiB
TypeScript
84 lines
3.6 KiB
TypeScript
// VA-P23 · safe email-HTML rendering (M09 Block F — CSP-hardened container).
|
|
//
|
|
// Email bodies are UNTRUSTED third-party HTML — the classic stored-XSS vector.
|
|
// Sanitization = DOMPurify (isomorphic build: jsdom on the server, native DOM
|
|
// in the browser; team decision 2026-07-02) + an email-specific layer on top:
|
|
// - external images are BLOCKED by default (`src` moved to `data-hg-blocked-src`,
|
|
// the UI re-enables them via the "Bilder laden" opt-in → allowExternalImages)
|
|
// - inline `cid:`/`data:` images stay (no network fetch)
|
|
// - style url(...) references are stripped (CSS-based tracking pixels)
|
|
// - links are forced to target="_blank" rel="noopener noreferrer nofollow"
|
|
// - forms/inputs are removed entirely (no credential phishing surface)
|
|
|
|
import DOMPurify from "isomorphic-dompurify";
|
|
|
|
export interface SanitizedEmailHtml {
|
|
readonly html: string;
|
|
/** Number of external images that were blocked (drives the opt-in banner). */
|
|
readonly blockedImageCount: number;
|
|
}
|
|
|
|
const FORBIDDEN_TAGS = ["form", "input", "select", "textarea", "button", "iframe", "frame", "object", "embed", "base", "meta", "link"];
|
|
const EXTERNAL_SRC = /^(https?:)?\/\//i;
|
|
const CSS_URL_PATTERN = /url\s*\(/i;
|
|
|
|
let hooksInstalled = false;
|
|
let hookState: { allowExternalImages: boolean; blockedImageCount: number } = { allowExternalImages: false, blockedImageCount: 0 };
|
|
|
|
function ensureHooks(): void {
|
|
if (hooksInstalled) return;
|
|
hooksInstalled = true;
|
|
DOMPurify.addHook("afterSanitizeAttributes", (node) => {
|
|
const tag = node.tagName?.toLowerCase();
|
|
// Block external image loads unless the user opted in ("Bilder laden").
|
|
if (tag === "img") {
|
|
const src = node.getAttribute("src") ?? "";
|
|
if (EXTERNAL_SRC.test(src) && !hookState.allowExternalImages) {
|
|
node.setAttribute("data-hg-blocked-src", src);
|
|
node.removeAttribute("src");
|
|
node.setAttribute("alt", node.getAttribute("alt") ?? "");
|
|
hookState.blockedImageCount += 1;
|
|
}
|
|
node.removeAttribute("srcset");
|
|
node.removeAttribute("loading");
|
|
}
|
|
// Styles: no url(...) (CSS tracking pixels / exfil).
|
|
const style = node.getAttribute?.("style");
|
|
if (style && CSS_URL_PATTERN.test(style)) {
|
|
node.setAttribute("style", style.replace(/url\s*\([^)]*\)/gi, "none"));
|
|
}
|
|
if (node.hasAttribute?.("background")) {
|
|
node.removeAttribute("background");
|
|
}
|
|
// Links open sandboxed in a new tab.
|
|
if (tag === "a") {
|
|
node.setAttribute("target", "_blank");
|
|
node.setAttribute("rel", "noopener noreferrer nofollow");
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Sanitize an email HTML body for rendering. NOT idempotent state-wise — the
|
|
* blocked-image counter is per call (module-level hook state, single-threaded
|
|
* per request in Node).
|
|
*/
|
|
export function sanitizeEmailHtml(html: string, options: { allowExternalImages?: boolean } = {}): SanitizedEmailHtml {
|
|
ensureHooks();
|
|
hookState = { allowExternalImages: options.allowExternalImages ?? false, blockedImageCount: 0 };
|
|
const clean = DOMPurify.sanitize(html, {
|
|
USE_PROFILES: { html: true },
|
|
FORBID_TAGS: FORBIDDEN_TAGS,
|
|
FORBID_ATTR: ["onerror", "onload", "onclick", "formaction", "ping"],
|
|
ALLOW_DATA_ATTR: false,
|
|
ADD_ATTR: ["data-hg-blocked-src", "target", "rel"],
|
|
});
|
|
return { html: clean, blockedImageCount: hookState.blockedImageCount };
|
|
}
|
|
|
|
/** Plain-text fallback rendering (bodyText mails) — escaped, newlines kept. */
|
|
export function renderPlainTextAsHtml(text: string): string {
|
|
const escaped = text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
return `<pre style="white-space:pre-wrap;font-family:inherit">${escaped}</pre>`;
|
|
}
|