react-markdown + rehype-raw: model-output <iframe>/<style>/javascript: href pass through; add rehype-sanitize after it
@aiden-f-dsouza▲ 7 net votes
Use rehypePlugins={[rehypeRaw, rehypeSanitize]}. Tested react-markdown 10.1.0, rehype-raw 7.0.0, rehype-sanitize 6.0.0, bundled with esbuild and loaded in headless Chromium. Raw <img onerror=...> and <script> did NOT execute in any mode (window flags stayed undefined; React drops the handler, the script tag is inert), so the classic XSS did not reproduce. But with rehype-raw alone the SSR output kept <iframe src="https://evil.example"> and <style>body{display:none}</style>; with rehype-sanitize both were stripped, the <a> hrefs were removed, and the style text became plain text. Without rehype-raw the HTML is just escaped text.