The Content Security Policy (CSP) is an essential security feature implemented in modern web development to prevent various forms of attacks, especially Cross-Site Scripting (XSS), data injection, and other code execution vulnerabilities. By using CSP, developers can control which resources can be loaded by the browser, creating a "white-list" of safe resources and reducing the risk of malicious code execution. This chapter will guide you through the entire concept of CSP, from its basics to advanced topics, with practical examples.
CSP is a security mechanism enforced by browsers to restrict and control the resources (like JavaScript, CSS, images, etc.) that a website can load. This is done by setting rules, usually in the HTTP headers or HTML <meta>
tags, that specify the sources from which content can be loaded.
CSP helps prevent:
To understand the importance of CSP, let’s consider a basic example:
";
In this example, an XSS vulnerability exists because an attacker could inject a script that shows an alert. CSP helps block such unauthorized scripts.
CSP can be set in two main ways:
Content-Security-Policy
header on the server.<meta>
tag within the HTML <head>
.An example of setting a basic CSP policy in an HTTP header:
Content-Security-Policy: default-src 'self'; script-src 'self' https://trusted-scripts.com
This CSP:
'self'
).https://trusted-scripts.com
.Alternatively, we can add this policy in the <meta>
tag:
default-src 'self'
: Only load resources from the same origin.script-src
: Limits script loading to the same origin and a specific trusted domain.Output: With this CSP in place, any attempt to load scripts from untrusted sources will be blocked by the browser.
CSP offers several directives to define specific content sources:
Explanation
Output: Only resources from defined sources will load. Any other sources will be blocked, helping prevent code injection.
To allow inline scripts selectively, CSP supports nonces (numbers used only once).
script-src 'nonce-abc123'
: Allows inline scripts with the specific nonce.<script nonce="abc123">
: Script tag includes matching nonce, so it is allowed to execute.You can set CSP to report violations by adding a report-uri
directive. This way, you can track attempted violations.
Content-Security-Policy: default-src 'self'; report-uri /csp-report-endpoint
report-uri
: Logs violations to a specified endpoint without blocking them (report-only mode).
'self'
) is allowed for scripts, styles, and images.frame-ancestors 'none'
prevents the page from being framed, adding anti-clickjacking protection.
img-src 'self' data:
allows images from the same origin and inline base64-encoded images.
The Content Security Policy (CSP) is a critical security feature that can significantly reduce the risk of XSS attacks, data exfiltration, and unauthorized resource usage on a webpage. By defining strict policies, developers can control the sources from which content is loaded, ensuring that only trusted scripts, styles, images, and other resources are used. Happy coding !❤️