--- id: frontend-security version: "1.2.0" title: "Frontend Security" description: "Browser-side hardening: XSS, CSP, CORS, SRI, DOM clobbering, iframe sandboxing, Trusted Types" category: prevention severity: high applies_to: - "when generating HTML / JSX / Vue / Svelte templates" - "when wiring up response headers in a web app" - "when adding third-party script tags or CDN resources" languages: ["html", "javascript", "typescript", "tsx", "jsx", "vue", "svelte"] token_budget: minimal: 1000 compact: 1350 full: 2800 rules_path: "rules/" related_skills: ["cors-security", "auth-security", "logging-security"] last_updated: "2026-07-03" sources: - "OWASP XSS Prevention Cheat Sheet" - "OWASP Content Security Policy Cheat Sheet" - "CWE-79: Improper Neutralization of Input During Web Page Generation" - "MDN Trusted Types" --- # Frontend Security ## Rules (for AI agents) ### ALWAYS - Treat all user/URL/storage data as untrusted. Render via framework escaping (`{}` in JSX/Vue/Svelte, `{{ }}` in templating). For raw HTML use a vetted sanitizer (DOMPurify) with a strict allowlist. - Send a strict `Content-Security-Policy` header. Minimum production baseline: `default-src 'self'; script-src 'self' 'nonce-'; object-src 'none'; base-uri 'self'; frame-ancestors 'none'; form-action 'self'; upgrade-insecure-requests`. Use nonces or hashes — never `'unsafe-inline'` for `script-src`. - Set `Strict-Transport-Security: max-age=63072000; includeSubDomains; preload`, `X-Content-Type-Options: nosniff`, `Referrer-Policy: no-referrer-when-downgrade` or stricter, and `Permissions-Policy` to drop unused features. - Add `integrity="sha384-..." crossorigin="anonymous"` to every `