Instructions for WebSocket usage (e.g. Phoenix Channels and LiveView)
#271 opened on Sep 11, 2019
Repository metrics
- Stars
- (1,670 stars)
- PR merge metrics
- (No merged PRs in 30d)
Description
It's not obvious how to deal with Pow sessions and WebSockets. There are a few caveats to using WebSockets since browsers don't enforce CORS. Also, Phoenix LiveView won't run subsequent requests through the endpoint (so @current_user is not available).
Some details on WebSocket security: https://devcenter.heroku.com/articles/websocket-security https://gist.github.com/subudeepak/9897212
Support for pulling session data in WebSockets was added to Phoenix in 1.4.7:
socket "/socket", AppWeb.UserSocket,
websocket: [
connect_info: [:peer_data, :x_headers, :uri, session: [store: :cookie]]
]
A few questions I want to answer are:
- Should the session be fetched for requests after initial handshake?
- If so, should the session be renewed after timeout in the socket? This would require the reply to update the session cookie.
- If not, should the socket be signed somehow, e.g. like a signed url? Not sure if this even makes sense.
- What should happen if the session expires while a socket is open (e.g. someone logs out). Should it be aware, and close the socket (if possible)?
I haven't worked much with WebSockets, so I'll have to read up on this and experiment. I will see if I can find some best practices when it comes to sessions and WebSockets. Any comments are welcome 😄
Here's a few links that may be of interest:
https://www.owasp.org/index.php/Testing_WebSockets_(OTG-CLIENT-010) https://cheatsheetseries.owasp.org/cheatsheets/HTML5_Security_Cheat_Sheet.html#websockets https://spring.io/projects/spring-session https://github.com/spring-projects/spring-session https://www.christian-schneider.net/CrossSiteWebSocketHijacking.html https://abhirockzz.wordpress.com/2017/06/03/accessing-http-session-in-websocket-endpoint/ https://abhirockzz.wordpress.com/2017/06/03/accessing-http-session-in-websocket-endpoint/