w3c / webappsec

Web Application Security Working Group repo
https://www.w3.org/groups/wg/webappsec/
Other
605 stars 148 forks source link

Clarify CSP header recommendations for non-HTML pages #520

Open kravietz opened 7 years ago

kravietz commented 7 years ago

As some application security vendors started checking for the presence of the CSP header and raise lack of it as an issue, I think it's necessary to clarify when precisely for which resources it's really recommended to return CSP header. As it seems to me, the CSP is only meaningful for text/html documents since they are the original place where all the resources regulated by a CSP (CSS, images, script etc) are referred from.

The CSP header delivery is discussed in paragraph 3 of the CSP3 draft and the applicability to HTML documents is kind of implied by the alternative delivery via <meta> but never stated explicitly.

annevk commented 7 years ago

It's applicable to all workers and I think it has an effect on all resources with respect to cookies actually (although I don't recall the directive for that).

Furthermore, given that you can navigate to any resource and turn it into a document that way, it probably makes sense to just use it everywhere. Otherwise someone could navigate an <iframe> to an image and maybe bypass some stuff through that document. (Although arguably you've already lost at that point.)

Malvoz commented 5 years ago

There's little documentation on which resources we should respond with a CSP, while we could send it for everything - concerns are "how much “bloat” it will add" (header bloat).

Currently webhint.io warns against CSP for content other than text/html. Fixed in https://github.com/webhintio/hint/pull/2618.

The "Workers & CSP" relation is documented here: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers#Content_security_policy

@annevk not narrowing this down to CSP, do you think spec editors are willing specify to which MIME-type any respective HTTP header applies to?

annevk commented 5 years ago

How is it redundant for text/xml and friends?

Also, that ignores workers, which use a JavaScript MIME type (typically).

briansmith commented 5 years ago

I think that CSP for non-HTML resources is something that just hasn't been decided yet. According to https://pdfium.googlesource.com/pdfium/+/master/README.md, pdfium (Chrome's PDF viewer) may be built with JS support so IMO it would be good for it to honor CSP when rendering PDFs. (I myself have run into cases where the lack of JS support for PDFs has caused real inconvenience.)

More generally, I would say that browsers should pass the Content-Security-Policy response header field to any plugins (or similar) that will render non-HTML content in the page so that the plugin can enforce it within the content. (Whether browsers currently take this advice is different than whether they should eventually take it. Whether plugins currently do such enforcement is different than whether they should.) On the other hand, consider https://stackoverflow.com/questions/37795479/opening-a-pdf-embedded-in-iframe-in-chrome-with-content-security-policy-plugin; based on that, it seems some implementations may currently do something that's incompatible with what I'm suggesting here.

If you think that Content-Security-Policy is irrelevant for a particular document, and/or the server hasn't been configured to have a different CSP for a given response, then I would recommend sending Content-Security-Policy: base-uri 'none'; default-src 'none'. If you are using HTTP/2 then, after the first such response, this will be compressed to almost nothing for future responses.

If there is no Content-Type then browsers will do sniffing (sometimes). Thus you should generally assume the worst when there is no Content-Type header field. Thus Content-Security-Policy: base-uri 'none'; default-src 'none' is important when there is no Content-Type too, unless/until somebody writes down the specific rules for when omitting the CSP header field is safe.

Malvoz commented 4 years ago

Related issue https://github.com/w3c/webappsec-csp/issues/336.

Malvoz commented 4 years ago

Per @briansmith's recommendation of base-uri 'none'; default-src 'none' for when Content-Type is missing, I'd expand that policy to at least:

base-uri 'none';
default-src 'none';
form-action 'none';
frame-ancestors 'none'

As default-src only cover fetch directives.

SmashManiac commented 3 weeks ago

Edge's developer tools currently flags CSP headers on most non-HTML files as a performance issue, referencing the same webhint no-html-only-headers rule mentioned in https://github.com/w3c/webappsec/issues/520#issuecomment-488145783. I'm not sure if this is a good recommendation due to the reasons @briansmith previously listed. It may be worth revisiting this issue sooner rather than later to prevent the spread of misinformation one way or the other.

amalkrishnan43216 commented 3 weeks ago

On a related note, not supporting CSP headers is marked as a security issue of 'medium severity' by vulnerability scanners. There is a higher level of maturity now for CSP because it's in fetch. The lack of CSP support is very visibly felt.

On Mon, Aug 19, 2024 at 7:10 AM Guillaume Fortin-Debigaré < @.***> wrote:

Edge's developer tools currently flags CSP headers on most non-HTML files as a performance issue, referencing the same webhint no-html-only-headers rule mentioned in #520 (comment) https://github.com/w3c/webappsec/issues/520#issuecomment-488145783. I'm not sure if this is a good recommendation due to the reasons @briansmith https://github.com/briansmith previously listed. It may be worth revisiting this issue sooner rather than later to prevent the spread of misinformation one way or the other.

— Reply to this email directly, view it on GitHub https://github.com/w3c/webappsec/issues/520#issuecomment-2295511699, or unsubscribe https://github.com/notifications/unsubscribe-auth/BJUZPPYW5UTECCFNMK5W2HTZSFEKFAVCNFSM6AAAAABMW4FT2KVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEOJVGUYTCNRZHE . You are receiving this because you are subscribed to this thread.Message ID: @.***>