
default-src'self'https://example.comContent Security Policy (CSP) is a powerful security mechanism that helps prevent cross-site scripting (XSS) attacks, data injection attacks, and other code injection vulnerabilities. This comprehensive guide covers all CSP directives and source expressions to help you create robust security policies for your web applications.
CSP allows website administrators to control which resources are allowed to load on their web pages. By implementing a well-configured CSP, you can significantly reduce the attack surface of your application and protect your users from malicious content injection.
CSP policies are typically implemented via the Content-Security-Policy HTTP header. This is the most secure and flexible method for deploying CSP policies.
Content-Security-Policy: default-src 'self'; script-src 'self' https://trusted.comCSP can also be implemented using HTML meta tags, though this method has some limitations compared to HTTP headers.
<meta http-equiv="Content-Security-Policy" content="default-src 'self'">Prevents cross-site scripting attacks by controlling script execution
Blocks unauthorized data injection and code execution attempts
Provides detailed reporting on policy violations and attacks
| Directive | Purpose | Example Usage |
|---|---|---|
default-src | Fallback for all resource types | default-src 'self'; |
script-src | Controls JavaScript execution | script-src 'self' 'unsafe-inline' |
style-src | Controls CSS stylesheet loading | style-src 'self' https://fonts.googleapis.com |
img-src | Controls image source loading | img-src 'self' data: https: |
frame-ancestors | Controls iframe embedding permissions | frame-ancestors 'none' |
Begin with Content-Security-Policy-Report-Only to test your policy
Start with default-src 'self' and add exceptions as needed
Set up CSP reporting endpoints to track policy violations
Move to enforcement mode after thorough testing and validation
CSP directives define the policy for different types of resources. Each directive controls what sources are allowed for specific resource types, providing granular control over your website's security policy.
base-uriRestricts the URLs which can be used in a document's <base> element.
base-uri 'self'base-uri 'none'child-srcDefines valid sources for web workers and nested browsing contexts loaded using elements such as <frame> and <iframe>.
child-src 'self'child-src 'none'connect-srcRestricts the URLs which can be loaded using script interfaces like XMLHttpRequest, WebSocket, fetch(), etc.
connect-src 'self'connect-src https://api.example.comdefault-srcServes as a fallback for other CSP fetch directives. Sets default policy for loading content.
default-src 'self'default-src 'none'fenced-frame-srcSpecifies valid sources for nested browsing contexts loading using the Fenced Frame API.
fenced-frame-src 'self'fenced-frame-src https://ads.example.comfont-srcSpecifies valid sources for fonts loaded using @font-face.
font-src 'self'font-src 'self' https://fonts.googleapis.comframe-srcSpecifies valid sources for nested browsing contexts loading using <frame> and <iframe>.
frame-src 'none'frame-src 'self'img-srcSpecifies valid sources of images and favicons.
img-src 'self'img-src 'self' data: https:manifest-srcSpecifies valid sources of application manifest files.
manifest-src 'self'manifest-src https://cdn.example.commedia-srcSpecifies valid sources for loading media using <audio>, <video> and <track> elements.
media-src 'self'media-src https://media.example.comobject-srcSpecifies valid sources for <object>, <embed>, and <applet> elements.
object-src 'none'object-src 'self'prefetch-srcSpecifies valid sources to be prefetched or prerendered.
prefetch-src 'self'prefetch-src https://cdn.example.comscript-srcSpecifies valid sources for JavaScript including inline event handlers.
script-src 'self'script-src 'self' 'unsafe-inline'script-src-attrSpecifies valid sources for JavaScript inline event handlers.
script-src-attr 'none'script-src-attr 'unsafe-inline'script-src-elemSpecifies valid sources for JavaScript <script> elements.
script-src-elem 'self'script-src-elem https://cdn.example.comstyle-srcSpecifies valid sources for CSS stylesheets.
style-src 'self'style-src 'self' 'unsafe-inline'style-src-attrSpecifies valid sources for inline styles applied to individual DOM elements.
style-src-attr 'none'style-src-attr 'unsafe-inline'style-src-elemSpecifies valid sources for stylesheets <style> elements and <link> elements with rel='stylesheet'.
style-src-elem 'self'style-src-elem https://cdn.example.comworker-srcSpecifies valid sources for Worker, SharedWorker, or ServiceWorker scripts.
worker-src 'self'worker-src 'none'report-toDefines a reporting group which CSP violations should be sent to.
report-to csp-endpointreport-to defaultreport-uriInstructs the user agent to report attempts to violate the Content Security Policy. Superseded by report-to directive.
report-uri https://example.com/csp-reportreport-uri /csp-violation-report-endpoint/block-all-mixed-contentPrevents loading any assets using HTTP when the page is loaded using HTTPS.
block-all-mixed-contentplugin-typesRestricts the set of plugins that can be embedded into a document by limiting the types of resources which can be loaded.
plugin-types application/pdfplugin-types application/pdf application/x-shockwave-flashreferrerControls the referrer information sent when following links from the document.
referrer no-referrerreferrer originrequire-sri-forRequires Subresource Integrity for scripts or styles on the page.
require-sri-for scriptrequire-sri-for stylerequire-trusted-types-forEnforces Trusted Types at the DOM sink level.
require-trusted-types-for 'script'sandboxEnables a sandbox for the requested resource similar to the iframe sandbox attribute.
sandboxsandbox allow-scriptstrusted-typesSpecifies an allowlist of Trusted Types policy names created by Document.createElement().
trusted-types myPolicytrusted-types policy1 policy2upgrade-insecure-requestsInstructs user agents to treat all of a site's insecure URLs as though they have been replaced with secure URLs.
upgrade-insecure-requestsSource expressions define which locations are allowed to serve resources. Understanding these expressions is crucial for creating effective CSP policies that balance security with functionality.
'none'Blocks all sources. Nothing will be allowed to load from any source.
object-src 'none'base-uri 'none''self'Allows resources from the same origin (same protocol, domain, and port).
script-src 'self'style-src 'self''unsafe-inline'Allows inline scripts, styles, and event handlers. Not recommended for security.
script-src 'self' 'unsafe-inline'style-src 'self' 'unsafe-inline''unsafe-eval'Allows the use of eval() and similar dynamic code execution methods.
script-src 'self' 'unsafe-eval''unsafe-hashes'Allows inline event handlers that match specified hashes.
script-src 'self' 'unsafe-hashes' 'sha256-abc123...''strict-dynamic'Allows scripts loaded by already-trusted scripts to load additional scripts.
script-src 'strict-dynamic' 'nonce-abc123'script-src 'strict-dynamic' 'sha256-xyz789'*Allows resources from any source. Not recommended for most directives.
img-src *media-src *https://*.example.comAllows resources from any subdomain of a specific domain over HTTPS.
script-src https://*.googleapis.comimg-src https://*.example.comdata:Allows data: URIs (base64 encoded inline resources).
img-src 'self' data:font-src 'self' data:blob:Allows blob: URIs (binary large objects created by JavaScript).
media-src 'self' blob:img-src 'self' blob:http:Allows any HTTP URL. Not recommended due to security risks.
img-src https: http:default-src https: http:https:Allows any HTTPS URL. More secure than HTTP but still very permissive.
default-src 'self' https:img-src https:ws:Allows WebSocket connections over HTTP (unencrypted).
connect-src 'self' ws://localhost:3000wss:Allows WebSocket connections over HTTPS (encrypted).
connect-src 'self' wss://secure-websocket.example.comhttps://example.comAllows resources from a specific HTTPS domain.
script-src 'self' https://cdn.example.comconnect-src https://api.example.comhttps://api.example.comAllows resources from a specific API endpoint over HTTPS.
connect-src 'self' https://api.example.comscript-src https://api.example.com'self' instead of wildcard (*) sources when possible'unsafe-inline''unsafe-inline' and 'unsafe-eval' in production*) sources unless absolutely necessaryContent Security Policy (CSP) is a security standard that helps prevent XSS attacks by controlling which resources are allowed to load on a webpage. It works by defining a whitelist of trusted sources for different types of content.
CSP can be implemented using HTTP headers (Content-Security-Policy) or HTML meta tags. The header method is preferred as it's more secure and flexible. Start with a report-only policy to test before enforcing.
'self' and specific domains?'self' allows resources from the same origin (protocol, domain, port), while specific domains allow resources from those exact domains. 'self' is generally more secure and maintainable.
'unsafe-inline' and 'unsafe-eval'?These directives significantly weaken CSP protection by allowing inline scripts and dynamic code evaluation, which are common attack vectors for XSS. Use nonces or hashes instead for inline content.