paragonie / csp-builder

Build Content-Security-Policy headers from a JSON file (or build them programmatically)
https://paragonie.com/projects
MIT License
544 stars 39 forks source link

"support older browsers" nonce fix #31

Open strider72 opened 6 years ago

strider72 commented 6 years ago

Older versions of iOS Safari (iOS 9 and earlier) don't understand CSP nonces. So when using nonces, if you want those browsers to work you have to add unsafe-inline as well. Of course, this is less secure again.

Firefox and Edge ignore the "unsafe-inline" directive if nonces are also called, so this is fine in those browsers; but... I can't determine if Chrome or newer versions of iOS Safari (10+) do the same. Thus, I'm not positive that just adding unsafe-inline is the correct (safe) fix. Worth investigating though.

paragonie-scott commented 6 years ago

Are these versions of iOS Safari still supported?

(I'm asking because I don't own any Apple products, so I don't have a frame of reference. This could be an "Internet Explorer 11" bug or an "Internet Explorer 5.5" bug and I wouldn't know which.)

strider72 commented 6 years ago

Apple seems to come out with a new iOS about every year or so, and the current version is 11 – so iOS 9 certainly isn't new, but not ancient either. Maybe on par with IE 11 or possibly 10.

A lot of people are still on iOS 9 or 10 because certain iPhones and iPads max out at that OS.

I believe it falls within a reasonable “older browsers”. iOS 5 or lower would be analogous to IE 6....

(Edit: more like iOS 1 or 2....)

strider72 commented 6 years ago

Looked it up. iOS 9 was introduced in September 2015

strider72 commented 6 years ago

Okay, I did some testing with an inline not-nonced Style, and Script, tag. So... CSP script-src has a nonce set, AND 'unsafe-inline' set to true.
CSP style-src has a nonce set, AND 'unsafe-inline' set to true.

"Pass" means the browser blocked the non-nonced Style and Script. "Fail" means it did not block the non-nonced Style or Script:

Results:

  1. If your CSP sets default-src to 'none', but style-src or script-src to a nonce, old browsers will fail to run those scripts or styles, because they don't understand nonces and fall back to the default 'none'.
  2. If a browser that does understand CSP nonces has a nonce set AND unsafe-inline set, it ignores the unsafe-inline. (Note: In csp-builder you have to set the unsafe-inline before you set the nonce!) THEREFORE:
  3. To properly support older browsers, when setting nonces you also have to set unsafe-inline.
Rendez commented 5 years ago

Hi, this is very interesting. The CSP evaluator also recommends this approach, so we could the unsafe-inline directive be included if a nonce is set and supportOldBrowsers is true? screenshot 2019-02-25 at 10 23 34