Protect your JavaScript with Encrypted Authorship Watermarking and Secure Delivery.
Definition: Defines approved content sources to prevent XSS.
Content Security Policy (CSP) is a security standard introduced to prevent a variety of attacks, including Cross-Site Scripting (XSS) and data injection attacks. It was first proposed by Mozilla in 2009 and has since been adopted by major web browsers as a means to enhance web security by allowing web developers to control resources the client is allowed to load for a given page.
CSP is implemented as an HTTP header that allows website administrators to specify which resources can be loaded by the browser. The main components of CSP include:
CSP offers a variety of features aimed at enhancing web security:
Implementing CSP involves setting the Content-Security-Policy HTTP header in server responses. A basic example to allow scripts from the same origin might look like this:
Content-Security-Policy: script-src 'self'
For testing purposes, you can use the Content-Security-Policy-Report-Only header to monitor potential violations without enforcing the policy.
Here is a simple example of a CSP header that allows scripts and styles from the same origin and images from any source:
Content-Security-Policy:
default-src 'self';
script-src 'self';
style-src 'self';
img-src *;
This policy restricts scripts and styles to the same origin, while images can be loaded from any source.
CSP is widely supported across modern web browsers, including Chrome, Firefox, Safari, and Edge. The community actively contributes to its evolution through discussions and proposals in the W3C Web Application Security Working Group.
CSP can be compared to other security measures like Subresource Integrity (SRI) and HTTP Strict Transport Security (HSTS). While SRI ensures the integrity of specific resources, CSP provides a broader framework for controlling resource loading. HSTS, on the other hand, is focused solely on enforcing secure connections.
Strengths:
Weaknesses:
For advanced usage, consider using CSP with nonce-based policies to allow specific inline scripts. This requires generating a unique nonce for each request and including it in the CSP header and the script tags.
Another advanced topic is using CSP with a reporting endpoint to monitor and analyze policy violations, which can provide insights into potential security issues.
The future of CSP includes potential enhancements like better integration with other security standards and more granular control over resource loading. The community is also exploring ways to simplify policy configuration and improve reporting capabilities.
Views: 61 – Last updated: Three days ago: Saturday 06-12-2025