Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/postgrex/protocol.ex
Original file line number Diff line number Diff line change
Expand Up @@ -922,6 +922,7 @@ defmodule Postgrex.Protocol do
buffer
)
else
status = %{status | opts: Keyword.delete(status.opts, :password)}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this!

💡 One suggestion: the password is not the only secret in the connect state. status.ssl can hold the client private key and its passphrase, and status.opts also has the username, hostname and database, which DBConnection treats as sensitive.

Suggested change
status = %{status | opts: Keyword.delete(status.opts, :password)}
status = Map.drop(status, [:opts, :ssl])

init_recv(s, status, buffer)
end

Expand Down
35 changes: 35 additions & 0 deletions test/protocol_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,41 @@ defmodule Postgrex.ProtocolTest do
assert_receive {:sent, <<?S, 4::32>>}
end

test "connection errors after authentication do not include the password" do
{:ok, listener} = :gen_tcp.listen(0, [:binary, active: false])
{:ok, port} = :inet.port(listener)

server =
Task.async(fn ->
{:ok, socket} = :gen_tcp.accept(listener)
{:ok, <<size::32>>} = :gen_tcp.recv(socket, 4)
{:ok, _startup} = :gen_tcp.recv(socket, size - 4)

:ok =
:gen_tcp.send(socket, [
backend_message(?R, <<0::32>>),
backend_message(?A, [<<123::32>>, "events", 0, "ready", 0])
])

:gen_tcp.close(socket)
end)

opts =
Postgrex.Utils.default_opts(
hostname: "localhost",
port: port,
database: "postgrex_test",
password: "do-not-log-me",
types: Postgrex.DefaultTypes
)

error = assert_raise MatchError, fn -> Protocol.connect(opts) end
Task.await(server)
:gen_tcp.close(listener)

refute Exception.message(error) =~ "do-not-log-me"
end

defp backend_message(type, data) do
[type, <<IO.iodata_length(data) + 4::32>>, data]
end
Expand Down
Loading