@@ -17,137 +17,62 @@ defmodule Sentry.PlugTest do
1717 )
1818 end
1919
20- test "exception makes call to Sentry API" do
20+ test "default data scrubbing" do
21+ Code . compile_string ( """
22+ defmodule DefaultConfigApp do
23+ use Plug.Router
24+ use Plug.ErrorHandler
25+ use Sentry.Plug
26+ plug :match
27+ plug :dispatch
28+ forward("/", to: Sentry.ExampleApp)
29+ end
30+ """ )
31+
2132 bypass = Bypass . open ( )
2233
2334 Bypass . expect ( bypass , fn conn ->
2435 { :ok , body , conn } = Plug.Conn . read_body ( conn )
25- assert body =~ "RuntimeError"
26- assert body =~ "ExampleApp"
27- assert conn . request_path == "/api/1/store/"
28- assert conn . method == "POST"
36+ json = Poison . decode! ( body )
37+ assert json [ "request" ] [ "cookies" ] == % { }
38+ assert json [ "request" ] [ "headers" ] == % { "content-type" => "application/json" }
2939 Plug.Conn . resp ( conn , 200 , ~s< {"id": "340"}> )
3040 end )
3141
3242 modify_env ( :sentry , dsn: "http://public:secret@localhost:#{ bypass . port } /1" )
3343
3444 assert_raise ( RuntimeError , "Error" , fn ->
35- conn ( :get , "/error_route" )
36- |> Sentry.ExampleApp . call ( [ ] )
37- end )
38- end
39-
40- test "builds request data" do
41- conn =
42- conn ( :get , "/error_route?key=value" )
43- |> put_req_cookie ( "cookie_key" , "cookie_value" )
44- |> put_req_header ( "accept-language" , "en-US" )
45-
46- request_data =
47- Sentry.Plug . build_request_interface_data (
48- conn ,
49- header_scrubber: & Sentry.Plug . default_header_scrubber / 1
50- )
51-
52- assert request_data [ :url ] =~ ~r/ \/ error_route$/
53- assert request_data [ :method ] == "GET"
54- assert request_data [ :data ] == % { }
55-
56- assert request_data [ :headers ] == % {
57- "cookie" => "cookie_key=cookie_value" ,
58- "accept-language" => "en-US"
59- }
60-
61- assert request_data [ :cookies ] == % { "cookie_key" => "cookie_value" }
62- assert request_data [ :query_string ] == "key=value"
63- assert is_binary ( request_data [ :env ] [ "REMOTE_ADDR" ] )
64- assert is_integer ( request_data [ :env ] [ "REMOTE_PORT" ] )
65- assert is_binary ( request_data [ :env ] [ "SERVER_NAME" ] )
66- assert is_integer ( request_data [ :env ] [ "SERVER_PORT" ] )
67- end
68-
69- test "handles data scrubbing" do
70- conn =
71- conn ( :post , "/error_route" , % {
72- "hello" => "world" ,
73- "password" => "test" ,
74- "cc" => "4242424242424242"
75- } )
76- |> put_req_cookie ( "cookie_key" , "cookie_value" )
77- |> put_req_header ( "accept-language" , "en-US" )
78- |> put_req_header ( "authorization" , "ignorme" )
79-
80- scrubber = fn conn ->
81- conn . params
82- |> Enum . filter ( fn { key , val } ->
83- # Matches Credit Cards
84- ! ( key in ~w( password passwd secret credit_card) ||
85- Regex . match? ( ~r/ ^(?:\d [ -]*?){13,16}$/ , val ) )
86- end )
87- |> Enum . into ( % { } )
88- end
89-
90- options = [ body_scrubber: scrubber , header_scrubber: & Sentry.Plug . default_header_scrubber / 1 ]
91- request_data = Sentry.Plug . build_request_interface_data ( conn , options )
92- assert request_data [ :method ] == "POST"
93- assert request_data [ :data ] == % { "hello" => "world" }
94-
95- assert request_data [ :headers ] == % {
96- "cookie" => "cookie_key=cookie_value" ,
97- "accept-language" => "en-US" ,
98- "content-type" => "multipart/mixed; boundary=plug_conn_test"
99- }
100-
101- assert request_data [ :cookies ] == % { "cookie_key" => "cookie_value" }
102- end
103-
104- test "gets request_id" do
105- conn =
106- conn ( :get , "/error_route" )
107- |> Plug.Conn . put_resp_header ( "x-request-id" , "my_request_id" )
108-
109- request_data =
110- Sentry.Plug . build_request_interface_data ( conn , request_id_header: "x-request-id" )
111-
112- assert request_data [ :env ] [ "REQUEST_ID" ] == "my_request_id"
113- end
114-
115- test "default data scrubbing" do
116- conn =
11745 conn ( :post , "/error_route" , % {
11846 "secret" => "world" ,
11947 "password" => "test" ,
12048 "passwd" => "4242424242424242" ,
12149 "credit_card" => "4197 7215 7810 8280" ,
12250 "count" => 334 ,
123- "is_admin" => false ,
12451 "cc" => "4197-7215-7810-8280" ,
12552 "another_cc" => "4197721578108280" ,
12653 "user" => % { "password" => "mypassword" }
12754 } )
128-
129- request_data =
130- Sentry.Plug . build_request_interface_data (
131- conn ,
132- body_scrubber: & Sentry.Plug . default_body_scrubber / 1
133- )
134-
135- assert request_data [ :method ] == "POST"
136-
137- assert request_data [ :data ] == % {
138- "secret" => "*********" ,
139- "password" => "*********" ,
140- "count" => 334 ,
141- "is_admin" => false ,
142- "passwd" => "*********" ,
143- "credit_card" => "*********" ,
144- "cc" => "*********" ,
145- "another_cc" => "*********" ,
146- "user" => % { "password" => "*********" }
147- }
55+ |> update_req_cookie ( "secret" , "secretvalue" )
56+ |> update_req_cookie ( "regular" , "value" )
57+ |> put_req_header ( "authorization" , "secrets" )
58+ |> put_req_header ( "authentication" , "secrets" )
59+ |> put_req_header ( "content-type" , "application/json" )
60+ |> DefaultConfigApp . call ( [ ] )
61+ end )
14862 end
14963
15064 test "handles data scrubbing with file upload" do
65+ Code . compile_string ( """
66+ defmodule ScrubbingWithFileApp do
67+ use Plug.Router
68+ use Plug.ErrorHandler
69+ use Sentry.Plug
70+ plug :match
71+ plug :dispatch
72+ forward("/", to: Sentry.ExampleApp)
73+ end
74+ """ )
75+
15176 bypass = Bypass . open ( )
15277
15378 Bypass . expect ( bypass , fn conn ->
@@ -166,7 +91,57 @@ defmodule Sentry.PlugTest do
16691 |> put_req_cookie ( "cookie_key" , "cookie_value" )
16792 |> put_req_header ( "accept-language" , "en-US" )
16893 |> put_req_header ( "authorization" , "ignorme" )
169- |> Sentry.ExampleApp . call ( [ ] )
94+ |> ScrubbingWithFileApp . call ( [ ] )
95+ end )
96+ end
97+
98+ test "custom cookie scrubbing" do
99+ Code . compile_string ( """
100+ defmodule CustomCookieScrubberApp do
101+ use Plug.Router
102+ use Plug.ErrorHandler
103+ use Sentry.Plug, cookie_scrubber: fn(conn) ->
104+ Map.take(conn.req_cookies, ["regular"])
105+ end
106+ plug :match
107+ plug :dispatch
108+ forward("/", to: Sentry.ExampleApp)
109+ end
110+ """ )
111+
112+ bypass = Bypass . open ( )
113+
114+ Bypass . expect ( bypass , fn conn ->
115+ { :ok , body , conn } = Plug.Conn . read_body ( conn )
116+ json = Poison . decode! ( body )
117+ assert json [ "request" ] [ "cookies" ] == % { "regular" => "value" }
118+ Plug.Conn . resp ( conn , 200 , ~s< {"id": "340"}> )
119+ end )
120+
121+ modify_env ( :sentry , dsn: "http://public:secret@localhost:#{ bypass . port } /1" )
122+
123+ assert_raise ( RuntimeError , "Error" , fn ->
124+ conn ( :get , "/error_route" )
125+ |> update_req_cookie ( "secret" , "secretvalue" )
126+ |> update_req_cookie ( "regular" , "value" )
127+ |> CustomCookieScrubberApp . call ( [ ] )
170128 end )
171129 end
130+
131+ defp update_req_cookie ( conn , name , value ) do
132+ req_headers =
133+ conn . req_headers
134+ |> Enum . into ( % { } )
135+ |> Map . update ( "cookie" , "#{ name } =#{ value } " , fn val ->
136+ Plug.Conn.Cookies . decode ( val )
137+ |> Map . put ( name , value )
138+ |> Enum . map ( fn { cookie_name , cookie_value } ->
139+ "#{ cookie_name } =#{ cookie_value } "
140+ end )
141+ |> Enum . join ( "; " )
142+ end )
143+ |> Enum . into ( [ ] )
144+
145+ % Plug.Conn { conn | req_headers: req_headers }
146+ end
172147end
0 commit comments