Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

piwik.js can't be loaded due to the CSP (Content Security Policy) #30

Closed
npXXX opened this issue Feb 3, 2017 · 13 comments
Closed

piwik.js can't be loaded due to the CSP (Content Security Policy) #30

npXXX opened this issue Feb 3, 2017 · 13 comments

Comments

@npXXX
Copy link

npXXX commented Feb 3, 2017

Hi, sadly chrome refuses to load the piwik.js, because of a violation of the content security policy.
On mobile devices, like my Android Phone, there is no such problem and Piwik is tracking the visits without a problem. Just desktop browsers don't work (as long as i can see).

@Alex-Ba
Copy link

Alex-Ba commented Feb 4, 2017

Hi,

hit the same bug.

Notice: i host nextcloud / owncloud and piwik on the same domain.

first i noticed there is no 'self' in the CSP header so i looked how to fix this issue
found some infos in the nextcloud community forums: https://help.nextcloud.com/t/content-security-policy-in-nextcloud/5288/4

tried tweaking the piwik\appinfo\app.php :

 if (array_key_exists('HTTP_HOST', $_SERVER) && $_SERVER['HTTP_HOST'] !== $url) {
      $policy = new OCP\AppFramework\Http\ContentSecurityPolicy ();
      $policy->addAllowedScriptDomain($url);
      $policy->addAllowedImageDomain($url);
      \OC::$server->getContentSecurityPolicyManager()->addDefaultPolicy($policy);
   } else {
      $policy = new OCP\AppFramework\Http\ContentSecurityPolicy ();
      $policy->addAllowedScriptDomain('\'self\'');
      $policy->addAllowedImageDomain('\'self\'');
      \OC::$server->getContentSecurityPolicyManager()->addDefaultPolicy($policy);
   }

unfortunately this doesn't has the desired effect (no self in the CSP Header)

so i ended up with this tweak:

#   if (array_key_exists('HTTP_HOST', $_SERVER) && $_SERVER['HTTP_HOST'] !== $url) {
      $policy = new OCP\AppFramework\Http\ContentSecurityPolicy ();
      $policy->addAllowedScriptDomain($url);
      $policy->addAllowedImageDomain($url);
      \OC::$server->getContentSecurityPolicyManager()->addDefaultPolicy($policy);
#   }

aka commented the HTTP_HOST header check in app.php

maybe someone knows an better solution for this issue :-)

regards Alex

@npXXX
Copy link
Author

npXXX commented Feb 5, 2017

Hey Alex,
thank you! Finally I can track my NextCloud ;)
Thanks for sharing your solution!

Regards, Nils

@sualko
Copy link
Owner

sualko commented Feb 6, 2017

Mh. I think the cleaner solution would be to improve the host/url check. But anyway I am wondering, why self is missing in the csp header...

@smoix
Copy link

smoix commented Apr 12, 2017

I also encounter this issue on Nextcloud 11, but the effect depends on the browser...with the default code, tracking works on Safari, IE and Edge but fails with Firefox and Chrome. Modifying the code like Alex-Ba suggests "works" but I'm really not skilled enough to track the root cause and fix anything.

@Xqua
Copy link

Xqua commented May 29, 2017

The problem comes from the parse_url

   $piwik = json_decode(OCP\Config::getAppValue('piwik', 'piwik'));
# Comment out the parse url and add your URL here   
# $url = parse_url($piwik->url, PHP_URL_HOST);
   $url = 'piwik.MY_URL.cc';

#   if (array_key_exists('HTTP_HOST', $_SERVER) && $_SERVER['HTTP_HOST'] !== $url) {
      $policy = new OCP\AppFramework\Http\ContentSecurityPolicy ();
      $policy->addAllowedScriptDomain($url);
      $policy->addAllowedImageDomain($url);
      \OC::$server->getContentSecurityPolicyManager()->addDefaultPolicy($policy);
#   }
}

@sualko
Copy link
Owner

sualko commented May 30, 2017

@Xqua can you explain that in more detail? What is the value of $piwik->url and the corresponding return value of parse_url?

@Xqua
Copy link

Xqua commented May 30, 2017

I did this a while back ... I think it was returning a nil or some sort.

@sualko sualko closed this as completed in a83bcff May 30, 2017
@sualko
Copy link
Owner

sualko commented May 30, 2017

Hope the fix works for you guys.

@Xqua
Copy link

Xqua commented May 31, 2017

I'll try it next week and get back to you !

sualko added a commit that referenced this issue Nov 29, 2017
@sualko sualko closed this as completed Mar 31, 2018
@ghost
Copy link

ghost commented May 2, 2019

The errors re-appears in Nextcloud 16.

  • PHP7.3
  • MariaDB
  • Debian 9.9 stretch
  • NGINX 1.16 TLSv1.2

grafik

All of your mentioned fixes causes the app to change its state from being enabled to disabled.
May i ask and please you to re-investigate further? If you are interested in - let me know how to assist you or grant access to my environment.

Cheers, Carsten (https://www.c-rieger.de)

@Xqua
Copy link

Xqua commented May 2, 2019

I gave up on NextCloud, sorry I won't be able to help.

@MinIsMin
Copy link

@criegerde #70 should fix it, please let me know if it doesn't.

The addon will be disabled if the app is not marked as compliant (e.g. error in code)

@ghost
Copy link

ghost commented May 16, 2019

@MinIsMin : Great, it works with #70
Many THANKS!

sualko added a commit that referenced this issue May 21, 2019
add self to csp if matomo is running on the same host

related to #30, obsolete #70
sualko added a commit that referenced this issue Aug 22, 2019
add self to csp if matomo is running on the same host

related to #30, obsolete #70
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants