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:
- Third party plugins like w3Total Cache plugin which changes the code and creates some inline scripts and css need to be hashed
- 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
header
script really the one you used? As you see per syntax highlighting, it is wrong because of your usage of'
. Tryheader("Content .. default-src 'self' ...");
instead.