rack 2.2.24 → 2.2.26

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: 5905364bd5b40d622e2e10330911370ec92f23e5afa1aa58283c2f0cf9a5782d
4
- data.tar.gz: '09c4da33251a32194f0a656767150eaa17367f8e42ee45735971e963d0e43991'
3
+ metadata.gz: e4626f4273d7e4d1e6da249949be7a63375a2a91deab1219093fc053b0ab368f
4
+ data.tar.gz: 3ce5279dbb9bae73dc9fa52807acf315f84cfb1174fc9a31f1b48b4a3cf33644
5
5
  SHA512:
6
- metadata.gz: 5ee4b678c01a6bb97074b87886498d8aa3bce9b61322cb95cc7908dec5e751fdfb8b238bc98956719505b394de21d1f6787d2593378b1fee4a6fe10b9aea3d5b
7
- data.tar.gz: 032c32c7ebb220355578577fce8ad49c67f869b0ba60e533c3e1178b3265086d50e34de0912f00dc9bfdfb4042d446bc2b1ac8bc62916835521b4cf3786de341
6
+ metadata.gz: 3ed69fae635a177af85133348ac99118fe2a1861d58eb6a41415da21575cb8197ece9d578342e2826f646c7a4e2c2d349d79f802dc7e33e1d69a3b02a874c780
7
+ data.tar.gz: eec9ff7a269ce2c7e51703aa7e48c562b163e77c6957ba601b34e516694c8b142ef1c1468e1ffa474cd87f37712439ad1e52d6f8125389d51a7395365854e471
data/CHANGELOG.md CHANGED
@@ -2,6 +2,31 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. For info on how to format all future additions to this file please reference [Keep A Changelog](https://keepachangelog.com/en/1.0.0/).
4
4
 
5
+ ## [2.2.26] - 2026-11-20
6
+
7
+ ### Security
8
+
9
+ - [GHSA-r243-pfwh-7rpx](https://github.com/advisories/GHSA-r243-pfwh-7rpx) Multipart boundary was extracted by substring match rather than parameter parsing, allowing parser differentials and WAF bypass.
10
+ - [GHSA-mhj6-xc86-54m8](https://github.com/advisories/GHSA-mhj6-xc86-54m8) `Rack::Directory` listed directories outside the root when `..` followed a symbolic link.
11
+ - [GHSA-frgv-948x-h96g](https://github.com/advisories/GHSA-frgv-948x-h96g) `Rack::Multipart::Parser` accepted raw CR and LF characters inside quoted multipart header values, preserving them in parsed parameter names.
12
+ - [GHSA-f7cc-57h7-qrcg](https://github.com/advisories/GHSA-f7cc-57h7-qrcg) `Rack::QueryParser` separator pattern allocated about 48 bytes per space after `&`, and nested parameter name splitting about 48 bytes per bracket, allowing denial of service via memory amplification. The `COMMON_SEP` entries for `&` and `;` are now Strings rather than Regexps.
13
+
14
+ ### Changed
15
+
16
+ - The multipart `Content-Type` header is now parsed according to RFC 9110. An unquoted `boundary` containing characters outside the token grammar (such as `=`, `/`, `:`, `?`, parentheses, or spaces), or parameters not separated by `;`, now raise `EOFError` instead of being accepted. Quoted boundaries are unaffected. ([@jeremyevans](https://github.com/jeremyevans), [@tenderlove](https://github.com/tenderlove))
17
+
18
+ ## [2.2.25] - 2026-10-30
19
+
20
+ ### Security
21
+
22
+ - [GHSA-5v64-555j-wjxm](https://github.com/advisories/GHSA-5v64-555j-wjxm) Multipart parser retains parsed data in its scan buffer, allowing denial of service via memory exhaustion.
23
+ - [GHSA-wvg4-6p58-3gh3](https://github.com/advisories/GHSA-wvg4-6p58-3gh3) `Rack::Static` header rules were matched against an unnormalized path, allowing dot segments to bypass them.
24
+ - [GHSA-p8x9-v4hv-2w4g](https://github.com/advisories/GHSA-p8x9-v4hv-2w4g) Multipart requests with more than one `boundary` parameter were accepted when the first value was empty or began with a quote, semicolon, or comma, bypassing the guard added for CVE-2026-26961.
25
+
26
+ ### Fixed
27
+
28
+ - Multipart parser: scan boundary lines with a literal newline search instead of a backtracking regexp, which allocated tens of megabytes per scan for long lines and made the scan buffer fix above ineffective on this series.
29
+
5
30
  ## [2.2.24] - 2026-08-13
6
31
 
7
32
  ### Security
@@ -95,6 +95,7 @@ table { width:100%%; }
95
95
  if client_error_response = check_bad_request(path_info) || check_forbidden(path_info)
96
96
  client_error_response
97
97
  else
98
+ path_info = Utils.clean_path_info(path_info)
98
99
  path = ::File.join(@root, path_info)
99
100
  list_path(env, path, path_info, script_name)
100
101
  end
@@ -131,7 +132,7 @@ table { width:100%%; }
131
132
 
132
133
  # Globbing not safe as path could contain glob metacharacters
133
134
  body = DirectoryBody.new(@root, path, ->(basename) do
134
- stat = stat(::File.join(path, basename))
135
+ stat = stat(basename == '..' ? ::File.dirname(path) : ::File.join(path, basename))
135
136
  next unless stat
136
137
 
137
138
  url = ::File.join(*url_head + [Utils.escape_path(basename)])
@@ -20,6 +20,9 @@ module Rack
20
20
 
21
21
  BOUNDARY_REGEX = /\A([^\n]*(?:\n|\Z))/
22
22
 
23
+ LINE_END = /\n/
24
+ private_constant :LINE_END
25
+
23
26
  BOUNDARY_START_LIMIT = 16 * 1024
24
27
  private_constant :BOUNDARY_START_LIMIT
25
28
 
@@ -85,19 +88,78 @@ module Rack
85
88
  MultipartInfo = Struct.new :params, :tmp_files
86
89
  EMPTY = MultipartInfo.new(nil, [])
87
90
 
91
+ # Parse the boundary parameter from a multipart content type, following
92
+ # the media type grammar in RFC 9110 Section 8.3.1:
93
+ #
94
+ # media-type = type "/" subtype parameters
95
+ # parameters = *( OWS ";" OWS [ parameter ] )
96
+ # parameter = parameter-name "=" parameter-value
97
+ # parameter-value = ( token / quoted-string )
98
+ #
99
+ # Returns nil if the content type is not multipart or has no boundary
100
+ # parameter. Raises EOFError if the parameters are malformed, the boundary
101
+ # is empty, or there is more than one boundary parameter.
88
102
  def self.parse_boundary(content_type)
89
- return unless content_type
90
- data = content_type.match(MULTIPART)
91
- return unless data
103
+ return unless content_type && /\Amultipart\//ni.match?(content_type)
92
104
 
93
- unless data[1].empty?
94
- raise EOFError, "whitespace between boundary parameter name and equal sign"
95
- end
96
- if data.post_match =~ /boundary\s*=/i
97
- raise EOFError, "multiple boundary parameters found in multipart content type"
105
+ scanner = StringScanner.new(content_type.b)
106
+ return unless scanner.skip(/multipart\/[!#$%&'*+\-.^_`|~0-9A-Za-z]+/ni)
107
+
108
+ boundary = nil
109
+ boundary_seen = false
110
+ ows = /[ \t]*/n
111
+ # From RFC 9110 Section 5.6.2
112
+ token = /[!#$%&'*+\-.^_`|~0-9A-Za-z]+/n
113
+
114
+ loop do
115
+ scanner.skip(ows)
116
+ break if scanner.eos?
117
+
118
+ unless scanner.skip(/;/n)
119
+ raise EOFError, "invalid parameters in multipart content type"
120
+ end
121
+
122
+ scanner.skip(ows)
123
+ # Empty parameter, such as in "a=b;;c=d" or a trailing ";".
124
+ next unless name = scanner.scan(token)
125
+ is_boundary = name.casecmp("boundary") == 0
126
+ # RFC 2231 forms such as boundary*, boundary*0, or boundary*0*. Rack
127
+ # does not use them, but some parsers (for example Go's
128
+ # mime.ParseMediaType) prefer them over a plain boundary parameter,
129
+ # so treat them as boundary parameters when checking for duplicates.
130
+ is_rfc2231_boundary = !is_boundary && /\Aboundary\*/ni.match?(name)
131
+
132
+ unless scanner.skip(/=/n)
133
+ if is_boundary && scanner.match?(/\s+=/n)
134
+ raise EOFError, "whitespace between boundary parameter name and equal sign"
135
+ end
136
+ raise EOFError, "invalid parameter in multipart content type"
137
+ end
138
+
139
+ if scanner.scan(/"((?:[\t \x21\x23-\x5B\x5D-\x7E\x80-\xFF]|\\[\t \x21-\x7E\x80-\xFF])*)"/n)
140
+ value = scanner[1]
141
+ value.gsub!(/\\(.)/mn, '\1')
142
+ elsif !(value = scanner.scan(token))
143
+ raise EOFError, "invalid parameter value in multipart content type"
144
+ end
145
+
146
+ next unless is_boundary || is_rfc2231_boundary
147
+
148
+ if boundary_seen
149
+ raise EOFError, "multiple boundary parameters found in multipart content type"
150
+ end
151
+ boundary_seen = true
152
+
153
+ next unless is_boundary
154
+
155
+ if value.empty?
156
+ raise EOFError, "empty boundary in multipart content type"
157
+ end
158
+
159
+ boundary = value
98
160
  end
99
161
 
100
- data[2]
162
+ boundary
101
163
  end
102
164
 
103
165
  def self.parse(io, content_length, content_type, tmpfile, bufsize, qp)
@@ -346,6 +408,7 @@ module Rack
346
408
  # buffering. Note that the actual limit is the higher of 64KB and the buffer size (1MB by default)
347
409
  raise EOFError, "multipart mime part header too large" if @sbuf.rest.bytesize > MIME_HEADER_BYTESIZE_LIMIT
348
410
 
411
+ @sbuf.string = @sbuf.rest
349
412
  return :want_read
350
413
  end
351
414
  end
@@ -366,8 +429,8 @@ module Rack
366
429
  update_retained_size(body.bytesize) if @body_retained
367
430
  @collector.on_mime_body @mime_index, body
368
431
  @sbuf.pos += delta
369
- @sbuf.string = @sbuf.rest
370
432
  end
433
+ @sbuf.string = @sbuf.rest
371
434
  :want_read
372
435
  end
373
436
  end
@@ -386,7 +449,7 @@ module Rack
386
449
  # end of the boundary. If we don't find the start or end of the
387
450
  # boundary, clear the buffer and return nil.
388
451
  def consume_boundary
389
- while read_buffer = @sbuf.scan_until(BOUNDARY_REGEX)
452
+ while read_buffer = scan_line
390
453
  case read_buffer.strip
391
454
  when full_boundary then return :BOUNDARY
392
455
  when @end_boundary then return :END_BOUNDARY
@@ -395,6 +458,21 @@ module Rack
395
458
  end
396
459
  end
397
460
 
461
+ # Return the next line of the buffer including its trailing "\n", or the
462
+ # rest of the buffer if it contains no "\n", and advance past it. This is
463
+ # equivalent to `@sbuf.scan_until(BOUNDARY_REGEX)`, but searching for the
464
+ # "\n" directly avoids the regexp engine's backtracking stack, which grows
465
+ # by one entry per byte matched by `[^\n]*` (tens of MB for a 1 MB line).
466
+ def scan_line
467
+ if line = @sbuf.scan_until(LINE_END)
468
+ line
469
+ else
470
+ line = @sbuf.rest
471
+ @sbuf.terminate
472
+ line
473
+ end
474
+ end
475
+
398
476
  def get_filename(head)
399
477
  filename = nil
400
478
  case head
@@ -15,9 +15,11 @@ module Rack
15
15
  MULTIPART = %r|\Amultipart/.*?boundary(\s*)=\"?([^\";,]+)\"?|ni
16
16
  TOKEN = /[^\s()<>,;:\\"\/\[\]?=]+/
17
17
  CONDISP = /Content-Disposition:\s*#{TOKEN}\s*/i
18
- VALUE = /"(?:\\"|[^"])*"|#{TOKEN}/
18
+ # Quoted strings may not contain a raw CR or LF. Folded header lines are
19
+ # unfolded before matching, so any remaining line break is a header terminator.
20
+ VALUE = /"(?:\\"|[^"\r\n])*"|#{TOKEN}/
19
21
  BROKEN = /^#{CONDISP}.*;\s*filename=(#{VALUE})/i
20
- MULTIPART_CONTENT_TYPE = /Content-Type: (.*)#{EOL}/ni
22
+ MULTIPART_CONTENT_TYPE = /Content-Type: ([^\r\n]*)/ni
21
23
  MULTIPART_CONTENT_DISPOSITION = /Content-Disposition:[^:]*;\s*name=(#{VALUE})/ni
22
24
  MULTIPART_CONTENT_ID = /Content-ID:\s*([^#{EOL}]*)/ni
23
25
  # Updated definitions from RFC 2231
@@ -4,8 +4,8 @@ module Rack
4
4
  class QueryParser
5
5
  (require_relative 'core_ext/regexp'; using ::Rack::RegexpExtensions) if RUBY_VERSION < '2.4'
6
6
 
7
- DEFAULT_SEP = /[&;] */n
8
- COMMON_SEP = { ";" => /[;] */n, ";," => /[;,] */n, "&" => /[&] */n }
7
+ DEFAULT_SEP = /[&;]/n
8
+ COMMON_SEP = { ";" => ";", ";," => /[;,]/n, "&" => "&" }
9
9
 
10
10
  # ParameterTypeError is the error that is raised when incoming structural
11
11
  # parameters (parsed by parse_nested_query) contain conflicting types.
@@ -71,7 +71,7 @@ module Rack
71
71
 
72
72
  params = make_params
73
73
 
74
- check_query_string(qs, d).split(d ? (COMMON_SEP[d] || /[#{d}] */n) : DEFAULT_SEP).each do |p|
74
+ split_query_string(qs, d).each do |p|
75
75
  next if p.empty?
76
76
  k, v = p.split('=', 2).map!(&unescaper)
77
77
 
@@ -98,7 +98,7 @@ module Rack
98
98
  params = make_params
99
99
 
100
100
  unless qs.nil? || qs.empty?
101
- check_query_string(qs, d).split(d ? (COMMON_SEP[d] || /[#{d}] */n) : DEFAULT_SEP).each do |p|
101
+ split_query_string(qs, d).each do |p|
102
102
  k, v = p.split('=', 2).map! { |s| unescape(s) }
103
103
 
104
104
  normalize_params(params, k, v, param_depth_limit)
@@ -116,9 +116,16 @@ module Rack
116
116
  def normalize_params(params, name, v, depth)
117
117
  raise ParamsTooDeepError if depth <= 0
118
118
 
119
- name =~ %r(\A[\[\]]*([^\[\]]+)\]*)
120
- k = $1 || ''
121
- after = $' || ''
119
+ # Equivalent to name =~ /\A[\[\]]*([^\[\]]+)\]*/ with k = $1 and
120
+ # after = $', but a quantified regexp allocates backtracking state for
121
+ # every character matched, so locate the key with single character
122
+ # searches instead, which do not backtrack.
123
+ k = after = ''
124
+ if name && (start = name.index(/[^\[\]]/))
125
+ stop = name.index(/[\[\]]/, start) || name.length
126
+ k = name[start, stop - start]
127
+ after = name[(name.index(/[^\]]/, stop) || name.length)..-1]
128
+ end
122
129
 
123
130
  if k.empty?
124
131
  if !v.nil? && name == "[]"
@@ -136,8 +143,12 @@ module Rack
136
143
  params[k] ||= []
137
144
  raise ParameterTypeError, "expected Array (got #{params[k].class.name}) for param `#{k}'" unless params[k].is_a?(Array)
138
145
  params[k] << v
139
- elsif after =~ %r(^\[\]\[([^\[\]]+)\]$) || after =~ %r(^\[\](.+)$)
140
- child_key = $1
146
+ elsif after.start_with?('[]')
147
+ # Recognize x[][y] (hash inside array) parameters
148
+ unless after[2] == '[' && after.end_with?(']') && (child_key = after[3, after.length-4]) && !child_key.empty? && !child_key.index('[') && !child_key.index(']')
149
+ # Handle other nested array parameters
150
+ child_key = after[2, after.length]
151
+ end
141
152
  params[k] ||= []
142
153
  raise ParameterTypeError, "expected Array (got #{params[k].class.name}) for param `#{k}'" unless params[k].is_a?(Array)
143
154
  if params_hash_type?(params[k].last) && !params_hash_has_key?(params[k].last, child_key)
@@ -175,15 +186,35 @@ module Rack
175
186
  def params_hash_has_key?(hash, key)
176
187
  return false if /\[\]/.match?(key)
177
188
 
178
- key.split(/[\[\]]+/).inject(hash) do |h, part|
179
- next h if part == ''
180
- return false unless params_hash_type?(h) && h.key?(part)
181
- h[part]
189
+ # Equivalent to walking key.split(/[\[\]]+/), but a quantified regexp
190
+ # allocates backtracking state for every bracket matched, so find the
191
+ # segments with single character searches instead, which do not
192
+ # backtrack.
193
+ pos = 0
194
+ while (start = key.index(/[^\[\]]/, pos))
195
+ stop = key.index(/[\[\]]/, start) || key.length
196
+ part = key[start, stop - start]
197
+ return false unless params_hash_type?(hash) && hash.key?(part)
198
+ hash = hash[part]
199
+ pos = stop
182
200
  end
183
201
 
184
202
  true
185
203
  end
186
204
 
205
+ def split_query_string(qs, d)
206
+ pairs = check_query_string(qs, d).split(d ? (COMMON_SEP[d] || /[#{d}]/n) : DEFAULT_SEP)
207
+
208
+ # Remove spaces following the separator. A quantified regexp such as
209
+ # /& */ allocates backtracking state for every space matched, so find
210
+ # the first non-space character instead, which does not backtrack.
211
+ pairs.each do |pair|
212
+ pair.slice!(0, pair.index(/[^ ]/) || pair.length) if pair.getbyte(0) == 32
213
+ end
214
+
215
+ pairs
216
+ end
217
+
187
218
  def check_query_string(qs, sep)
188
219
  if qs
189
220
  if qs.bytesize > @bytesize_limit
data/lib/rack/static.rb CHANGED
@@ -168,7 +168,7 @@ module Rack
168
168
 
169
169
  # Convert HTTP header rules to HTTP headers
170
170
  def applicable_rules(path)
171
- path = ::Rack::Utils.unescape_path(path)
171
+ path = ::Rack::Utils.clean_path_info(::Rack::Utils.unescape_path(path))
172
172
 
173
173
  @header_rules.find_all do |rule, new_headers|
174
174
  case rule
data/lib/rack/version.rb CHANGED
@@ -20,7 +20,7 @@ module Rack
20
20
  VERSION.join(".")
21
21
  end
22
22
 
23
- RELEASE = "2.2.24"
23
+ RELEASE = "2.2.26"
24
24
 
25
25
  # Return the Rack release as a dotted string.
26
26
  def self.release
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.24
4
+ version: 2.2.26
5
5
  platform: ruby
6
6
  authors:
7
7
  - Leah Neukirchen