-
Notifications
You must be signed in to change notification settings - Fork 15
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
Issues with gatsby-image #3
Comments
Unfortunately, it's not a bug and can't be fixed unless the gatsby-image changes its behavior. Gatsby-image uses inline styles in style attributes (as opposed to inline <style> elements). According to the CSP specification, hashes should apply to inline <style> elements only, not to style attributes. |
I have my image mess up to 20x20 size. I am unsure if this is the cause of that. Any suggestion how to use this with gatsby image? |
hey @k8martian, you can add a custom {
resolve: `gatsby-plugin-csp`,
options: {
directives: {
"style-src": "'self' 'unsafe-inline'",
}
}
} That should resolve your issue with gatsby-image. |
@thomkrupa I'm having issues with that, because a nonce is set in style-src, so 'unsafe-inline' is ignored. From the browser console: |
It looks like
|
@NWRichmond exactly. I will update readme to make it more clear. |
readme still not updated |
CSP rules are there for a reason, setting "style-src" to "unsafe-inline" is convenient for the developer but bad for the user as it's a possible vector for XSS attacks. See these references for more info: |
@skoldborg while I agree that the best practice is to have a totally buttoned-up CSP, at the time of this Issue's creation,
If you have suggestions for accommodating inline styles in style attributes without |
Hi, same here, I'm trying to add CSP to my gatsby project and solved almost all the issues, but the problem comes with afaik this CSP plugin does not look for, and apply hashes to, scripts and styles outside of head, prebody, etc, as noted here. the possible solution is to make the plugin generate sha256 hashes also for styles and scripts inside the body. anyone able to create a pr with a fix? cheers! |
Hi all! The specificities of the project are:
I'm using export const onRenderBody = ({pathname, bodyHtml}, { enabled, hashesCacheFilePath },) => {
if (!enabled) {
return;
}
console.log(`Building body hashes for file '${pathname}'`);
const regex = /<script.*?>(.*?)<\/script>/g
const hashes = [...bodyHtml.matchAll(regex)]
.map(match => match[1])
.filter(htmlString => htmlString)
.map(computeHashOnHtmlString);
if(hashes.length === 0){
return;
}
const currentHashes = JSON.parse(fs.readFileSync(hashesCacheFilePath));
fs.writeFileSync(
hashesCacheFilePath,
JSON.stringify(uniq([...currentHashes, ...hashes])),
"utf8",
);
} I still have the I don't know how to translate this to using the It would easy to make this work for the styles. Hope this helps! |
From what I understand based on the mozilla.org docs on CSP Style Sources, if styles are applied directly on the elements using Javascript, nonces are very effective. Another alternative is like @thomkrupa mentions, to use Quoting the section from over there:
I use If it were possible to configure gatsby-image to use The alternative to support the inline styles through
|
@NWRichmond, if setting the inline styles through Javascript is an option, if could be possible to get a good solution working for it perhaps? |
I'm having issues with gatsby-image that generates inline styles.
gatsbyjs/gatsby#10890 (comment)
The text was updated successfully, but these errors were encountered: