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.

Issue fork matomo-3137978

Command icon 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

Grimreaper created an issue. See original summary.

grimreaper’s picture

Assigned: grimreaper » Unassigned
Issue summary: View changes
Status: Active » Needs review
Related issues: +#3099548: Implement Policy Alter event in other modules
StatusFileSize
new2.34 KB

Here is a patch. Thanks for the review.

Status: Needs review » Needs work

The last submitted patch, 2: matomo-csp_support-3137978-2.patch, failed testing. View results

grimreaper’s picture

Status: Needs work » Needs review
StatusFileSize
new3.01 KB

Use a ServiceProvider to declare the service conditionally if the csp module is enabled.

gapple’s picture

The 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

  public function onCspPolicyAlter(PolicyAlterEvent $alterEvent) {
    $policy = $alterEvent->getPolicy();
    self::fallbackAwareAppendIfEnabled($policy, 'script-src', [Csp::POLICY_UNSAFE_INLINE]);
    self::fallbackAwareAppendIfEnabled($policy, 'script-src-elem', [Csp::POLICY_UNSAFE_INLINE]);
  }

  private static function fallbackAwareAppendIfEnabled(Csp $policy, $directive, $value) {
    if ($policy->hasDirective($directive)) {
      $policy->appendDirective($directive, $value);
      return;
    }

    // Duplicate the closest fallback directive with a value.
    foreach (Csp::getDirectiveFallbackList($directive) as $fallback) {
      if ($policy->hasDirective($fallback)) {
        $policy->setDirective($directive, $policy->getDirective($fallback));
        $policy->appendDirective($directive, $value);
        return;
      }
    }
  }

grimreaper’s picture

Thanks 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.

grimreaper’s picture

Status: Needs review » Needs work

Upon further testing.

The configured Matomo domain name should also be dynamically added in script-src.

grimreaper’s picture

Status: Needs work » Needs review
StatusFileSize
new4.5 KB
new4.77 KB

Here is an updated patch.

Due to comment 7, I took previous review comments into account too.

Waiting for maintainer feedback.

grimreaper’s picture

StatusFileSize
new75.49 KB

I wonder if the HTTP URL should be authorized.

grimreaper’s picture

Patch updated.

Mozilla Observatory was not liking it too.

sleitner’s picture

Wouldn'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?

grimreaper’s picture

Hello @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,

gapple’s picture

@sleitner avoiding inline code is preferred, but passing data through drupalSettings is 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.

gapple’s picture

I 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.

grimreaper’s picture

Hello,

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.

grimreaper’s picture

Assigned: Unassigned » grimreaper

Updating this issue regarding comments.

Switching to PR.

grimreaper’s picture

Assigned: grimreaper » Unassigned

PR created.

Thanks for the review!

tobiasb’s picture

StatusFileSize
new4.11 KB

I added script-src-elem again.

grimreaper’s picture

StatusFileSize
new6.16 KB

I 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.

  • Grimreaper committed 476e95d on 8.x-1.x
    Issue #3137978 by Grimreaper, tobiasb, gapple, sleitner: Support Content...
grimreaper’s picture

Status: Needs review » Fixed

Tests 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.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.