rack 2.2.25 → 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: c5322ca41af487959f08981c9964d7ef877f82b0ee4374e4338bedbeb93d3538
4
- data.tar.gz: 4accf7c52defac80efd55caef60ef967f1a813743d8487723e733f66591fce5d
3
+ metadata.gz: e4626f4273d7e4d1e6da249949be7a63375a2a91deab1219093fc053b0ab368f
4
+ data.tar.gz: 3ce5279dbb9bae73dc9fa52807acf315f84cfb1174fc9a31f1b48b4a3cf33644
5
5
  SHA512:
6
- metadata.gz: 4b5cedb21343a2e062ca5daf8c55f88fe29bc7d884244dcd13ca6cad858e61764b18f71ea93cfb6a538bc03dadbb82ad7471895d1f6cbc1b2bc727651a807a11
7
- data.tar.gz: 5bc5a948d20e77d1c831173b4ba5ba5b661bc985f60a6bf9fd1224033c6725ed2c8753f39766a3fe386b56457de9439ace3459cd0f1f650dd0c7fdb2effdcc13
6
+ metadata.gz: 3ed69fae635a177af85133348ac99118fe2a1861d58eb6a41415da21575cb8197ece9d578342e2826f646c7a4e2c2d349d79f802dc7e33e1d69a3b02a874c780
7
+ data.tar.gz: eec9ff7a269ce2c7e51703aa7e48c562b163e77c6957ba601b34e516694c8b142ef1c1468e1ffa474cd87f37712439ad1e52d6f8125389d51a7395365854e471
data/CHANGELOG.md CHANGED
@@ -2,6 +2,19 @@
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
+
5
18
  ## [2.2.25] - 2026-10-30
6
19
 
7
20
  ### 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)])
@@ -88,23 +88,78 @@ module Rack
88
88
  MultipartInfo = Struct.new :params, :tmp_files
89
89
  EMPTY = MultipartInfo.new(nil, [])
90
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.
91
102
  def self.parse_boundary(content_type)
92
- return unless content_type
93
- data = content_type.match(MULTIPART)
94
- return unless data
103
+ return unless content_type && /\Amultipart\//ni.match?(content_type)
95
104
 
96
- unless data[1].empty?
97
- raise EOFError, "whitespace between boundary parameter name and equal sign"
98
- end
99
- # Count across the whole header rather than only after the match: the
100
- # lazy prefix in MULTIPART can skip a first boundary parameter whose
101
- # value is empty or starts with a quote, semicolon, or comma. Also
102
- # count RFC 2231 boundary* parameters, which some parsers prefer.
103
- if content_type.scan(/boundary(?:\*\d*\*?)?\s*=/i).size > 1
104
- 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
105
160
  end
106
161
 
107
- data[2]
162
+ boundary
108
163
  end
109
164
 
110
165
  def self.parse(io, content_length, content_type, tmpfile, bufsize, qp)
@@ -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/version.rb CHANGED
@@ -20,7 +20,7 @@ module Rack
20
20
  VERSION.join(".")
21
21
  end
22
22
 
23
- RELEASE = "2.2.25"
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.25
4
+ version: 2.2.26
5
5
  platform: ruby
6
6
  authors:
7
7
  - Leah Neukirchen