kit icon indicating copy to clipboard operation
kit copied to clipboard

Add `unsafe-inline` to CSP `script-src` when using nonces/hashes

Open aradalvand opened this issue 3 years ago • 3 comments

Describe the problem

One of the recommendations by https://csp-evaluator.withgoogle.com regarding script-src is the following:

Consider adding 'unsafe-inline' (ignored by browsers supporting nonces/hashes) to be backward compatible with older browsers.

image

Describe the proposed solution

Add unsafe-inline after none-* to the script-src option by default, or provide a way for users to do it.

Alternatives considered

I tried adding it myself with this configuration:

const config = {
  kit: {
    csp: {
      directives: {
        'script-src': ['unsafe-inline']
      },
    }
  }
};

But this replaces the whole thing, and removes the nonce-*, so the resulting CSP would just be:

script-src 'unsafe-inline'

Importance

nice to have

Additional Information

No response

aradalvand avatar Dec 07 '22 00:12 aradalvand

Do we know what "older browsers" is specifically? If it's IE11 which we don't support anyway we might as well skip this.

Also not sure if this should be given the enhancement or bug label depending on how we see that, so I gave it both.

dummdidumm avatar Jan 09 '23 17:01 dummdidumm

Do we know what "older browsers" is specifically? If it's IE11 which we don't support anyway we might as well skip this.

@dummdidumm I think we do, nonces and hashes were added in CSP Level 2, so any browser that doesn't support CSP 2 or onwards would be what "older browsers" refers to here — see caniuse.com/contentsecuritypolicy2

As you can see in the caniuse chart, it's not just IE11, but also Safari < 3.1, Chrome < 39, Edge < 18, and Firefox < 30, most notably.

aradalvand avatar Jan 09 '23 19:01 aradalvand

I'm doubting we want to support several-year-old versions of browsers that update themselves by default, either.

Conduitry avatar Jan 10 '23 01:01 Conduitry