/** * The HTML sanitizer of the storefront: one implementation for the server and the browser, and for every runtime the * template is deployed to (Node, Vercel, Cloudflare Workers). * * Why not `isomorphic-dompurify`: on the server it loads jsdom, and jsdom reads `/browser/default-stylesheet.css` from * the file system when it loads. A Worker has no such file, so every page that sanitised merchant HTML answered 500. * DOMPurify with linkedom was tried and REJECTED: DOMPurify detects the unsupported DOM and returns its input * UNSANITISED (script, onerror and javascript: all survived), without an error. Never put DOMPurify on a DOM that is not * a full browser DOM. * * This module has no DOM dependency (htmlparser2 underneath), so server HTML equals client HTML (no hydration mismatch). * * The allow-lists are the ones the previous DOMPurify configuration had; `scripts/check-sanitizer.mjs` (run on every * publish of the CLI) feeds an XSS corpus through both entry points and fails if anything dangerous survives. */ import sanitizeHtmlLib from 'sanitize-html'; /** Embeds the merchant may include. An iframe survives only when its src starts with one of these. */ const TRUSTED_IFRAME_PREFIXES = [ 'https://www.youtube.com/embed/', 'https://www.youtube-nocookie.com/embed/', 'https://player.vimeo.com/video/', ]; const PRODUCT_TAGS = [ 'p', 'br', 'strong', 'b', 'em', 'i', 'u', 's', 'strike', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'ul', 'ol', 'li', 'a', 'blockquote', 'code', 'pre', 'div', 'span', 'img', 'table', 'thead', 'tbody', 'tr', 'th', 'td', 'colgroup', 'col', 'iframe', 'video', 'source', ]; // DOMPurify's default tag set plus iframe, which `sanitizeHtml` (rich text, pages, FAQ) allowed. const CONTENT_TAGS = [ ...PRODUCT_TAGS, 'abbr', 'address', 'article', 'aside', 'caption', 'center', 'cite', 'dd', 'del', 'details', 'dfn', 'dl', 'dt', 'figcaption', 'figure', 'footer', 'header', 'hr', 'ins', 'kbd', 'main', 'mark', 'nav', 'q', 'section', 'small', 'sub', 'summary', 'sup', 'tfoot', 'audio', ]; const PRODUCT_ATTRS = [ 'href', 'target', 'rel', 'class', 'src', 'alt', 'width', 'height', 'colspan', 'rowspan', 'allow', 'allowfullscreen', 'frameborder', 'loading', 'title', 'controls', 'poster', 'preload', 'muted', 'loop', 'playsinline', 'type', ]; const CONTENT_ATTRS = [...PRODUCT_ATTRS, 'id', 'dir', 'lang', 'scrolling', 'style']; // Inline styles survive only for these properties and only with plain values: rich text keeps its colours, alignment and // sizes, while position, url(), expression() and anything else is dropped. Product descriptions get no style attribute. const COLOR = [/^#[0-9a-f]{3,8}$/i, /^rgba?\([0-9\s,.%]+\)$/i, /^[a-z]{3,20}$/i]; const LENGTH = [/^-?[0-9]{1,4}(\.[0-9]{1,3})?(px|em|rem|%|pt)?$/]; const ALLOWED_STYLES = { '*': { color: COLOR, 'background-color': COLOR, 'text-align': [/^(left|right|center|justify|start|end)$/], direction: [/^(ltr|rtl)$/], 'font-size': LENGTH, 'line-height': LENGTH, 'font-weight': [/^(normal|bold|[1-9]00)$/], 'font-style': [/^(normal|italic)$/], 'text-decoration': [/^(none|underline|line-through)$/], margin: LENGTH, 'margin-top': LENGTH, 'margin-bottom': LENGTH, padding: LENGTH, width: LENGTH, height: LENGTH, }, }; function run(html: string, tags: string[], attrs: string[], demoteH1 = false): string { return sanitizeHtmlLib(html, { allowedTags: tags, allowedAttributes: { '*': attrs }, allowedSchemes: ['http', 'https', 'mailto', 'tel'], allowedSchemesByTag: { img: ['http', 'https', 'data'], source: ['http', 'https'], video: ['http', 'https'], }, allowedSchemesAppliedToAttributes: ['href', 'src', 'poster'], allowProtocolRelative: false, ...(attrs.includes('style') ? { allowedStyles: ALLOWED_STYLES } : {}), exclusiveFilter: (frame) => frame.tag === 'iframe' && !TRUSTED_IFRAME_PREFIXES.some((prefix) => (frame.attribs.src || '').startsWith(prefix)), transformTags: { // A product page already has one H1, the product name. A heading level 1 inside the merchant's description (pasted // from a word processor or another store) would make a second one, which search engines read as a page without a // clear title, so a description's H1 becomes an H2. Pages and rich text blocks keep theirs. ...(demoteH1 ? { h1: 'h2' } : {}), // A link that opens a new tab never gets the opener, and carries nofollow. a: (tagName, attribs) => ({ tagName, attribs: attribs.target === '_blank' ? { ...attribs, rel: 'noopener noreferrer nofollow' } : attribs, }), // A data: image must be an actual image type. A browser does not run HTML from an , but there is no reason to // let `data:text/html` (or anything else) through where only a picture belongs. img: (tagName, attribs) => { const src = attribs.src || ''; if (/^\s*data:/i.test(src) && !/^\s*data:image\/(png|jpe?g|gif|webp|avif);base64,/i.test(src)) { const { src: _dropped, ...rest } = attribs; return { tagName, attribs: rest }; } return { tagName, attribs }; }, }, }); } /** Product descriptions: the tighter list. */ export function sanitizeProduct(html: string): string { return html ? run(html, PRODUCT_TAGS, PRODUCT_ATTRS, true) : ''; } /** Rich text blocks, pages, FAQ answers: DOMPurify's default tag set plus iframe. */ export function sanitizeContent(html: string): string { return html ? run(html, CONTENT_TAGS, CONTENT_ATTRS) : ''; }