Any diagram text/config that can originate from users (node labels, tooltips, markdown, theme/style tokens, and link targets) must be treated as untrusted. Apply context-appropriate escaping/sanitization and avoid security-by-regex.
Practical rules:
.html(...), sanitize the string (e.g., DOMPurify/rehype-sanitize) and/or ensure all metacharacters are escaped. If you only need plain text, prefer .text(...).url(...) in SVG/CSS, use CSS.escape() (or equivalent) instead of manual replace chains.javascript: in any “click”/link rendering path. Add tests that ensure unsafe schemes are rejected/neutralized.Example patterns:
tooltipEl.text(userProvidedTitle); // avoid .html for untrusted strings
const safeToken = CSS.escape(untrustedToken);
const css = `url(${safeToken})`;
function isValidHexColor(s: string) {
return /^#?([0-9a-f]{3}|[0-9a-f]{6})$/i.test(s);
}
if (theme.primaryColor && !isValidHexColor(theme.primaryColor)) throw new Error('Invalid color');
Enter the URL of a public GitHub repository