Skip to content

Commit 5dc3c93

Browse files
Merge pull request #442 from joladev/support-lists-in-scrubbing
Adds support for lists in scrubbing logic
2 parents 2948492 + e47eb6d commit 5dc3c93

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

lib/sentry/plug_context.ex

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ defmodule Sentry.PlugContext do
171171
end
172172

173173
@doc """
174-
Recursively scrubs a map that may have nested maps
174+
Recursively scrubs a map that may have nested maps or lists
175175
176176
Accepts a list of keys to scrub, and a list of options to configure
177177
@@ -206,11 +206,35 @@ defmodule Sentry.PlugContext do
206206
is_map(value) ->
207207
scrub_map(value, scrubbed_keys, opts)
208208

209+
is_list(value) ->
210+
scrub_list(value, scrubbed_keys, opts)
211+
209212
true ->
210213
value
211214
end
212215

213216
{key, value}
214217
end)
215218
end
219+
220+
@spec scrub_list(list(), list(String.t()), keyword()) :: list()
221+
defp scrub_list(list, scrubbed_keys, opts) do
222+
Enum.map(list, fn value ->
223+
cond do
224+
is_map(value) && Map.has_key?(value, :__struct__) ->
225+
value
226+
|> Map.from_struct()
227+
|> scrub_map(scrubbed_keys, opts)
228+
229+
is_map(value) ->
230+
scrub_map(value, scrubbed_keys, opts)
231+
232+
is_list(value) ->
233+
scrub_list(value, scrubbed_keys, opts)
234+
235+
true ->
236+
value
237+
end
238+
end)
239+
end
216240
end

test/plug_context_test.exs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,10 @@ defmodule Sentry.PlugContextTest do
8282
"count" => 334,
8383
"cc" => "4197-7215-7810-8280",
8484
"another_cc" => "4197721578108280",
85-
"user" => %{"password" => "mypassword"}
85+
"user" => %{"password" => "mypassword"},
86+
"payments" => [
87+
%{"yet_another_cc" => "4197-7215-7810-8280"}
88+
]
8689
})
8790
|> put_req_cookie("secret", "secretvalue")
8891
|> put_req_cookie("regular", "value")
@@ -101,7 +104,10 @@ defmodule Sentry.PlugContextTest do
101104
"passwd" => "*********",
102105
"password" => "*********",
103106
"secret" => "*********",
104-
"user" => %{"password" => "*********"}
107+
"user" => %{"password" => "*********"},
108+
"payments" => [
109+
%{"yet_another_cc" => "*********"}
110+
]
105111
}
106112
end
107113

0 commit comments

Comments
 (0)