-
-
Notifications
You must be signed in to change notification settings - Fork 200
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
CSP issues in version 0.1.1 #106
Comments
Infer the current rules from https://beta.flathub.org and inscribe them in the policy. From my testing using Laboratory (Content Security Policy / CSP Toolkit) everything seems to work just fine. The only caveat is that one inline script is blocked (`Content Security Policy: The page's settings blocked the loading of a resource at data:text/javascript;base64,IWZ1bmN0aW9u… ("script-src").`). This script is injected by the `next-themes` package, and the only way to make it run is to add `data:` to `script-src`, which is a trade-off that I don't think it's worth it, especially since the theme switching seems to work just fine without this script. Note that in the next version of the package we should be able to add a hash exception for this script: pacocoursey/next-themes#106, which is a much better trade-off. Note that I haven't tested being authenticated and anything payment related, so a few things might be necessary.
Infer the current rules from https://beta.flathub.org and inscribe them in the policy. From my testing using Laboratory (Content Security Policy / CSP Toolkit) everything seems to work just fine. The only caveat is that one inline script is blocked (`Content Security Policy: The page's settings blocked the loading of a resource at data:text/javascript;base64,IWZ1bmN0aW9u… ("script-src").`). This script is injected by the `next-themes` package, and the only way to make it run is to add `data:` to `script-src`, which is a trade-off that I don't think it's worth it, especially since the theme switching seems to work just fine without this script. Note that in the next version of the package we should be able to add a hash exception for this script: pacocoursey/next-themes#106, which is a much better trade-off. Note that I haven't tested being authenticated and anything payment related, so a few things might be necessary. The CSP string might not be very readable, but it's easy to test by copy-pasting it into Laboratory (Content Security Policy / CSP Toolkit) and similar tools, so I've left it like that (instead of storing it into an object and stringifying it later).
Infer the current rules from https://beta.flathub.org and inscribe them in the policy. From my testing using Laboratory (Content Security Policy / CSP Toolkit) everything seems to work just fine. The only caveat is that one inline script is blocked (`Content Security Policy: The page's settings blocked the loading of a resource at data:text/javascript;base64,IWZ1bmN0aW9u… ("script-src").`). This script is injected by the `next-themes` package, and the only way to make it run is to add `data:` to `script-src`, which is a trade-off that I don't think it's worth it, especially since the theme switching seems to work just fine without this script. Note that in the next version of the package we should be able to add a hash exception for this script: pacocoursey/next-themes#106, which is a much better trade-off. Note that I haven't tested being authenticated and anything payment related, so a few things might be necessary. The CSP string might not be very readable, but it's easy to test by copy-pasting it into Laboratory (Content Security Policy / CSP Toolkit) and similar tools, so I've left it like that (instead of storing it into an object and stringifying it later).
So how do we negate the CSP error in v0.2? |
@dan2kx, you could include the hash exception for the inline script in the CSP Option 1 (suggested): |
Apply the suggested fix from pacocoursey/next-themes#106 (comment). The only downside is that if the script changes when the dependency is upgraded, the hash would need to be updated accordingly. Closes flathub-infra#298.
Apply the suggested fix from pacocoursey/next-themes#106 (comment). The only downside is that if the script changes when the dependency is upgraded, the hash would need to be updated accordingly. Closes #298.
How would I create a new sha256 for the latest version? |
I ran the following code in the console of my browser to get the hash value. // Extract the content from the script element output by next-themes and store it in data
const script_content = document.querySelector('#__next > script').innerHTML,
enc = new TextEncoder(),
data = enc.encode(script_content);
// Function to encode the content of an array buffer to BASE64
function _arrayBufferToBase64( buffer ) {
var binary = '';
var bytes = new Uint8Array( buffer );
var len = bytes.byteLength;
for (var i = 0; i < len; i++) {
binary += String.fromCharCode( bytes[ i ] );
}
return window.btoa( binary );
}
// Calculate the cryptographic digest from the content of data, and output it as a Hash value after encoding it with BASE64
crypto.subtle.digest('SHA-256', data).then(function(val){
const digest = ["sha256", _arrayBufferToBase64(val)].join('-');
console.log(`The digest for your script is: ${digest}`);
}); Ref. https://www.vector-logic.com/blog/posts/generating-csp-hash-from-browser-console |
Hello,
In version
0.1.1
there is still a problem with CSPscript-src
directive because it requiresdata:
to work correctly.I see that it's fixed in
0.2.0-beta.0
, and the<script>
tag now renders inside the<body>
element with normal content (not base64 encoded). With this in place, it's possible to add a hash exception to the CSP policy. Are there any plans to release it?The text was updated successfully, but these errors were encountered: