rack 3.1.24 → 3.2.0
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.
Potentially problematic release.
This version of rack might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/CHANGELOG.md +57 -134
- data/README.md +41 -28
- data/SPEC.rdoc +199 -306
- data/lib/rack/auth/abstract/request.rb +2 -0
- data/lib/rack/builder.rb +6 -0
- data/lib/rack/conditional_get.rb +4 -3
- data/lib/rack/constants.rb +1 -0
- data/lib/rack/directory.rb +4 -8
- data/lib/rack/files.rb +1 -1
- data/lib/rack/head.rb +2 -3
- data/lib/rack/lint.rb +430 -457
- data/lib/rack/media_type.rb +6 -7
- data/lib/rack/mock_response.rb +19 -25
- data/lib/rack/multipart/parser.rb +42 -181
- data/lib/rack/multipart/uploaded_file.rb +42 -5
- data/lib/rack/query_parser.rb +43 -48
- data/lib/rack/request.rb +50 -65
- data/lib/rack/rewindable_input.rb +4 -1
- data/lib/rack/sendfile.rb +21 -51
- data/lib/rack/show_exceptions.rb +4 -2
- data/lib/rack/show_status.rb +0 -2
- data/lib/rack/static.rb +3 -7
- data/lib/rack/utils.rb +28 -129
- data/lib/rack/version.rb +4 -8
- data/lib/rack.rb +0 -1
- metadata +3 -3
- data/lib/rack/logger.rb +0 -23
data/lib/rack/media_type.rb
CHANGED
|
@@ -14,12 +14,11 @@ module Rack
|
|
|
14
14
|
# For more information on the use of media types in HTTP, see:
|
|
15
15
|
# http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7
|
|
16
16
|
def type(content_type)
|
|
17
|
-
return nil unless content_type
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
end
|
|
17
|
+
return nil unless content_type && !content_type.empty?
|
|
18
|
+
type = content_type.split(SPLIT_PATTERN, 2).first
|
|
19
|
+
type.rstrip!
|
|
20
|
+
type.downcase!
|
|
21
|
+
type
|
|
23
22
|
end
|
|
24
23
|
|
|
25
24
|
# The media type parameters provided in CONTENT_TYPE as a Hash, or
|
|
@@ -33,7 +32,7 @@ module Rack
|
|
|
33
32
|
# and "text/plain;charset" will return { 'charset' => '' }, similarly to
|
|
34
33
|
# the query params parser (barring the latter case, which returns nil instead)).
|
|
35
34
|
def params(content_type)
|
|
36
|
-
return {} if content_type.nil?
|
|
35
|
+
return {} if content_type.nil? || content_type.empty?
|
|
37
36
|
|
|
38
37
|
content_type.split(SPLIT_PATTERN)[1..-1].each_with_object({}) do |s, hsh|
|
|
39
38
|
s.strip!
|
data/lib/rack/mock_response.rb
CHANGED
|
@@ -10,33 +10,27 @@ module Rack
|
|
|
10
10
|
# MockRequest.
|
|
11
11
|
|
|
12
12
|
class MockResponse < Rack::Response
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
@path = args["path"]
|
|
25
|
-
@domain = args["domain"]
|
|
26
|
-
@expires = args["expires"]
|
|
27
|
-
@secure = args["secure"]
|
|
28
|
-
end
|
|
13
|
+
class Cookie
|
|
14
|
+
attr_reader :name, :value, :path, :domain, :expires, :secure
|
|
15
|
+
|
|
16
|
+
def initialize(args)
|
|
17
|
+
@name = args["name"]
|
|
18
|
+
@value = args["value"]
|
|
19
|
+
@path = args["path"]
|
|
20
|
+
@domain = args["domain"]
|
|
21
|
+
@expires = args["expires"]
|
|
22
|
+
@secure = args["secure"]
|
|
23
|
+
end
|
|
29
24
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
25
|
+
def method_missing(method_name, *args, &block)
|
|
26
|
+
@value.send(method_name, *args, &block)
|
|
27
|
+
end
|
|
28
|
+
# :nocov:
|
|
29
|
+
ruby2_keywords(:method_missing) if respond_to?(:ruby2_keywords, true)
|
|
30
|
+
# :nocov:
|
|
36
31
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
end
|
|
32
|
+
def respond_to_missing?(method_name, include_all = false)
|
|
33
|
+
@value.respond_to?(method_name, include_all) || super
|
|
40
34
|
end
|
|
41
35
|
end
|
|
42
36
|
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require 'set'
|
|
4
3
|
require 'strscan'
|
|
5
4
|
|
|
6
5
|
require_relative '../utils'
|
|
@@ -34,11 +33,23 @@ module Rack
|
|
|
34
33
|
EOL = "\r\n"
|
|
35
34
|
FWS = /[ \t]+(?:\r\n[ \t]+)?/ # whitespace with optional folding
|
|
36
35
|
HEADER_VALUE = "(?:[^\r\n]|\r\n[ \t])*" # anything but a non-folding CRLF
|
|
37
|
-
MULTIPART = %r|\Amultipart
|
|
36
|
+
MULTIPART = %r|\Amultipart/.*boundary=\"?([^\";,]+)\"?|ni
|
|
38
37
|
MULTIPART_CONTENT_TYPE = /^Content-Type:#{FWS}?(#{HEADER_VALUE})/ni
|
|
39
38
|
MULTIPART_CONTENT_DISPOSITION = /^Content-Disposition:#{FWS}?(#{HEADER_VALUE})/ni
|
|
40
39
|
MULTIPART_CONTENT_ID = /^Content-ID:#{FWS}?(#{HEADER_VALUE})/ni
|
|
41
40
|
|
|
41
|
+
# Rack::Multipart::Parser handles parsing of multipart/form-data requests.
|
|
42
|
+
#
|
|
43
|
+
# File Parameter Contents
|
|
44
|
+
#
|
|
45
|
+
# When processing file uploads, the parser returns a hash containing
|
|
46
|
+
# information about uploaded files. For +file+ parameters, the hash includes:
|
|
47
|
+
#
|
|
48
|
+
# * +:filename+ - The original filename, already URL decoded by the parser
|
|
49
|
+
# * +:type+ - The content type of the uploaded file
|
|
50
|
+
# * +:name+ - The parameter name from the form
|
|
51
|
+
# * +:tempfile+ - A Tempfile object containing the uploaded data
|
|
52
|
+
# * +:head+ - The raw header content for this part
|
|
42
53
|
class Parser
|
|
43
54
|
BUFSIZE = 1_048_576
|
|
44
55
|
TEXT_PLAIN = "text/plain"
|
|
@@ -48,34 +59,6 @@ module Rack
|
|
|
48
59
|
Tempfile.new(["RackMultipart", extension])
|
|
49
60
|
}
|
|
50
61
|
|
|
51
|
-
BOUNDARY_START_LIMIT = 16 * 1024
|
|
52
|
-
private_constant :BOUNDARY_START_LIMIT
|
|
53
|
-
|
|
54
|
-
MIME_HEADER_BYTESIZE_LIMIT = 64 * 1024
|
|
55
|
-
private_constant :MIME_HEADER_BYTESIZE_LIMIT
|
|
56
|
-
|
|
57
|
-
env_int = lambda do |key, val|
|
|
58
|
-
if str_val = ENV[key]
|
|
59
|
-
begin
|
|
60
|
-
val = Integer(str_val, 10)
|
|
61
|
-
rescue ArgumentError
|
|
62
|
-
raise ArgumentError, "non-integer value provided for environment variable #{key}"
|
|
63
|
-
end
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
val
|
|
67
|
-
end
|
|
68
|
-
|
|
69
|
-
BUFFERED_UPLOAD_BYTESIZE_LIMIT = env_int.call("RACK_MULTIPART_BUFFERED_UPLOAD_BYTESIZE_LIMIT", 16 * 1024 * 1024)
|
|
70
|
-
private_constant :BUFFERED_UPLOAD_BYTESIZE_LIMIT
|
|
71
|
-
|
|
72
|
-
bytesize_limit = env_int.call("RACK_MULTIPART_PARSER_BYTESIZE_LIMIT", 10 * 1024 * 1024 * 1024)
|
|
73
|
-
PARSER_BYTESIZE_LIMIT = bytesize_limit > 0 ? bytesize_limit : nil
|
|
74
|
-
private_constant :PARSER_BYTESIZE_LIMIT
|
|
75
|
-
|
|
76
|
-
CONTENT_DISPOSITION_QUOTED_ESCAPES_LIMIT = env_int.call("RACK_MULTIPART_CONTENT_DISPOSITION_QUOTED_ESCAPES_LIMIT", 8 * 1024)
|
|
77
|
-
private_constant :CONTENT_DISPOSITION_QUOTED_ESCAPES_LIMIT
|
|
78
|
-
|
|
79
62
|
class BoundedIO # :nodoc:
|
|
80
63
|
def initialize(io, content_length)
|
|
81
64
|
@io = io
|
|
@@ -108,78 +91,11 @@ module Rack
|
|
|
108
91
|
MultipartInfo = Struct.new :params, :tmp_files
|
|
109
92
|
EMPTY = MultipartInfo.new(nil, [])
|
|
110
93
|
|
|
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.
|
|
122
94
|
def self.parse_boundary(content_type)
|
|
123
|
-
return unless content_type
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
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
|
|
180
|
-
end
|
|
181
|
-
|
|
182
|
-
boundary
|
|
95
|
+
return unless content_type
|
|
96
|
+
data = content_type.match(MULTIPART)
|
|
97
|
+
return unless data
|
|
98
|
+
data[1]
|
|
183
99
|
end
|
|
184
100
|
|
|
185
101
|
def self.parse(io, content_length, content_type, tmpfile, bufsize, qp)
|
|
@@ -188,10 +104,6 @@ module Rack
|
|
|
188
104
|
boundary = parse_boundary content_type
|
|
189
105
|
return EMPTY unless boundary
|
|
190
106
|
|
|
191
|
-
if PARSER_BYTESIZE_LIMIT && content_length && content_length > PARSER_BYTESIZE_LIMIT
|
|
192
|
-
raise Error, "multipart Content-Length #{content_length} exceeds limit of #{PARSER_BYTESIZE_LIMIT} bytes"
|
|
193
|
-
end
|
|
194
|
-
|
|
195
107
|
if boundary.length > 70
|
|
196
108
|
# RFC 1521 Section 7.2.1 imposes a 70 character maximum for the boundary.
|
|
197
109
|
# Most clients use no more than 55 characters.
|
|
@@ -306,10 +218,6 @@ module Rack
|
|
|
306
218
|
|
|
307
219
|
@state = :FAST_FORWARD
|
|
308
220
|
@mime_index = 0
|
|
309
|
-
@body_retained = nil
|
|
310
|
-
@retained_size = 0
|
|
311
|
-
@total_bytes_read = (0 if PARSER_BYTESIZE_LIMIT)
|
|
312
|
-
@content_disposition_quoted_escapes = 0
|
|
313
221
|
@collector = Collector.new tempfile
|
|
314
222
|
|
|
315
223
|
@sbuf = StringScanner.new("".dup)
|
|
@@ -321,7 +229,6 @@ module Rack
|
|
|
321
229
|
end
|
|
322
230
|
|
|
323
231
|
def parse(io)
|
|
324
|
-
@total_bytes_read &&= nil if io.is_a?(BoundedIO)
|
|
325
232
|
outbuf = String.new
|
|
326
233
|
read_data(io, outbuf)
|
|
327
234
|
|
|
@@ -348,7 +255,8 @@ module Rack
|
|
|
348
255
|
@collector.each do |part|
|
|
349
256
|
part.get_data do |data|
|
|
350
257
|
tag_multipart_encoding(part.filename, part.content_type, part.name, data)
|
|
351
|
-
|
|
258
|
+
name, data = handle_dummy_encoding(part.name, data)
|
|
259
|
+
@query_parser.normalize_params(@params, name, data)
|
|
352
260
|
end
|
|
353
261
|
end
|
|
354
262
|
MultipartInfo.new @params.to_params_hash, @collector.find_all(&:file?).map(&:body)
|
|
@@ -356,21 +264,9 @@ module Rack
|
|
|
356
264
|
|
|
357
265
|
private
|
|
358
266
|
|
|
359
|
-
def dequote(str) # From WEBrick::HTTPUtils
|
|
360
|
-
ret = (/\A"(.*)"\Z/ =~ str) ? $1 : str.dup
|
|
361
|
-
ret.gsub!(/\\(.)/, "\\1")
|
|
362
|
-
ret
|
|
363
|
-
end
|
|
364
|
-
|
|
365
267
|
def read_data(io, outbuf)
|
|
366
268
|
content = io.read(@bufsize, outbuf)
|
|
367
269
|
handle_empty_content!(content)
|
|
368
|
-
if @total_bytes_read
|
|
369
|
-
@total_bytes_read += content.bytesize
|
|
370
|
-
if @total_bytes_read > PARSER_BYTESIZE_LIMIT
|
|
371
|
-
raise Error, "multipart upload exceeds limit of #{PARSER_BYTESIZE_LIMIT} bytes"
|
|
372
|
-
end
|
|
373
|
-
end
|
|
374
270
|
@sbuf.concat(content)
|
|
375
271
|
end
|
|
376
272
|
|
|
@@ -398,10 +294,6 @@ module Rack
|
|
|
398
294
|
|
|
399
295
|
# retry for opening boundary
|
|
400
296
|
else
|
|
401
|
-
# We raise if we don't find the multipart boundary, to avoid unbounded memory
|
|
402
|
-
# buffering. Note that the actual limit is the higher of 16KB and the buffer size (1MB by default)
|
|
403
|
-
raise Error, "multipart boundary not found within limit" if @sbuf.string.bytesize > BOUNDARY_START_LIMIT
|
|
404
|
-
|
|
405
297
|
# no boundary found, keep reading data
|
|
406
298
|
return :want_read
|
|
407
299
|
end
|
|
@@ -420,19 +312,13 @@ module Rack
|
|
|
420
312
|
|
|
421
313
|
CONTENT_DISPOSITION_MAX_PARAMS = 16
|
|
422
314
|
CONTENT_DISPOSITION_MAX_BYTES = 1536
|
|
423
|
-
OBS_UNFOLD = /\r\n([ \t])/
|
|
424
|
-
private_constant :OBS_UNFOLD
|
|
425
315
|
def handle_mime_head
|
|
426
316
|
if @sbuf.scan_until(@head_regex)
|
|
427
317
|
head = @sbuf[1]
|
|
428
318
|
content_type = head[MULTIPART_CONTENT_TYPE, 1]
|
|
429
|
-
content_type.gsub!(OBS_UNFOLD, '\\1') if content_type
|
|
430
319
|
if (disposition = head[MULTIPART_CONTENT_DISPOSITION, 1]) &&
|
|
431
320
|
disposition.bytesize <= CONTENT_DISPOSITION_MAX_BYTES
|
|
432
321
|
|
|
433
|
-
# Implement OBS unfolding (RFC 5322 Section 2.2.3)
|
|
434
|
-
disposition.gsub!(OBS_UNFOLD, '\\1')
|
|
435
|
-
|
|
436
322
|
# ignore actual content-disposition value (should always be form-data)
|
|
437
323
|
i = disposition.index(';')
|
|
438
324
|
disposition.slice!(0, i+1)
|
|
@@ -470,11 +356,6 @@ module Rack
|
|
|
470
356
|
# stop parsing parameter value if found ending quote
|
|
471
357
|
break if c == '"'
|
|
472
358
|
|
|
473
|
-
@content_disposition_quoted_escapes += 1
|
|
474
|
-
if @content_disposition_quoted_escapes > CONTENT_DISPOSITION_QUOTED_ESCAPES_LIMIT
|
|
475
|
-
raise Error, "number of quoted escapes during content disposition parsing exceeds limit"
|
|
476
|
-
end
|
|
477
|
-
|
|
478
359
|
escaped_char = disposition.slice!(0, 1)
|
|
479
360
|
if param == 'filename' && escaped_char != '"'
|
|
480
361
|
# Possible IE uploaded filename, append both escape backslash and value
|
|
@@ -515,7 +396,6 @@ module Rack
|
|
|
515
396
|
end
|
|
516
397
|
else
|
|
517
398
|
name = head[MULTIPART_CONTENT_ID, 1]
|
|
518
|
-
name.gsub!(OBS_UNFOLD, '\\1') if name
|
|
519
399
|
end
|
|
520
400
|
|
|
521
401
|
if filename_star
|
|
@@ -530,31 +410,16 @@ module Rack
|
|
|
530
410
|
name = filename || "#{content_type || TEXT_PLAIN}[]".dup
|
|
531
411
|
end
|
|
532
412
|
|
|
533
|
-
# Mime part head data is retained for both TempfilePart and BufferPart
|
|
534
|
-
# for the entireity of the parse, even though it isn't used for BufferPart.
|
|
535
|
-
update_retained_size(head.bytesize)
|
|
536
|
-
|
|
537
|
-
# If a filename is given, a TempfilePart will be used, so the body will
|
|
538
|
-
# not be buffered in memory. However, if a filename is not given, a BufferPart
|
|
539
|
-
# will be used, and the body will be buffered in memory.
|
|
540
|
-
@body_retained = !filename
|
|
541
|
-
|
|
542
413
|
@collector.on_mime_head @mime_index, head, filename, content_type, name
|
|
543
414
|
@state = :MIME_BODY
|
|
544
415
|
else
|
|
545
|
-
|
|
546
|
-
# buffering. Note that the actual limit is the higher of 64KB and the buffer size (1MB by default)
|
|
547
|
-
raise Error, "multipart mime part header too large" if @sbuf.rest.bytesize > MIME_HEADER_BYTESIZE_LIMIT
|
|
548
|
-
|
|
549
|
-
@sbuf.string = @sbuf.rest
|
|
550
|
-
return :want_read
|
|
416
|
+
:want_read
|
|
551
417
|
end
|
|
552
418
|
end
|
|
553
419
|
|
|
554
420
|
def handle_mime_body
|
|
555
421
|
if (body_with_boundary = @sbuf.check_until(@body_regex)) # check but do not advance the pointer yet
|
|
556
422
|
body = body_with_boundary.sub(@body_regex_at_end, '') # remove the boundary from the string
|
|
557
|
-
update_retained_size(body.bytesize) if @body_retained
|
|
558
423
|
@collector.on_mime_body @mime_index, body
|
|
559
424
|
@sbuf.pos += body.length + 2 # skip \r\n after the content
|
|
560
425
|
@state = :CONSUME_TOKEN
|
|
@@ -563,23 +428,14 @@ module Rack
|
|
|
563
428
|
# Save what we have so far
|
|
564
429
|
if @rx_max_size < @sbuf.rest_size
|
|
565
430
|
delta = @sbuf.rest_size - @rx_max_size
|
|
566
|
-
|
|
567
|
-
update_retained_size(body.bytesize) if @body_retained
|
|
568
|
-
@collector.on_mime_body @mime_index, body
|
|
431
|
+
@collector.on_mime_body @mime_index, @sbuf.peek(delta)
|
|
569
432
|
@sbuf.pos += delta
|
|
433
|
+
@sbuf.string = @sbuf.rest
|
|
570
434
|
end
|
|
571
|
-
@sbuf.string = @sbuf.rest
|
|
572
435
|
:want_read
|
|
573
436
|
end
|
|
574
437
|
end
|
|
575
438
|
|
|
576
|
-
def update_retained_size(size)
|
|
577
|
-
@retained_size += size
|
|
578
|
-
if @retained_size > BUFFERED_UPLOAD_BYTESIZE_LIMIT
|
|
579
|
-
raise Error, "multipart data over retained size limit"
|
|
580
|
-
end
|
|
581
|
-
end
|
|
582
|
-
|
|
583
439
|
# Scan until the we find the start or end of the boundary.
|
|
584
440
|
# If we find it, return the appropriate symbol for the start or
|
|
585
441
|
# end of the boundary. If we don't find the start or end of the
|
|
@@ -636,27 +492,32 @@ module Rack
|
|
|
636
492
|
body.force_encoding(encoding)
|
|
637
493
|
end
|
|
638
494
|
|
|
639
|
-
ENCODING_NAMES = Set.new(Encoding.name_list.map(&:downcase)).freeze
|
|
640
|
-
private_constant :ENCODING_NAMES
|
|
641
|
-
|
|
642
495
|
# Return the related Encoding object. However, because
|
|
643
496
|
# enc is submitted by the user, it may be invalid, so
|
|
644
497
|
# use a binary encoding in that case.
|
|
645
498
|
def find_encoding(enc)
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
rescue ArgumentError
|
|
650
|
-
nil
|
|
651
|
-
end
|
|
652
|
-
end || Encoding::BINARY
|
|
499
|
+
Encoding.find enc
|
|
500
|
+
rescue ArgumentError
|
|
501
|
+
Encoding::BINARY
|
|
653
502
|
end
|
|
654
503
|
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
504
|
+
REENCODE_DUMMY_ENCODINGS = {
|
|
505
|
+
# ISO-2022-JP is a legacy but still widely used encoding in Japan
|
|
506
|
+
# Here we convert ISO-2022-JP to UTF-8 so that it can be handled.
|
|
507
|
+
Encoding::ISO_2022_JP => true
|
|
508
|
+
|
|
509
|
+
# Other dummy encodings are rarely used and have not been supported yet.
|
|
510
|
+
# Adding support for them will require careful considerations.
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
def handle_dummy_encoding(name, body)
|
|
514
|
+
# A string object with a 'dummy' encoding does not have full functionality and can cause errors.
|
|
515
|
+
# So here we covert it to UTF-8 so that it can be handled properly.
|
|
516
|
+
if name.encoding.dummy? && REENCODE_DUMMY_ENCODINGS[name.encoding]
|
|
517
|
+
name = name.encode(Encoding::UTF_8)
|
|
518
|
+
body = body.encode(Encoding::UTF_8)
|
|
519
|
+
end
|
|
520
|
+
return name, body
|
|
660
521
|
end
|
|
661
522
|
|
|
662
523
|
def handle_empty_content!(content)
|
|
@@ -5,14 +5,47 @@ require 'fileutils'
|
|
|
5
5
|
|
|
6
6
|
module Rack
|
|
7
7
|
module Multipart
|
|
8
|
+
# Despite the misleading name, UploadedFile is designed for use for
|
|
9
|
+
# preparing multipart file upload bodies, generally for use in tests.
|
|
10
|
+
# It is not designed for and should not be used for handling uploaded
|
|
11
|
+
# files (there is no need for that, since Rack's multipart parser
|
|
12
|
+
# already creates Tempfiles for that). Using this with non-trusted
|
|
13
|
+
# filenames can create a security vulnerability.
|
|
14
|
+
#
|
|
15
|
+
# You should only use this class if you plan on passing the instances
|
|
16
|
+
# to Rack::MockRequest for use in creating multipart request bodies.
|
|
17
|
+
#
|
|
18
|
+
# UploadedFile delegates most methods to the tempfile it contains.
|
|
8
19
|
class UploadedFile
|
|
9
|
-
|
|
10
|
-
#
|
|
20
|
+
# The provided name of the file. This generally is the basename of
|
|
21
|
+
# path provided during initialization, but it can contain slashes if they
|
|
22
|
+
# were present in the filename argument when the instance was created.
|
|
11
23
|
attr_reader :original_filename
|
|
12
24
|
|
|
13
|
-
# The content type of the
|
|
25
|
+
# The content type of the instance.
|
|
14
26
|
attr_accessor :content_type
|
|
15
27
|
|
|
28
|
+
# Create a new UploadedFile. For backwards compatibility, this accepts
|
|
29
|
+
# both positional and keyword versions of the same arguments:
|
|
30
|
+
#
|
|
31
|
+
# filepath/path :: The path to the file
|
|
32
|
+
# ct/content_type :: The content_type of the file
|
|
33
|
+
# bin/binary :: Whether to set binmode on the file before copying data into it.
|
|
34
|
+
#
|
|
35
|
+
# If both positional and keyword arguments are present, the keyword arguments
|
|
36
|
+
# take precedence.
|
|
37
|
+
#
|
|
38
|
+
# The following keyword-only arguments are also accepted:
|
|
39
|
+
#
|
|
40
|
+
# filename :: Override the filename to use for the file. This is so the
|
|
41
|
+
# filename for the upload does not need to match the basename of
|
|
42
|
+
# the file path. This should not contain slashes, unless you are
|
|
43
|
+
# trying to test how an application handles invalid filenames in
|
|
44
|
+
# multipart upload bodies.
|
|
45
|
+
# io :: Use the given IO-like instance as the tempfile, instead of creating
|
|
46
|
+
# a Tempfile instance. This is useful for building multipart file
|
|
47
|
+
# upload bodies without a file being present on the filesystem. If you are
|
|
48
|
+
# providing this, you should also provide the filename argument.
|
|
16
49
|
def initialize(filepath = nil, ct = "text/plain", bin = false,
|
|
17
50
|
path: filepath, content_type: ct, binary: bin, filename: nil, io: nil)
|
|
18
51
|
if io
|
|
@@ -28,15 +61,19 @@ module Rack
|
|
|
28
61
|
@content_type = content_type
|
|
29
62
|
end
|
|
30
63
|
|
|
64
|
+
# The path of the tempfile for the instance, if the tempfile has a path.
|
|
65
|
+
# nil if the tempfile does not have a path.
|
|
31
66
|
def path
|
|
32
67
|
@tempfile.path if @tempfile.respond_to?(:path)
|
|
33
68
|
end
|
|
34
69
|
alias_method :local_path, :path
|
|
35
70
|
|
|
36
|
-
|
|
37
|
-
|
|
71
|
+
# Return true if the tempfile responds to the method.
|
|
72
|
+
def respond_to_missing?(*args)
|
|
73
|
+
@tempfile.respond_to?(*args)
|
|
38
74
|
end
|
|
39
75
|
|
|
76
|
+
# Delegate method missing calls to the tempfile.
|
|
40
77
|
def method_missing(method_name, *args, &block) #:nodoc:
|
|
41
78
|
@tempfile.__send__(method_name, *args, &block)
|
|
42
79
|
end
|
data/lib/rack/query_parser.rb
CHANGED
|
@@ -5,8 +5,8 @@ require 'uri'
|
|
|
5
5
|
|
|
6
6
|
module Rack
|
|
7
7
|
class QueryParser
|
|
8
|
-
DEFAULT_SEP =
|
|
9
|
-
COMMON_SEP = { ";" =>
|
|
8
|
+
DEFAULT_SEP = /& */n
|
|
9
|
+
COMMON_SEP = { ";" => /; */n, ";," => /[;,] */n, "&" => /& */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.
|
|
@@ -57,8 +57,6 @@ module Rack
|
|
|
57
57
|
PARAMS_LIMIT = env_int.call("RACK_QUERY_PARSER_PARAMS_LIMIT", 4096)
|
|
58
58
|
private_constant :PARAMS_LIMIT
|
|
59
59
|
|
|
60
|
-
attr_reader :bytesize_limit
|
|
61
|
-
|
|
62
60
|
def initialize(params_class, param_depth_limit, bytesize_limit: BYTESIZE_LIMIT, params_limit: PARAMS_LIMIT)
|
|
63
61
|
@params_class = params_class
|
|
64
62
|
@param_depth_limit = param_depth_limit
|
|
@@ -71,14 +69,9 @@ module Rack
|
|
|
71
69
|
# to parse cookies by changing the characters used in the second parameter
|
|
72
70
|
# (which defaults to '&').
|
|
73
71
|
def parse_query(qs, separator = nil, &unescaper)
|
|
74
|
-
unescaper ||= method(:unescape)
|
|
75
|
-
|
|
76
72
|
params = make_params
|
|
77
73
|
|
|
78
|
-
|
|
79
|
-
next if p.empty?
|
|
80
|
-
k, v = p.split('=', 2).map!(&unescaper)
|
|
81
|
-
|
|
74
|
+
each_query_pair(qs, separator, unescaper) do |k, v|
|
|
82
75
|
if cur = params[k]
|
|
83
76
|
if cur.class == Array
|
|
84
77
|
params[k] << v
|
|
@@ -93,6 +86,19 @@ module Rack
|
|
|
93
86
|
return params.to_h
|
|
94
87
|
end
|
|
95
88
|
|
|
89
|
+
# Parses a query string by breaking it up at the '&', returning all key-value
|
|
90
|
+
# pairs as an array of [key, value] arrays. Unlike parse_query, this preserves
|
|
91
|
+
# all duplicate keys rather than collapsing them.
|
|
92
|
+
def parse_query_pairs(qs, separator = nil)
|
|
93
|
+
pairs = []
|
|
94
|
+
|
|
95
|
+
each_query_pair(qs, separator) do |k, v|
|
|
96
|
+
pairs << [k, v]
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
pairs
|
|
100
|
+
end
|
|
101
|
+
|
|
96
102
|
# parse_nested_query expands a query string into structural types. Supported
|
|
97
103
|
# types are Arrays, Hashes and basic value types. It is possible to supply
|
|
98
104
|
# query strings with parameters of conflicting types, in this case a
|
|
@@ -101,17 +107,11 @@ module Rack
|
|
|
101
107
|
def parse_nested_query(qs, separator = nil)
|
|
102
108
|
params = make_params
|
|
103
109
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
k, v = p.split('=', 2).map! { |s| unescape(s) }
|
|
107
|
-
|
|
108
|
-
_normalize_params(params, k, v, 0)
|
|
109
|
-
end
|
|
110
|
+
each_query_pair(qs, separator) do |k, v|
|
|
111
|
+
_normalize_params(params, k, v, 0)
|
|
110
112
|
end
|
|
111
113
|
|
|
112
114
|
return params.to_h
|
|
113
|
-
rescue ArgumentError => e
|
|
114
|
-
raise InvalidParameterError, e.message, e.backtrace
|
|
115
115
|
end
|
|
116
116
|
|
|
117
117
|
# normalize_params recursively expands parameters into structural types. If
|
|
@@ -208,49 +208,44 @@ module Rack
|
|
|
208
208
|
def params_hash_has_key?(hash, key)
|
|
209
209
|
return false if /\[\]/.match?(key)
|
|
210
210
|
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
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
|
|
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]
|
|
222
215
|
end
|
|
223
216
|
|
|
224
217
|
true
|
|
225
218
|
end
|
|
226
219
|
|
|
227
|
-
def
|
|
228
|
-
|
|
220
|
+
def each_query_pair(qs, separator, unescaper = nil)
|
|
221
|
+
return if !qs || qs.empty?
|
|
229
222
|
|
|
230
|
-
|
|
231
|
-
|
|
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
|
|
223
|
+
if qs.bytesize > @bytesize_limit
|
|
224
|
+
raise QueryLimitError, "total query size (#{qs.bytesize}) exceeds limit (#{@bytesize_limit})"
|
|
235
225
|
end
|
|
236
226
|
|
|
237
|
-
pairs
|
|
238
|
-
end
|
|
227
|
+
pairs = qs.split(separator ? (COMMON_SEP[separator] || /[#{separator}] */n) : DEFAULT_SEP, @params_limit + 1)
|
|
239
228
|
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
end
|
|
229
|
+
if pairs.size > @params_limit
|
|
230
|
+
param_count = pairs.size + pairs.last.count(separator || "&")
|
|
231
|
+
raise QueryLimitError, "total number of query parameters (#{param_count}) exceeds limit (#{@params_limit})"
|
|
232
|
+
end
|
|
245
233
|
|
|
246
|
-
|
|
247
|
-
|
|
234
|
+
if unescaper
|
|
235
|
+
pairs.each do |p|
|
|
236
|
+
next if p.empty?
|
|
237
|
+
k, v = p.split('=', 2).map!(&unescaper)
|
|
238
|
+
yield k, v
|
|
248
239
|
end
|
|
249
|
-
|
|
250
|
-
qs
|
|
251
240
|
else
|
|
252
|
-
|
|
241
|
+
pairs.each do |p|
|
|
242
|
+
next if p.empty?
|
|
243
|
+
k, v = p.split('=', 2).map! { |s| unescape(s) }
|
|
244
|
+
yield k, v
|
|
245
|
+
end
|
|
253
246
|
end
|
|
247
|
+
rescue ArgumentError => e
|
|
248
|
+
raise InvalidParameterError, e.message, e.backtrace
|
|
254
249
|
end
|
|
255
250
|
|
|
256
251
|
def unescape(string, encoding = Encoding::UTF_8)
|