homura-runtime 0.3.2 → 0.3.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 +4 -4
- data/CHANGELOG.md +18 -0
- data/exe/compile-assets +2 -2
- data/exe/compile-erb +5 -7
- data/lib/homura/runtime/build_support.rb +19 -2
- data/lib/homura/runtime/version.rb +1 -1
- data/vendor/rack/auth/abstract/handler.rb +41 -0
- data/vendor/rack/auth/abstract/request.rb +51 -0
- data/vendor/rack/auth/basic.rb +58 -0
- data/vendor/rack/bad_request.rb +8 -0
- data/vendor/rack/body_proxy.rb +63 -0
- data/vendor/rack/builder.rb +315 -0
- data/vendor/rack/cascade.rb +67 -0
- data/vendor/rack/common_logger.rb +94 -0
- data/vendor/rack/conditional_get.rb +87 -0
- data/vendor/rack/config.rb +22 -0
- data/vendor/rack/constants.rb +68 -0
- data/vendor/rack/content_length.rb +34 -0
- data/vendor/rack/content_type.rb +33 -0
- data/vendor/rack/deflater.rb +159 -0
- data/vendor/rack/directory.rb +210 -0
- data/vendor/rack/etag.rb +71 -0
- data/vendor/rack/events.rb +172 -0
- data/vendor/rack/files.rb +224 -0
- data/vendor/rack/head.rb +25 -0
- data/vendor/rack/headers.rb +238 -0
- data/vendor/rack/lint.rb +1000 -0
- data/vendor/rack/lock.rb +29 -0
- data/vendor/rack/media_type.rb +42 -0
- data/vendor/rack/method_override.rb +56 -0
- data/vendor/rack/mime.rb +694 -0
- data/vendor/rack/mock.rb +3 -0
- data/vendor/rack/mock_request.rb +161 -0
- data/vendor/rack/mock_response.rb +147 -0
- data/vendor/rack/multipart/generator.rb +99 -0
- data/vendor/rack/multipart/parser.rb +586 -0
- data/vendor/rack/multipart/uploaded_file.rb +82 -0
- data/vendor/rack/multipart.rb +77 -0
- data/vendor/rack/null_logger.rb +48 -0
- data/vendor/rack/protection/authenticity_token.rb +256 -0
- data/vendor/rack/protection/base.rb +140 -0
- data/vendor/rack/protection/content_security_policy.rb +80 -0
- data/vendor/rack/protection/cookie_tossing.rb +77 -0
- data/vendor/rack/protection/escaped_params.rb +93 -0
- data/vendor/rack/protection/form_token.rb +25 -0
- data/vendor/rack/protection/frame_options.rb +39 -0
- data/vendor/rack/protection/http_origin.rb +43 -0
- data/vendor/rack/protection/ip_spoofing.rb +27 -0
- data/vendor/rack/protection/json_csrf.rb +60 -0
- data/vendor/rack/protection/path_traversal.rb +45 -0
- data/vendor/rack/protection/referrer_policy.rb +27 -0
- data/vendor/rack/protection/remote_referrer.rb +22 -0
- data/vendor/rack/protection/remote_token.rb +24 -0
- data/vendor/rack/protection/session_hijacking.rb +37 -0
- data/vendor/rack/protection/strict_transport.rb +41 -0
- data/vendor/rack/protection/version.rb +7 -0
- data/vendor/rack/protection/xss_header.rb +27 -0
- data/vendor/rack/protection.rb +58 -0
- data/vendor/rack/query_parser.rb +261 -0
- data/vendor/rack/recursive.rb +66 -0
- data/vendor/rack/reloader.rb +112 -0
- data/vendor/rack/request.rb +818 -0
- data/vendor/rack/response.rb +403 -0
- data/vendor/rack/rewindable_input.rb +116 -0
- data/vendor/rack/runtime.rb +35 -0
- data/vendor/rack/sendfile.rb +197 -0
- data/vendor/rack/session/abstract/id.rb +533 -0
- data/vendor/rack/session/constants.rb +13 -0
- data/vendor/rack/session/cookie.rb +292 -0
- data/vendor/rack/session/encryptor.rb +415 -0
- data/vendor/rack/session/pool.rb +76 -0
- data/vendor/rack/session/version.rb +10 -0
- data/vendor/rack/session.rb +12 -0
- data/vendor/rack/show_exceptions.rb +433 -0
- data/vendor/rack/show_status.rb +121 -0
- data/vendor/rack/static.rb +188 -0
- data/vendor/rack/tempfile_reaper.rb +44 -0
- data/vendor/rack/urlmap.rb +99 -0
- data/vendor/rack/utils.rb +631 -0
- data/vendor/rack/version.rb +17 -0
- data/vendor/rack.rb +66 -0
- metadata +76 -1
|
@@ -0,0 +1,586 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'strscan'
|
|
4
|
+
|
|
5
|
+
require_relative '../utils'
|
|
6
|
+
require_relative '../bad_request'
|
|
7
|
+
|
|
8
|
+
module Rack
|
|
9
|
+
module Multipart
|
|
10
|
+
class MultipartPartLimitError < Errno::EMFILE
|
|
11
|
+
include BadRequest
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
class MultipartTotalPartLimitError < StandardError
|
|
15
|
+
include BadRequest
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Use specific error class when parsing multipart request
|
|
19
|
+
# that ends early.
|
|
20
|
+
class EmptyContentError < ::EOFError
|
|
21
|
+
include BadRequest
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Base class for multipart exceptions that do not subclass from
|
|
25
|
+
# other exception classes for backwards compatibility.
|
|
26
|
+
class BoundaryTooLongError < StandardError
|
|
27
|
+
include BadRequest
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Prefer to use the BoundaryTooLongError class or Rack::BadRequest.
|
|
31
|
+
Error = BoundaryTooLongError
|
|
32
|
+
|
|
33
|
+
EOL = "\r\n"
|
|
34
|
+
FWS = /[ \t]+(?:\r\n[ \t]+)?/ # whitespace with optional folding
|
|
35
|
+
HEADER_VALUE = "(?:[^\r\n]|\r\n[ \t])*" # anything but a non-folding CRLF
|
|
36
|
+
MULTIPART = %r|\Amultipart/.*boundary=\"?([^\";,]+)\"?|ni
|
|
37
|
+
MULTIPART_CONTENT_TYPE = /^Content-Type:#{FWS}?(#{HEADER_VALUE})/ni
|
|
38
|
+
MULTIPART_CONTENT_DISPOSITION = /^Content-Disposition:#{FWS}?(#{HEADER_VALUE})/ni
|
|
39
|
+
MULTIPART_CONTENT_ID = /^Content-ID:#{FWS}?(#{HEADER_VALUE})/ni
|
|
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
|
|
53
|
+
class Parser
|
|
54
|
+
BUFSIZE = 1_048_576
|
|
55
|
+
TEXT_PLAIN = "text/plain"
|
|
56
|
+
TEMPFILE_FACTORY = lambda { |filename, content_type|
|
|
57
|
+
extension = ::File.extname(filename.gsub("\0", '%00'))[0, 129]
|
|
58
|
+
|
|
59
|
+
Tempfile.new(["RackMultipart", extension])
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
BOUNDARY_START_LIMIT = 16 * 1024
|
|
63
|
+
private_constant :BOUNDARY_START_LIMIT
|
|
64
|
+
|
|
65
|
+
MIME_HEADER_BYTESIZE_LIMIT = 64 * 1024
|
|
66
|
+
private_constant :MIME_HEADER_BYTESIZE_LIMIT
|
|
67
|
+
|
|
68
|
+
env_int = lambda do |key, val|
|
|
69
|
+
if str_val = ENV[key]
|
|
70
|
+
begin
|
|
71
|
+
val = Integer(str_val, 10)
|
|
72
|
+
rescue ArgumentError
|
|
73
|
+
raise ArgumentError, "non-integer value provided for environment variable #{key}"
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
val
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
BUFFERED_UPLOAD_BYTESIZE_LIMIT = env_int.call("RACK_MULTIPART_BUFFERED_UPLOAD_BYTESIZE_LIMIT", 16 * 1024 * 1024)
|
|
81
|
+
private_constant :BUFFERED_UPLOAD_BYTESIZE_LIMIT
|
|
82
|
+
|
|
83
|
+
class BoundedIO # :nodoc:
|
|
84
|
+
def initialize(io, content_length)
|
|
85
|
+
@io = io
|
|
86
|
+
@content_length = content_length
|
|
87
|
+
@cursor = 0
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def read(size, outbuf = nil)
|
|
91
|
+
return if @cursor >= @content_length
|
|
92
|
+
|
|
93
|
+
left = @content_length - @cursor
|
|
94
|
+
|
|
95
|
+
str = if left < size
|
|
96
|
+
@io.read left, outbuf
|
|
97
|
+
else
|
|
98
|
+
@io.read size, outbuf
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
if str
|
|
102
|
+
@cursor += str.bytesize
|
|
103
|
+
else
|
|
104
|
+
# Raise an error for mismatching content-length and actual contents
|
|
105
|
+
raise EOFError, "bad content body"
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
str
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
MultipartInfo = Struct.new :params, :tmp_files
|
|
113
|
+
EMPTY = MultipartInfo.new(nil, [])
|
|
114
|
+
|
|
115
|
+
def self.parse_boundary(content_type)
|
|
116
|
+
return unless content_type
|
|
117
|
+
data = content_type.match(MULTIPART)
|
|
118
|
+
return unless data
|
|
119
|
+
data[1]
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def self.parse(io, content_length, content_type, tmpfile, bufsize, qp)
|
|
123
|
+
return EMPTY if 0 == content_length
|
|
124
|
+
|
|
125
|
+
boundary = parse_boundary content_type
|
|
126
|
+
return EMPTY unless boundary
|
|
127
|
+
|
|
128
|
+
if boundary.length > 70
|
|
129
|
+
# RFC 1521 Section 7.2.1 imposes a 70 character maximum for the boundary.
|
|
130
|
+
# Most clients use no more than 55 characters.
|
|
131
|
+
raise BoundaryTooLongError, "multipart boundary size too large (#{boundary.length} characters)"
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
io = BoundedIO.new(io, content_length) if content_length
|
|
135
|
+
|
|
136
|
+
parser = new(boundary, tmpfile, bufsize, qp)
|
|
137
|
+
parser.parse(io)
|
|
138
|
+
|
|
139
|
+
parser.result
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
class Collector
|
|
143
|
+
class MimePart < Struct.new(:body, :head, :filename, :content_type, :name)
|
|
144
|
+
def get_data
|
|
145
|
+
data = body
|
|
146
|
+
if filename == ""
|
|
147
|
+
# filename is blank which means no file has been selected
|
|
148
|
+
return
|
|
149
|
+
elsif filename
|
|
150
|
+
body.rewind if body.respond_to?(:rewind)
|
|
151
|
+
|
|
152
|
+
# Take the basename of the upload's original filename.
|
|
153
|
+
# This handles the full Windows paths given by Internet Explorer
|
|
154
|
+
# (and perhaps other broken user agents) without affecting
|
|
155
|
+
# those which give the lone filename.
|
|
156
|
+
fn = filename.split(/[\/\\]/).last
|
|
157
|
+
|
|
158
|
+
data = { filename: fn, type: content_type,
|
|
159
|
+
name: name, tempfile: body, head: head }
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
yield data
|
|
163
|
+
end
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
class BufferPart < MimePart
|
|
167
|
+
def file?; false; end
|
|
168
|
+
def close; end
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
class TempfilePart < MimePart
|
|
172
|
+
def file?; true; end
|
|
173
|
+
def close; body.close; end
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
include Enumerable
|
|
177
|
+
|
|
178
|
+
def initialize(tempfile)
|
|
179
|
+
@tempfile = tempfile
|
|
180
|
+
@mime_parts = []
|
|
181
|
+
@open_files = 0
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
def each
|
|
185
|
+
@mime_parts.each { |part| yield part }
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
def on_mime_head(mime_index, head, filename, content_type, name)
|
|
189
|
+
check_total_part_limit
|
|
190
|
+
|
|
191
|
+
if filename
|
|
192
|
+
# This will raise an exception if we are at the limit:
|
|
193
|
+
check_file_part_limit
|
|
194
|
+
|
|
195
|
+
body = @tempfile.call(filename, content_type)
|
|
196
|
+
body.binmode if body.respond_to?(:binmode)
|
|
197
|
+
klass = TempfilePart
|
|
198
|
+
@open_files += 1
|
|
199
|
+
else
|
|
200
|
+
body = String.new
|
|
201
|
+
klass = BufferPart
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
@mime_parts[mime_index] = klass.new(body, head, filename, content_type, name)
|
|
205
|
+
end
|
|
206
|
+
|
|
207
|
+
def on_mime_body(mime_index, content)
|
|
208
|
+
@mime_parts[mime_index].body << content
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
def on_mime_finish(mime_index)
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
private
|
|
215
|
+
|
|
216
|
+
def check_file_part_limit
|
|
217
|
+
file_limit = Utils.multipart_file_limit
|
|
218
|
+
|
|
219
|
+
if file_limit && file_limit > 0
|
|
220
|
+
if (@open_files + 1) >= file_limit
|
|
221
|
+
@mime_parts.each(&:close)
|
|
222
|
+
raise MultipartPartLimitError, 'Maximum file multiparts in content reached'
|
|
223
|
+
end
|
|
224
|
+
end
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
def check_total_part_limit
|
|
228
|
+
part_limit = Utils.multipart_total_part_limit
|
|
229
|
+
|
|
230
|
+
if part_limit && part_limit > 0
|
|
231
|
+
if (@mime_parts.size + 1) >= part_limit
|
|
232
|
+
@mime_parts.each(&:close)
|
|
233
|
+
raise MultipartTotalPartLimitError, 'Maximum total multiparts in content reached'
|
|
234
|
+
end
|
|
235
|
+
end
|
|
236
|
+
end
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
attr_reader :state
|
|
240
|
+
|
|
241
|
+
def initialize(boundary, tempfile, bufsize, query_parser)
|
|
242
|
+
@query_parser = query_parser
|
|
243
|
+
@params = query_parser.make_params
|
|
244
|
+
@bufsize = bufsize
|
|
245
|
+
|
|
246
|
+
@state = :FAST_FORWARD
|
|
247
|
+
@mime_index = 0
|
|
248
|
+
@body_retained = nil
|
|
249
|
+
@retained_size = 0
|
|
250
|
+
@collector = Collector.new tempfile
|
|
251
|
+
|
|
252
|
+
@sbuf = StringScanner.new("".dup)
|
|
253
|
+
@body_regex = /(?:#{EOL}|\A)--#{Regexp.quote(boundary)}(?:#{EOL}|--)/m
|
|
254
|
+
@body_regex_at_end = /#{@body_regex}\z/m
|
|
255
|
+
@end_boundary_size = boundary.bytesize + 4 # (-- at start, -- at finish)
|
|
256
|
+
@rx_max_size = boundary.bytesize + 6 # (\r\n-- at start, either \r\n or -- at finish)
|
|
257
|
+
@head_regex = /(.*?#{EOL})#{EOL}/m
|
|
258
|
+
end
|
|
259
|
+
|
|
260
|
+
def parse(io)
|
|
261
|
+
outbuf = String.new
|
|
262
|
+
read_data(io, outbuf)
|
|
263
|
+
|
|
264
|
+
loop do
|
|
265
|
+
status =
|
|
266
|
+
case @state
|
|
267
|
+
when :FAST_FORWARD
|
|
268
|
+
handle_fast_forward
|
|
269
|
+
when :CONSUME_TOKEN
|
|
270
|
+
handle_consume_token
|
|
271
|
+
when :MIME_HEAD
|
|
272
|
+
handle_mime_head
|
|
273
|
+
when :MIME_BODY
|
|
274
|
+
handle_mime_body
|
|
275
|
+
else # when :DONE
|
|
276
|
+
return
|
|
277
|
+
end
|
|
278
|
+
|
|
279
|
+
read_data(io, outbuf) if status == :want_read
|
|
280
|
+
end
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
def result
|
|
284
|
+
@collector.each do |part|
|
|
285
|
+
part.get_data do |data|
|
|
286
|
+
tag_multipart_encoding(part.filename, part.content_type, part.name, data)
|
|
287
|
+
name, data = handle_dummy_encoding(part.name, data)
|
|
288
|
+
@query_parser.normalize_params(@params, name, data)
|
|
289
|
+
end
|
|
290
|
+
end
|
|
291
|
+
MultipartInfo.new @params.to_params_hash, @collector.find_all(&:file?).map(&:body)
|
|
292
|
+
end
|
|
293
|
+
|
|
294
|
+
private
|
|
295
|
+
|
|
296
|
+
def read_data(io, outbuf)
|
|
297
|
+
content = io.read(@bufsize, outbuf)
|
|
298
|
+
handle_empty_content!(content)
|
|
299
|
+
@sbuf.concat(content)
|
|
300
|
+
end
|
|
301
|
+
|
|
302
|
+
# This handles the initial parser state. We read until we find the starting
|
|
303
|
+
# boundary, then we can transition to the next state. If we find the ending
|
|
304
|
+
# boundary, this is an invalid multipart upload, but keep scanning for opening
|
|
305
|
+
# boundary in that case. If no boundary found, we need to keep reading data
|
|
306
|
+
# and retry. It's highly unlikely the initial read will not consume the
|
|
307
|
+
# boundary. The client would have to deliberately craft a response
|
|
308
|
+
# with the opening boundary beyond the buffer size for that to happen.
|
|
309
|
+
def handle_fast_forward
|
|
310
|
+
while true
|
|
311
|
+
case consume_boundary
|
|
312
|
+
when :BOUNDARY
|
|
313
|
+
# found opening boundary, transition to next state
|
|
314
|
+
@state = :MIME_HEAD
|
|
315
|
+
return
|
|
316
|
+
when :END_BOUNDARY
|
|
317
|
+
# invalid multipart upload
|
|
318
|
+
if @sbuf.pos == @end_boundary_size && @sbuf.rest == EOL
|
|
319
|
+
# stop parsing a buffer if a buffer is only an end boundary.
|
|
320
|
+
@state = :DONE
|
|
321
|
+
return
|
|
322
|
+
end
|
|
323
|
+
|
|
324
|
+
# retry for opening boundary
|
|
325
|
+
else
|
|
326
|
+
# We raise if we don't find the multipart boundary, to avoid unbounded memory
|
|
327
|
+
# buffering. Note that the actual limit is the higher of 16KB and the buffer size (1MB by default)
|
|
328
|
+
raise Error, "multipart boundary not found within limit" if @sbuf.string.bytesize > BOUNDARY_START_LIMIT
|
|
329
|
+
|
|
330
|
+
# no boundary found, keep reading data
|
|
331
|
+
return :want_read
|
|
332
|
+
end
|
|
333
|
+
end
|
|
334
|
+
end
|
|
335
|
+
|
|
336
|
+
def handle_consume_token
|
|
337
|
+
tok = consume_boundary
|
|
338
|
+
# break if we're at the end of a buffer, but not if it is the end of a field
|
|
339
|
+
@state = if tok == :END_BOUNDARY || (@sbuf.eos? && tok != :BOUNDARY)
|
|
340
|
+
:DONE
|
|
341
|
+
else
|
|
342
|
+
:MIME_HEAD
|
|
343
|
+
end
|
|
344
|
+
end
|
|
345
|
+
|
|
346
|
+
CONTENT_DISPOSITION_MAX_PARAMS = 16
|
|
347
|
+
CONTENT_DISPOSITION_MAX_BYTES = 1536
|
|
348
|
+
def handle_mime_head
|
|
349
|
+
if @sbuf.scan_until(@head_regex)
|
|
350
|
+
head = @sbuf[1]
|
|
351
|
+
content_type = head[MULTIPART_CONTENT_TYPE, 1]
|
|
352
|
+
if (disposition = head[MULTIPART_CONTENT_DISPOSITION, 1]) &&
|
|
353
|
+
disposition.bytesize <= CONTENT_DISPOSITION_MAX_BYTES
|
|
354
|
+
|
|
355
|
+
# ignore actual content-disposition value (should always be form-data)
|
|
356
|
+
i = disposition.index(';')
|
|
357
|
+
disposition.slice!(0, i+1)
|
|
358
|
+
param = nil
|
|
359
|
+
num_params = 0
|
|
360
|
+
|
|
361
|
+
# Parse parameter list
|
|
362
|
+
while i = disposition.index('=')
|
|
363
|
+
# Only parse up to max parameters, to avoid potential denial of service
|
|
364
|
+
num_params += 1
|
|
365
|
+
break if num_params > CONTENT_DISPOSITION_MAX_PARAMS
|
|
366
|
+
|
|
367
|
+
# Found end of parameter name, ensure forward progress in loop
|
|
368
|
+
param = disposition.slice!(0, i+1)
|
|
369
|
+
|
|
370
|
+
# Remove ending equals and preceding whitespace from parameter name
|
|
371
|
+
param.chomp!('=')
|
|
372
|
+
param.lstrip!
|
|
373
|
+
|
|
374
|
+
if disposition[0] == '"'
|
|
375
|
+
# Parameter value is quoted, parse it, handling backslash escapes
|
|
376
|
+
disposition.slice!(0, 1)
|
|
377
|
+
value = String.new
|
|
378
|
+
|
|
379
|
+
while i = disposition.index(/(["\\])/)
|
|
380
|
+
c = $1
|
|
381
|
+
|
|
382
|
+
# Append all content until ending quote or escape
|
|
383
|
+
value << disposition.slice!(0, i)
|
|
384
|
+
|
|
385
|
+
# Remove either backslash or ending quote,
|
|
386
|
+
# ensures forward progress in loop
|
|
387
|
+
disposition.slice!(0, 1)
|
|
388
|
+
|
|
389
|
+
# stop parsing parameter value if found ending quote
|
|
390
|
+
break if c == '"'
|
|
391
|
+
|
|
392
|
+
escaped_char = disposition.slice!(0, 1)
|
|
393
|
+
if param == 'filename' && escaped_char != '"'
|
|
394
|
+
# Possible IE uploaded filename, append both escape backslash and value
|
|
395
|
+
value << c << escaped_char
|
|
396
|
+
else
|
|
397
|
+
# Other only append escaped value
|
|
398
|
+
value << escaped_char
|
|
399
|
+
end
|
|
400
|
+
end
|
|
401
|
+
else
|
|
402
|
+
if i = disposition.index(';')
|
|
403
|
+
# Parameter value unquoted (which may be invalid), value ends at semicolon
|
|
404
|
+
value = disposition.slice!(0, i)
|
|
405
|
+
else
|
|
406
|
+
# If no ending semicolon, assume remainder of line is value and stop
|
|
407
|
+
# parsing
|
|
408
|
+
disposition.strip!
|
|
409
|
+
value = disposition
|
|
410
|
+
disposition = ''
|
|
411
|
+
end
|
|
412
|
+
end
|
|
413
|
+
|
|
414
|
+
case param
|
|
415
|
+
when 'name'
|
|
416
|
+
name = value
|
|
417
|
+
when 'filename'
|
|
418
|
+
filename = value
|
|
419
|
+
when 'filename*'
|
|
420
|
+
filename_star = value
|
|
421
|
+
# else
|
|
422
|
+
# ignore other parameters
|
|
423
|
+
end
|
|
424
|
+
|
|
425
|
+
# skip trailing semicolon, to proceed to next parameter
|
|
426
|
+
if i = disposition.index(';')
|
|
427
|
+
disposition.slice!(0, i+1)
|
|
428
|
+
end
|
|
429
|
+
end
|
|
430
|
+
else
|
|
431
|
+
name = head[MULTIPART_CONTENT_ID, 1]
|
|
432
|
+
end
|
|
433
|
+
|
|
434
|
+
if filename_star
|
|
435
|
+
encoding, _, filename = filename_star.split("'", 3)
|
|
436
|
+
filename = normalize_filename(filename || '')
|
|
437
|
+
filename.force_encoding(find_encoding(encoding))
|
|
438
|
+
elsif filename
|
|
439
|
+
filename = normalize_filename(filename)
|
|
440
|
+
end
|
|
441
|
+
|
|
442
|
+
if name.nil? || name.empty?
|
|
443
|
+
name = filename || "#{content_type || TEXT_PLAIN}[]".dup
|
|
444
|
+
end
|
|
445
|
+
|
|
446
|
+
# Mime part head data is retained for both TempfilePart and BufferPart
|
|
447
|
+
# for the entireity of the parse, even though it isn't used for BufferPart.
|
|
448
|
+
update_retained_size(head.bytesize)
|
|
449
|
+
|
|
450
|
+
# If a filename is given, a TempfilePart will be used, so the body will
|
|
451
|
+
# not be buffered in memory. However, if a filename is not given, a BufferPart
|
|
452
|
+
# will be used, and the body will be buffered in memory.
|
|
453
|
+
@body_retained = !filename
|
|
454
|
+
|
|
455
|
+
@collector.on_mime_head @mime_index, head, filename, content_type, name
|
|
456
|
+
@state = :MIME_BODY
|
|
457
|
+
else
|
|
458
|
+
# We raise if the mime part header is too large, to avoid unbounded memory
|
|
459
|
+
# buffering. Note that the actual limit is the higher of 64KB and the buffer size (1MB by default)
|
|
460
|
+
raise Error, "multipart mime part header too large" if @sbuf.string.bytesize > MIME_HEADER_BYTESIZE_LIMIT
|
|
461
|
+
|
|
462
|
+
return :want_read
|
|
463
|
+
end
|
|
464
|
+
end
|
|
465
|
+
|
|
466
|
+
def handle_mime_body
|
|
467
|
+
if (body_with_boundary = @sbuf.check_until(@body_regex)) # check but do not advance the pointer yet
|
|
468
|
+
body = body_with_boundary.sub(@body_regex_at_end, '') # remove the boundary from the string
|
|
469
|
+
update_retained_size(body.bytesize) if @body_retained
|
|
470
|
+
@collector.on_mime_body @mime_index, body
|
|
471
|
+
@sbuf.pos += body.length + 2 # skip \r\n after the content
|
|
472
|
+
@state = :CONSUME_TOKEN
|
|
473
|
+
@mime_index += 1
|
|
474
|
+
else
|
|
475
|
+
# Save what we have so far
|
|
476
|
+
if @rx_max_size < @sbuf.rest_size
|
|
477
|
+
delta = @sbuf.rest_size - @rx_max_size
|
|
478
|
+
body = @sbuf.peek(delta)
|
|
479
|
+
update_retained_size(body.bytesize) if @body_retained
|
|
480
|
+
@collector.on_mime_body @mime_index, body
|
|
481
|
+
@sbuf.pos += delta
|
|
482
|
+
@sbuf.string = @sbuf.rest
|
|
483
|
+
end
|
|
484
|
+
:want_read
|
|
485
|
+
end
|
|
486
|
+
end
|
|
487
|
+
|
|
488
|
+
def update_retained_size(size)
|
|
489
|
+
@retained_size += size
|
|
490
|
+
if @retained_size > BUFFERED_UPLOAD_BYTESIZE_LIMIT
|
|
491
|
+
raise Error, "multipart data over retained size limit"
|
|
492
|
+
end
|
|
493
|
+
end
|
|
494
|
+
|
|
495
|
+
# Scan until the we find the start or end of the boundary.
|
|
496
|
+
# If we find it, return the appropriate symbol for the start or
|
|
497
|
+
# end of the boundary. If we don't find the start or end of the
|
|
498
|
+
# boundary, clear the buffer and return nil.
|
|
499
|
+
def consume_boundary
|
|
500
|
+
if read_buffer = @sbuf.scan_until(@body_regex)
|
|
501
|
+
read_buffer.end_with?(EOL) ? :BOUNDARY : :END_BOUNDARY
|
|
502
|
+
else
|
|
503
|
+
@sbuf.terminate
|
|
504
|
+
nil
|
|
505
|
+
end
|
|
506
|
+
end
|
|
507
|
+
|
|
508
|
+
def normalize_filename(filename)
|
|
509
|
+
if filename.scan(/%.?.?/).all? { |s| /%[0-9a-fA-F]{2}/.match?(s) }
|
|
510
|
+
filename = Utils.unescape_path(filename)
|
|
511
|
+
end
|
|
512
|
+
|
|
513
|
+
filename.scrub!
|
|
514
|
+
|
|
515
|
+
filename.split(/[\/\\]/).last || String.new
|
|
516
|
+
end
|
|
517
|
+
|
|
518
|
+
CHARSET = "charset"
|
|
519
|
+
deprecate_constant :CHARSET
|
|
520
|
+
|
|
521
|
+
def tag_multipart_encoding(filename, content_type, name, body)
|
|
522
|
+
name = name.to_s
|
|
523
|
+
encoding = Encoding::UTF_8
|
|
524
|
+
|
|
525
|
+
name.force_encoding(encoding)
|
|
526
|
+
|
|
527
|
+
return if filename
|
|
528
|
+
|
|
529
|
+
if content_type
|
|
530
|
+
list = content_type.split(';')
|
|
531
|
+
type_subtype = list.first
|
|
532
|
+
type_subtype.strip!
|
|
533
|
+
if TEXT_PLAIN == type_subtype
|
|
534
|
+
rest = list.drop 1
|
|
535
|
+
rest.each do |param|
|
|
536
|
+
k, v = param.split('=', 2)
|
|
537
|
+
k.strip!
|
|
538
|
+
v.strip!
|
|
539
|
+
v = v[1..-2] if v.start_with?('"') && v.end_with?('"')
|
|
540
|
+
if k == "charset"
|
|
541
|
+
encoding = find_encoding(v)
|
|
542
|
+
end
|
|
543
|
+
end
|
|
544
|
+
end
|
|
545
|
+
end
|
|
546
|
+
|
|
547
|
+
name.force_encoding(encoding)
|
|
548
|
+
body.force_encoding(encoding)
|
|
549
|
+
end
|
|
550
|
+
|
|
551
|
+
# Return the related Encoding object. However, because
|
|
552
|
+
# enc is submitted by the user, it may be invalid, so
|
|
553
|
+
# use a binary encoding in that case.
|
|
554
|
+
def find_encoding(enc)
|
|
555
|
+
Encoding.find enc
|
|
556
|
+
rescue ArgumentError
|
|
557
|
+
Encoding::BINARY
|
|
558
|
+
end
|
|
559
|
+
|
|
560
|
+
REENCODE_DUMMY_ENCODINGS = {
|
|
561
|
+
# ISO-2022-JP is a legacy but still widely used encoding in Japan
|
|
562
|
+
# Here we convert ISO-2022-JP to UTF-8 so that it can be handled.
|
|
563
|
+
Encoding::ISO_2022_JP => true
|
|
564
|
+
|
|
565
|
+
# Other dummy encodings are rarely used and have not been supported yet.
|
|
566
|
+
# Adding support for them will require careful considerations.
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
def handle_dummy_encoding(name, body)
|
|
570
|
+
# A string object with a 'dummy' encoding does not have full functionality and can cause errors.
|
|
571
|
+
# So here we covert it to UTF-8 so that it can be handled properly.
|
|
572
|
+
if name.encoding.dummy? && REENCODE_DUMMY_ENCODINGS[name.encoding]
|
|
573
|
+
name = name.encode(Encoding::UTF_8)
|
|
574
|
+
body = body.encode(Encoding::UTF_8)
|
|
575
|
+
end
|
|
576
|
+
return name, body
|
|
577
|
+
end
|
|
578
|
+
|
|
579
|
+
def handle_empty_content!(content)
|
|
580
|
+
if content.nil? || content.empty?
|
|
581
|
+
raise EmptyContentError
|
|
582
|
+
end
|
|
583
|
+
end
|
|
584
|
+
end
|
|
585
|
+
end
|
|
586
|
+
end
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'tempfile'
|
|
4
|
+
require 'fileutils'
|
|
5
|
+
|
|
6
|
+
module Rack
|
|
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.
|
|
19
|
+
class UploadedFile
|
|
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.
|
|
23
|
+
attr_reader :original_filename
|
|
24
|
+
|
|
25
|
+
# The content type of the instance.
|
|
26
|
+
attr_accessor :content_type
|
|
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.
|
|
49
|
+
def initialize(filepath = nil, ct = "text/plain", bin = false,
|
|
50
|
+
path: filepath, content_type: ct, binary: bin, filename: nil, io: nil)
|
|
51
|
+
if io
|
|
52
|
+
@tempfile = io
|
|
53
|
+
@original_filename = filename
|
|
54
|
+
else
|
|
55
|
+
raise "#{path} file does not exist" unless ::File.exist?(path)
|
|
56
|
+
@original_filename = filename || ::File.basename(path)
|
|
57
|
+
@tempfile = Tempfile.new([@original_filename, ::File.extname(path)], encoding: Encoding::BINARY)
|
|
58
|
+
@tempfile.binmode if binary
|
|
59
|
+
FileUtils.copy_file(path, @tempfile.path)
|
|
60
|
+
end
|
|
61
|
+
@content_type = content_type
|
|
62
|
+
end
|
|
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.
|
|
66
|
+
def path
|
|
67
|
+
@tempfile.path if @tempfile.respond_to?(:path)
|
|
68
|
+
end
|
|
69
|
+
alias_method :local_path, :path
|
|
70
|
+
|
|
71
|
+
# Return true if the tempfile responds to the method.
|
|
72
|
+
def respond_to_missing?(*args)
|
|
73
|
+
@tempfile.respond_to?(*args)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# Delegate method missing calls to the tempfile.
|
|
77
|
+
def method_missing(method_name, *args, &block) #:nodoc:
|
|
78
|
+
@tempfile.__send__(method_name, *args, &block)
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|