Skip to content

Commit

Permalink
Raise Rack::Multipart::EmptyContentError instead of EOFError for pars…
Browse files Browse the repository at this point in the history
…ing empty body

This makes it simpler to catch this specific error.

Fixes rack#1603
  • Loading branch information
jeremyevans committed Sep 6, 2020
1 parent 79f9493 commit 3561361
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -18,6 +18,7 @@ All notable changes to this project will be documented in this file. For info on
- `Rack::HTTP_VERSION` has been removed and the `HTTP_VERSION` env setting is no longer set in the CGI and Webrick handlers . ([#970](https://github.com/rack/rack/issues/970), [@jeremyevans](https://github.com/jeremyevans))
- `Rack::Request#[]` and `#[]=` now warn even in non-verbose mode. ([#1277](https://github.com/rack/rack/issues/1277), [@jeremyevans](https://github.com/jeremyevans))
- Decrease default allowed parameter recursion level from 100 to 32. ([#1640](https://github.com/rack/rack/issues/1640), [@jeremyevans](https://github.com/jeremyevans))
- Attempting to parse a multipart response with an empty body now raises Rack::Multipart::EmptyContentError. ([#1603](https://github.com/rack/rack/issues/1603), [@jeremyevans](https://github.com/jeremyevans))

### Fixed

Expand Down
6 changes: 5 additions & 1 deletion lib/rack/multipart/parser.rb
Expand Up @@ -6,6 +6,10 @@ module Rack
module Multipart
class MultipartPartLimitError < Errno::EMFILE; end

# Use specific error class when parsing multipart request
# that ends early.
class EmptyContentError < ::EOFError; end

class Parser
(require_relative '../core_ext/regexp'; using ::Rack::RegexpExtensions) if RUBY_VERSION < '2.4'

Expand Down Expand Up @@ -356,7 +360,7 @@ def tag_multipart_encoding(filename, content_type, name, body)

def handle_empty_content!(content)
if content.nil? || content.empty?
raise EOFError
raise EmptyContentError
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/spec_multipart.rb
Expand Up @@ -151,7 +151,7 @@ def rd.rewind; end
env = Rack::MockRequest.env_for '/', fixture
lambda {
Rack::Multipart.parse_multipart(env)
}.must_raise EOFError
}.must_raise Rack::Multipart::EmptyContentError
rd.close

err = thr.value
Expand Down

0 comments on commit 3561361

Please sign in to comment.