-
Notifications
You must be signed in to change notification settings - Fork 78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Multiple CSP headers restriction is unclear #215
Comments
The third is a requirement on the client. |
@annevk even though server is restricted from sending multiple headers, client must be ready for it? |
Basically. There's a lot of that in web platform standards. |
Got it, thank you for the clarification! |
The server can send multiple CSP headers provided they are not for the same resource, you could send two headers, one for 'script-src' and one for 'font-src', but not two for 'font-src'. That is my interpretation. |
Server can send multiple This paragraph provides more details. Here is an example. |
@shekyan That's interesting. I just stumbled upon some contradictory behavior in Chrome: I am generating Union merging: https://cspvalidator.org/#headerValue%5B%5D=script-src+'nonce-123'&headerValue%5B%5D=script-src+'nonce-456'&strategy=union Intersection merging: https://cspvalidator.org/#headerValue%5B%5D=script-src+'nonce-123'&headerValue%5B%5D=script-src+'nonce-456'&strategy=intersection And btw @ameshkov, I agree with you, that it is in no way clearly defined. |
I am also seeing this behavior on my own site, where I specified default and script-src across two different headers (as it was easier to implement on my edge node), the second header seems to be ignored as the script is denied regardless of my second header. I've tried to use cspvalidator, but it doesn't seem to give me much information on what union or intersect is doing. My attention however, has been drawn to the semicolons at the end.
I suspect that the browser concatenates the CSP values, and ; deliminates. Also regarding default
We should expect that script-src is respected when provided with default-src.
Reading this, I'm not sure that the specification requires either union or intersection. Just implement one of them and you meet the spec. Which leaves it up to browser implementation. I found this https://www.usenix.org/system/files/conference/usenixsecurity17/sec17-calzavara.pdf which is some sec talk about this issue, but there is no CSP-Union headers or anything in HTTP spec. (This is probably a useless link, only bother reading it if you like reading) I've looked at Google and Mozilla websites and find nothing of value regarding the CSP implementation. Can anyone gather anything concretely from the CSP spec that tells in certain words what the browsers must do when combining policies? |
This section further details the policy processing: https://www.w3.org/TR/CSP2/#enforce @cnsgithub How are you running your script via ajax? I didn't think a browser executes a script that is simply requested. What is the Content-Type of the response? Are you trying to use JSONP? |
@nevercast The script contained in the XHR response is executed by means of jquery's var preservedScriptAttributes = {
type: true,
src: true,
noModule: true
};
function DOMEval( code, doc, node ) {
doc = doc || document;
var i,
script = doc.createElement( "script" );
script.text = code;
if ( node ) {
for ( i in preservedScriptAttributes ) {
if ( node[ i ] ) {
script[ i ] = node[ i ];
script.setAttribute( i, node[ i ] );
}
}
}
doc.head.appendChild( script ).parentNode.removeChild( script );
} As you can see this function is currently buggy and needs to be fixed by adding the However, fixing this is not sufficient since the Here is an example of how it's working currently without CSP: https://www.primefaces.org/showcase/ui/ajax/process.xhtml. To test it just enter a surname and click the This Surname button. The XHR response will look like (shortened): <?xml version='1.0' encoding='UTF-8'?>
<partial-response><changes><update id="j_idt638:grid"><![CDATA[<table id="j_idt638:grid" cellpadding="5" style="margin-bottom:10px">
...
<tr>
<td><label id="j_idt638:j_idt640" class="ui-outputlabel ui-widget" for="j_idt638:surname">Surname:<span class="ui-outputlabel-rfi">*</span></label></td>
<td><input id="j_idt638:surname" name="j_idt638:surname" type="text" value="test" aria-required="true" class="ui-inputfield ui-inputtext ui-widget ui-state-default ui-corner-all" data-p-label="Surname" data-p-rmsg="Surname is required." data-p-required="true" /><script id="j_idt638:surname_s" type="text/javascript">PrimeFaces.cw("InputText","widget_j_idt638_surname",{id:"j_idt638:surname"});</script></td>
</tr>
</tbody>
</table>
]]></update><update id="j_id1:javax.faces.ViewState:0"><![CDATA[468100927453503308:-4747695267725471375]]></update></changes></partial-response> Notice the script block that gets executed by jquery's What I am trying to do now to get CSP support integrated in PrimeFaces JSF library is to dynamically add The <script id="j_idt638:surname_s" type="text/javascript" nonce="456">PrimeFaces.cw("InputText","widget_j_idt638_surname",{id:"j_idt638:surname"});</script> And the response header would be However, even when using the fixed jquery Hence the question how browsers must behave according to the specification? And what would be the proposed way for framework developers to get CSP working in highly dynamic applications where scripts cannot be predicted at the start (i.e. when sending the very first response)? I think JavaServer Faces is just one prominent example. Non-union merging of CSP directives would knock out those frameworks trying to support CSP. Any help would be appreciated. |
Can someone give me a hint if it would be safe to generate a unique |
@cnsgithub Provided the only script that is running when the page is loaded is your own, and you're downloading your script over HTTPS so that it can't be intercepted. It should be "safe" (Understanding the limitations). Keep in mind, any script running on your page could inspect the nonce and drop it in their own, but they would have to be running their code already (and if they are running already, the nonce is practically irrelevant). Disclaimer: Not a security professional. |
In a nutshell: you can set multiple
Specifically, for the JSF case you're talking about, the core issue is that the application wants to execute inline scripts which are fetched in a separate request, and therefore by default will have a different Note that this discussion is probably not a great fit for a spec bug, I'd suggest moving it to Stack Overflow or a similar forum (I'm happy to chime in there in more detail). |
@arturjanc Would you like to chime in again at stackoverflow? |
I don't understand why the specification says that. The CSP header is defined to be a comma-delimited field, which means:
means exactly the same thing as:
|
Nope. |
I am working on a middleware that attaches nonces on CSP headers. I started testing with multiple CSP headers and the spec didn't look very clear for me. I experimented and figured out some things also @arturjanc answer brought some clarity about how nonces should be attached. While testing I faced an issue when returning those 2 headers back to the browser:
I tested with an inline script:
This didn't fire the alert. I also tried changing the order of the headers but no luck. Is the correct way to serve:
Interesting to hear your opinion @cnsgithub and @shekyan |
All policies specified are applied separately, in order for a request (or response, or inline behavior) not to be blocked, it would need to be allowed by each policy - this matches what you're experiencing. This is described at https://www.w3.org/TR/CSP3/#multiple-policies and in the respective algorithms in the spec (e.g. https://www.w3.org/TR/CSP3/#should-block-request). |
Could you elaborate? From RFC 7230 #3.2.2:
And "below" is noted only UPD: sorry for bothering. I've found the answer myself. CSP header format is the following:
which expands to this:
so, while @briansmith comment is correct, there is no contradiction in the spec. |
We're struggling to understand what does that clause mean in the documentation:
https://www.w3.org/TR/CSP2/#content-security-policy-header-field
As I understand, the first part means that server must not send multiple CSP headers in a single response, while the third part implies, that it could.
I see, that in the draft
MUST NOT
was replaced withSHOULD NOT
, but anyway, it stays unclear.The text was updated successfully, but these errors were encountered: