forked from getsentry/sentry-elixir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplug_test.exs
237 lines (199 loc) · 7.3 KB
/
plug_test.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
defmodule Sentry.PlugTest do
use ExUnit.Case
use Plug.Test
import Sentry.TestEnvironmentHelper
test "non-existent route exceptions are ignored" do
exception = %FunctionClauseError{arity: 4, function: :do_match, module: Sentry.ExampleApp}
assert ^exception =
assert_raise(
FunctionClauseError,
"no function clause matching in Sentry.ExampleApp.do_match/4",
fn ->
conn(:get, "/not_found")
|> Sentry.ExampleApp.call([])
end
)
end
test "overriding handle_errors/2" do
Code.compile_string("""
defmodule OverrideApp do
use Plug.Router
use Plug.ErrorHandler
use Sentry.Plug
plug :match
plug :dispatch
forward("/", to: Sentry.ExampleApp)
defp handle_errors(conn, %{kind: _kind, reason: _reason, stack: _stack} = error) do
super(conn, error)
send_resp(conn, conn.status, "Something went terribly wrong")
end
end
""")
bypass = Bypass.open()
Bypass.expect(bypass, fn conn ->
Plug.Conn.resp(conn, 200, ~s<{"id": "340"}>)
end)
modify_env(:sentry, dsn: "http://public:secret@localhost:#{bypass.port}/1")
conn = conn(:post, "/error_route", %{})
assert_raise(Plug.Conn.WrapperError, "** (RuntimeError) Error", fn ->
OverrideApp.call(conn, [])
end)
assert {500, _headers, "Something went terribly wrong"} = sent_resp(conn)
end
test "default data scrubbing" do
Code.compile_string("""
defmodule DefaultConfigApp do
use Plug.Router
use Plug.ErrorHandler
use Sentry.Plug
plug :match
plug :dispatch
forward("/", to: Sentry.ExampleApp)
end
""")
bypass = Bypass.open()
Bypass.expect(bypass, fn conn ->
{:ok, body, conn} = Plug.Conn.read_body(conn)
json = Jason.decode!(body)
assert json["request"]["cookies"] == %{}
assert json["request"]["headers"] == %{"content-type" => "application/json"}
Plug.Conn.resp(conn, 200, ~s<{"id": "340"}>)
end)
modify_env(:sentry, dsn: "http://public:secret@localhost:#{bypass.port}/1")
assert_raise(Plug.Conn.WrapperError, "** (RuntimeError) Error", fn ->
conn(:post, "/error_route", %{
"secret" => "world",
"password" => "test",
"passwd" => "4242424242424242",
"credit_card" => "4197 7215 7810 8280",
"count" => 334,
"cc" => "4197-7215-7810-8280",
"another_cc" => "4197721578108280",
"user" => %{"password" => "mypassword"}
})
|> update_req_cookie("secret", "secretvalue")
|> update_req_cookie("regular", "value")
|> put_req_header("authorization", "secrets")
|> put_req_header("authentication", "secrets")
|> put_req_header("content-type", "application/json")
|> DefaultConfigApp.call([])
end)
end
test "handles data scrubbing with file upload" do
Code.compile_string("""
defmodule ScrubbingWithFileApp do
use Plug.Router
use Plug.ErrorHandler
use Sentry.Plug
plug :match
plug :dispatch
forward("/", to: Sentry.ExampleApp)
end
""")
bypass = Bypass.open()
Bypass.expect(bypass, fn conn ->
{:ok, body, conn} = Plug.Conn.read_body(conn)
json = Jason.decode!(body)
assert is_map(json["request"]["data"]["image"])
assert json["request"]["data"]["password"] == "*********"
Plug.Conn.resp(conn, 200, ~s<{"id": "340"}>)
end)
modify_env(:sentry, dsn: "http://public:secret@localhost:#{bypass.port}/1")
upload = %Plug.Upload{path: "test/fixtures/my_image.png", filename: "my_image.png"}
assert_raise(Plug.Conn.WrapperError, "** (RuntimeError) Error", fn ->
conn(:post, "/error_route", %{"image" => upload, "password" => "my_password"})
|> put_req_cookie("cookie_key", "cookie_value")
|> put_req_header("accept-language", "en-US")
|> put_req_header("authorization", "ignorme")
|> ScrubbingWithFileApp.call([])
end)
end
test "custom cookie scrubbing" do
Code.compile_string("""
defmodule CustomCookieScrubberApp do
use Plug.Router
use Plug.ErrorHandler
use Sentry.Plug, cookie_scrubber: fn(conn) ->
Map.take(conn.req_cookies, ["regular"])
end
plug :match
plug :dispatch
forward("/", to: Sentry.ExampleApp)
end
""")
bypass = Bypass.open()
Bypass.expect(bypass, fn conn ->
{:ok, body, conn} = Plug.Conn.read_body(conn)
json = Jason.decode!(body)
assert json["request"]["cookies"] == %{"regular" => "value"}
Plug.Conn.resp(conn, 200, ~s<{"id": "340"}>)
end)
modify_env(:sentry, dsn: "http://public:secret@localhost:#{bypass.port}/1")
assert_raise(Plug.Conn.WrapperError, "** (RuntimeError) Error", fn ->
conn(:get, "/error_route")
|> update_req_cookie("secret", "secretvalue")
|> update_req_cookie("regular", "value")
|> CustomCookieScrubberApp.call([])
end)
end
test "collects feedback" do
Code.compile_string("""
defmodule CollectFeedbackApp do
use Plug.Router
use Plug.ErrorHandler
use Sentry.Plug, collect_feedback: [enabled: true, options: %{title: "abc-123"}]
plug :match
plug :dispatch
forward("/", to: Sentry.ExampleApp)
end
""")
bypass = Bypass.open()
Bypass.expect(bypass, fn conn ->
{:ok, _body, _conn} = Plug.Conn.read_body(conn)
Plug.Conn.resp(conn, 200, ~s<{"id": "340"}>)
end)
modify_env(:sentry, dsn: "http://public:secret@localhost:#{bypass.port}/1")
conn =
conn(:get, "/error_route")
|> Plug.Conn.put_req_header("accept", "text/html")
assert_raise(Plug.Conn.WrapperError, "** (RuntimeError) Error", fn ->
CollectFeedbackApp.call(conn, [])
end)
assert_received {:plug_conn, :sent}
assert {500, _headers, body} = sent_resp(conn)
assert body =~ "340"
assert body =~ "sentry-cdn"
assert body =~ ~s{"title":"abc-123"}
end
test "request url" do
# Default ports
conn = conn(:get, "http://www.example.com:80/error_route")
%{url: url} = Sentry.Plug.build_request_interface_data(conn, [])
assert url == "http://www.example.com/error_route"
conn = conn(:get, "https://www.example.com:443/error_route")
%{url: url} = Sentry.Plug.build_request_interface_data(conn, [])
assert url == "https://www.example.com/error_route"
# Custom ports
conn = conn(:get, "http://www.example.com:1234/error_route")
%{url: url} = Sentry.Plug.build_request_interface_data(conn, [])
assert url == "http://www.example.com:1234/error_route"
conn = conn(:get, "https://www.example.com:1234/error_route")
%{url: url} = Sentry.Plug.build_request_interface_data(conn, [])
assert url == "https://www.example.com:1234/error_route"
end
defp update_req_cookie(conn, name, value) do
req_headers =
conn.req_headers
|> Enum.into(%{})
|> Map.update("cookie", "#{name}=#{value}", fn val ->
Plug.Conn.Cookies.decode(val)
|> Map.put(name, value)
|> Enum.map(fn {cookie_name, cookie_value} ->
"#{cookie_name}=#{cookie_value}"
end)
|> Enum.join("; ")
end)
|> Enum.into([])
%Plug.Conn{conn | req_headers: req_headers}
end
end