0

I'm trying to understand why certain CSPs block inline javascript. Is the concern that a user will be able to insert javascript that will then be served to other users? If so, isn't this an issue with the fact that a user somehow has the ability to serve whatever they want to users from your site? How is this a CSP problem?

marked as duplicate by Steffen Ullrich, Steve, Serge Ballesta, Anders, Rory Alsop Oct 11 '17 at 15:51

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

0

Is the concern that a user will be able to insert javascript that will then be served to other users?

Yes, and that JS can do whatever it wants, just like a full script tag, with a few minor inconvenient syntax restrictions.

If so, isn't this an issue with the fact that a user somehow has the ability to serve whatever they want to users from your site?

Yes, you don't want that happening at all, CSP or not. You want to convey the user's intent, not republish the user's input. CSP is not the front line of defense, it's a backup to mitigate the harm from mistakes like not enough sanitizing, too-smart template engines, and plain bugs.

How is this a CSP problem?

It's content, and it relates to security, so the Content Security Policy is a good place to address the issue, right?

  • In what situation would preventing only inline JS or event handlers provide a level of protection? If a user can insert arbitrary HTML, can't they just insert a <script> tag linking the JS of their choice? – Daniel B Oct 8 '17 at 6:01
  • in the situation where you manage html safely, typically using a CMS or subset like markdown to accept incoming user content. The script tag attack is well-known and defended already. You are right that some parts of the CSP standard don't make much sense in complete isolation. – dandavis Oct 8 '17 at 6:03
  • 1
    @DanielB: If you use a CSP that allows loading script from foreign sites, you've screwed up [your attempt to write a secure CSP]. If you use a CSP that allows inline script, you've screwed up. If your CSP both prevents loading foreign scripts, AND prevents inline scripts, (*AND prevents eval, etc.) then you've become nigh-invulnerable to XSS, at least for CSP-supporting clients. If you leave any of those protections out, you may be vulnerable to XSS. Of course, other non-script insertion attacks may still work, and some people still use IE11, so you still need to secure your user input. – CBHacking Oct 9 '17 at 7:12
  • @CBHacking Thanks! That makes sense. It just feels like such a weird rule to have in isolation, like if you separated the rule for having nonces into 'has-nonce' and 'actually-checks-if-nonce-is-valid". – Daniel B Oct 9 '17 at 7:18
  • 1
    @DanielB No prob. The reason for the fine-grained rules is mostly to enable partial restriction for backward compatibility. For example, let's say you have an existing site that loads all its scripts from trusted sources (so you can set their sources in CSP and disallow all others), but also uses inline event handlers (so you cannot disallow inline script). This is less secure, but means you can be secure against a situation where somebody can inject a <script> tag but not, for some reason, an event handler (perhaps your input filter rejects = characters). Slightly better than nothing. – CBHacking Oct 9 '17 at 7:32
1

If so, isn't this an issue with the fact that a user somehow has the ability to serve whatever they want to users from your site?

It is actually very common that users have the ability to serve their own content. Just have a look at all the social networks and public blog sites where users can provide their own content within a site they don't own.

How is this a CSP problem?

Usually the content added by users gets sanitized, i.e. script and other things are removed. But, this sanitizing might have bugs. CSP offers an additional line of defense: if CSP disallows inline script no inline script will be executed even though some inline script might have bypassed the sanitizing.

Not the answer you're looking for? Browse other questions tagged or ask your own question.