padrino-admin 0.18.2 → 0.18.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d4201a847361929f410a70a3254c1a71b1af1f16e599e8c289fd19352424ef6b
4
- data.tar.gz: dd2c63edb9c513fc90122af641619ce19502de997c2a1d870f08e7ae7256d516
3
+ metadata.gz: 3298f87780583cc70be64bf1b70054096d92c6d3522e6e2358bde771a6cdeab8
4
+ data.tar.gz: b9cd6fd46fa6be02b2d0d81ecdaa9a6c6d59512e7c94c61379fb1b9bc17d83f3
5
5
  SHA512:
6
- metadata.gz: 10f18575e2a4d40c57c4ce49923be8b84964fa83fbf55e9ba5884d88d7641e0aa72d5a82ef5dcc884b3ead6e069551afaebb120e72f09b50a605426fe1c9cafb
7
- data.tar.gz: 1ce89c2fd6e3721ece45a02b950063928df23f97655458d37dccb5d1c9bd89d2cbac4257930fcfd02ddea73c65db69de06a6757af4001b19d88aad75d3c7d060
6
+ metadata.gz: 540c82c29e9038b37b60c4031583266545494ea719039a1ee5d274c12cc8b1048a34414f50b95d1a8bd6915ceb090c93f31f14e989ccfa51368b5083b2948719
7
+ data.tar.gz: '08fc77b959b415fe104dac6a3dbb3cf14e24e2edf846ec364e25dd5eae8a8de32a9be2830b8ec76962eb4120d9c83741a0dfc6a5518c652bf91957873f44e7c5'
@@ -36,6 +36,10 @@ module Padrino
36
36
  # This base access control class where roles are defined as are authorizations.
37
37
  #
38
38
  class Base
39
+ # How many forms of a request path deny Regexps are checked against at most; a path with more
40
+ # is denied (deny_forms).
41
+ MAX_DENY_FORMS = 64
42
+
39
43
  def initialize
40
44
  @roles, @authorizations, @project_modules = [], [], []
41
45
  end
@@ -104,8 +108,9 @@ module Padrino
104
108
  #
105
109
  def allowed?(account = nil, path = nil)
106
110
  path = '/' if path.nil? || path.empty?
107
- # Compare the decoded path; scrub invalid UTF-8 ('/%ff') so Regexp rules can match it.
108
- path = Rack::Utils.unescape_path(path.b).force_encoding(Encoding::UTF_8).scrub
111
+ # Allow rules see the decoded path the router matches ('%2F' stays inside its segment); deny
112
+ # rules see every form the router may serve it as (deny_forms).
113
+ paths = Padrino::Routing.path_forms(path)
109
114
  role = account.role.to_sym rescue nil
110
115
  authorizations = @authorizations.find_all { |auth| auth.roles.include?(:any) }
111
116
  allowed_paths = authorizations.map(&:allowed).flatten.uniq
@@ -120,13 +125,30 @@ module Padrino
120
125
  denied_paths += authorizations.map(&:allowed).flatten.uniq # remove paths explicitly allowed for other roles
121
126
  denied_paths += authorizations.map(&:denied).flatten.uniq # remove paths explicitly denied to other roles
122
127
  end
123
- return true if allowed_paths.any? { |p| path_within?(path, p) }
124
- return false if denied_paths.any? { |p| path.start_with?(p) } # loose on purpose: fails closed
128
+ # A path with a newline satisfies no allow rule.
129
+ return denied_paths.empty? if paths.any? { |form| form.include?("\n") }
130
+ return true if allowed_paths.any? { |p| path_within?(paths.first, p) }
131
+ return true if denied_paths.empty?
132
+
133
+ forms = denied_paths.any?(Regexp) ? deny_forms(paths) : paths
134
+ return false unless forms
135
+ return false if denied_paths.any? { |p| forms.any? { |form| form.start_with?(p) } } # loose on purpose: fails closed
125
136
  true
126
137
  end
127
138
 
128
139
  private
129
140
 
141
+ # The forms of the path (Routing.path_forms), without its trailing slashes, and without a
142
+ # format after a dot of its last segment, as a route may serve any of those ('/billing.json'
143
+ # and '/billing/' from '/billing'). Other delimiters start a different page ('/billing-day').
144
+ # A String rule needs none of them: each is a prefix of a decoded form, which start_with?
145
+ # already covers. Returns nil for a path with more than MAX_DENY_FORMS forms, so that it is
146
+ # denied rather than checked in part.
147
+ def deny_forms(paths)
148
+ forms = Padrino::Routing.served_forms(paths).first(MAX_DENY_FORMS + 1)
149
+ forms unless forms.size > MAX_DENY_FORMS
150
+ end
151
+
130
152
  # An allowed path covers itself, its sub-paths and its formats ('/posts' allows '/posts',
131
153
  # '/posts/1' and '/posts.json'), but not unrelated paths that merely start with it ('/posts_admin').
132
154
  # A Regexp matches at the start of the path, as on the deny side.
@@ -50,6 +50,238 @@ describe 'AdminApplication' do
50
50
  assert_equal 'unauthenticated', body
51
51
  end
52
52
 
53
+ it 'should require login for a path a protect rule covers, with an encoded letter' do
54
+ mock_app do
55
+ register Padrino::Admin::AccessControl
56
+ enable :sessions
57
+
58
+ access_control.roles_for :any do |role|
59
+ role.protect '/foo'
60
+ end
61
+
62
+ get('/foo') { 'foo' }
63
+ get('/bar') { 'bar' }
64
+ end
65
+
66
+ ['/%66oo', '/f%6Fo', '/%66oo/'].each do |path|
67
+ get path
68
+ assert_equal "You don't have permission for this resource", body, path
69
+ end
70
+
71
+ get '/%62ar'
72
+ assert_equal 'bar', body
73
+ end
74
+
75
+ it 'should keep an encoded slash inside the path segment it is in' do
76
+ mock_app do
77
+ register Padrino::Admin::AccessControl
78
+ enable :sessions
79
+
80
+ access_control.roles_for :any do |role|
81
+ role.protect '/'
82
+ role.allow '/sessions'
83
+ end
84
+
85
+ get('/sessions/:id') { 'session' }
86
+ get('/:page') { "page #{params[:page]}" }
87
+ end
88
+
89
+ get '/sessions/new'
90
+ assert_equal 'session', body
91
+ ['/sessions%2Fnew', '/sessions%2fnew', '/%73essions%2Fnew'].each do |path|
92
+ get path
93
+ assert_equal "You don't have permission for this resource", body, path
94
+ end
95
+ assert @app.access_control.allowed?(nil, '/sessions/a%2Fb')
96
+ refute @app.access_control.allowed?(nil, '/sessions%2F')
97
+ end
98
+
99
+ it 'should deny a path a protect rule covers, whose encoded slash a splat route decodes' do
100
+ mock_app do
101
+ register Padrino::Admin::AccessControl
102
+ enable :sessions
103
+
104
+ access_control.roles_for :any do |role|
105
+ role.protect '/files/report'
106
+ end
107
+
108
+ get('/files/:name') { "name #{params[:name]}" }
109
+ get('/*path') { "file #{params[:path]}" }
110
+ end
111
+
112
+ ['/files/report', '/files%2Freport', '/files%2freport', '/%66iles%2Freport/1',
113
+ '/files%252Freport', '/files/%2572eport', '/%2566iles/report', '/files/rep%256Frt'].each do |path|
114
+ get path
115
+ assert_equal "You don't have permission for this resource", body, path
116
+ refute @app.access_control.allowed?(nil, path), path
117
+ end
118
+ get '/files/public'
119
+ assert_equal 'name public', body
120
+ end
121
+
122
+ describe 'with Regexp rules' do
123
+ def denied
124
+ "You don't have permission for this resource"
125
+ end
126
+
127
+ def mock_filtered_app(&rules)
128
+ mock_app do
129
+ register Padrino::Admin::AccessControl
130
+ enable :sessions
131
+ access_control.roles_for(:any, &rules)
132
+ get('/billing/:a/:b') { 'billing page' }
133
+ get('/admin/:id') { 'admin page' }
134
+ get('/:x/:y') { 'two' }
135
+ get('/:x') { 'one' }
136
+ end
137
+ end
138
+
139
+ def assert_not_served(*paths)
140
+ paths.each do |path|
141
+ get path
142
+ assert_includes [401, 404], status, path
143
+ refute_includes ['one', 'two', 'billing page', 'admin page'], body, path
144
+ end
145
+ end
146
+
147
+ it 'should not serve a path that decodes to a newline with an allow Regexp' do
148
+ mock_filtered_app do |role|
149
+ role.protect '/'
150
+ role.allow %r{\A/public$}
151
+ role.allow %r{/lobby$}
152
+ end
153
+ get '/public'
154
+ assert_equal 'one', body
155
+ get '/lobby'
156
+ assert_equal 'one', body
157
+ get '/public/billing'
158
+ assert_equal [401, denied], [status, body]
159
+ assert_not_served '/public%0A/billing', '/lobby%0A/billing'
160
+ end
161
+
162
+ it 'should not serve a path that decodes to a newline with an allow Regexp in a path a protect rule covers' do
163
+ mock_filtered_app do |role|
164
+ role.protect %r{\A/billing/}
165
+ role.allow %r{\A/billing/ok$}
166
+ end
167
+ get '/billing/ok'
168
+ assert_equal 'two', body
169
+ get '/billing/ok/x'
170
+ assert_equal [401, denied], [status, body]
171
+ assert_not_served '/billing/ok%0A/x'
172
+ end
173
+
174
+ it 'should not serve a path that decodes to a newline with a protect Regexp' do
175
+ mock_filtered_app do |role|
176
+ role.protect %r{\A/admin/.*\z}
177
+ role.protect %r{^/staff/.+$}
178
+ role.protect %r{\A/billing\z}
179
+ end
180
+ get '/other/x'
181
+ assert_equal 'two', body
182
+ assert_not_served '/admin/1', '/admin/a%0Ab', '/staff/x', '/staff/%0Ab', '/billing', '/billing%0A'
183
+ end
184
+
185
+ it 'should deny the path with a trailing slash for a protect Regexp' do
186
+ mock_filtered_app do |role|
187
+ role.protect %r{\A/billing\z}
188
+ role.protect %r{\A/files/[^/]+\z}
189
+ end
190
+ ['/billings', '/billing/x', '/filesx'].each do |path|
191
+ get path
192
+ assert_equal 200, status, path
193
+ end
194
+ ['/billing', '/billing/', '/billing//', '/%62illing/', '/files/a/', '/files/a//'].each do |path|
195
+ get path
196
+ assert_equal [401, denied], [status, body], path
197
+ refute @app.access_control.allowed?(nil, path), path
198
+ end
199
+ end
200
+
201
+ it 'should deny the path with a trailing slash for an app mounted under a sub-path' do
202
+ mock_filtered_app do |role|
203
+ role.protect %r{\A/billing\z}
204
+ role.allow %r{\A/public$}
205
+ end
206
+ request = Rack::MockRequest.new(Padrino::Router.new({ path: '/blog', to: @app }, { path: '/', to: @app }))
207
+ assert_equal 'one', request.get('/blog/public').body
208
+ assert_equal 401, request.get('/blog/billing/').status
209
+ ['/blog/public%0A/billing', '/blog/billing%0A'].each do |path|
210
+ assert_equal 401, request.get(path).status, path
211
+ end
212
+ end
213
+
214
+ it 'should deny the path with a format for a protect Regexp' do
215
+ mock_app do
216
+ register Padrino::Admin::AccessControl
217
+ enable :sessions
218
+ access_control.roles_for :any do |role|
219
+ role.protect %r{\A/billing\z}
220
+ role.protect %r{\A/stock\z}
221
+ end
222
+ get(:billing, map: '/billing', provides: %i[html json]) { 'billing' }
223
+ get('/stock(.:format)?') { 'stock' }
224
+ get('/:x') { 'one' }
225
+ end
226
+ ['/billings', '/stocks', '/billing-day', '/billing-json', '/billing_x'].each do |path|
227
+ get path
228
+ assert_equal 'one', body, path
229
+ assert @app.access_control.allowed?(nil, path), path
230
+ end
231
+ ['/billing', '/billing/', '/billing.json', '/billing.json/', '/%62illing.json', '/stock', '/stock.json', '/stock.a.b'].each do |path|
232
+ get path
233
+ assert_equal [401, denied], [status, body], path
234
+ refute @app.access_control.allowed?(nil, path), path
235
+ end
236
+ end
237
+
238
+ it 'should deny a path with too many forms to check when a protect Regexp exists' do
239
+ mock_app do
240
+ register Padrino::Admin::AccessControl
241
+ enable :sessions
242
+ access_control.roles_for(:any) { |role| role.protect %r{\A/reports/x\z} }
243
+ get('/:name') { 'page' }
244
+ end
245
+ get "/other#{'.a' * 10}"
246
+ assert_equal 'page', body
247
+ get "/other#{'.a' * 70}"
248
+ assert_equal [401, denied], [status, body]
249
+ end
250
+
251
+ it 'should answer allowed? for a path with a newline from its deny rules only' do
252
+ mock_app do
253
+ register Padrino::Admin::AccessControl
254
+ access_control.roles_for(:any) do |role|
255
+ role.protect '/admin'
256
+ role.allow %r{\A/public$}
257
+ role.allow '/lobby'
258
+ end
259
+ end
260
+ ["/public\n/admin", '/public%0A/admin', '/lobby%0A', '/lobby/a%250A', '/other%0A'].each do |path|
261
+ refute @app.access_control.allowed?(nil, path), path
262
+ end
263
+ assert @app.access_control.allowed?(nil, '/public')
264
+ mock_app do
265
+ register Padrino::Admin::AccessControl
266
+ access_control.roles_for(:any) { |role| role.allow %r{\A/public$} }
267
+ end
268
+ assert @app.access_control.allowed?(nil, '/public%0A/admin')
269
+ end
270
+
271
+ it 'should keep the allow rules of a role for a path with a control character' do
272
+ mock_app do
273
+ register Padrino::Admin::AccessControl
274
+ access_control.roles_for(:any) { |role| role.protect '/' }
275
+ access_control.roles_for(:editor) { |role| role.allow '/posts' }
276
+ access_control.roles_for(:admin) { |role| role.allow '/accounts' }
277
+ end
278
+ editor = Struct.new(:role).new('editor')
279
+ assert @app.access_control.allowed?(editor, '/posts/a%09b')
280
+ assert @app.access_control.allowed?(editor, '/posts/a%0Db')
281
+ refute @app.access_control.allowed?(editor, '/accounts/a%09b')
282
+ end
283
+ end
284
+
53
285
  it 'should set basic roles with store location and login page' do
54
286
  mock_app do
55
287
  set :app_name, :basic_app
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: padrino-admin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.18.2
4
+ version: 0.18.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Padrino Team
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2026-11-01 00:00:00.000000000 Z
14
+ date: 2026-11-04 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: padrino-core
@@ -19,28 +19,28 @@ dependencies:
19
19
  requirements:
20
20
  - - '='
21
21
  - !ruby/object:Gem::Version
22
- version: 0.18.2
22
+ version: 0.18.4
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
27
  - - '='
28
28
  - !ruby/object:Gem::Version
29
- version: 0.18.2
29
+ version: 0.18.4
30
30
  - !ruby/object:Gem::Dependency
31
31
  name: padrino-helpers
32
32
  requirement: !ruby/object:Gem::Requirement
33
33
  requirements:
34
34
  - - '='
35
35
  - !ruby/object:Gem::Version
36
- version: 0.18.2
36
+ version: 0.18.4
37
37
  type: :runtime
38
38
  prerelease: false
39
39
  version_requirements: !ruby/object:Gem::Requirement
40
40
  requirements:
41
41
  - - '='
42
42
  - !ruby/object:Gem::Version
43
- version: 0.18.2
43
+ version: 0.18.4
44
44
  description: Admin View for Padrino applications
45
45
  email: padrinorb@gmail.com
46
46
  executables: []