Cross-Site Scripting, CSP, and Clickjacking
A document title should appear as text, even when it contains angle brackets. Cross-site scripting (XSS) occurs when untrusted content is interpreted as executable page content.
On this page
Render text in its intended context
For ordinary text in the DOM, assign textContent or use framework text interpolation with automatic escaping. Stored XSS starts with saved data, reflected XSS uses data returned in a response, and DOM-based XSS arises in client-side processing. Rich HTML needs a maintained HTML sanitizer. For links, separately validate the URL scheme and destination.
const heading = document.createElement("h2");
heading.textContent = "Draft <April>";
document.body.append(heading);
// The page displays the literal text: Draft <April>
Limit scripts and frame embedding
This policy suits a page with external scripts from its own origin and no embedding. frame-ancestors prevents another page from framing it, reducing clickjacking, where a person is misled into clicking a disguised control. Test a policy with Content-Security-Policy-Report-Only before enforcement. For apps needing inline scripts, design a nonce- or hash-based policy. Send these headers on the HTML response.
Content-Security-Policy: default-src 'self'; script-src 'self'; object-src 'none'; base-uri 'none'; frame-ancestors 'none'
X-Content-Type-Options: nosniff
Referrer-Policy: strict-origin-when-cross-origin