rack 3.1.22 → 3.1.24

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: f3f9b36f03c948233dee682ef0a4724302be7f64dee1b599d45c7ef4068c6b0c
4
- data.tar.gz: 428c57448bdc479dd1bab423ff9df7376e660a9603b648de733c7c08ae186693
3
+ metadata.gz: 4f9f5949aafc42d0d4f0604f1dd1814d9afef49c59bef8a861fd94ea1b33f9a4
4
+ data.tar.gz: 540e0171cd56eca530429bea72df6db1d715e553bf80a716aa3784ada8af02b2
5
5
  SHA512:
6
- metadata.gz: f25b66f8c806f983f6ec504f5c97ffeec03d1994d8ba508763fb903923c28a9d6a85feab7bba9f6da248e90ef6697dabda9da32ea62d3935259ea4e963ad79d2
7
- data.tar.gz: 2cc6a87b8003ed98a246913322e266e83e7c3cd1247e27e2540dbed28910cfef76c34dbd635f26da547817f2136d87468be6b3c98566c627fbf9b854f51cce62
6
+ metadata.gz: cf70345c136c1e76a6a4794454cda220e1be0047c6fed75d6c33f1449008fd0ff5dd76f96081a368ae8492fbcf11cd82e5bcc61eb8d8fbcc3f608be33f608d51
7
+ data.tar.gz: cbf782156cc1ec35e0f38e5587a12a88d2edafbe7800158bdec7bb3ee5e93bf894ff363ab0e5f7aff9cb34d594c3d408c934a89110ac74c3fdfe584f4cbfca72
data/CHANGELOG.md CHANGED
@@ -2,6 +2,33 @@
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
+ ## [3.1.24] - 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-58mf-c3jj-277p](https://github.com/advisories/GHSA-58mf-c3jj-277p) Multipart parser searched the load path for every unknown part `charset`, allowing denial of service via CPU exhaustion.
11
+ - [GHSA-mhj6-xc86-54m8](https://github.com/advisories/GHSA-mhj6-xc86-54m8) `Rack::Directory` listed directories outside the root when `..` followed a symbolic link.
12
+ - [GHSA-frgv-948x-h96g](https://github.com/advisories/GHSA-frgv-948x-h96g) `Rack::Multipart::Parser` did not unfold a folded `Content-ID` header, preserving CRLF in the parsed parameter name.
13
+ - [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. `DEFAULT_SEP` and the `COMMON_SEP` entries for `&` and `;` are now Strings rather than Regexps.
14
+
15
+ ### Changed
16
+
17
+ - 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 `Rack::Multipart::Error` instead of being accepted. Quoted boundaries are unaffected. ([@jeremyevans](https://github.com/jeremyevans), [@tenderlove](https://github.com/tenderlove))
18
+
19
+ ### Fixed
20
+
21
+ - `Rack::Multipart` no longer raises `TypeError` for a part with `charset=internal` when no default internal encoding is set; the part is treated as binary.
22
+
23
+ ## [3.1.23] - 2026-10-30
24
+
25
+ ### Security
26
+
27
+ - [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.
28
+ - [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.
29
+ - [GHSA-2j8w-xcrc-x985](https://github.com/advisories/GHSA-2j8w-xcrc-x985) `Rack::Request#host_with_port`, `#base_url`, and `#url` returned the raw authority when it was invalid; `host_with_port` now returns `nil` in that case.
30
+ - [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.
31
+
5
32
  ## [3.1.22] - 2026-08-13
6
33
 
7
34
  ### Security
@@ -101,6 +101,7 @@ table { width:100%%; }
101
101
  if client_error_response = check_bad_request(path_info) || check_forbidden(path_info)
102
102
  client_error_response
103
103
  else
104
+ path_info = Utils.clean_path_info(path_info)
104
105
  path = ::File.join(@root, path_info)
105
106
  list_path(env, path, path_info, script_name)
106
107
  end
@@ -137,7 +138,7 @@ table { width:100%%; }
137
138
 
138
139
  # Globbing not safe as path could contain glob metacharacters
139
140
  body = DirectoryBody.new(@root, path, ->(basename) do
140
- stat = stat(::File.join(path, basename))
141
+ stat = stat(basename == '..' ? ::File.dirname(path) : ::File.join(path, basename))
141
142
  next unless stat
142
143
 
143
144
  url = ::File.join(*url_head + [Utils.escape_path(basename)])
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'set'
3
4
  require 'strscan'
4
5
 
5
6
  require_relative '../utils'
@@ -107,19 +108,78 @@ module Rack
107
108
  MultipartInfo = Struct.new :params, :tmp_files
108
109
  EMPTY = MultipartInfo.new(nil, [])
109
110
 
111
+ # Parse the boundary parameter from a multipart content type, following
112
+ # the media type grammar in RFC 9110 Section 8.3.1:
113
+ #
114
+ # media-type = type "/" subtype parameters
115
+ # parameters = *( OWS ";" OWS [ parameter ] )
116
+ # parameter = parameter-name "=" parameter-value
117
+ # parameter-value = ( token / quoted-string )
118
+ #
119
+ # Returns nil if the content type is not multipart or has no boundary
120
+ # parameter. Raises Error if the parameters are malformed, the boundary
121
+ # is empty, or there is more than one boundary parameter.
110
122
  def self.parse_boundary(content_type)
111
- return unless content_type
112
- data = content_type.match(MULTIPART)
113
- return unless data
123
+ return unless content_type && content_type.match?(/\Amultipart\//ni)
114
124
 
115
- unless data[1].empty?
116
- raise Error, "whitespace between boundary parameter name and equal sign"
117
- end
118
- if data.post_match.match?(/boundary\s*=/i)
119
- raise BoundaryTooLongError, "multiple boundary parameters found in multipart content type"
125
+ scanner = StringScanner.new(content_type.b)
126
+ return unless scanner.skip(/multipart\/[!#$%&'*+\-.^_`|~0-9A-Za-z]+/ni)
127
+
128
+ boundary = nil
129
+ boundary_seen = false
130
+ ows = /[ \t]*/n
131
+ # From RFC 9110 Section 5.6.2
132
+ token = /[!#$%&'*+\-.^_`|~0-9A-Za-z]+/n
133
+
134
+ loop do
135
+ scanner.skip(ows)
136
+ break if scanner.eos?
137
+
138
+ unless scanner.skip(/;/n)
139
+ raise Error, "invalid parameters in multipart content type"
140
+ end
141
+
142
+ scanner.skip(ows)
143
+ # Empty parameter, such as in "a=b;;c=d" or a trailing ";".
144
+ next unless name = scanner.scan(token)
145
+ is_boundary = name.casecmp?("boundary")
146
+ # RFC 2231 forms such as boundary*, boundary*0, or boundary*0*. Rack
147
+ # does not use them, but some parsers (for example Go's
148
+ # mime.ParseMediaType) prefer them over a plain boundary parameter,
149
+ # so treat them as boundary parameters when checking for duplicates.
150
+ is_rfc2231_boundary = !is_boundary && name.match?(/\Aboundary\*/ni)
151
+
152
+ unless scanner.skip(/=/n)
153
+ if is_boundary && scanner.match?(/\s+=/n)
154
+ raise Error, "whitespace between boundary parameter name and equal sign"
155
+ end
156
+ raise Error, "invalid parameter in multipart content type"
157
+ end
158
+
159
+ if scanner.scan(/"((?:[\t \x21\x23-\x5B\x5D-\x7E\x80-\xFF]|\\[\t \x21-\x7E\x80-\xFF])*)"/n)
160
+ value = scanner[1]
161
+ value.gsub!(/\\(.)/mn, '\1')
162
+ elsif !(value = scanner.scan(token))
163
+ raise Error, "invalid parameter value in multipart content type"
164
+ end
165
+
166
+ next unless is_boundary || is_rfc2231_boundary
167
+
168
+ if boundary_seen
169
+ raise BoundaryTooLongError, "multiple boundary parameters found in multipart content type"
170
+ end
171
+ boundary_seen = true
172
+
173
+ next unless is_boundary
174
+
175
+ if value.empty?
176
+ raise Error, "empty boundary in multipart content type"
177
+ end
178
+
179
+ boundary = value
120
180
  end
121
181
 
122
- data[2]
182
+ boundary
123
183
  end
124
184
 
125
185
  def self.parse(io, content_length, content_type, tmpfile, bufsize, qp)
@@ -455,6 +515,7 @@ module Rack
455
515
  end
456
516
  else
457
517
  name = head[MULTIPART_CONTENT_ID, 1]
518
+ name.gsub!(OBS_UNFOLD, '\\1') if name
458
519
  end
459
520
 
460
521
  if filename_star
@@ -485,6 +546,7 @@ module Rack
485
546
  # buffering. Note that the actual limit is the higher of 64KB and the buffer size (1MB by default)
486
547
  raise Error, "multipart mime part header too large" if @sbuf.rest.bytesize > MIME_HEADER_BYTESIZE_LIMIT
487
548
 
549
+ @sbuf.string = @sbuf.rest
488
550
  return :want_read
489
551
  end
490
552
  end
@@ -505,8 +567,8 @@ module Rack
505
567
  update_retained_size(body.bytesize) if @body_retained
506
568
  @collector.on_mime_body @mime_index, body
507
569
  @sbuf.pos += delta
508
- @sbuf.string = @sbuf.rest
509
570
  end
571
+ @sbuf.string = @sbuf.rest
510
572
  :want_read
511
573
  end
512
574
  end
@@ -574,13 +636,27 @@ module Rack
574
636
  body.force_encoding(encoding)
575
637
  end
576
638
 
639
+ ENCODING_NAMES = Set.new(Encoding.name_list.map(&:downcase)).freeze
640
+ private_constant :ENCODING_NAMES
641
+
577
642
  # Return the related Encoding object. However, because
578
643
  # enc is submitted by the user, it may be invalid, so
579
644
  # use a binary encoding in that case.
580
645
  def find_encoding(enc)
581
- Encoding.find enc
582
- rescue ArgumentError
583
- Encoding::BINARY
646
+ if respect_requested_encoding?(enc)
647
+ begin
648
+ Encoding.find(enc)
649
+ rescue ArgumentError
650
+ nil
651
+ end
652
+ end || Encoding::BINARY
653
+ end
654
+
655
+ # Whether to respect the given encoding (given as a string). If a
656
+ # requested encoding is not respected, binary encoding is used.
657
+ # The string may be nil or in any encoding, so compare it as bytes.
658
+ def respect_requested_encoding?(encoding_string)
659
+ ENCODING_NAMES.include?(encoding_string.to_s.b.downcase(:ascii))
584
660
  end
585
661
 
586
662
  def handle_empty_content!(content)
@@ -5,8 +5,8 @@ require 'uri'
5
5
 
6
6
  module Rack
7
7
  class QueryParser
8
- DEFAULT_SEP = /& */n
9
- COMMON_SEP = { ";" => /; */n, ";," => /[;,] */n, "&" => /& */n }
8
+ DEFAULT_SEP = "&"
9
+ COMMON_SEP = { ";" => ";", ";," => /[;,]/n, "&" => "&" }
10
10
 
11
11
  # ParameterTypeError is the error that is raised when incoming structural
12
12
  # parameters (parsed by parse_nested_query) contain conflicting types.
@@ -75,7 +75,7 @@ module Rack
75
75
 
76
76
  params = make_params
77
77
 
78
- check_query_string(qs, separator).split(separator ? (COMMON_SEP[separator] || /[#{separator}] */n) : DEFAULT_SEP).each do |p|
78
+ split_query_string(qs, separator).each do |p|
79
79
  next if p.empty?
80
80
  k, v = p.split('=', 2).map!(&unescaper)
81
81
 
@@ -102,7 +102,7 @@ module Rack
102
102
  params = make_params
103
103
 
104
104
  unless qs.nil? || qs.empty?
105
- check_query_string(qs, separator).split(separator ? (COMMON_SEP[separator] || /[#{separator}] */n) : DEFAULT_SEP).each do |p|
105
+ split_query_string(qs, separator).each do |p|
106
106
  k, v = p.split('=', 2).map! { |s| unescape(s) }
107
107
 
108
108
  _normalize_params(params, k, v, 0)
@@ -208,15 +208,35 @@ module Rack
208
208
  def params_hash_has_key?(hash, key)
209
209
  return false if /\[\]/.match?(key)
210
210
 
211
- key.split(/[\[\]]+/).inject(hash) do |h, part|
212
- next h if part == ''
213
- return false unless params_hash_type?(h) && h.key?(part)
214
- h[part]
211
+ # Equivalent to walking key.split(/[\[\]]+/), but a quantified regexp
212
+ # allocates backtracking state for every bracket matched, so find the
213
+ # segments with single character searches instead, which do not
214
+ # backtrack.
215
+ pos = 0
216
+ while (start = key.index(/[^\[\]]/, pos))
217
+ stop = key.index(/[\[\]]/, start) || key.length
218
+ part = key[start, stop - start]
219
+ return false unless params_hash_type?(hash) && hash.key?(part)
220
+ hash = hash[part]
221
+ pos = stop
215
222
  end
216
223
 
217
224
  true
218
225
  end
219
226
 
227
+ def split_query_string(qs, separator)
228
+ pairs = check_query_string(qs, separator).split(separator ? (COMMON_SEP[separator] || /[#{separator}]/n) : DEFAULT_SEP)
229
+
230
+ # Remove spaces following the separator. A quantified regexp such as
231
+ # /& */ allocates backtracking state for every space matched, so find
232
+ # the first non-space character instead, which does not backtrack.
233
+ pairs.each do |pair|
234
+ pair.slice!(0, pair.index(/[^ ]/) || pair.length) if pair.getbyte(0) == 32
235
+ end
236
+
237
+ pairs
238
+ end
239
+
220
240
  def check_query_string(qs, sep)
221
241
  if qs
222
242
  if qs.bytesize > @bytesize_limit
data/lib/rack/request.rb CHANGED
@@ -320,8 +320,10 @@ module Rack
320
320
  end
321
321
 
322
322
  def host_with_port(authority = self.authority)
323
- host, _, port = split_authority(authority)
323
+ parts = split_authority(authority)
324
+ return nil if parts.empty?
324
325
 
326
+ host, _, port = parts
325
327
  if port == DEFAULT_PORTS[self.scheme]
326
328
  host
327
329
  else
@@ -591,7 +593,8 @@ module Rack
591
593
  end
592
594
 
593
595
  def base_url
594
- "#{scheme}://#{host_with_port}"
596
+ hostport = host_with_port || host_with_port(server_authority)
597
+ "#{scheme}://#{hostport}"
595
598
  end
596
599
 
597
600
  # Tries to return a remake of the original request URL as a string.
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
@@ -12,7 +12,7 @@
12
12
  # so it should be enough just to <tt>require 'rack'</tt> in your code.
13
13
 
14
14
  module Rack
15
- RELEASE = "3.1.22"
15
+ RELEASE = "3.1.24"
16
16
 
17
17
  # Return the Rack release as a dotted string.
18
18
  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: 3.1.22
4
+ version: 3.1.24
5
5
  platform: ruby
6
6
  authors:
7
7
  - Leah Neukirchen