Subject: [websec] Content sniffing

From: "Richard L. Barnes" <[email-masked]>
Date: Mon, 9 Jul 2012 16:55:35 -0400

Related to draft-ietf-websec-mime-sniff, an example of sniffing gone awry:
<http://lcamtuf.coredump.cx/squirrel/>

It's a valid JPEG image that contains and HTML snippet in a comment segment.  As a result, when a browser loads the URL expecting an image, it renders the image content, and when it expects HTML, it skips the binary junk at the top and renders the HTML [*]. (In both cases, the server reports Content-Type text/html.)   What's even more startling is that Chrome helpfully adds the binary junk at the top as the first child of the <body> element in the parsed DOM!

--Richard

[*] At least in Chrome 20.0.1132.47

---

From: Adam Barth <[email-masked]>
Date: Mon, 9 Jul 2012 14:05:26 -0700

Why is this sniffing gone awry?  Nothing bad seems to have happened in
this example.

Adam

On Mon, Jul 9, 2012 at 1:55 PM, Richard L. Barnes <[email-masked]> wrote:
> _______________________________________________
> websec mailing list
> [email-masked]
> https://www.ietf.org/mailman/listinfo/websec

---

From: "Richard L. Barnes" <[email-masked]>
Date: Mon, 9 Jul 2012 19:19:50 -0400

I haven't thought much about this, but a couple of thoughts:

The binary prologue means that the document is not valid HTML, so in principle, it shouldn't be accepted as HTML.  It makes you wonder what other stuff you could put in there that the browser would stuff into the DOM without it being obvious on the wire, say, to a proxy.  I'm imagining things like encrypted / compressed Javascript code that could be unpacked by the more obviously HTML part of the page.  

In a related vein, the "Text or Binary" section of draft-ietf-websec-mime-sniff says that nothing scriptable must come out of sniffing a binary blob.  Yet in this case, it produced "text/html", which is obviously scriptable.  

--Richard

---

From: Adam Barth <[email-masked]>
Date: Mon, 9 Jul 2012 16:24:07 -0700

> The binary prologue means that the document is not valid HTML, so in principle, it shouldn't be accepted as HTML.  It makes you wonder what other stuff you could put in there that the browser would…

You don't have to imagine.  It's specified in HTML5.

> In a related vein, the "Text or Binary" section of draft-ietf-websec-mime-sniff says that nothing scriptable must come out of sniffing a binary blob.  Yet in this case, it produced "text/html", whi…

The browser isn't sniffing HTML in this case.  The server sent a
Content-Type header with text/html.

Adam

---

From: "Richard L. Barnes" <[email-masked]>
Date: Mon, 9 Jul 2012 21:42:17 -0400

> You don't have to imagine.  It's specified in HTML5.

Could you clarify?  What is "it"?  Reference would be helpful.

Is there really a use case for inserting into the DOM arbitrary octets that are not syntactically part of the HTML page?

--Richard

---

From: Adam Barth <[email-masked]>
Date: Mon, 9 Jul 2012 18:53:34 -0700

> Could you clarify?  What is "it"?  Reference would be helpful.

You mentioned that you were wondering what "other stuff" you could put
there that the browser would stuff into the DOM.  The HTML
specification [1] defines precisely what DOM you'll get for every
possible input, so you don't need to wonder.

> Is there really a use case for inserting into the DOM arbitrary octets that are not syntactically part of the HTML page?

This topic has been discussed at length in the HTML working group.
It's probably not worth re-hashing it on this list.  The short answer
is that it's what web sites expect browsers to do.

Adam

[1] http://whatwg.org/specs/web-apps/current-work/ (or
http://www.w3.org/TR/html5/ if you want to see the more official but
less up-to-day version).