DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question
  1. Home
  2. Articles
  3. Frontend Engineering
  4. Content Security Policy: Preventing XSS at the Browser Level
XLinkedInReddit
MediumFrontend Engineering

Content Security Policy: Preventing XSS at the Browser Level

D
DevPrep Team
February 10, 2026·1 min read·0
Table of Contents
  • What is CSP?
  • Setting CSP
  • Directive Reference
  • Nonce-Based CSP (Recommended)
  • Strict CSP Template
  • Report-Only Mode
  • Common Pitfalls

CSP is the most powerful browser-level defense against XSS attacks. At Google, every application has a strict CSP.

What is CSP?

A Content Security Policy tells the browser which resources are allowed to load and execute. If an attacker injects a script, the browser blocks it.

Setting CSP

// HTTP Header (preferred)
Content-Security-Policy: default-src 'self'; script-src 'self' 'nonce-abc123'

// Meta tag (limited)
<meta http-equiv="Content-Security-Policy" content="default-src 'self'">

Directive Reference

DirectiveControlsExample
default-srcFallback for all'self'
script-srcJavaScript'self' 'nonce-abc'
style-srcCSS'self' 'unsafe-inline'
img-srcImages'self' data: https:
connect-srcFetch/XHR/WS'self' https://api.example.com
font-srcFonts'self' https://fonts.gstatic.com
frame-srciframes'none'
frame-ancestorsWho can frame you'none' (prevents clickjacking)

Nonce-Based CSP (Recommended)

// Server generates random nonce per request
const nonce = crypto.randomBytes(16).toString("base64");

// Header
Content-Security-Policy: script-src 'nonce-${nonce}'

// HTML — only scripts with matching nonce execute
<script nonce="${nonce}">
  // This runs ✅
  console.log("Legitimate script");
</script>

<script>
  // This is blocked ❌ (no nonce)
  alert("XSS attack!");
</script>

Strict CSP Template

Content-Security-Policy:
  default-src 'none';
  script-src 'nonce-{random}' 'strict-dynamic';
  style-src 'self' 'unsafe-inline';
  img-src 'self' data: https:;
  font-src 'self';
  connect-src 'self' https://api.example.com;
  frame-ancestors 'none';
  base-uri 'none';
  form-action 'self';
  upgrade-insecure-requests;

Report-Only Mode

// Test CSP without breaking anything
Content-Security-Policy-Report-Only: 
  default-src 'self';
  report-uri /csp-violations;

// Violations are reported but not blocked
// Monitor for a week before enforcing

Common Pitfalls

  • Never use 'unsafe-eval' — it defeats CSP's purpose
  • 'unsafe-inline' for scripts is dangerous — use nonces instead
  • 'strict-dynamic' allows dynamically created scripts by trusted scripts
  • Always include frame-ancestors 'none' to prevent clickjacking

Related Articles

MediumFrontend Engineering

System Design #12: Design a Multi-Step Form Wizard

7 min read
MediumFrontend Engineering

Mastering Senior-Level JavaScript Interview Concepts

2 min read
MediumFrontend Engineering

System Design #9: Design a Collaborative Text Editor

9 min read

Comments (0)

Sign in to leave a comment.

No comments yet. Be the first to comment.

Table of Contents

  • What is CSP?
  • Setting CSP
  • Directive Reference
  • Nonce-Based CSP (Recommended)
  • Strict CSP Template
  • Report-Only Mode
  • Common Pitfalls

Series

View all Frontend Engineering articles →

Practice

  • JavaScript
  • DSA
  • Machine Coding
  • System Design

Resources

  • Learning Tracks
  • Articles
  • Roadmaps
  • Compare Concepts
  • Glossary
  • Developer Tools
  • All Questions

Company

  • About
  • Pricing

Legal

  • Privacy Policy
  • Terms of Service
DevPrep

© 2026 DevPrep. All rights reserved.