I am using Helmet.contentSecurityPolicy, and here the gist of my object:
MY SCRIPTS ARE NOT LOADING..... this isn't loaded, but you can see I have it in my trusted items;
NOT LOADING: THESE ARE ITEMS LOADED THRU GOOGLETAGMANGER, BUT I HAVE A NONCE ON THAT?
AND IN the script tag for some of them, like googleTagmanager, I added the nonce.. Now, for some, I couldn't add, but I put them explicitly in the config.
example of nonce in script tag:
<script nonce="2d4f393ea5bc957db4f385232a53fcc8" async src="https://www.googletagmanager.com/gtag/js?id=*******"></script>
THOSE LOCALHOST ONES These are created by webpack, but I have clearly have "localhost" in my accecptable items....so I am confused. Any help?
The errors, are like the following: But I do HAVE the nonce tag (in some of them) AND you can see I include "unsafe-inline".
Refused to load the script '<URL>' because it violates the following Content Security Policy directive: "script-src 'unsafe-eval' 'unsafe-inline' nonce-a449a007188e909846c2e74148c3e1b0 <URL> *.kustomerapp.com/ <URL> *.segment.com/ <URL> *.cloudfront.net <URL> *.stripe.com <URL> *.split.io <URL> *.googletagmanager.com 'self' <URL> ws://localhost:*". Note that 'script-src-elem' was not explicitly set, so 'script-src' is used as a fallback.
MY HELMET MIDDLEWARE TO BRING IN..
import helmet from 'helmet';
const trusted = [
"'self'",
];
if (process.env.NODE_ENV !== 'production') {
trusted.push('http://localhost:*', 'ws://localhost:*');
}
export default function contentSecurityPolicy(nonce) {
return helmet.contentSecurityPolicy({
directives: {
defaultSrc: trusted,
scriptSrc: [
"'unsafe-eval'",
"'unsafe-inline'",
`nonce-${nonce}`,
'https://www.googletagmanager.com',
'*.googletagmanager.com',
].concat(trusted),
styleSrc: [
"'unsafe-inline'",
'*.gstatic.com',
'*.googleapis.com',
'https://*.typography.com',
].concat(trusted),
frameSrc: [
'*.stripe.com',
].concat(trusted),
fontSrc: [
'*.cloudflare.com',
'https://*.cloudflare.com',
'*.bootstrapcdn.com',
'*.googleapis.com',
'*.gstatic.com',
'data',
].concat(trusted),
imgSrc: [
'www.googletagmanager.com',
].concat(trusted),
},
// set to true if you only want to report errors
reportOnly: false,
// set to true if you want to set all headers
setAllHeaders: false,
// set to true if you want to force buggy CSP in Safari 5
safari5: false
});
};
A bit of my server code for context:
const nonce = crypto.randomBytes(16).toString('hex');
const app = new Express();
app.use(cookieParser());
app.use(helmet());
app.use(helmet.referrerPolicy({ policy: 'same-origin' }));
app.use(contentSecurityPolicy(nonce));
[![enter image description here][2]][2]
I am a newbie here but I noticed that in your error:
Refused to load the script '<URL>' because it violates the following Content Security Policy directive: "script-src 'unsafe-eval' 'unsafe-inline' nonce-a449a007188e909846c2e74148c3e1b0
the nonce-a449a007188e909846c2e74148c3e1b0
is lacking the '
s so I would consider modifying your contentSecurityPolicy
function as:
export default function contentSecurityPolicy(nonce) {
return helmet.contentSecurityPolicy({
directives: {
defaultSrc: trusted,
scriptSrc: [
"'unsafe-eval'",
"'unsafe-inline'",
`'nonce-${nonce}'`,
'https://www.googletagmanager.com',
'*.googletagmanager.com',
].concat(trusted),
...
}
});
}
Add the '
s when writting the nonce-${nonce}
part.
Reference: Helmet JS in the Reference > helmet.contentSecurityPolicy(options) > Examples > // Sets "Content-Security-Policy: default-src 'self';script-src 'self' 'nonce-e33ccde670f149c1789b1e1e113b0916'" section
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With