DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question
← From Beginner to Expert in Fundamentals of Frontend
  • Semantic HTML
  • Accessibility (a11y)
  • Forms & Validation
  • Document Metadata & The <head>
  • The Document Object Model (DOM)
  • Character Encoding & Entities
  • Data Attributes & Custom Data
  • Template, Slot & Web Components
  • Responsive Images: Picture, Source & srcset
  • Loading Strategies & Resource Hints
  • The Native Dialog Element
  • Audio, Video & Media APIs
  • SVG Integration & Canvas Basics
  • Advanced Input Types & Datalist
  • Details, Summary, Progress & Meter
  • Global Attributes & Iframe Security
  • Microdata, Schema.org & Structured Data
  • Open Graph & Social Meta Tags
  • Security Headers & CSP in HTML
  • Link Relations & Canonical Tags
← From Beginner to Expert in Fundamentals of Frontend

Security Headers & CSP in HTML

15 min read
CSPContent-Security-PolicyX-Frame-Optionshttp-equivnoncestrict-dynamic

Security Headers You Can Set in HTML

While security headers are best set on the server, the <meta http-equiv> tag can set some policies directly in HTML.

Content Security Policy (CSP)

CSP tells the browser which sources are allowed to load scripts, styles, images, etc. It is the most powerful defense against XSS.

CSP Directives

DirectiveControlsExample
script-srcJavaScript sourcesscript-src 'self' cdn.example.com
style-srcCSS sourcesstyle-src 'self' 'unsafe-inline'
img-srcImage sourcesimg-src 'self' data: https:
connect-srcXHR/Fetch/WS URLsconnect-src 'self' api.example.com
frame-ancestorsWho can embed youframe-ancestors 'none'
default-srcFallback for alldefault-src 'self'

Nonce-based CSP (Modern Approach)

Instead of allowing inline scripts globally (unsafe-inline), generate a unique nonce per request and only allow scripts with that nonce.

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.