Render untrusted text safely
Render data as text by default
Cross-site scripting (XSS) occurs when untrusted data is interpreted as executable content in a browser. React normally treats strings in JSX children as text, not HTML markup. This protects that rendering context but does not make arbitrary URLs, HTML sinks, scripts or third-party components safe. Data from a database, API or another user remains untrusted for the context in which it is used. Use a simple text display when rich HTML is unnecessary.
import React from 'react';export default function RequestNote() { const note = '<strong>Handle gently</strong>'; return <p>{note}</p>;}The visitor should see the literal angle brackets and the words Handle gently, not a bold element. The example uses inert markup to distinguish text from parsed HTML. The dangerous HTML escape hatch, dangerouslySetInnerHTML, requests raw HTML insertion; never pass untrusted input directly. When a product genuinely needs formatted user content, select a maintained vetted HTML sanitizer, configure the smallest permitted tag/attribute set, review URL schemes and test its exact version. Do not write a regex sanitizer or modify sanitized markup with unreviewed code. Sanitization and context-sensitive output handling are different operations.
Constrain a link to the intended destination
const origin = 'https://desk.example';export function safeHelpLink(value) { if (typeof value !== 'string' || !value.trim()) return null; try { const url = new URL(value, origin); if (url.protocol !== 'https:' || url.origin !== origin || url.username || url.password) return null; if (!['/help', '/help/loans'].includes(url.pathname) || url.search || url.hash) return null; return url.href; } catch { return null; }}This deliberately narrow policy permits only two fictional help pages, with no query or fragment. URL parsing and canonical origin comparison matter more than substring matching. This is not a general-purpose URL sanitizer or a guarantee about server redirects; the approved endpoints must also avoid unsafe redirects. In a real component use the returned URL only when non-null, otherwise show plain text or no link. Use a trusted base from configuration, never a user-supplied base.
Add browser enforcement as defense in depth
export const productionHeaders = { 'Content-Security-Policy': "default-src 'self'; script-src 'self'; object-src 'none'; base-uri 'none'; frame-ancestors 'none'; form-action 'self'; connect-src 'self'", 'X-Frame-Options': 'DENY', 'Referrer-Policy': 'no-referrer', 'X-Content-Type-Options': 'nosniff'};This is a static candidate for a small production SPA with self-hosted scripts, styles and assets; it is not applied by React and has not been tested with a bundler. Send policy as HTTP response headers. A strict CSP may need per-response nonces or hashes depending on the integration; do not casually enable unsafe inline execution to fix violations. Start with report-only where appropriate, inspect reports privately and test enforcement on the real deployment. frame-ancestors restricts embedding and helps prevent clickjacking; it is not supported in a CSP meta tag. Trusted Types can require trusted objects at supported DOM injection sinks and restrict policy creation, but the policy itself must transform content safely. Check browser and React/sanitizer integration before enforcement; no universal support claim. CSP is additional protection, not a substitute for safe rendering. Practice: test two approved help paths and two rejected destinations using fictional URLs, then explain why CSP cannot make a missing server permission check safe.