Problem/Motivation
Content Security Policy is a browser feature that helps prevent XSS and other attacks by sending a header that informs the browser of trusted sources for page resources.
In modern browsers, it replaces the use of X-Frame-Options with the directive frame-ancestors.
Additional directives such as default-src, script-src, and style-src may be difficult to provide default a value in core such as 'self' because it will block third-party or inline resources that site builders expect to load.
For CSP spec see:
- http://www.w3.org/TR/CSP/
- https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Securi...
- https://www.owasp.org/index.php/Clickjacking_Defense_Cheat_Sheet
The contrib modules Content-Security-Policy and Security Kit provide a user interface for configuring a site's Content Security Policy headers, but core should provide a reasonable default policy for sites.
The Reporting API contrib module provides a local endpoint for violation reports, and hosted services for monitoring violations are also available.
The potential for abuse of violation reporting (DoS attacks, bogus data, etc) is outlined at https://www.virtuesecurity.com/blog/abusing-csp-violation-reporting/
Proposed resolution
Implement a basic CSP header for Drupal core configurable through a service parameter, which replaces X-Frame-Options: SAMEORIGIN
Content-Security-Policy: frame-ancestors 'self'
For backwards compatibility of sites that have altered X-Frame-Options from the value set by core and not set a Content Security Policy header, translate the value to an equivalent Content-Security-Policy: frame-ancestors value and throw a deprecation warning.
Remaining tasks
- Service parameter name:
http.response.content_security_policy(likehttp.response.debug_cacheability_headers) orcsp.config(likecors.config) or ? - Should a
X-Frame-Optionsheader present on a response be removed in 11.x, since browsers will ignore it when CSP is present? - Decide on additional directives to add by default, such as
script-src * 'unsafe-inline'; object-src 'none' - Create followup to remove compatibility layer in 12.0.0, and set default policy in
core.services.yml, anddefault.services.yml
User interface changes
None
API changes
A new services parameter http.response.content_security_policy has two properties, report_only and enforced, for setting a site's default Content Security Policies.
Modules can modify or replace the default policy headers by implementing a response subscriber.
Draft Change Record
Drupal 11.0 and earlier set a X-Frame-Options: SAMEORIGIN header by default, which can be overridden or removed by other modules.
In Drupal 11.1 and later, this will be replaced with a Content Security Policy header that includes frame-ancestors 'self'. To change the value of the Content Security Policy header, edit the http.response.content_security_policy service parameter.
For backwards compatibility on sites that have customized the X-Frame-Options header, Drupal 11.x will translate it to an equivalent Content-Security-Policy header, unless a customized policy has been defined in the services parameter.
Starting in Drupal 12, any X-Frame-Options header will be ignored, and Content-Security-Policy: frame-ancestors 'self' will be set by default.
| Comment | File | Size | Author |
|---|---|---|---|
| #46 | 2513356-nr-bot.txt | 2.96 KB | needs-review-queue-bot |
| #44 | 2513356-nr-bot.txt | 2.95 KB | needs-review-queue-bot |
| #37 | 2513356-nr-bot.txt | 1.63 KB | needs-review-queue-bot |
Issue fork drupal-2513356
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #1
pwolanin commentedComment #2
pwolanin commentedComment #3
pwolanin commentedComment #4
fabianx commentedNice!
Some questions:
- Can we via our libraries mechanism automatically emit headers for external JS added via that way?
- Is it possible to also restrict directories? Given we know all directories due to aggregation could we restrict those directories?
Comment #5
pwolanin commentedFrom discussion with webchick, having a CSP in core that restricts JS execution should be 8.1.x material.
Not sure about X-Frame-Options: SAMEORIGIN however
Comment #6
fabianx commentedEven if we make it an optional hardening?
Comment #7
pwolanin commentedyes, optional == contrib. I think for 8.0.x
Comment #8
fabianx commentedWell, core needs to support the API from day 0 - even if then only the contrib module enables it.
Adding at 8.1 will potentially break contrib modules and it is one of the most important security hardenings we can do.
It is also just 'ready' now. And the web is evolving ...
Comment #12
wim leers#2789139: [upstream] CSP requires 'unsafe-inline' because of CKEditor 4 also helps make this possible! :)
Comment #14
gappleCore currently still requires
'unsafe-inline'when CSS aggregation is disabled due to #2897408: Remove IE9 support from CssCollectionRenderer and provide it in contrib instead.--
I've started working on a contrib module to provide automated CSP headers at https://www.drupal.org/project/csp
Currently it parses defined libraries to add a
Content-Security-Policy-Report-Onlyheader with the necessary domains forscript-srcandstyle-src. It doesn't actually report anywhere yet other than the browser console, but a built-in report endpoint is also in the works.#2895245: API for modules to alter policy considers adding an additional attribute to library definitions to support additional policies such as
connect-src.--
Policies can only specify domains, not directories, but I have another experiment where I used Apache rewrite rules to establish separate subdomains restricted to their responsibilities, altered URIs to their respective subdomains, and applied the policy:
Comment #15
gappleComment #16
anybodyComment #18
gappleI reopened #2789139: [upstream] CSP requires 'unsafe-inline' because of CKEditor 4, since CKEditor still requires
'unsafe-inline'. The improvements in CKEditor 4.7 only removed the need for'unsafe-eval'.Comment #20
gappleAny documentation or articles I've seen so far only uses domains, and I haven't tested the state of browser support, but the spec does allow restricting by directory (CSP Level 3 - 6.6.2.10. path-part matching).
So this would theoretically be possible:
Comment #21
gappleCSP module has seen significant improvements, and has a stable release now.
I've proposed using an additional property in library definitions to enable support for additional directives: #2895245: API for modules to alter policy
Comment #30
mxr576#2789139: [upstream] CSP requires 'unsafe-inline' because of CKEditor 4 comment #30
Is that unblock this issue?
Comment #31
gappleA remaining issue is that the
core/drupal.ajaxlibrary still requiresstyle-src 'unsafe-inline': #3110517: Improve Drupal\Core\Ajax\AddCssCommand to accept an array of CSS assets----
The CSP module's default enforced policy is currently quite lenient (
frame-ancestors 'self'; object-src 'none'), because it's reasonably likely that something likedefault-src 'self'will block expected functionality and has the potential to DDOS your own site with the default of logging violation reports to the site log (if not properly configured in a dev environment before deploying to an active site).The default report-only policy is stricter (adding
style-srcandscript-srcdirectives, and will include any relevant domains from library definitions), but also only reports to the browser console in anticipation of the potential initial violations.Comment #33
bkosborneI guess the X-Frame-Options header that core adds via FinishResponseSubscriber should also be removed when this is done. The "frame-ancestors" directive in the CSP header is well supported and obsoletes X-Frame-Options.
Comment #34
gappleWith IE no longer being supported, core could replace its default
X-Frame-Options: SAMEORIGINwithContent-Security-Policy: frame-ancestors 'self'. Starting to always add a default CSP header could cause problems for a site that setsX-Frame-Options: DENYthough. (It looks like IE was the only browser to supportALLOW-FROM).A possible transition is:
[11.1] Add a late-subscriber that translates x-frame-options to a CSP frame-ancestors if the response does not have a CSP header (assume if a CSP header is set but does not include frame-ancestors, then it was intended to allow any frame parent). Issue a deprecation warning if the response has a value other than
X-Frame-Options: SAMEORIGINto notify users to use CSP instead.[12.0] Remove the late subscriber, and replace
X-Frame-Optionswith a static CSP policy that includesframe-ancestors 'self'. A site that uses the Content-Security-Policy module, SecKit, or a custom listener to set the CSP header will override this default value.My suggestion for a minimal static policy that core can add by default:
The simplest way to allow modifying the default CSP (or adding a CSP-Report-Only), would be to have a container parameter. Users wanting a more flexible implementation can use the CSP module.
Comment #36
gappleA slightly rough MR:
- Introduce a new service parameter for setting static CSP values
- A late acting response subscriber will translate
X-Frame-OptionstoContent-Secutity-Policy: frame-ancestors- If a CSP policy is set (via service parameter, or another module like CSP or seckit), then that value is not changed.
- The
X-Frame-Optionsheader is always removed - either it's being replaced by core with an equivalent CSP policy, or we assume that the CSP policy set by the user has their desiredframe-ancestorsvalue (including the option of being omitted).- If
X-Frame-Optionsis not set toSAMEORIGIN, then a deprecation warning is issued to use CSP to set the value. (If the value is still the defaultSAMEORIGIN, then a future version of Drupal changing toframe-ancestors 'self'will not change browser behaviour).-
ALLOW-FROMis ignored by modern browsers (and equivalent to not sending theX-Frame-Optionsheader), but is translated to a CSP value if used.In a future version of Drupal:
- The service parameter can be set to a default enforced CSP value
- The late acting event subscriber can be completely removed
- Core can stop setting
X-Frame-Options- Modules (such as CSP or seckit) will still override core's default (now static) value.
Why the CSP policy value:
-
script-src * 'unsafe-inline'will only blockeval(). Not including'unsafe-inline'would have the potential to break existing sites (and hopefully its presence leads people to explore making sure it's not required...)-
object-src 'none'is recommended to block legacy HTML elements-
frame-ancestorsreplacesX-Frame-Options- I don't think there's other values that can safely be added as a default enforced policy directive without a reasonable risk of negatively affecting some sites. (maybe
base-uri 'self'?)Comment #37
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #38
gappleUpdate issue summary
Comment #39
gappleRebased and fixed the bot's previous issue.
In my previous comment I wasn't sure of the core dev timeline, but this looks like it's something that could be put in 10.4, with the compatibility later removed in 11.0.0.
Comment #40
smustgrave commentedAdded some comments to MR.
Comment #41
gappleComment #42
smustgrave commentedCan be rebased so tests appear green please.
Also is it meant to be draft?
For the CR you can actually go ahead and create it just don't check published yet till the issue is merged.
Comment #43
gapple- Squashed and rebased
- Draft Change Record created
- Postponed issue for 12.x created
I'm not sure why tests are failing on
ImageStylesPathAndUrlTest:L315Remaining questions:
- Service parameter name: http.response.content_security_policy (like http.response.debug_cacheability_headers) or csp.config (like cors.config) or ?
- Should any X-Frame-Options header present on a response be removed, since browsers will ignore it when CSP is present?
- Review additional directives to add by default:
script-src * 'unsafe-inline'; object-src 'none'Comment #44
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #45
smustgrave commentedFalse positive
Comment #46
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #47
gappleSomething that came up in #2868079: Add a default Content-Security-Policy-header for svg files is that there may be a need for different policies in certain cases - uploaded files should probably have scripts and styles blocked, though SVGs may need inline styles, image data, and fonts. Private files can have the header applied from the event subscriber, but public files would require an addition to .htaccess.
Instead of a single set of policies, the service parameter could be keyed by use:
Comment #48
sudhanshu.b commentedFor SVG images,
I have spotted this error, which says I need `data:` whitelisted.
Are there plans for making it completely CSP compatible, ie. without using the `data:` source?
If yes, do you have an issue created for it?
If no, can you provide a justification on that please?
Comment #50
gapple@sudhanshu.b
That policy violation would occur when svg images are inlined on the page through a data uri, without it being allowed by the policy: e.g. HTML
<img src="data:image/svg+xml,[...]">, or a CSSurl('data:image/svg+xml;[...]').The default policy proposed here does not include an
img-srcdirective that would raise that violation.The options that would work without requiring
img-src data:in the CSP policy are:- Including the SVG from an external source:
<img src="/sites/default/files/image.svg">- Inlining the full SVG markup in HTML, if the SVG is know to be from a safe source. However this will be less safe for dynamic image sources than allowing
data:because embedded SVG elements can include scripts that will be executed in the page context, while<img src="data:[...]">will have JS disabled.