The default bug view has changed. See this FAQ.

WebSockets don't trigger content policies

RESOLVED FIXED in mozilla11



Networking: WebSockets
6 years ago
5 years ago


(Reporter: Wladimir Palant, Assigned: khuey)


(Blocks: 2 bugs)

Dependency tree / graph

Firefox Tracking Flags

(Not tracked)


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


(1 attachment, 2 obsolete attachments)



6 years ago
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 (, restart browser.
* Go to
* Press Ctrl-Shift-V to open the list of blockable items that Adblock Plus shows.

Expected results:

ws:// 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 (
Depends on: 667490
Assignee: nobody → khuey
Created attachment 570671 [details] [diff] [review]
Attachment #570671 - Flags: review?(Olli.Pettay)

Comment 2

6 years ago
Comment on attachment 570671 [details] [diff] [review]

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

>+                                 mURI,
>+                                 mPrincipal,
>+                                 originDoc,
>+                                 EmptyCString(),
>+                                 nsnull,
>+                                 &shouldLoad,
>+                                 nsContentUtils::GetContentPolicy(),
>+                                 nsContentUtils::GetSecurityManager());
>+  NS_ENSURE_SUCCESS(rv, rv);
>+  if (NS_CP_REJECTED(shouldLoad)) {
>+    // Disallowed by content policy.
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+
Last Resolved: 6 years ago
Resolution: --- → FIXED
Target Milestone: --- → mozilla10
Blocks: 698411
Resolution: FIXED → ---
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...
Created attachment 571335 [details] [diff] [review]

More like this, then.
Attachment #570671 - Attachment is obsolete: true
Attachment #571335 - Flags: review?(bugs)
Created attachment 571336 [details] [diff] [review]
Now with qref

Forgot to qref
Attachment #571335 - Attachment is obsolete: true
Attachment #571335 - Flags: review?(bugs)
Attachment #571336 - Flags: review?(bugs)


6 years ago
Attachment #571336 - Flags: review?(dveditz)
Attachment #571336 - Flags: review?(bugs)
Attachment #571336 - Flags: review+
Comment on attachment 571335 [details] [diff] [review]

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

   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
Attachment #571336 - Flags: review?(dveditz) → review+
+  /**
+   * Indicated a WebSocket load.
+   */

Also, s/Indicated/Indicates/
Last Resolved: 6 years ago5 years ago
Resolution: --- → FIXED
Target Milestone: mozilla10 → mozilla11


5 years ago
Depends on: 716841


5 years ago
No longer depends on: 716841
You need to log in before you can comment on or make changes to this bug.