padrino-core 0.18.2 → 0.18.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/padrino-core/application/authenticity_token.rb +18 -2
- data/lib/padrino-core/application/routing.rb +155 -12
- data/lib/padrino-core/filter.rb +177 -13
- data/lib/padrino-core/path_router/compiler.rb +9 -17
- data/lib/padrino-core/path_router/matcher.rb +8 -2
- data/lib/padrino-core/path_router/route.rb +2 -1
- data/lib/padrino-core/version.rb +1 -1
- data/test/test_csrf_protection.rb +255 -0
- data/test/test_filters.rb +1391 -0
- data/test/test_routing.rb +186 -0
- metadata +4 -4
|
@@ -146,6 +146,261 @@ describe 'Application' do
|
|
|
146
146
|
end
|
|
147
147
|
end
|
|
148
148
|
|
|
149
|
+
describe 'with :except option and an encoded path' do
|
|
150
|
+
before do
|
|
151
|
+
mock_app do
|
|
152
|
+
enable :sessions
|
|
153
|
+
set :protect_from_csrf, except: ['/hook', %r{\A/(?!admin)}]
|
|
154
|
+
post('/hook') { 'hook' }
|
|
155
|
+
post('/admin') { 'admin' }
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
it 'should compare the path the router matches' do
|
|
160
|
+
post '/hook'
|
|
161
|
+
assert_equal 200, status
|
|
162
|
+
post '/%68ook'
|
|
163
|
+
assert_equal 200, status
|
|
164
|
+
post '/admin'
|
|
165
|
+
assert_equal 403, status
|
|
166
|
+
post '/%61dmin'
|
|
167
|
+
assert_equal 403, status
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
it 'should stay on for a path with a newline' do
|
|
171
|
+
mock_app do
|
|
172
|
+
enable :sessions
|
|
173
|
+
set :protect_from_csrf, except: [%r{^/api/}, "/hook\nx"]
|
|
174
|
+
post('/:tenant/api/:id') { 'tenant' }
|
|
175
|
+
post('/api/x') { 'api' }
|
|
176
|
+
post('/hook:rest') { 'hook' }
|
|
177
|
+
end
|
|
178
|
+
post '/api/x'
|
|
179
|
+
assert_equal 200, status
|
|
180
|
+
post '/acme%0A/api/b'
|
|
181
|
+
assert_equal 403, status
|
|
182
|
+
post '/hook%0Ax'
|
|
183
|
+
assert_equal 403, status
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
it 'should answer 403 for a path that decodes to a newline before filters run' do
|
|
187
|
+
seen = []
|
|
188
|
+
mock_app do
|
|
189
|
+
enable :sessions
|
|
190
|
+
set :protect_from_csrf, except: [%r{^/api/}]
|
|
191
|
+
before { seen << params[:tenant] }
|
|
192
|
+
not_found do
|
|
193
|
+
seen << :not_found
|
|
194
|
+
'missing'
|
|
195
|
+
end
|
|
196
|
+
post('/:tenant/api/:id') { 'tenant' }
|
|
197
|
+
end
|
|
198
|
+
post '/acme%0A/api/b'
|
|
199
|
+
assert_equal 403, status
|
|
200
|
+
assert_empty seen
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
it 'should not raise without a path' do
|
|
204
|
+
env = Rack::MockRequest.env_for('/')
|
|
205
|
+
env.delete('PATH_INFO')
|
|
206
|
+
token = Padrino::AuthenticityToken.new(->(_env) { [200, {}, []] }, except: ['/a'])
|
|
207
|
+
refute token.send(:except?, env)
|
|
208
|
+
end
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
describe 'with :except option on every form of a path' do
|
|
212
|
+
def mock_anchored_app(*except)
|
|
213
|
+
mock_app do
|
|
214
|
+
enable :sessions
|
|
215
|
+
set :protect_from_csrf, except: except
|
|
216
|
+
post('/admin(.:format)?') { 'admin' }
|
|
217
|
+
post('/other(.:format)?') { 'other' }
|
|
218
|
+
post('/') { 'root' }
|
|
219
|
+
end
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
%w[/admin /%61dmin /admin/ /%61dmin/ /admin.json /admin%2Ejson /admin.json/ /admin.a.b].each do |path|
|
|
223
|
+
it "should stay on for #{path}" do
|
|
224
|
+
mock_anchored_app('/hook', %r{\A/(?!admin\z)})
|
|
225
|
+
post path
|
|
226
|
+
assert_equal 403, status
|
|
227
|
+
end
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
%w[/other /other/ /other.json /%6Fther.json].each do |path|
|
|
231
|
+
it "should stay off for #{path}" do
|
|
232
|
+
mock_anchored_app('/hook', %r{\A/(?!admin\z)})
|
|
233
|
+
post path
|
|
234
|
+
assert_equal 200, status
|
|
235
|
+
end
|
|
236
|
+
end
|
|
237
|
+
|
|
238
|
+
it 'should stay on for a suffix of a dotted name' do
|
|
239
|
+
mock_app do
|
|
240
|
+
enable :sessions
|
|
241
|
+
set :protect_from_csrf, except: [%r{\A/(?!admin\.v1\z)}]
|
|
242
|
+
post('/admin.v1(.:format)?') { 'admin' }
|
|
243
|
+
end
|
|
244
|
+
post '/admin.v1.json'
|
|
245
|
+
assert_equal 403, status
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
it 'should keep exact String entries' do
|
|
249
|
+
mock_app do
|
|
250
|
+
enable :sessions
|
|
251
|
+
set :protect_from_csrf, except: ['/hook', '/', '/feed.json']
|
|
252
|
+
post('/hook') { 'hook' }
|
|
253
|
+
post('/feed.json') { 'feed' }
|
|
254
|
+
post('/') { 'root' }
|
|
255
|
+
end
|
|
256
|
+
%w[/hook / /feed.json].each do |path|
|
|
257
|
+
post path
|
|
258
|
+
assert_equal 200, status, path
|
|
259
|
+
end
|
|
260
|
+
post '/hook/'
|
|
261
|
+
assert_equal 403, status
|
|
262
|
+
end
|
|
263
|
+
|
|
264
|
+
it 'should take the forms of a path from different entries' do
|
|
265
|
+
mock_app do
|
|
266
|
+
enable :sessions
|
|
267
|
+
set :protect_from_csrf, except: ['/hook/', '/hook']
|
|
268
|
+
post('/hook') { 'hook' }
|
|
269
|
+
end
|
|
270
|
+
post '/hook/'
|
|
271
|
+
assert_equal 200, status
|
|
272
|
+
end
|
|
273
|
+
|
|
274
|
+
%w[/api/.json /api/.json/ /api// /api/].each do |path|
|
|
275
|
+
it "should stay on for #{path} with a slash in the Regexp" do
|
|
276
|
+
mock_app do
|
|
277
|
+
enable :sessions
|
|
278
|
+
set :protect_from_csrf, except: [%r{^/api/}]
|
|
279
|
+
post('/api/?', provides: :any) { 'api' }
|
|
280
|
+
end
|
|
281
|
+
post path
|
|
282
|
+
assert_equal 403, status
|
|
283
|
+
end
|
|
284
|
+
end
|
|
285
|
+
|
|
286
|
+
it 'should stay on for a path with trailing slashes unless the path with one is exempt too' do
|
|
287
|
+
mock_app do
|
|
288
|
+
enable :sessions
|
|
289
|
+
set :protect_from_csrf, except: [%r{\A/x(//)?\z}]
|
|
290
|
+
post('/x') { 'x' }
|
|
291
|
+
end
|
|
292
|
+
post '/x'
|
|
293
|
+
assert_equal 200, status
|
|
294
|
+
%w[/x/ /x//].each do |path|
|
|
295
|
+
post path
|
|
296
|
+
assert_equal 403, status, path
|
|
297
|
+
end
|
|
298
|
+
end
|
|
299
|
+
|
|
300
|
+
it 'should keep String entries written with a trailing slash' do
|
|
301
|
+
mock_app do
|
|
302
|
+
enable :sessions
|
|
303
|
+
set :protect_from_csrf, except: ['/api/', '/hook/']
|
|
304
|
+
post('/api/?') { 'api' }
|
|
305
|
+
post('/hook/?') { 'hook' }
|
|
306
|
+
end
|
|
307
|
+
%w[/api/ /hook/ /%68ook/].each do |path|
|
|
308
|
+
post path
|
|
309
|
+
assert_equal 200, status, path
|
|
310
|
+
end
|
|
311
|
+
post '/hook'
|
|
312
|
+
assert_equal 403, status
|
|
313
|
+
end
|
|
314
|
+
|
|
315
|
+
it 'should exempt only what a String entry with an encoded slash names' do
|
|
316
|
+
mock_app do
|
|
317
|
+
enable :sessions
|
|
318
|
+
set :protect_from_csrf, except: ['/a%2Fb']
|
|
319
|
+
post('/*rest') { 'rest' }
|
|
320
|
+
end
|
|
321
|
+
post '/a%2Fb'
|
|
322
|
+
assert_equal 403, status
|
|
323
|
+
post '/a/b'
|
|
324
|
+
assert_equal 403, status
|
|
325
|
+
end
|
|
326
|
+
|
|
327
|
+
it 'should take a path from both of its decoded forms' do
|
|
328
|
+
mock_app do
|
|
329
|
+
enable :sessions
|
|
330
|
+
set :protect_from_csrf, except: ['/a%2Fb', '/a/b']
|
|
331
|
+
post('/*rest') { 'rest' }
|
|
332
|
+
end
|
|
333
|
+
post '/a%2Fb'
|
|
334
|
+
assert_equal 200, status
|
|
335
|
+
end
|
|
336
|
+
|
|
337
|
+
it 'should strip a dot suffix from the last segment only' do
|
|
338
|
+
mock_app do
|
|
339
|
+
enable :sessions
|
|
340
|
+
set :protect_from_csrf, except: [%r{\A/v1\.2/}]
|
|
341
|
+
post('/v1.2/hook') { 'hook' }
|
|
342
|
+
post('/v1.2/hook.json') { 'hook' }
|
|
343
|
+
end
|
|
344
|
+
%w[/v1.2/hook /v1.2/hook.json].each do |path|
|
|
345
|
+
post path
|
|
346
|
+
assert_equal 200, status, path
|
|
347
|
+
end
|
|
348
|
+
end
|
|
349
|
+
|
|
350
|
+
it 'should stay on for a long path of dots' do
|
|
351
|
+
mock_app do
|
|
352
|
+
enable :sessions
|
|
353
|
+
set :protect_from_csrf, except: ['/hook', %r{\A/(?!admin\z)}]
|
|
354
|
+
post('/:name') { 'name' }
|
|
355
|
+
end
|
|
356
|
+
post "/#{'.' * 80_000}"
|
|
357
|
+
assert_equal 403, status
|
|
358
|
+
post "/admin#{'.x' * 40_000}"
|
|
359
|
+
assert_equal 403, status
|
|
360
|
+
end
|
|
361
|
+
|
|
362
|
+
it 'should keep a String entry for a path of many dots' do
|
|
363
|
+
path = "/#{(['a'] * 40).join('.')}"
|
|
364
|
+
mock_app do
|
|
365
|
+
enable :sessions
|
|
366
|
+
set :protect_from_csrf, except: [path]
|
|
367
|
+
post('/:name') { 'name' }
|
|
368
|
+
end
|
|
369
|
+
post path
|
|
370
|
+
assert_equal 200, status
|
|
371
|
+
end
|
|
372
|
+
|
|
373
|
+
it 'should exempt a path with a few dots' do
|
|
374
|
+
mock_app do
|
|
375
|
+
enable :sessions
|
|
376
|
+
set :protect_from_csrf, except: [%r{\A/a}]
|
|
377
|
+
post('/:name') { 'name' }
|
|
378
|
+
end
|
|
379
|
+
post "/a#{'.a' * 15}"
|
|
380
|
+
assert_equal 200, status
|
|
381
|
+
end
|
|
382
|
+
|
|
383
|
+
it 'should keep a Regexp prefix on a path with a dot' do
|
|
384
|
+
mock_app do
|
|
385
|
+
enable :sessions
|
|
386
|
+
set :protect_from_csrf, except: [%r{\A/webhooks/}]
|
|
387
|
+
post('/webhooks/stripe.json') { 'stripe' }
|
|
388
|
+
end
|
|
389
|
+
post '/webhooks/stripe.json'
|
|
390
|
+
assert_equal 200, status
|
|
391
|
+
end
|
|
392
|
+
|
|
393
|
+
it 'should stay on for a Regexp that needs the dot suffix' do
|
|
394
|
+
mock_app do
|
|
395
|
+
enable :sessions
|
|
396
|
+
set :protect_from_csrf, except: [/\.json\z/]
|
|
397
|
+
post('/feed.json') { 'feed' }
|
|
398
|
+
end
|
|
399
|
+
post '/feed.json'
|
|
400
|
+
assert_equal 403, status
|
|
401
|
+
end
|
|
402
|
+
end
|
|
403
|
+
|
|
149
404
|
describe 'with custom protection options' do
|
|
150
405
|
before do
|
|
151
406
|
@token =
|