brendan

brendan

I would like to discuss how phoenix can be improved regarding websocket connection validation with Phoenix Channels.

The issue:

Connecting to a websocket generally involves some kind of authentication/authorization and it needs to be possible to close the websocket connection if this fails.
Additionally, in order to have different client behaviour based on the reason that it was closed, we need to be able to pass at least a reason code, if not a full message on or before closing the connection.
E.g. if the reason is that the auth token is invalid, then the client should not attempt to reconnect until it has acquired a new token.
This should be flexible so that application developers can implement their custom requirements.
However currently it is not possible to both close the connection and provide a reason that is accessible to the client (at least for the browser websocket api).

Existing discussions

There are existing discussions regarding this that have been pretty dismissive of this issue:

Workarounds

The proposed workaround (and the one we currently use) is to require the client to join some special auth channel after connecting, before connecting to any other channels. As you can imagine, this results in multiple issues:

  1. All channels need to perform some kind of validation on join. This isn’t that bad since it can just delegate to a module which checks the socket.assigns.
  2. You can only do auth after the client has decided to join a channel. There is no escape hatch to send a message to the client before they have joined any channels. This makes the server vulnerable to DOS attacks since the client can just open websocket connections without joining any channels and the server will just keep these idle connections running because auth isn’t done on websocket connect.

This leaves us stuck between 2 places.

  1. Either close the websocket connection without a reason, which means the client will always continue attempting to reconnect, even if their token etc is invalid. This leaves us with misbehaving clients resulting in us getting kinda DOS’d by our own clients.
  2. Make it very easy for a bad actor to perform a DOS attack on the server.

Solution

I understand that phoenix channels are transport agnostic and that the correct behaviour cannot be implemented in the channels, but we need to figure out a way to handle this correctly in specific transports.

I think the most flexible solution would be providing some way to send a message from the server to the client without needing the client to join any channel, but if this is not realistically possible given the channel protocol, then we should at least provide a way to send a close frame on the socket connect. Note however that close frames are quite limited. This could be done by instead of rejecting the http upgrade on a connect error, we successfully upgrade the connection and then immediately sending a close frame. Having looked at the phoenix source, this seems like a minor change, except for the fact that cowboy doesn’t seem to support sending custom close frames currently, so we would have to add that support to cowboy first.

I hope that we can have a fruitful discussion here and find a good solution to a problem that I believe a lot of developers have with websocket/phoenix channels currently.

Showing Posts 1 to 1

brendan

brendan OP

I have created a draft PR that solves this issue at [Socket] send connect error to client and close by BrendanBall · Pull Request #4886 · phoenixframework/phoenix · GitHub.
Previously if socket connect handler returns an error
then websocket upgrade would be rejected.
Browser websocket api doesn’t support getting the status code on upgrade
failure.
This instead successfully upgrades to websocket then sends the error as
the first message and immediately closes the socket.

— All posts loaded —

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 91898 914
New
AstonJ
The obligatory hello world thread! Who are you and where are you from? :stuck_out_tongue:
4616 55835 594
New
byu
@chrismccord : I just saw the Extract AGENTS.md from Phoenix.new into phx.new generator commit to the phoenix project. My initial shotgu...
New
arcanemachine
I was working on an Ecto migration and I needed a timestamp. So, for the nth time, I looked up the different data types for timestamps, a...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New
Herve37
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using. We’re particularly inte...
New
matt-savvy
Is there a word for the ~> symbol used in Version strings? Do you also just call it a Squiggle Arrow™ ?!
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews