hawkyre

hawkyre

I have a cowboy server that has had a normal API where I use plug, and I use plug to encode and decode the request’s user session. However, now I need to use websockets as well, and I can’t use plug with the cowboy websockets afaik so I’m kinda stuck on how to retrieve and decode the session.

Showing Posts 1 to 4

ku1ik

ku1ik

Have you found a way to solve this? I have a similar situation as you.

hawkyre

hawkyre OP

I have discontinued that application, but I remember having solved this problem. I believe I found how Plug decrypts the cookie and implemented those methods myself. Here’s the little bit of relevant code I was able to find, even though I can’t promise it will work perfectly. You’ll have to adjust some things for sure. Let me know if you’re able to solve it fully.

defmodule LLServer.Web.Websockets.HubChat do
  @behaviour :cowboy_websocket
  alias Plug.Crypto.{KeyGenerator, MessageEncryptor, MessageVerifier}
  alias Plug.Conn.Cookies

  defp derive(secret_key_base, key, key_opts) do
    secret_key_base
    |> KeyGenerator.generate(key, key_opts)
  end

  defp read_raw_cookie(raw_cookie, opts) do
    signing_salt = derive(opts.secret_key_base, opts.signing_salt, opts.key_opts)

    case opts do
      %{encryption_salt: nil} ->
        MessageVerifier.verify(raw_cookie, signing_salt)

      %{encryption_salt: _} ->
        encryption_salt = derive(opts.secret_key_base, opts.encryption_salt, opts.key_opts)
        MessageEncryptor.decrypt(raw_cookie, encryption_salt, signing_salt)
    end
    |> case do
      :error ->
        nil

      {:ok, result} ->
        (result |> Plug.Crypto.non_executable_binary_to_term([:safe]))["session_token"]
    end
  end

  def init(request, _state) do
    opts = %{
      key_opts: [
        length: 64
      ],
      secret_key_base: Application.fetch_env!(:llserver, :session_secret_key_base),
      encryption_salt: Application.fetch_env!(:llserver, :session_encryption_salt),
      signing_salt: Application.fetch_env!(:llserver, :session_signing_salt)
    }

    cookies = request.headers["cookie"]

    ll_cookie = Cookies.decode(cookies)["_ll_session"]

    session = read_raw_cookie(ll_cookie, opts)
    ...
  end
end
LostKobrakai

LostKobrakai

While there are indeed ways to get access to the cookie it needs to be mentioned that phoenix doesn‘t expose it by design. Websockets lack working under the same site policy in browsers, so any website not just your own could establish a websocket connection to your server and the browser would provide the users cookie. Therefore do not use the cookie for authentication. This is knows as cross site websocket hijacking CSWSH.

Phoenix has guides on how to handle authentication with channels and LV. Follow them for a secure approach to authentication over websockets.

ku1ik

ku1ik

CSWSH is real, and I believe it’s wise that Phoenix doesn’t make it too easy :+1:

In my specific case, the websocket URL is unique and unguessable (contains long, random token), and I need user ID from the session to verify if the resource is owned by the user. Given one needs to obtain the secret websocket URL first, which is only accessible to the authenticated user, I believe in my case there’s no practical risk.

I’ve settled on the following code to extract user ID from the session in cowboy_websocket handler:

@session_key Keyword.fetch!(Application.compile_env!(:asciinema, :session_opts), :key)
@signing_salt Keyword.fetch!(Application.compile_env!(:asciinema, :session_opts), :signing_salt)

defp user_id_from_session(req) do
  cookies = :cowboy_req.parse_cookies(req)

  with {_, cookie} <- List.keyfind(cookies, @session_key, 0) do
    secret_key_base = Application.fetch_env!(:asciinema, Endpoint)[:secret_key_base]
    conn = %{secret_key_base: secret_key_base}
    opts = Plug.Session.COOKIE.init(signing_salt: @signing_salt)
    {:term, session} = Plug.Session.COOKIE.get(conn, cookie, opts)

    session["user_id"]
  end
end
— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
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 &amp; 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
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews