On this page
CSP, Cookies & XSS/CSRF Mitigations
Prev: Same-Origin Policy & CORS | Up: security | Next: tutorials
Rel: Nodes & Elements, Storage & State, Forms, Validation & Fetch
Ref: MDN Web Docs — Content Security Policy (CSP)
Tags: #public #web #platform #security #csp #cookies
Contents
Content Security Policy (CSP)
CSP restricts where scripts, styles, images, and network connections may load from, acting as a strong defense-in-depth layer against script injection:
Content-Security-Policy: default-src 'self'; script-src 'self'; style-src 'self'; object-src 'none'; base-uri 'self'; frame-ancestors 'none'
Hardened Session Cookies
Set-Cookie: session_id=opaque_token; HttpOnly; Secure; SameSite=Lax; Path=/
HttpOnly: Preventsdocument.cookiefrom reading the token in JavaScript.Secure: Transmits the cookie only over HTTPS.SameSite=Lax(orStrict): Blocks cross-site POST requests from automatically attaching the cookie.
Preventing XSS & CSRF
- XSS: Never interpolate untrusted data into
innerHTMLor HTML strings without escaping; prefertextContent,createElement, and server-sidehtml.escape(). - CSRF: Combine
SameSite=Lax|Strictcookies with anti-CSRF synchronizer tokens (or custom request headers checked server-side) on all state-changing endpoints.
Linked from
Related notes
- tuts/web/security Linked here; links to this page; shared tags: #platform, #security, #web
- tuts/web/security/same_origin_and_cors Linked here; links to this page; shared tags: #platform, #security, #web
- tuts/web/apis/storage_and_state Linked here; shared tags: #platform, #web
- tuts/web/dom/nodes_and_elements Linked here; shared tags: #platform, #web
- tuts/web/tutorials Linked here; shared tags: #platform, #web
- tuts/web/tutorials/forms_validation_and_fetch Linked here; shared tags: #platform, #web
- tuts/databases/postgresql/security_roles_and_rls links to this page; shared tags: #security
- tuts/web shared tags: #platform, #web
- tuts/web/apis shared tags: #platform, #web
- tuts/web/apis/fetch_and_networking shared tags: #platform, #web