From 4ead482e5044bb3003bff6911b9dbe82df92d07c Mon Sep 17 00:00:00 2001 From: Christoph Wagner Date: Tue, 23 Oct 2018 13:58:47 +0200 Subject: [PATCH] Fix multipart parser for large files #1308 --- lib/rack/multipart/parser.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/rack/multipart/parser.rb b/lib/rack/multipart/parser.rb index da49c01b3..e24f864ca 100644 --- a/lib/rack/multipart/parser.rb +++ b/lib/rack/multipart/parser.rb @@ -182,7 +182,7 @@ def initialize(boundary, tempfile, bufsize, query_parser) @collector = Collector.new tempfile @sbuf = StringScanner.new("".dup) - @body_regex = /(.*?)(#{EOL})?#{Regexp.quote(@boundary)}(#{EOL}|--)/m + @body_regex = /(?:#{EOL})?#{Regexp.quote(@boundary)}(?:#{EOL}|--)/m @rx_max_size = EOL.size + @boundary.bytesize + [EOL.size, '--'.size].max @head_regex = /(.*?#{EOL})#{EOL}/m end @@ -265,8 +265,8 @@ def handle_mime_head end def handle_mime_body - if @sbuf.check_until(@body_regex) # check but do not advance the pointer yet - body = @sbuf[1] + if (body_with_boundary = @sbuf.check_until(@body_regex)) # check but do not advance the pointer yet + body = body_with_boundary.sub(/#{@body_regex}\z/m, '') # remove the boundary from the string @collector.on_mime_body @mime_index, body @sbuf.pos += body.length + 2 # skip \r\n after the content @state = :CONSUME_TOKEN