Lesson 39 of 55
HTMLSecurity – Content Security Policy (CSP)
9 min
Course progress71%
What you'll learn
Keep these ideas in mind as you work through the lesson.
Lesson 39 of 55
HTML9 min
Keep these ideas in mind as you work through the lesson.
CSP is delivered via HTTP header or <meta> tag to restrict which resources can be loaded.
<meta http-equiv='Content-Security-Policy' content="default-src 'self'; img-src https://images.example.com; script-src 'self' 'sha256-abc123'; style-src 'self' https://fonts.googleapis.com;" />
default-src – Fallback for all resource types.script-src, style-src, img-src, font-src, connect-src, media-src, object-src – Fine‑grain control.'sha256-…') or nonces ('nonce-…') allow specific inline scripts/styles.report-uri / report-to – URLs where violation reports (JSON) are sent.Content-Security-Policy-Report-Only – Lets you monitor violations without blocking resources; ideal for testing.'unsafe-inline' defeats CSP’s purpose; avoid it unless you have a strong reason and use nonces instead.upgrade-insecure-requests when a site serves mixed content.<meta>? ✅default-src set to 'self'? ✅Report‑Only mode used during rollout? ✅Tip: Start with a permissive policy in Report‑Only mode, gather violation reports, then tighten the policy step‑by‑step.