# Web and frontend security Apply these rules when changing browser code, cookies, redirects, or defenses against XSS and CSRF. * Escape or sanitize untrusted content; never inject untrusted HTML. Bypass framework sanitization (for example Angular `bypassSecurityTrust*` or React `dangerouslySetInnerHTML`) only for compile-time constants, developer-controlled values, or the output of a maintained sanitizer, and state the reason in a comment at the call site. * Encode output for its exact context (HTML, attributes, JavaScript, CSS, or URL). Prefer text APIs and framework-safe templates; use a maintained sanitizer only when rich HTML is required. * Protect state-changing browser requests from CSRF where applicable; validate origin or anti-CSRF tokens and do not treat CORS as authorization. * Use `HttpOnly`, `Secure`, and appropriate `SameSite` cookie settings for sensitive sessions; session rotation follows the authentication rule. * Validate redirect targets against an allowlist. Avoid sensitive data in URLs, referrers, browser history, analytics, and error reports. * Apply a restrictive Content Security Policy and clickjacking protection (`frame-ancestors`) where applicable. Do not weaken security headers to accommodate unsafe inline content without a reviewed design. * Treat frontend code, source maps, and browser storage as visible, and never rely on client-side checks for authorization. * Never place server secrets (API keys, client secrets, signing or encryption keys, connection strings) in frontend code, bundles, public runtime configuration, or browser storage. * For user sessions in browser apps, prefer `HttpOnly` cookies or a backend-for-frontend (BFF) that keeps OAuth tokens on the server. If the browser app must hold tokens, keep access tokens in memory and short-lived, use refresh-token rotation, and never store refresh tokens in `localStorage` or `sessionStorage` (RFC 10017, OAuth 2.0 for Browser-Based Applications). This is the single rule for browser token handling; language and framework rules defer to it. * Protect state-changing operations against unintended form submissions; require explicit confirmation for high-impact actions. * Keep dependencies and third-party scripts minimal. Apply integrity and origin controls to externally hosted resources where supported.