Skip to content

Commit

Permalink
feat: auth error caused by invalid characters in user or db_name (#334)
Browse files Browse the repository at this point in the history
  • Loading branch information
abc3 authored May 2, 2024
1 parent dbb48ff commit f468217
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1.49
1.1.50
26 changes: 18 additions & 8 deletions lib/supavisor/client_handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -154,16 +154,26 @@ defmodule Supavisor.ClientHandler do
Logger.debug("ClientHandler: Client startup message: #{inspect(hello)}")
{type, {user, tenant_or_alias, db_name}} = HH.parse_user_info(hello.payload)

log_level =
case hello.payload["options"]["log_level"] do
nil -> nil
level -> String.to_existing_atom(level)
end
not_allowed = ["\"", "\\"]

if String.contains?(user, not_allowed) or String.contains?(db_name, not_allowed) do
reason = "Invalid characters in user or db_name"
Logger.error("ClientHandler: #{inspect(reason)}")
Telem.client_join(:fail, data.id)
HH.send_error(data.sock, "XX000", "Authentication error, reason: #{inspect(reason)}")
{:stop, {:shutdown, :invalid_characters}}
else
log_level =
case hello.payload["options"]["log_level"] do
nil -> nil
level -> String.to_existing_atom(level)
end

H.set_log_level(log_level)
H.set_log_level(log_level)

{:keep_state, %{data | log_level: log_level},
{:next_event, :internal, {:hello, {type, {user, tenant_or_alias, db_name}}}}}
{:keep_state, %{data | log_level: log_level},
{:next_event, :internal, {:hello, {type, {user, tenant_or_alias, db_name}}}}}
end

{:error, error} ->
Logger.error("ClientHandler: Client startup message error: #{inspect(error)}")
Expand Down
22 changes: 22 additions & 0 deletions test/integration/proxy_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,28 @@ defmodule Supavisor.Integration.ProxyTest do
assert [%Postgrex.Result{rows: [["1"]]}] = P.SimpleConnection.call(pid, {:query, "select 1;"})
end

test "invalid characters in user or db_name" do
Process.flag(:trap_exit, true)
db_conf = Application.get_env(:supavisor, Repo)

url =
"postgresql://user\"user.#{@tenant}:#{db_conf[:password]}@#{db_conf[:hostname]}:#{Application.get_env(:supavisor, :proxy_port_transaction)}/postgres\\\\\\\\\"\\"

assert =
{:error,
{_,
{:stop,
%Postgrex.Error{
postgres: %{
code: :internal_error,
message: "Authentication error, reason: \"Invalid characters in user or db_name\"",
pg_code: "XX000",
severity: "FATAL",
unknown: "FATAL"
}
}, _}}} = parse_uri(url) |> single_connection()
end

defp single_connection(db_conf, c_port \\ nil) when is_list(db_conf) do
port = c_port || db_conf[:port]

Expand Down

0 comments on commit f468217

Please sign in to comment.