Search code examples
httphttp-headerscontent-security-policyhttp-protocols

Is Content-Security-Policy header applicable only for text/html Content-Type?


From the OWASP's website https://cheatsheetseries.owasp.org/cheatsheets/Content_Security_Policy_Cheat_Sheet.html:

Send a Content-Security-Policy HTTP response header from your web server.

Content-Security-Policy: ...

Using a header is the preferred way and supports the full CSP feature set. Send it in all HTTP responses, not just the index page.

I don't understand how that could be true as it is possible to set the Content-Security-Policy by using a meta tag in the HTML. I also don't see how the policy can apply to anything else but HTML pages.

Does anyone have idea why that statement above was made and if it is safe to only send HTTP header Content-Security-Policy for text/html responses?

By the way, the policy is too big and I would like to sent as fewer bytes as possible.


Solution

  • This is still something that’s not formally specified and there ai still some debate on this: https://github.com/w3c/webappsec/issues/520

    In general there’s two arguments here:

    On the one hand some other file types (XML, PDF, perhaps even SVGs) could benefit from CSP and any resource could become the page by right clicking and opening in a separate tab.

    On the other hand CSPs can get quite big and are usually written for HTML pages. So a bit wasteful to send on other resources and most of it won’t be relevant.

    The right answer (as suggested by above) is probably to have a reduced, and very strict, CSP for all non-HTML responses.

    But I think for most people having it on the HTML only will be good enough and bring most of the benefits of CSP. Then again CSP is an advanced technique so if going as far as that, then why not do it properly?