Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use session data on websocket handshake

Tags:

If a logged in user navigates to a certain area of the site which is to use WebSockets, How can I grab that session Id so I can identify him on the server?

My server is basically an endless while loop which holds information about all connected users and stuff, so in order to grab that id I figured the only suitable moment is at the handshake, but unfortunately the handshake's request headers contain no cookie data:

Request Headers

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
Accept-Encoding: gzip, deflate Accept-Language: en-US,en;q=0.5
Cache-Control: no-cache
Connection: keep-alive, Upgrade
DNT: 1
Host: 192.168.1.2:9300
Origin: http://localhost
Pragma: no-cache
Sec-WebSocket-Key: 5C7zarsxeh1kdcAIdjQezg==
Sec-WebSocket-Version: 13
Upgrade: websocket
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0

So how can I really grab that id? I thought I could somehow force javascript to send cookie data along with that request but any self-respecting website in 2014 will have httpOnly session cookies so that wont work out. Any help is greatly appreciated!

Here's a link for the server I'm using: https://github.com/Flynsarmy/PHPWebSocket-Chat/blob/master/class.PHPWebSocket.php (thanks to accepted answer)

like image 985
php_nub_qq Avatar asked Mar 09 '14 17:03

php_nub_qq


People also ask

Does WebSocket use session?

A Web Socket session represents a conversation between two web socket endpoints. As soon as the websocket handshake completes successfully, the web socket implementation provides the endpoint an open websocket session.

How do you test a WebSocket handshake?

Click echo.websocket.org in the Name column, representing the WebSocket connection. Client the Headers tab. This tab shows the WebSocket handshake (upgrade request and response).

What is the purpose of WebSocket handshake?

The handshake starts with an HTTP request/response, allowing servers to handle HTTP connections as well as WebSocket connections on the same port. Once the connection is established, communication switches to a bidirectional binary protocol which does not conform to the HTTP protocol.

How do you send data to a WebSocket?

To send a message through the WebSocket connection you call the send() method on your WebSocket instance; passing in the data you want to transfer. socket. send(data); You can send both text and binary data through a WebSocket.


1 Answers

http only cookies as well as secure cookies work fine with websocket.

Some websocket modules have chosen to ignore cookies in the request, so you need to read the specs of the module.

Try: websocket node: https://github.com/Worlize/WebSocket-Node.

Make sure to use the secure websocket protocol as wss://xyz.com

Update:

Also, chrome will not show the cookies in the "inspect element" Network tab.

In node try dumping the request, something like:

 wsServer.on('request', function(request) {    console.log(request);    console.log(request.cookies); // works in websocket node  } 

If you see the cookies somewhere in the log...you've got it.

If you're using secure-only cookies, you need to be in secure web sockets: wss://

Update2:

The cookies are passed in the initial request. Chrome does not show it (all the time) as sometimes it shows provisional headers which omits cookie information.

It is up to the websocket server to do 'something' with the cookies and attach them to each request.

Looking at the code of your server: https://github.com/Flynsarmy/PHPWebSocket-Chat/blob/master/class.PHPWebSocket.php I do not see the word "cookie" anywhere, so it is not being nicely packaged and attached to each websocket connection. I could be wrong, that's why you might want to contact the developer and see if the whole header is being attached to each connection and how to access it.

This I can say for certain: If you're using secure cookies then cookies will not be transmitted unless you use the secure websocket wss://mysite.com. Plain ws://mysite.com will not work.

Also, cookies will only be transmitted in the request if the domain is the same as the webpage.

like image 81
Brian McGinity Avatar answered Oct 12 '22 17:10

Brian McGinity