Content Security Policy (CSP) is an HTTP header that provides a robust mechanism to mitigate certain types of attacks, such as Cross-Site Scripting (XSS) and data injection. Here are the top 50 features and directives supported by CSP:
1-10: Basic Content Control Directives
- default-src: Sets a default policy for all resource types not explicitly mentioned in other directives.
- script-src: Specifies allowed sources for JavaScript.
- style-src: Specifies allowed sources for stylesheets.
- img-src: Defines valid sources for images.
- connect-src: Controls URLs that can be loaded using script interfaces (e.g.,
fetch
,XMLHttpRequest
). - font-src: Defines valid sources for font resources.
- object-src: Specifies valid sources for the
<object>
,<embed>
, and<applet>
elements. - media-src: Defines sources for
<audio>
and<video>
elements. - frame-src: Specifies valid sources for embedding
<frame>
and<iframe>
content. - worker-src: Specifies valid sources for Worker and SharedWorker scripts.
11-20: Advanced and Control Directives
- child-src: Controls resources for nested browsing contexts, like
<frame>
and<iframe>
. - form-action: Restricts the URLs to which forms can be submitted.
- frame-ancestors: Specifies valid parents that may embed the current page using
<frame>
,<iframe>
, or<object>
. - sandbox: Applies restrictions to a page or frame, similar to the
sandbox
attribute on an<iframe>
. - plugin-types: Restricts the MIME types of plugins that can be loaded.
- base-uri: Restricts the URLs to which a document’s
<base>
element can point. - manifest-src: Specifies allowed sources for manifest files.
- prefetch-src: Specifies valid sources for prefetching or prerendering.
- navigate-to: Limits the URLs the page can navigate to using links or redirects.
- require-trusted-types-for: Enforces the use of
Trusted Types
to prevent XSS.
21-30: Subresource Integrity and Input Restrictions
- block-all-mixed-content: Blocks all mixed content requests.
- upgrade-insecure-requests: Automatically upgrades HTTP requests to HTTPS.
- trusted-types: Defines policies that control which functions can create DOM objects from strings.
- report-uri: Specifies where violation reports are sent.
- report-to: Sends violation reports to a
Reporting API
endpoint. - nonce-attribute: Allows the use of a nonce (
'nonce-xxxx'
) to whitelist scripts or styles. - strict-dynamic: Works with
'unsafe-inline'
and nonces to allow only dynamically created scripts. - unsafe-inline: Allows inline JavaScript and CSS (should be avoided in strict CSP).
- unsafe-eval: Allows the use of
eval()
and similar functions (should be avoided in strict CSP). - self: A source keyword that allows loading from the same origin.
31-40: Source Control Keywords
- none: Disallows all content for the directive.
- unsafe-hashes: Allows the use of specific hashes for inline scripts.
- strict-origin: Restricts requests to the same origin, with a strict scheme.
- strict-origin-when-cross-origin: Allows restricted cross-origin requests while protecting sensitive data.
- data:: Allows inline data URIs (e.g.,
data:image/png
). - blob:: Allows
blob:
URLs. - filesystem:: Allows
filesystem:
URLs. - https:: Restricts resources to HTTPS sources.
- http:: (not recommended) Allows resources from insecure HTTP.
- ‘unsafe-inline-hashes’: Allows using hashes with inline code.
41-50: Reporting and Miscellaneous
- report-sample: Allows the browser to include sample code in reports.
- script-src-attr: Specifies valid sources for inline event handlers and script attributes.
- style-src-attr: Specifies valid sources for inline
style
attributes. - style-src-elem: Specifies valid sources for
<style>
and<link>
elements. - script-src-elem: Specifies valid sources for external scripts.
- cross-origin-embedder-policy: Used for embedding security and cross-origin isolation.
- cross-origin-opener-policy: Prevents cross-origin communication between windows.
- cross-origin-resource-policy: Controls the sharing of resources with cross-origin requests.
- unsafe-hashes-with-fallback: Allows fallback behavior when using unsafe hashes.
- worker-src-attr: Allows customization of inline worker scripts.
Summary
These directives and features offer fine-grained control over the resources a web page can load. A well-implemented CSP reduces attack surfaces significantly, improving security against content-based attacks. Use tools like CSP Evaluator to analyze and optimize your CSP policies.