Hello,
Last week, I have been playing around security HTTP headers and Content-Security-Policy.
I had been recommended to use the Content-Security-Policy module https://www.drupal.org/project/csp, as it allows to provide fined grained options for this header depending on context.
I will upload a patch for Matomo for which "script-src 'unsafe-inline'" is required due to the inline Javascript snippet injected on pages.
I don't know if a solution of generated HTML file like the https://www.drupal.org/project/google_tag module does, will be possible. I saw that matomo_page_attachments is doing a lot personnalization to the JS Snippet.
| Comment | File | Size | Author |
|---|---|---|---|
| #20 | matomo-3137978-20.patch | 6.16 KB | grimreaper |
| #19 | matomo-csp_support-3137978-19.patch | 4.11 KB | tobiasb |
| #15 | interdiff-3137978-10-15.txt | 1.22 KB | grimreaper |
| #15 | matomo-csp_support-3137978-15.patch | 4.61 KB | grimreaper |
| #10 | matomo-csp_support-3137978-10.patch | 4.4 KB | grimreaper |
Issue fork matomo-3137978
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 #2
grimreaperHere is a patch. Thanks for the review.
Comment #4
grimreaperUse a ServiceProvider to declare the service conditionally if the csp module is enabled.
Comment #5
gappleThe policy alteration should consider the fallback directive (
default-src), and overriding directive (script-src-elem).A publicly available helper function is proposed in #3099423: Helper for altering directives with fallback, but for now can also be copied as a private method on the event subscriber
Comment #6
grimreaperThanks a lot @gapple for your feedback.
I will wait for the maintainer feedback before going forward.
And maybe waiting for #3099423: Helper for altering directives with fallback to be fixed.
Comment #7
grimreaperUpon further testing.
The configured Matomo domain name should also be dynamically added in script-src.
Comment #8
grimreaperHere is an updated patch.
Due to comment 7, I took previous review comments into account too.
Waiting for maintainer feedback.
Comment #9
grimreaperI wonder if the HTTP URL should be authorized.
Comment #10
grimreaperPatch updated.
Mozilla Observatory was not liking it too.
Comment #11
sleitner commentedWouldn't it be better to avoid unsafe-inline completely by using JSON like it is used for the drupal-settings-json in core ( https://www.drupal.org/node/2513818 ) or by using data attributes in tags?
Comment #12
grimreaperHello @sleitner,
Thanks a lot for the suggestion!
Didn't know this change record.
Yes, if unsafe-inline can be avoided, it would be better.
I will not have time to provide a patch but would be happy to test one if provided.
Regards,
Comment #13
gapple@sleitner avoiding inline code is preferred, but passing data through
drupalSettingsis probably a major-version worthy refactor.e.g. I wrote the Googalytics module as a replacement for the legacy Google Analytics module to be compatible with CSP (among other features). Sites with a basic implementation can just swap modules and match configuration, but any custom implementation will likely need to be rebuilt to be compatible with the different API.
This patch makes the module work in it's current state, and an updated version of the module can remove or update the CSP directive changes as needed.
Comment #14
gappleI can't commit to a timeline ATM, but I'm up to lead or support a refactor of the module similar to Googalytics' API, if the module maintainer(s) are open to it.
Comment #15
grimreaperHello,
Updating patch from comment 10. I have updated Matomo Drupal module to 8.x-1.11 and Matomo to 4 and now a connect-src directive is required.
Comment #16
grimreaperUpdating this issue regarding comments.
Switching to PR.
Comment #18
grimreaperPR created.
Thanks for the review!
Comment #19
tobiasbI added script-src-elem again.
Comment #20
grimreaperI have updated the MR to go back to script-src-elem.
Here is a patch for the current state.
If someone else can confirm it works I will merge it.
Comment #22
grimreaperTests are passing. I have merged it and will make a new release because otherwise due to recent code changes applying patches from the latest release is hard.