6
6
class HostAuthorizationTest < ActionDispatch ::IntegrationTest
7
7
App = -> env { [ 200 , { } , %w( Success ) ] }
8
8
9
- test "blocks requests to unallowed host" do
9
+ test "blocks requests to unallowed host with empty body " do
10
10
@app = ActionDispatch ::HostAuthorization . new ( App , %w( only.com ) )
11
11
12
12
get "/"
13
13
14
+ assert_response :forbidden
15
+ assert_empty response . body
16
+ end
17
+
18
+ test "renders debug info when all requests considered as local" do
19
+ @app = ActionDispatch ::HostAuthorization . new ( App , %w( only.com ) )
20
+
21
+ get "/" , env : { "action_dispatch.show_detailed_exceptions" => true }
22
+
14
23
assert_response :forbidden
15
24
assert_match "Blocked host: www.example.com" , response . body
16
25
end
@@ -80,6 +89,7 @@ class HostAuthorizationTest < ActionDispatch::IntegrationTest
80
89
81
90
get "/" , env : {
82
91
"HOST" => "www.example.local" ,
92
+ "action_dispatch.show_detailed_exceptions" => true
83
93
}
84
94
85
95
assert_response :forbidden
@@ -100,6 +110,7 @@ class HostAuthorizationTest < ActionDispatch::IntegrationTest
100
110
101
111
get "/" , env : {
102
112
"HOST" => ".example.com" ,
113
+ "action_dispatch.show_detailed_exceptions" => true
103
114
}
104
115
105
116
assert_response :forbidden
@@ -126,7 +137,7 @@ class HostAuthorizationTest < ActionDispatch::IntegrationTest
126
137
test "sanitizes regular expressions to prevent accidental matches" do
127
138
@app = ActionDispatch ::HostAuthorization . new ( App , [ /w.example.co/ ] )
128
139
129
- get "/"
140
+ get "/" , env : { "action_dispatch.show_detailed_exceptions" => true }
130
141
131
142
assert_response :forbidden
132
143
assert_match "Blocked host: www.example.com" , response . body
@@ -149,6 +160,7 @@ class HostAuthorizationTest < ActionDispatch::IntegrationTest
149
160
get "/" , env : {
150
161
"HTTP_X_FORWARDED_HOST" => "127.0.0.1" ,
151
162
"HOST" => "www.example.com" ,
163
+ "action_dispatch.show_detailed_exceptions" => true
152
164
}
153
165
154
166
assert_response :forbidden
@@ -173,6 +185,7 @@ class HostAuthorizationTest < ActionDispatch::IntegrationTest
173
185
get "/" , env : {
174
186
"HTTP_X_FORWARDED_HOST" => "localhost" ,
175
187
"HOST" => "www.example.com" ,
188
+ "action_dispatch.show_detailed_exceptions" => true
176
189
}
177
190
178
191
assert_response :forbidden
@@ -185,6 +198,7 @@ class HostAuthorizationTest < ActionDispatch::IntegrationTest
185
198
get "/" , env : {
186
199
"HTTP_X_FORWARDED_HOST" => "sub.domain.com" ,
187
200
"HOST" => "domain.com" ,
201
+ "action_dispatch.show_detailed_exceptions" => true
188
202
}
189
203
190
204
assert_response :forbidden
@@ -215,7 +229,7 @@ class HostAuthorizationTest < ActionDispatch::IntegrationTest
215
229
test "exclude misses block unallowed hosts" do
216
230
@app = ActionDispatch ::HostAuthorization . new ( App , "only.com" , exclude : -> ( req ) { req . path == "/bar" } )
217
231
218
- get "/foo"
232
+ get "/foo" , env : { "action_dispatch.show_detailed_exceptions" => true }
219
233
220
234
assert_response :forbidden
221
235
assert_match "Blocked host: www.example.com" , response . body
@@ -226,6 +240,7 @@ class HostAuthorizationTest < ActionDispatch::IntegrationTest
226
240
227
241
get "/" , env : {
228
242
"HOST" => "attacker.com#x.example.com" ,
243
+ "action_dispatch.show_detailed_exceptions" => true
229
244
}
230
245
231
246
assert_response :forbidden
@@ -237,6 +252,7 @@ class HostAuthorizationTest < ActionDispatch::IntegrationTest
237
252
238
253
get "/" , env : {
239
254
"HOST" => "sub-example.com" ,
255
+ "action_dispatch.show_detailed_exceptions" => true
240
256
}
241
257
242
258
assert_response :forbidden
@@ -248,4 +264,30 @@ class HostAuthorizationTest < ActionDispatch::IntegrationTest
248
264
ActionDispatch ::HostAuthorization . new ( App , "example.com" , -> ( env ) { true } )
249
265
end
250
266
end
267
+
268
+ test "uses logger from the env" do
269
+ @app = ActionDispatch ::HostAuthorization . new ( App , %w( only.com ) )
270
+ output = StringIO . new
271
+
272
+ get "/" , env : { "action_dispatch.logger" => Logger . new ( output ) }
273
+
274
+ assert_response :forbidden
275
+ assert_match "Blocked host: www.example.com" , output . rewind && output . read
276
+ end
277
+
278
+ test "uses ActionView::Base logger when no logger in the env" do
279
+ @app = ActionDispatch ::HostAuthorization . new ( App , %w( only.com ) )
280
+ output = StringIO . new
281
+ logger = Logger . new ( output )
282
+
283
+ _old , ActionView ::Base . logger = ActionView ::Base . logger , logger
284
+ begin
285
+ get "/"
286
+ ensure
287
+ ActionView ::Base . logger = _old
288
+ end
289
+
290
+ assert_response :forbidden
291
+ assert_match "Blocked host: www.example.com" , output . rewind && output . read
292
+ end
251
293
end
0 commit comments