Skip to content

Commit

Permalink
support unicode in username, separate auth_request for scram and md5
Browse files Browse the repository at this point in the history
  • Loading branch information
abc3 committed Oct 9, 2023
1 parent 529c9c9 commit a1d12c2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
8 changes: 4 additions & 4 deletions lib/supavisor/client_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,8 @@ defmodule Supavisor.ClientHandler do
{user, nil}

matches ->
{pos, _} = List.last(matches)
{name, "." <> external_id} = String.split_at(user, pos)
{pos, 1} = List.last(matches)
<<name::size(pos)-binary, ?., external_id::binary>> = user
{name, external_id}
end
end
Expand Down Expand Up @@ -456,7 +456,7 @@ defmodule Supavisor.ClientHandler do
@spec handle_exchange(S.sock(), {atom(), fun()}) :: {:ok, binary() | nil} | {:error, String.t()}
def handle_exchange({_, socket} = sock, {:auth_query_md5 = method, secrets}) do
salt = :crypto.strong_rand_bytes(4)
:ok = sock_send(sock, Server.auth_request(:md5, salt))
:ok = sock_send(sock, Server.md5_request(salt))

with {:ok,
%{
Expand All @@ -472,7 +472,7 @@ defmodule Supavisor.ClientHandler do
end

def handle_exchange({_, socket} = sock, {method, secrets}) do
:ok = sock_send(sock, Server.auth_request())
:ok = sock_send(sock, Server.scram_request())

with {:ok,
%{
Expand Down
16 changes: 9 additions & 7 deletions lib/supavisor/protocol/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ defmodule Supavisor.Protocol.Server do
@authentication_ok <<?R, 8::32, 0::32>>
@ready_for_query <<?Z, 5::32, ?I>>
@ssl_request <<8::32, 1234::16, 5679::16>>
@auth_request <<?R, 23::32, 10::32, "SCRAM-SHA-256", 0, 0>>
@scram_request <<?R, 23::32, 10::32, "SCRAM-SHA-256", 0, 0>>

defmodule Pkt do
@moduledoc "Representing a packet structure with tag, length, and payload fields."
Expand Down Expand Up @@ -265,12 +265,14 @@ defmodule Supavisor.Protocol.Server do
end
end

@spec auth_request(:scram | :md5) :: iodata
def auth_request(method \\ :scram, salt \\ nil) do
case method do
:scram -> @auth_request
:md5 -> [<<?R, 12::32, 5::32>>, salt]
end
@spec scram_request() :: iodata
def scram_request() do
@scram_request
end

@spec md5_request(<<_::32>>) :: iodata
def md5_request(salt) do
[<<?R, 12::32, 5::32>>, salt]
end

@spec exchange_first_message(binary, binary | boolean, pos_integer) :: binary
Expand Down
7 changes: 7 additions & 0 deletions test/supavisor/client_handler_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,12 @@ defmodule Supavisor.ClientHandlerTest do
assert user1 == user
assert external_id1 == external_id
end

test "unicode in username" do
payload = %{"user" => "тестовe.імʼя.external_id"}
{name, external_id} = ClientHandler.parse_user_info(payload)
assert name == "тестовe.імʼя"
assert external_id == "external_id"
end
end
end

0 comments on commit a1d12c2

Please sign in to comment.