Skip to content

Commit 9ef357f

Browse files
Merge pull request #208 from getsentry/plug-upload
Handle Plug Upload during scrubbing
2 parents 1c3472a + d871a50 commit 9ef357f

File tree

4 files changed

+35
-5
lines changed

4 files changed

+35
-5
lines changed

lib/sentry/plug.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,10 @@ defmodule Sentry.Plug do
176176
@scrubbed_value
177177
is_binary(value) && Regex.match?(@credit_card_regex, value) ->
178178
@scrubbed_value
179-
is_map(value) -> scrub_map(value)
180-
true -> value
179+
is_map(value) && !Map.has_key?(value, :__struct__) ->
180+
scrub_map(value)
181+
true ->
182+
value
181183
end
182184

183185
{key, value}

test/plug_test.exs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,26 @@ defmodule Sentry.PlugTest do
105105
"is_admin" => false, "passwd" => "*********", "credit_card" => "*********", "cc" => "*********",
106106
"another_cc" => "*********", "user" => %{"password" => "*********"}}
107107
end
108+
109+
test "handles data scrubbing with file upload" do
110+
bypass = Bypass.open
111+
Bypass.expect bypass, fn conn ->
112+
{:ok, body, conn} = Plug.Conn.read_body(conn)
113+
json = Poison.decode!(body)
114+
assert is_map(json["request"]["data"]["image"])
115+
assert json["request"]["data"]["password"] == "*********"
116+
Plug.Conn.resp(conn, 200, ~s<{"id": "340"}>)
117+
end
118+
119+
modify_env(:sentry, dsn: "http://public:secret@localhost:#{bypass.port}/1")
120+
upload = %Plug.Upload{path: "test/fixtures/my_image.png", filename: "my_image.png"}
121+
122+
assert_raise(RuntimeError, "Error", fn ->
123+
conn(:post, "/error_route", %{"image" => upload, "password" => "my_password"})
124+
|> put_req_cookie("cookie_key", "cookie_value")
125+
|> put_req_header("accept-language", "en-US")
126+
|> put_req_header("authorization", "ignorme")
127+
|> Sentry.ExampleApp.call([])
128+
end)
129+
end
108130
end

test/sources_test.exs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ defmodule Sentry.SourcesTest do
55

66
test "exception makes call to Sentry API" do
77
correct_context = %{"context_line" => " raise RuntimeError, \"Error\"",
8-
"post_context" => [" end", "", " match \"/error_route\" do"],
9-
"pre_context" => ["", " get \"/error_route\" do", " _ = conn"]
10-
}
8+
"post_context" => [" end", "", " post \"/error_route\" do"],
9+
"pre_context" => ["", " get \"/error_route\" do",
10+
" _ = conn"]
11+
}
1112
bypass = Bypass.open
1213
Bypass.expect bypass, fn conn ->
1314
{:ok, body, conn} = Plug.Conn.read_body(conn)

test/support/test_plug.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ defmodule Sentry.ExampleApp do
1414
raise RuntimeError, "Error"
1515
end
1616

17+
post "/error_route" do
18+
_ = conn
19+
raise RuntimeError, "Error"
20+
end
21+
1722
match "/error_route" do
1823
_ = conn
1924
raise RuntimeError, "Error"

0 commit comments

Comments
 (0)