Last Comment Bug 670687 - Origin handling in websockets is bogus
: Origin handling in websockets is bogus
Status: RESOLVED FIXED
[qa-]
:
Product: Core
Classification: Components
Component: Networking: WebSockets (show other bugs)
: Trunk
: x86 All
: -- normal (vote)
: mozilla8
Assigned To: Patrick McManus [:mcmanus]
:
Mentors:
Depends on:
Blocks: 676277
  Show dependency treegraph
 
Reported: 2011-07-11 09:51 PDT by Boris Zbarsky [:bz]
Modified: 2011-09-22 15:56 PDT (History)
12 users (show)
See Also:
Crash Signature:
(edit)
QA Whiteboard:
Iteration: ---
Points: ---
Has Regression Range: ---
Has STR: ---
+
fixed
+
fixed


Attachments
patch v1 (1.04 KB, patch)
2011-07-18 12:57 PDT, Patrick McManus [:mcmanus]
bzbarsky: review+
christian: approval‑mozilla‑aurora+
christian: approval‑mozilla‑beta+
Details | Diff | Splinter Review
message-event-should-be-utf.1 (2.24 KB, patch)
2011-07-22 11:53 PDT, Patrick McManus [:mcmanus]
bzbarsky: review+
jonas: review+
Details | Diff | Splinter Review

Description Boris Zbarsky [:bz] 2011-07-11 09:51:43 PDT
We get mUTF16Origin by calling GetOrigin on an nsIPrincipal.  The only place we seem to use it (modulo bug 662692) is to send the Sec-WebSocket-Origin header.  But per http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-10 that header is supposed to be the ASCII serialization of a webapps origin which is distinctly NOT what GetOrigin on an nsIPrincipal returns.  GetOrigin returns something that is neither a serialization of a webapps origin nor guaranteed to be ASCII.

Is there a reason we're using GetOrigin here instead of doing what the spec calls for?
Comment 1 Boris Zbarsky [:bz] 2011-07-11 09:55:17 PDT
This seems like a major bug in a security aspect of websockets, which are new in Fx6.  Requesting tracking pending security evaluation.
Comment 2 Boris Zbarsky [:bz] 2011-07-11 09:56:10 PDT
In particular, seems like we should just use nsContentUtils::GetASCIIOrigin here.
Comment 3 Christopher Blizzard (:blizzard) 2011-07-14 14:48:04 PDT
Over to Pat.  Pat, this is tracking Firefox 6 and Firefox 7.
Comment 4 Patrick McManus [:mcmanus] 2011-07-18 12:57:16 PDT
Created attachment 546609 [details] [diff] [review]
patch v1

(In reply to comment #0)
> http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-10 that
> header is supposed to be the ASCII serialization of a webapps origin which
> is distinctly NOT what GetOrigin on an nsIPrincipal returns.  GetOrigin
> returns something that is neither a serialization of a webapps origin nor
> guaranteed to be ASCII.
> 
> Is there a reason we're using GetOrigin here instead of doing what the spec
> calls for?

you've gotten radio silence from me on this because it's content code inherited from the original -76 implementation. I don't know what drove the choice.

That being said, I'm not sure I understand the difference beyond "it might not be ascii" - I get that part. Can you elucidate?

(patch attached)
Comment 5 Boris Zbarsky [:bz] 2011-07-18 13:34:25 PDT
Well, for example for a file:// URI the webapps origin is the string "null", which is decidedly not what nsIPrincipal::GetOrigin will return.  It'll return "file://" instead.
Comment 6 Boris Zbarsky [:bz] 2011-07-18 13:37:15 PDT
Comment on attachment 546609 [details] [diff] [review]
patch v1

r=me, though it seems like you can nix mUTF16Origin in favor of an nsCString mOrigin.
Comment 7 Patrick McManus [:mcmanus] 2011-07-18 14:00:42 PDT
Comment on attachment 546609 [details] [diff] [review]
patch v1

This doesn't necessarily seem important enough to move to aurora/beta but it was nom'd for tracking so I'll ask the cmte to decide.
Comment 8 Asa Dotzler [:asa] 2011-07-18 14:18:55 PDT
We were tracking this because it was described as "a major bug in a security aspect of websockets".  Do we have a better assessment of the security impact?
Comment 9 Boris Zbarsky [:bz] 2011-07-18 20:06:23 PDT
Someone familiar with the protocol would have to assess that.  The main web-facing impact of the bug is that non-ASCII hostnames generate incorrect Origin headers, at the very least.  What does the protocol use this information for?
Comment 10 Patrick McManus [:mcmanus] 2011-07-18 20:49:34 PDT
(In reply to comment #9)
> Someone familiar with the protocol would have to assess that.  The main
> web-facing impact of the bug is that non-ASCII hostnames generate incorrect
> Origin headers, at the very least.  What does the protocol use this
> information for?

The client puts it into a http request header. The server uses the value to decide whether or not to reject the handshake according to whatever (undefined) cross-origin policy it may care to implement.

I suppose it could screw up request header parsing on the server, but I don't believe you could inject an extra \r or \n through a multibyte utf-8 can you? That would be the biggest issue. (not my area of expertise but I recall all those multibyte sequences have their high bits set?).
Comment 11 Boris Zbarsky [:bz] 2011-07-18 21:12:14 PDT
Ok, so the most likely failure modes would be servers either accepting handshakes they didn't mean to or (more likely) refusing handshakes that they actually want to accept.  The latter would likely manifest itself as websocket randomly not working for non-ASCII hostnames.

> but I don't believe you could inject an extra \r or \n through a multibyte
> utf-8 can you?

You cannot, no.  The non-ascii parts of UTF-8 do all have the high bit set.
Comment 12 Marco Bonardo [::mak] 2011-07-19 08:10:22 PDT
http://hg.mozilla.org/mozilla-central/rev/4e34e92f1605
Comment 13 christian 2011-07-19 14:59:48 PDT
Comment on attachment 546609 [details] [diff] [review]
patch v1

Approved for both releases/mozilla-beta and releases/mozilla-aurora
Comment 15 Boris Zbarsky [:bz] 2011-07-22 10:43:52 PDT
Patrick, since you left mUTF16Origin in place, can you please file a bug to remove it and the unnecessary string conversions it involves?
Comment 16 Patrick McManus [:mcmanus] 2011-07-22 10:48:29 PDT
(In reply to comment #15)
> Patrick, since you left mUTF16Origin in place, can you please file a bug to
> remove it and the unnecessary string conversions it involves?

can you clarify why you think utf16origin is a bad idea? mUTF16Origin is used in createanddispatchmessageevent(), which is called everytime a websockets message is received and passed to JS.

Most of the other utf8 uses of origin are used one time.
Comment 17 Boris Zbarsky [:bz] 2011-07-22 11:06:23 PDT
Ah, I'd missed that use.  But shouldn't that be the Unicode origin, not the ASCII origin?  As in, didn't this patch break that usage?
Comment 18 Patrick McManus [:mcmanus] 2011-07-22 11:53:15 PDT
Created attachment 547763 [details] [diff] [review]
message-event-should-be-utf.1

keep mUTF16Origin (and make it UTF), and just deal with ASCII Origin at the callsite without a member variable.
Comment 19 Boris Zbarsky [:bz] 2011-07-22 12:00:17 PDT
Comment on attachment 547763 [details] [diff] [review]
message-event-should-be-utf.1

r=me, but please get someone who understands message events (sicking? smaug?) to double-check this as well.
Comment 20 Patrick McManus [:mcmanus] 2011-07-22 12:04:52 PDT
Comment on attachment 547763 [details] [diff] [review]
message-event-should-be-utf.1

jonas, can you eyeball this too per bz's request?
Comment 21 Jonas Sicking (:sicking) No longer reading bugmail consistently 2011-07-22 13:21:45 PDT
Comment on attachment 547763 [details] [diff] [review]
message-event-should-be-utf.1

Review of attachment 547763 [details] [diff] [review]:
-----------------------------------------------------------------

::: content/base/src/nsWebSocket.cpp
@@ +308,2 @@
>    rv = mWebSocketChannel->AsyncOpen(mOwner->mURI,
> +                                     asciiOrigin, this, nsnull);

Assuming 'asciiOrigin' is the value we send in a http header when connecting, then that seems correct. Except that the indentation is off by one.

@@ +1034,5 @@
>      return NS_ERROR_DOM_SYNTAX_ERR;
>    }
>  
> +  rv = nsContentUtils::GetUTFOrigin(mPrincipal, mUTF16Origin);
> +  NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_SYNTAX_ERR);

What is this used for? For the .origin property when dispatching 'message' events?

If so, are we grabbing the correct origin? Isn't mPrincipal the principal of the page opening the connection? It seems like we should use the origin of the server that we're connected to.
Comment 22 Patrick McManus [:mcmanus] 2011-07-25 05:59:53 PDT
> What is this used for? For the .origin property when dispatching 'message'
> events?
> 

yes - the .origin property.

> If so, are we grabbing the correct origin? Isn't mPrincipal the principal of
> the page opening the connection? It seems like we should use the origin of
> the server that we're connected to.

Honestly, that's inherited -76 code (well, I broke it be ascii, but it always derived from mPrincipal) so I'm not sure how the decision was made - your expertise is all I've got here.

Message Event defines:
"The origin attribute represents, in server-sent events and cross-document messaging, the origin of the document that sent the message (typically the scheme, hostname, and port of the document, but not its path or fragment identifier)."

So you're saying the the "document that sent the message" should be ws://foobar.com/whatever instead of the script at http://bazoff.com/hello.js ?
Comment 23 Wellington Fernando de Macedo 2011-07-31 08:35:15 PDT
> Message Event defines:
> "The origin attribute represents, in server-sent events and cross-document 
> messaging, the origin of the document that sent the message (typically the 
> scheme, hostname, and port of the document, but not its path or fragment 
> identifier)."
>
> So you're saying the the "document that sent the message" should be 
> ws://foobar.com/whatever instead of the script at http://bazoff.com/hello.js ?

Also in the MessageEvent spec there is:

"Except where otherwise specified, when the user agent creates and dispatches a message event (..) the origin attribute must be the empty string."

In the WebSockets spec the origin is used only when establishing the connection. In Server Sent Events, however, its spec defines to use the target url origin. So, as it is, the origin in the MessageEvents of WebSockets should be empty.
Comment 24 Jonas Sicking (:sicking) No longer reading bugmail consistently 2011-08-02 11:53:01 PDT
That sounds like a bug in the spec. We should make the origin be the origin of the server we're connected to.

I filed http://www.w3.org/Bugs/Public/show_bug.cgi?id=13525 on the spec.
Comment 25 Patrick McManus [:mcmanus] 2011-08-03 09:31:51 PDT
track the event.origin bits over in 676277 - this bug can remain about the Sec-WebSocket-Origin header.
Comment 26 Jonas Sicking (:sicking) No longer reading bugmail consistently 2011-08-03 20:35:33 PDT
Comment on attachment 547763 [details] [diff] [review]
message-event-should-be-utf.1

Review of attachment 547763 [details] [diff] [review]:
-----------------------------------------------------------------

r=me on the nsWebSocketEstablishedConnection::Init changs. r- on the rest as it seems wrong.
Comment 27 Curtis Koenig [:curtisk-use curtis.koenig+bzATgmail.com]] 2011-08-09 16:21:32 PDT
removing sec-review flag as this appears good
Comment 28 Anthony Hughes (:ashughes) [GFX][QA][Mentor] 2011-09-22 15:56:04 PDT
qa- as no QA fix verification necessary

Note You need to log in before you can comment on or make changes to this bug.