How the Content Security Policy - CSP is used to prevent security attacks
Content Security Policy (CSP) is a security feature that helps protect websites from various types of attacks, including Cross-Site Scripting (XSS) and data injection attacks. CSP allows website administrators to control the resources that a user agent (such as a web browser) is allowed to load and execute.
Key Concepts of CSP
-
Policy Definition: CSP is defined using HTTP headers or meta tags in HTML. The policy specifies which sources of content are allowed and which are not.
-
Directives: CSP policies consist of directives that specify the types of content that can be loaded and their allowed sources. Each directive applies to a specific type of content, such as scripts, stylesheets, images, or media.
-
Sources: Sources are the URIs from which content can be loaded. They can be specific domains, schemes, or special keywords.
Common Directives
- default-src: Specifies the default policy for loading content such as JavaScript, CSS, images, etc.
- script-src: Specifies valid sources for JavaScript.
- style-src: Specifies valid sources for stylesheets.
- img-src: Specifies valid sources for images.
- media-src: Specifies valid sources for media files (like audio and video).
- font-src: Specifies valid sources for fonts.
- object-src: Specifies valid sources for plugins like Flash.
- connect-src: Specifies valid sources for XMLHttpRequest, WebSocket, and EventSource connections.
- frame-src: Specifies valid sources for embedded frames.
- form-action: Restricts the URLs that can be used as the target of form submissions.
- base-uri: Restricts the URLs that can be used in a document’s
<base>
element.
Source Values
- 'self': Allows content from the same origin (same domain, protocol, and port).
- 'none': Disallows all content of that type.
- 'unsafe-inline': Allows inline resources such as
<script>
tags and inline event handlers. This is generally unsafe and should be avoided. - 'unsafe-eval': Allows the use of
eval()
and similar methods for creating code from strings. This is generally unsafe and should be avoided. - 'strict-dynamic': Allows dynamically added scripts to be executed.
- URLs: Specific URLs from which content can be loaded.
- data:: Allows
data:
URIs to be used as a content source. - https:: Allows loading of content over HTTPS only.
Example CSP Header
Here is an example of a CSP header that restricts the loading of content from specific sources:
Content-Security-Policy: default-src 'self'; script-src 'self' https://apis.example.com; style-src 'self' https://cdn.example.com; img-src 'self' data:; object-src 'none';
In this example:
- default-src 'self': By default, allow content only from the same origin.
- script-src 'self' https://apis.example.com: Allow scripts from the same origin and from
https://apis.example.com
. - style-src 'self' https://cdn.example.com: Allow stylesheets from the same origin and from
https://cdn.example.com
. - img-src 'self' data:: Allow images from the same origin and
data:
URIs. - object-src 'none': Disallow all plugins.
Implementing CSP
Using HTTP Headers
You can implement CSP by setting the Content-Security-Policy
HTTP header in your server configuration.
Apache:
<IfModule mod_headers.c>
Header set Content-Security-Policy "default-src 'self'; script-src 'self' https://apis.example.com; style-src 'self' https://cdn.example.com; img-src 'self' data:; object-src 'none';"
</IfModule>
Nginx:
add_header Content-Security-Policy "default-src 'self'; script-src 'self' https://apis.example.com; style-src 'self' https://cdn.example.com; img-src 'self' data:; object-src 'none';";
Using Meta Tags
You can also implement CSP using meta tags within your HTML:
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self' https://apis.example.com; style-src 'self' https://cdn.example.com; img-src 'self' data:; object-src 'none';">
CSP Reporting
CSP can also be configured to report violations to a specified endpoint. This is useful for monitoring and debugging CSP policies.
Example:
Content-Security-Policy: default-src 'self'; report-uri /csp-violation-report-endpoint/;
The server can then handle the incoming reports at /csp-violation-report-endpoint/
to log or analyze them.
Benefits of CSP
- Prevents XSS Attacks: By restricting the sources of scripts, CSP significantly reduces the risk of XSS attacks.
- Mitigates Data Injection Attacks: Limits the ability of attackers to inject malicious content.
- Enforces Secure Coding Practices: Encourages developers to use secure coding practices by avoiding inline scripts and styles.
- Improves Content Integrity: Ensures that content is loaded only from trusted sources, enhancing the overall security of the web application.
Limitations of CSP
- Complex Configuration: Properly configuring CSP can be complex and requires a thorough understanding of the resources your application uses.
- Maintenance: As the application evolves, the CSP policy must be updated to reflect changes in resource usage.
- Compatibility Issues: Older browsers may not fully support CSP, potentially reducing its effectiveness.