Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome Websockets CORS policy

I'm having trouble opening a websocket in Chrome. It seems that there is some CORS policy in chrome for websockets.

If I am on www.example.com and attempt to open the websocket at api.example.com it'll say pending on the console network tab, and will fire the onerror with a message WebSocket connection to 'wss://api.example.com' failed: Connection closed before receiving a handshake response. If I look at the server I do not see a request for a web socket connection being made, so there is no options request to respond to, or Ability to set an Access-Control-Allow-Origin header. However if I first make a request to api.example.com which on the browser will redirect me back to www.example.com it'll work fine.

Are you required to use the same origin for websocket requests in chrome?

Note: this issue is only with chrome.

like image 572
Josh Wilson Avatar asked Mar 25 '14 19:03

Josh Wilson


People also ask

Does CORS apply to WebSockets?

Cross-origin restrictions imposed by SOP and CORS policies do not apply to WebSockets because those restrictions are placed on HTTP responses while WebSockets work over WS (WebScoket) or WSS (WebSocketSecure) protocols. To initiate a WebSocket connection, however, the client has to connect to the server over HTTP.

How many WebSockets can chrome handle?

Chrome has a limit of 6 connections per host name, and a max of 10 connections. This essentially means that it can handle 6 requests at a time coming from the same host, and will handle 4 more coming from another host at the same time.

Does Google support WebSockets?

Google does not support WebSocket connections in Googlebot.


3 Answers

There is no browser enforced CORS with WebSocket. Here are 2 things that can go wrong (assuming you use both HTTPS and WSS):

  • the server enforces an Origin. The Origin HTTP header is set by the browser to the origin of the HTML page containing the JavaScript that is opening the WebSocket connection. A server MAY check that header and deny. But since you say other browsers are working (which?), this is unlikely
  • since you are using wss, the server certificate MUST be completely valid, and acceptable to the browser without any user interaction. Is this the case?
like image 172
oberstet Avatar answered Nov 01 '22 09:11

oberstet


I came across this issue again. I still haven't figured out why, but making an OPTIONS (or any other) request to the subdomain first allows the connection to be opened.

This only seems to be a problem with wss connections, and has popped up across multiple domains and certificates.

like image 34
Josh Wilson Avatar answered Nov 01 '22 09:11

Josh Wilson


This sounds like it has to do with making cross-origin requests that are not "simple". HTTP requests that are not simple are "preflighted" by the browser (Chrome at least) with "OPTIONS" automatically. I'm guessing the browser is just enforcing this behavior.

https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Simple_requests https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Preflighted_requests

like image 1
mhgbrown Avatar answered Nov 01 '22 10:11

mhgbrown