Skip to content

Security ​

React escapes text children ({comment.body}) automatically, and React 19 blocks javascript: URLs in href/src—leaving dangerouslySetInnerHTML as the primary template XSS sink.

In short

  • Prefer plain text children: render untrusted strings directly instead of using dangerouslySetInnerHTML (react-dangerous-html).
  • Sanitize with proof types: when rich HTML is required, sanitize it through defineSanitizer so the value carries the Html brand.

Unguarded dangerouslySetInnerHTML is rejected statically (react-dangerous-html):

tsx
type Comment = { readonly author: string; readonly body: string };

export function CommentView({ comment }: { comment: Comment }) {
  return <div dangerouslySetInnerHTML={{ __html: comment.body }} />;
}

✅ Accepted: render it as text

✓ Accepted0 diagnostics
tsx
type Comment = { readonly author: string; readonly body: string };

export function CommentView({ comment }: { comment: Comment }) {
  return <p>{comment.body}</p>;
}

When it must be HTML (a rich-text field, Markdown rendered on the server), pass it through the project's sanitizer, declared once with defineSanitizer (from @evobug_com/safe-ts-std/html). What it returns is an Html, which the rule accepts by type: kept in a const, passed as a prop typed Html, or chosen in a ?: (Proof types):

✓ Accepted0 diagnostics
tsx
import DOMPurify from "dompurify";
import { defineSanitizer } from "@evobug_com/safe-ts-std/html";

const sanitizeHtml = defineSanitizer((raw) => DOMPurify.sanitize(raw));

export function Article({ html }: { html: string }) {
  const clean = sanitizeHtml(html);
  return <div dangerouslySetInnerHTML={{ __html: clean }} />;
}

✅ Also accepted: a known sanitizer called in the prop itself, without defineSanitizer

✓ Accepted0 diagnostics
tsx
import DOMPurify from "dompurify";

export function Article({ html }: { html: string }) {
  return <div dangerouslySetInnerHTML={{ __html: DOMPurify.sanitize(html) }} />;
}

A function of yours is a sanitizer when every return of it is a known sanitizer's result, or when it is declared with defineSanitizer; its name doesn't make it one. These are the values every markup sink accepts, innerHTML and v-html too (markup-sink).

URLs ​

React 19 replaces a javascript: URL in href, src, action or formAction with one that throws when followed, so <a href={user.website}> can't run script. React 18 only warns in development: there, check URLs that come from users with safeHref(url) from @evobug_com/safe-ts-std/url, which keeps relative, http:, https:, mailto: and tel: URLs and gives "#" for anything else. SAFE-TS has no React rule for a URL in JSX; one set in code (link.href = url, window.open(url), location.assign(url)) is url-sink's.

Rules on this page: react-dangerous-html

MIT OR Apache-2.0 · Docs by Gemini 4 Neon Extra