Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Helmet and contentSecurityPolicy and using nonce AND adding it but still getting error

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? enter image description here

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]

like image 356
james emanon Avatar asked Oct 27 '22 23:10

james emanon


1 Answers

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

like image 52
Nico Serrano Avatar answered Dec 10 '22 03:12

Nico Serrano