问题
When implementing csp-header, I have specified my policy as:
default-src 'self'; script-src www.gstatic.com;
Since I have not declared script-src-elem
directive in my csp policy, as stated in this mdn documentation, I was expecting policy defined for script-src
to be used for script-src-elem
directive as well. However, I see violation being reported as "viloated-directive":"script-src-elem" "blocked-uri":"https://www.gstatic.com/blah/blah"
.
Any idea why this behavior is happening?
回答1:
After seeing this exact same pattern in some of my applications, I finally got to the root of this!
The weirdness we were seeing was that CSP reports were coming in for a hostname which was definitely in the script-src
directive; and we know that script-src-elem
is supposed to fall back to those directives. From that perspective, it should have been literally impossible for these reports to happen.
Here's what we found: the users these reports were coming from were using the PrivacyBadger browser extension, which was leading to false positive CSP reports for the hosts (Google) that it blocked. I didn't dig too far into it, but here's my theory on how that happens:
- The Content Security Policy performs a pre-request check for the JavaScript include on the page (eg. gstatic.com or google-analytics.com). The pre-request check passes, because the hostname is allowed in the policy.
- The browser initiates a request for the resource
- PrivacyBadger intercepts the request via the browser's onBeforeRequest API (see PrivacyBadger source and Chrome documentation)
- ProvacyBadger returns a surrogate data blob for the asset. It does this to ensure that code which relies on the real javascript (eg.
window.ga
) won't break. - The browser then performs a post-request check against the returned base64 blob
- The post-request check fails - because the policy does not allow
data:
forscript-src
- The browser sends a CSP report for the blocked asset.
This seems like it might be a browser bug - because the report reflects the original asset's third party hostname; while the blocked content is actually a data:
blob that was returned via the intercepted request.
回答2:
From the documentation you linked to: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/script-src-elem
The HTTP Content-Security-Policy (CSP) script-src-elem directive specifies valid sources for JavaScript elements, but not inline script event handlers like onclick.
Without seeing the rest of your code it is a safe assumption that you are seeing this error as a result of an inline event handler and will need to define script-src-elem
in your CSP policy.
回答3:
script-src-elem
definitely does fallback toscript-src
in browsers on the Chromium engine. Check the Chrome console, the warn will looks like: ... Note that 'script-src-elem' was not explicitly set, so 'script-src' is used as a fallback.
Gecko-browsers does not support script-src-elem
and use script-src
directly.
The CSP2-browsers in violation reports sends a violatied directive resulting after all fallback chain. But CSP3-browsers send a "theoretically" violated directive and than perform fallback if directive was omitted. This introduces some confusion.
script-src-elem
have nothing to do with inline event handler like onClick() -this is noted in MDN docs.script-src-elem
controls only<script>...</script>
and<script src='...'>
elements (and javascript-navigation)."blocked-uri":"https://www.gstatic.com/blah/blah"
says thathttps://www.gstatic.com
host-source was blocked, not inline event handler.
Inline event handlers do lock in the script-src-attr
directive and report will looks like "blocked-uri":"inline"
.
Looks like you edit a copy CSP, but server issues another as default. Please look the "original-policy" filed in the report's JSON. Is it contains you real CSP or some default one?
PS: To detail analyse what's going on it need to look a full violation report and a your full CSP (print screen of browser console messages will be very helpful). Because script-src www.gstatic.com;
is totally enough for CSP3-browsers to allow any resources from 'https://www.gstatic.com'. (CSP2-browsers requires more rules but you shown violation report sent by CSP3-browser).
来源:https://stackoverflow.com/questions/64322419/why-is-script-src-elem-not-using-values-from-script-src-as-a-fallback