Skip to content

Commit a0a80ab

Browse files
Merge pull request #417 from getsentry/public-scrub-map
make Sentry.PlugContext.scrub_map public
2 parents 49e6f1d + 005bcad commit a0a80ab

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

lib/sentry/plug_context.ex

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -167,25 +167,44 @@ defmodule Sentry.PlugContext do
167167

168168
@spec default_body_scrubber(Plug.Conn.t()) :: map()
169169
def default_body_scrubber(conn) do
170-
scrub_map(conn.params)
170+
scrub_map(conn.params, @default_scrubbed_param_keys)
171171
end
172172

173-
defp scrub_map(map) do
173+
@doc """
174+
Recursively scrubs a map that may have nested maps
175+
176+
Accepts a list of keys to scrub, and a list of options to configure
177+
178+
### Options
179+
* `:scrubbed_values_regular_expressions` - A list of regular expressions.
180+
Any binary values within the map that match any of the regular expressions
181+
will be scrubbed. Defaults to `[~r/^(?:\d[ -]*?){13,16}$/]`.
182+
* `:scrubbed_value` - The value to replace scrubbed values with.
183+
Defaults to `"*********"`.
184+
"""
185+
@spec scrub_map(map(), list(String.t()), keyword()) :: map()
186+
def scrub_map(map, scrubbed_keys, opts \\ []) do
187+
scrubbed_values_regular_expressions =
188+
Keyword.get(opts, :scrubbed_values_regular_expressions, [@credit_card_regex])
189+
190+
scrubbed_value = Keyword.get(opts, :scrubbed_value, @scrubbed_value)
191+
174192
Enum.into(map, %{}, fn {key, value} ->
175193
value =
176194
cond do
177-
Enum.member?(@default_scrubbed_param_keys, key) ->
178-
@scrubbed_value
195+
Enum.member?(scrubbed_keys, key) ->
196+
scrubbed_value
179197

180-
is_binary(value) && Regex.match?(@credit_card_regex, value) ->
181-
@scrubbed_value
198+
is_binary(value) &&
199+
Enum.any?(scrubbed_values_regular_expressions, &Regex.match?(&1, value)) ->
200+
scrubbed_value
182201

183202
is_map(value) && Map.has_key?(value, :__struct__) ->
184203
Map.from_struct(value)
185-
|> scrub_map()
204+
|> scrub_map(scrubbed_keys, opts)
186205

187206
is_map(value) ->
188-
scrub_map(value)
207+
scrub_map(value, scrubbed_keys, opts)
189208

190209
true ->
191210
value

0 commit comments

Comments
 (0)