0

Documentation about this says to avoid inline script and css

Error: Refused to execute inline script because it violates the following Content Security Policy directive:

What I have used so far:

header('Content-Security-Policy: default-src 'self' 'unsafe-inline' 'unsafe-eval' https: data:');

Sample to create WordPress Nonce

  <?php $nonce = wp_create_nonce( 'my--custom-nonce' ); ?>

  <script type="text/javascript" nonce="<?php echo $nonce; ?>">
       // javascript code here
  </script>

The above approach is working for custom code written

Problem Statement:

  1. Third party plugins like w3Total Cache plugin which changes the code and creates some inline scripts and css need to be hashed
  2. The Hashed item in Third party plugins like w3Total Cache plugin updates after specified hours of cache

Need solution to overcome this problem

Update: This is a sample working header used

$headers['Content-Security-Policy'] = 'default-src 'self'; object-src 'none'; base-uri 'self'; script-src 'self' 'nonce-nmzzRda5up2nk5SzIgzAsdf' 'nonce-nmzzRda5up2nk5SzIgzAasrg' 'sha256-0bC35y+eS0qvYbz6lHA9LnqYcLishfTOglWIPjH5D3w=' 'sha256-3vJQD+ZD/nmzzRda5up2nk5SzIgzAsdf+Xmt7ia3A6n='; style-src 'self' 'nonce-block23' 'nonce-block24' 'sha256-oxo6OVVgW-khNraMdgDf9znOWRfs3p5MTqI+610j9dg=' 'sha256-jbRPXw7ffjAQqjlmo-PRANlo1ySRbcKSr86bzVbHbgs=';';

NOTE: My header code is working perfectly for 1 day, all the errors are resolved, but after 12 am in the night the hashes get update for the third party plugin W3 Total Cache, Hence I need a solution that if the hashes changes I do not need to manually change them and can be managed automatically with having the CSP

I dont want to change the third party plugin W3 Total Cache core files as plugin updates would update my changes, and is not a good practice

Need solution to get the updated hash key to be added in my code for CSP

4
  • Is the header script really the one you used? As you see per syntax highlighting, it is wrong because of your usage of '. Try header("Content .. default-src 'self' ..."); instead.
    – kero
    Commented Aug 10, 2021 at 8:08
  • Is there more of the error message? It says "the following" but you haven't told us what followed.
    – Rup
    Commented Aug 10, 2021 at 8:40
  • If you're adding a nonce to inline scripts don't you need to specify the nonce in the CSP header? Else how will the browser know the nonce is correct? I can imagine this would cause problems with total cache though, if it doesn't know to cache the CSP header string, and wouldn't be any security help either if it ended up caching the whole page including the nonces rather than regenerating them for every request.
    – Rup
    Commented Aug 10, 2021 at 8:44
  • Hi @kero The header is working perfectly, But after a few hours I think 24 hours the hash gets updated, I need that updated hash to be added in my header. I cannot go and check the updated hash and copy it to my header always. Need to automate it. And editing plugin files is not a good practice Commented Aug 10, 2021 at 9:14

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.