Content-Security-Policy
The Content-Security-Policy header is a crucial security measure that helps protect websites from various types of attacks, primarily Cross-Site Scripting (XSS) and data injection attacks. Its main purposes are :
- Mitigating XSS risks by specifying which content sources are trusted
- Preventing unauthorized data exfiltration
- Providing protection against clickjacking attacks
- Reporting policy violations to specified endpoints
Learn how to set up the reporting endpoint
Security Best Practice
While CSP is a powerful security tool, it should be implemented as part of a defense-in-depth strategy. Don't rely solely on CSP - combine it with other security measures like input validation, output encoding, and secure coding practices.
How is the CSP Working
CSP works by declaring a set of content restrictions for a web resource. When a browser that supports CSP receives these restrictions, it enforces them, preventing the loading of resources from unapproved sources. This mechanism effectively blocks many types of attacks, including XSS and data injection.

Without CSP, the browser will load any resource from any origin, which poses significant security risks. Here's how it works:
- The browser receives the initial webpage.
- It then loads additional content like images, scripts, and stylesheets from various sources.
- Without restrictions, malicious actors can exploit this process:
- They might inject harmful scripts into the page.
- These scripts could then load from untrusted sources.
- This can lead to Cross-Site Scripting (XSS) attacks.
- Such attacks can steal sensitive data or manipulate page content.
How CSP Enhances Security
With CSP implemented, the browser enforces strict controls on resource loading:
- The server sends the CSP header along with the webpage.
- The browser interprets the CSP rules before loading any resources.
- Only resources from approved sources, as specified in the CSP, are loaded.
- Attempts to load resources from unapproved sources are blocked.
- Each time a resource is blocked, a report is sent to the specified endpoint.

Why use CSP reporting with CentralCSP
CentralCSP assists websites in implementing their Content Security Policy and their report-uri and report-to values. Start by setting up your reporting endpoint and create a strong CSP to protect against XSS (Cross-Site Scripting) attacks.
Learn more about CentralCSP

How to set up CSP reporting with CentralCSP
Setting up CSP violation reporting with CentralCSP is quick and straightforward. Follow these simple steps:
Create your Account
Sign up for a CentralCSP account in a minute. Get 14 days free trial.
Create a Reporting Endpoint
Once logged in, register you application and get your reporting endpoint. You'll receive a unique endpoint URL that looks like: https://report.centralcsp.com/[your-endpoint-id]
Configure Your CSP Headers
Add your new endpoint URL to your CSP configuration using both report-uri and report-to directives for maximum browser compatibility.
Monitor Violations
Access your CentralCSP dashboard to view and analyze any CSP violations in real-time. You'll receive detailed reports about blocked resources and potential security issues.
First Tier Benefits
The first tier includes all essential features: real-time violation reporting, detailed analytics, and support for multiple domains. Upgrade only when you need advanced features like custom alerting, API access, or higher volume reporting.
CSP Header Structure
The Content-Security-Policy header consists of one or more directives, separated by semicolons. Each directive specifies a resource type and its allowed sources. The basic structure is:
Content-Security-Policy: directive1-name source1 source2; directive2-name source1 source2
Implementation Tip
Start with a strict policy using 'default-src self;' and gradually allow specific resources as needed. This approach is more secure than starting with a permissive policy and trying to restrict it later.
Common directives include:
- default-src: The fall back for other resource types
- script-src: Valid sources for JavaScript
- style-src: Valid sources for stylesheets
- img-src: Valid sources for images
- connect-src: Valid targets for fetch, XMLHttpRequest, WebSocket
Full list of CSP directives and their usage
Example of a Content Security Policy
Content-Security-Policy: default-src 'self';
script-src 'self' https://script.com https://trusted.cdn.com;
style-src 'self' https://style.com;
img-src 'self' https://image.example.com/images/;
frame-src 'none';
This example policy allows resources to be loaded only from the site's own origin by default, with more specific rules for scripts, styles, images, and frames.
Common Pitfall
Avoid using 'unsafe-inline' and 'unsafe-eval' in your CSP unless absolutely necessary. These keywords significantly weaken your security posture by allowing potentially malicious inline scripts and dynamic code evaluation.
Tips & Tricks
CSP is an additional layer of security and should not be the only defense against attacks.
Testing your CSP thoroughly is crucial to avoid breaking legitimate functionality on your site.
Use the 'report-only' mode initially to identify potential issues before enforcing the policy.
Set up the reporting endpoint to monitor for CSP violations and fix them as soon as possible.
The Content-Security-Policy-Report-Only header is a powerful tool for testing and gradually implementing a Content Security Policy without breaking your website's functionality.
Content-Security-Policy-Report-Only
Purpose
This header allows you to:
- Test a CSP without enforcing it
- Collect violation reports to understand potential issues
- Gradually refine your policy before full implementation
How it Works
When you set a Content-Security-Policy-Report-Only header:
- The browser monitors for policy violations but doesn't enforce them
- Violations are reported to the specified endpoint
- Your website continues to function normally
Benefits of Using Report-Only Mode
Using the report-only mode before implementing a blocking policy offers several advantages:
- Identifies potential issues without affecting user experience
- Allows time to adjust the policy based on real-world usage
- Helps in creating a more accurate and effective CSP
- Reduces the risk of accidentally breaking website functionality
Security Risk: Long-term Report-Only Usage
While report-only mode is valuable for testing, leaving it enabled indefinitely creates a false sense of security. Attackers can still exploit vulnerabilities that your CSP would block, since report-only mode only logs violations without preventing them. Always plan to transition to enforcement mode once you've validated your policy.
Example
Content-Security-Policy-Report-Only: default-src 'self'; report-uri /csp-violation-report-endpoint/
Warning
While report-only mode is useful for testing, it should not be used indefinitely in production.
Aim to transition to a blocking policy as soon as you're confident in your CSP configuration.
Remember, the goal is to eventually implement a blocking Content-Security-Policy. Use the report-only mode as a stepping stone to create a robust and effective security policy for your website.
Values
Value Selection Guide
When choosing CSP values, prefer specific URLs over wildcards (* or https:). While wildcards are convenient, they reduce the effectiveness of your security policy by allowing resources from potentially untrusted sources.
Content Security Policy directives use various values to specify allowed sources and behaviors. These values define the rules for resource loading and execution in your web application.
Understanding these values is crucial for creating an effective and secure Content Security Policy.
Learn more about CSP valuesDirectives
Content Security Policy uses various directives to control different aspects of web security. These directives are categorized based on their functionality. Here's an overview of the available directives:
Fetch Directives
These directives control loading of various resource types:
child-src
connect-src
default-src
fenced-frame-src
font-src
frame-src
img-src
trusted-types
manifest-src
media-src
object-src
plugin-types
prefetch-src
script-src
script-src-attr
script-src-elem
style-src
style-src-attr
style-src-elem
worker-src
Reporting Directives
These directives are used to specify the reporting behavior of the CSP.
report-to
report-uri
Other Directives
Other directives that can be used in the Content-Security-Policy.
base-uri
sandbox
form-action
frame-ancestors
upgrade-insecure-requests
block-all-mixed-content
require-trusted-types-for
referrer
Using Deprecated and Experimental Directives
When implementing CSP, be cautious with deprecated and experimental directives. Deprecated directives may be removed in future browser versions and should be avoided in new implementations. Experimental directives, while promising, may change significantly or be removed before becoming standard. For production environments, stick to stable directives unless there's a compelling need for experimental features.
Tips & Tricks
Some directives may not be supported by all browsers. Always check browser compatibility before implementation.
Experimental directives are not widely supported and may change in the future.
Deprecated directives are no longer recommended for use.
It's recommended to start with a strict policy and relax it as needed, rather than starting with a loose policy.
Frequently Asked Questions
Can I use CSP and CSP-Report-Only simultaneously?
Yes, you can use both Content-Security-Policy and Content-Security-Policy-Report-Only headers simultaneously. This approach allows you to enforce certain policies while testing others. The enforced policy will be applied, while the report-only policy (CSP-Report-Only) will only generate reports for violations without blocking content. This is particularly useful when transitioning to a stricter policy or testing new directives.
Can I define the CSP in multiple headers?
Yes, you can define your Content Security Policy across multiple headers. Each header will be combined to create the final policy. However, it's important to note that if the same directive appears in multiple headers, the strictest policy will be applied. While this approach can be useful for organizing complex policies, it's generally recommended using a single, comprehensive header for clarity and ease of management.
Should I use the reporting endpoint?
Yes, it's highly recommended using a reporting endpoint. The reporting feature of CSP provides valuable insights into policy violations, helping you identify and fix issues quickly. By setting up a report-uri or report-to directive, you can receive detailed information about blocked content or other policy violations. This information is crucial for fine-tuning your CSP, especially when implementing a new policy or making changes to an existing one. However, ensure you have a system in place to handle and analyze these reports effectively.
Why use many directives?
Using multiple directives in your Content Security Policy allows for more granular control over different types of resources. Each directive targets a specific type of content (like scripts, styles, images, etc.), enabling you to set precise rules for each. This granularity helps in:
- Minimizing the attack surface by allowing only necessary sources for each resource type.
- Balancing security and functionality more effectively.
- Easier troubleshooting and policy refinement.
- Adapting to complex web applications with diverse resource requirements.