Help us improve
Share bugs, ideas, or general feedback.
From grimoire
Eliminates XSS by context-aware output encoding and content security policies. Trigger when rendering user content in HTML/DOM or building browser-rendered APIs.
npx claudepluginhub jeffreytse/grimoire --plugin grimoireHow this skill is triggered — by the user, by Claude, or both
Slash command
/grimoire:prevent-xssThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
Eliminate cross-site scripting by context-aware output encoding and a strict Content Security Policy — never trust user-supplied content as markup.
Prevents XSS attacks via input sanitization, output encoding, CSP headers, DOMPurify, and safe DOM APIs. Use for user-generated content, rich text editors, comments, and dynamic HTML.
Block script injection by encoding output, sanitizing HTML, and enforcing Content Security Policy.
Implement context-specific output encoding to prevent XSS and injection attacks. Encode HTML, URL, JavaScript, and other contexts appropriately.
Share bugs, ideas, or general feedback.
Eliminate cross-site scripting by context-aware output encoding and a strict Content Security Policy — never trust user-supplied content as markup.
Adopted by: Mandated by PCI DSS v4.0 Requirement 6.2.4 and OWASP Top 10 2021 (A03:Injection). Google, Meta, and GitHub all use context-aware escaping combined with CSP. React, Vue, Angular, and Svelte escape by default for text content. OWASP lists XSS as one of the most prevalent web vulnerabilities with over 20,000 CVEs. Impact: XSS enables session hijacking, credential theft, and full account takeover without any server-side vulnerability. Google's Bug Bounty program pays up to $31,337 for stored XSS. OWASP estimates XSS affects two-thirds of all web applications. Consistent output encoding reduces XSS to near-zero as a vulnerability class. Why best: Input sanitization (stripping tags on input) is the alternative — it fails to handle all encoding variations and breaks legitimate content. Output encoding at render time is context-specific and structurally correct regardless of how data was stored.
Sources: OWASP XSS Prevention Cheat Sheet; CWE-79; Google Bug Bounty data; OWASP Top 10 2021
Use a framework that auto-escapes by default — React ({} expressions), Vue ({{ }}), Angular ({{ }}), and Jinja2 ({{ }}) escape HTML entities automatically. Never use dangerouslySetInnerHTML, v-html, [innerHTML], or | safe unless the content is provably safe.
When rendering outside a framework, encode for context:
&, <, >, ", ' as HTML entities.&#xHH;.<script> tags using \uXXXX escaping; prefer data-* attributes over inline scripts.href, src, or query strings; validate that URLs start with https:// not javascript:.Never trust innerHTML, document.write, or eval with user data — use textContent for plain text, createElement/setAttribute for DOM construction.
// BAD
element.innerHTML = userInput;
// GOOD
element.textContent = userInput;
// GOOD — building elements safely
const a = document.createElement('a');
a.href = sanitizedUrl; // validate URL scheme first
a.textContent = userInput;
Deploy a Content Security Policy — see apply-content-security-policy. CSP is defense-in-depth: it limits the damage of any XSS that slips through by blocking script execution from unexpected origins.
For rich-text user content (Markdown, HTML editors) — use a dedicated sanitization library on the output side, not input side. Allowlist-based: DOMPurify (browser), bleach (Python), sanitize-html (Node). Never build your own HTML parser.
import DOMPurify from 'dompurify';
element.innerHTML = DOMPurify.sanitize(userHtml, { ALLOWED_TAGS: ['b', 'i', 'a'] });
Set HttpOnly and Secure flags on session cookies — HttpOnly prevents JavaScript from reading the cookie even if XSS occurs, limiting the blast radius.
Content-Type: application/json (not text/html) to prevent MIME sniffing and JSON injection.location.hash, document.referrer, or postMessage and injects it into the DOM — treat these as untrusted sources.<script> block — JavaScript context requires JavaScript string escaping, not HTML entity encoding.href values — javascript:alert(1) is a valid URL. Always validate URL scheme.