Skip to content

Commit 47bbb6e

Browse files
committed
hide the value of :password when the sasl logger is activated
1 parent 0bb7ef8 commit 47bbb6e

File tree

6 files changed

+80
-12
lines changed

6 files changed

+80
-12
lines changed

lib/mongo/app.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ defmodule Mongo.App do
77
children = [
88
worker(Mongo.IdServer, []),
99
worker(Mongo.PBKDF2Cache, []),
10+
worker(Mongo.PasswordSafe, []),
1011
worker(:gen_event, [local: Mongo.Events])
1112
]
1213

lib/mongo/auth.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
defmodule Mongo.Auth do
22
@moduledoc false
33

4+
alias Mongo.PasswordSafe
5+
46
def run(opts, state) do
57

68
db = opts[:database]
@@ -29,13 +31,13 @@ defmodule Mongo.Auth do
2931

3032
defp setup(opts) do
3133
username = opts[:username]
32-
password = opts[:password]
34+
password = PasswordSafe.get_pasword()
3335
auth = opts[:auth] || []
3436

3537
auth =
3638
Enum.map(auth, fn opts ->
3739
username = opts[:username]
38-
password = opts[:password]
40+
password = PasswordSafe.get_pasword()
3941
{username, password}
4042
end)
4143

lib/mongo/password_safe.ex

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
defmodule Mongo.PasswordSafe do
2+
@moduledoc """
3+
The password safe stores the password while parsing the url and/or the options to avoid it from logging while the sasl logger is activated.
4+
"""
5+
6+
@me __MODULE__
7+
8+
use GenServer
9+
10+
def start_link(_ \\ nil) do
11+
GenServer.start_link(__MODULE__, [], name: @me)
12+
end
13+
14+
def set_password(password) do
15+
GenServer.cast(@me, {:set, password})
16+
end
17+
18+
def get_pasword() do
19+
GenServer.call(@me, :get)
20+
end
21+
22+
def init([]) do
23+
{:ok, nil}
24+
end
25+
26+
def handle_cast({:set, password}, data) do
27+
{:noreply, password}
28+
end
29+
30+
def handle_call(:get, _from, password) do
31+
{:reply, password, password}
32+
end
33+
34+
end

lib/mongo/url_parser.ex

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,18 @@ defmodule Mongo.UrlParser do
9999
|> Macro.underscore()
100100
|> String.to_atom()
101101

102-
value = decode_password(key, value)
102+
value = decode_percent(key, value)
103103

104104
Keyword.put(opts, @driver_option_map[key] || key, value)
105105
end
106106
end
107107

108108
defp add_option(_other, acc), do: acc
109109

110-
defp decode_password(:username, value), do: URI.decode_www_form(value)
111-
defp decode_password(:password, value), do: URI.decode_www_form(value)
112-
defp decode_password(_other, value), do: value
110+
defp decode_percent(:username, value), do: URI.decode_www_form(value)
111+
defp decode_percent(:password, value), do: URI.decode_www_form(value)
112+
defp decode_percent(_other, value), do: value
113+
113114

114115
defp parse_query_options(opts, %{"options" => options}) when is_binary(options) do
115116
options
@@ -159,6 +160,16 @@ defmodule Mongo.UrlParser do
159160
{:ok, hosts}
160161
end
161162

163+
defp hide_password(opts) do
164+
case Keyword.get(opts, :password) do
165+
nil -> opts
166+
value ->
167+
with :ok <- Mongo.PasswordSafe.set_password(value) do
168+
Keyword.put(opts, :password, "*****")
169+
end
170+
end
171+
end
172+
162173
@spec parse_url(Keyword.t()) :: Keyword.t()
163174
def parse_url(opts) when is_list(opts) do
164175
with {url, opts} when is_binary(url) <- Keyword.pop(opts, :url),
@@ -172,6 +183,7 @@ defmodule Mongo.UrlParser do
172183
else
173184
_other -> opts
174185
end
186+
|> hide_password()
175187
end
176188

177189
def parse_url(opts), do: opts

test/mongo/password_safe_test.exs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
defmodule Mongo.PasswordSafeTest do
2+
@moduledoc false
3+
4+
use ExUnit.Case, async: false
5+
alias Mongo.UrlParser
6+
alias Mongo.PasswordSafe
7+
8+
#
9+
# When the sasl logger is activated like `--logger-sasl-reports true` then the supervisor reports all parameters when it starts a process. So, the password should not
10+
# used in the options
11+
#
12+
describe "parse_url and hide the password in options" do
13+
test "encoded password" do
14+
url = "mongodb://myDBReader:D1fficultP%[email protected]:27017/admin"
15+
opts = UrlParser.parse_url([url: url])
16+
17+
assert "*****" == Keyword.get(opts, :password)
18+
assert "D1fficultP@ssw0rd" == PasswordSafe.get_pasword()
19+
end
20+
end
21+
22+
end

test/mongo/url_parser_test.exs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ defmodule Mongo.UrlParserTest do
1414
"mongodb://user:[email protected]:27017,seed2.domain.com:27017,seed3.domain.com:27017/db_name?ssl=true&replicaSet=set-name&authSource=admin&maxPoolSize=5"
1515

1616
assert UrlParser.parse_url(url: url) == [
17+
password: "*****",
1718
username: "user",
18-
password: "password",
1919
database: "db_name",
2020
pool_size: 5,
2121
auth_source: "admin",
@@ -49,8 +49,8 @@ defmodule Mongo.UrlParserTest do
4949
test "url srv with user" do
5050
assert UrlParser.parse_url(url: "mongodb+srv://user:[email protected]") ==
5151
[
52+
password: "*****",
5253
username: "user",
53-
password: "password",
5454
ssl: true,
5555
auth_source: "thisDB",
5656
set_name: "repl0",
@@ -73,18 +73,15 @@ defmodule Mongo.UrlParserTest do
7373
end
7474
end
7575

76-
test "encoded password" do
77-
76+
test "encoded user" do
7877
real_username = "@:/skøl:@/"
7978
real_password = "@æœ{}%e()}@"
8079

8180
encoded_username = URI.encode_www_form(real_username)
8281
encoded_password = URI.encode_www_form(real_password)
8382
url = "mongodb://#{encoded_username}:#{encoded_password}@mymongodbserver:27017/admin"
8483
opts = UrlParser.parse_url(url: url)
85-
password = Keyword.get(opts, :password)
8684
username = Keyword.get(opts, :username)
87-
assert password == real_password
8885
assert username == real_username
8986
end
9087

0 commit comments

Comments
 (0)