Closed Bug 692067 Opened 13 years ago Closed 13 years ago

WebSockets don't trigger content policies

Categories

(Core :: Networking: WebSockets, defect)

defect
Not set
normal

Tracking

()

RESOLVED FIXED
mozilla11

People

(Reporter: jwkbugzilla, Assigned: khuey)

References

(Blocks 1 open bug, )

Details

(Whiteboard: [parity-Chrome][sg:low])

Attachments

(1 file, 2 obsolete files)

It seems that the current WebSockets implementation creates a connection without triggering content policies. I would have expected a content policies call in  nsWebSocket::Init(), probably with type nsIContentPolicy.TYPE_OTHER and the requesting document as context (similar to XMLHttpRequest). It is generally strange that aPrincipal parameter in this function is unused, shouldn't there be some security checks beyond HTTPS-to-HTTP check? Steps to reproduce:

* Install Adblock Plus from (https://addons.mozilla.org/addon/adblock-plus/), restart browser.
* Go to http://html5demos.com/web-socket
* Press Ctrl-Shift-V to open the list of blockable items that Adblock Plus shows.

Expected results:

ws://node.remysharp.com:8001 shows up in the list of requests made by the page.

Actual results (Firefox 7.0.1 and current 10.0a1 nightly):

No such request, web socket requests go completely unnoticed by extensions like Adblock Plus.

Note that Chrome correctly reports WebSocket requests via its webRequest API (http://code.google.com/chrome/extensions/trunk/experimental.webRequest.html).
Assignee: nobody → khuey
Attached patch Patch (obsolete) — Splinter Review
Attachment #570671 - Flags: review?(Olli.Pettay)
Comment on attachment 570671 [details] [diff] [review]
Patch


>+  // Check content policy.
>+  PRInt16 shouldLoad = nsIContentPolicy::ACCEPT;
>+  rv = NS_CheckContentLoadPolicy(nsIContentPolicy::TYPE_XMLHTTPREQUEST,
TYPE_DATAREQUEST

>+                                 mURI,
>+                                 mPrincipal,
>+                                 originDoc,
>+                                 EmptyCString(),
>+                                 nsnull,
>+                                 &shouldLoad,
>+                                 nsContentUtils::GetContentPolicy(),
>+                                 nsContentUtils::GetSecurityManager());
>+  NS_ENSURE_SUCCESS(rv, rv);
>+  if (NS_CP_REJECTED(shouldLoad)) {
>+    // Disallowed by content policy.
>+    return NS_ERROR_CONTENT_BLOCKED;
Ok, this is consistent with XHR.

Could you file a followup to change the error type. It should be something defined
in some spec. Maybe SECURITY_ERR or NETWORK_ERR.
Attachment #570671 - Flags: review?(Olli.Pettay) → review+
https://hg.mozilla.org/mozilla-central/rev/d40e649ff250
Status: NEW → RESOLVED
Closed: 13 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla10
This failed on tinderbox because nsNoDataProtocolContentPolicy is vetoing WebSockets loads since they come from a protocol that has URI_DOES_NOT_RETURN_DATA set.

I'm not really sure which side is broken here, the protocol or the content policy.  Boris?
The content policy is doing this very much on purpose.  It only exists to not allow loads of protocols that would cause external apps to run, unknown protocol alerts to pop up, and whatnot and whatnot, except in a very restricted set of contexts.

On the assumption that the protocol actually has DOES_NOT_RETURN_DATA for a reason, seems like the right fix is to use a new nsIContentPolicy type for this check and whitelist it in the content policy...
Attached patch Patch (obsolete) — Splinter Review
More like this, then.
Attachment #570671 - Attachment is obsolete: true
Attachment #571335 - Flags: review?(bugs)
Attached patch Now with qrefSplinter Review
Forgot to qref
Attachment #571335 - Attachment is obsolete: true
Attachment #571335 - Flags: review?(bugs)
Attachment #571336 - Flags: review?(bugs)
Attachment #571336 - Flags: review?(dveditz)
Attachment #571336 - Flags: review?(bugs)
Attachment #571336 - Flags: review+
Comment on attachment 571335 [details] [diff] [review]
Patch

Nix the trailing comma in the old comment?
dveditz, can you review this before the next aurora merge?
If web sockets weren't triggering content policies then they were also not controllable by CSP.
Whiteboard: [parity-Chrome] → [parity-Chrome][sg:low]
Comment on attachment 571336 [details] [diff] [review]
Now with qref

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

r=dveditz with those changes, especially the contentSecurityPolicy.js one

::: content/base/public/nsIContentPolicy.idl
@@ +145,1 @@
>    /* Please update nsContentBlocker when adding new content types. */

Websockets will bypass CSP, unlike EventSource which uses TYPE_DATAREQUEST. you'll need to add a line like the one for XHR at https://mxr.mozilla.org/mozilla-central/source/content/base/src/contentSecurityPolicy.js#104

   csp._MAPPINGS[cp.TYPE_WEBSOCKET]    = cspr_sd.XHR_SRC;

The "XHR_SRC" is on purpose, we're aliasing those together per the CSP spec (needs to be renamed "connect-src", but that's bug 666056)

::: extensions/permissions/nsContentBlocker.cpp
@@ +68,5 @@
>                                      "objectsubrequest",
>                                      "dtd",
>                                      "font",
> +                                    "media",
> +                                    "websocket"};

I guess "websocket" should be added to NS_CP_ContentTypeName(), too, but I can't find any callers so maybe we can just kill the whole function as DEADC0DE

https://mxr.mozilla.org/mozilla-central/source/content/base/public/nsContentPolicyUtils.h#124
Attachment #571336 - Flags: review?(dveditz) → review+
+  /**
+   * Indicated a WebSocket load.
+   */

Also, s/Indicated/Indicates/
https://hg.mozilla.org/mozilla-central/rev/0ca37155a577
Status: REOPENED → RESOLVED
Closed: 13 years ago13 years ago
Resolution: --- → FIXED
Target Milestone: mozilla10 → mozilla11
Depends on: 716841
No longer depends on: 716841
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: