Skip to content

Commit

Permalink
Handle invalid Content-Disposition filename encodings
Browse files Browse the repository at this point in the history
Use BINARY for this, as we do for multipart encodings.  Extract a
find_encoding method for this.
  • Loading branch information
jeremyevans committed Apr 28, 2023
1 parent 51b0c26 commit e3a9d3a
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions lib/rack/multipart/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ def handle_mime_head
if filename_star
encoding, _, filename = filename_star.split("'", 3)
filename = normalize_filename(filename || '')
filename.force_encoding(::Encoding.find(encoding))
filename.force_encoding(find_encoding(encoding))
elsif filename
filename = $1 if filename =~ /^"(.*)"$/
filename = normalize_filename(filename)
Expand Down Expand Up @@ -457,11 +457,7 @@ def tag_multipart_encoding(filename, content_type, name, body)
v.strip!
v = v[1..-2] if v.start_with?('"') && v.end_with?('"')
if k == "charset"
encoding = begin
Encoding.find v
rescue ArgumentError
Encoding::BINARY
end
encoding = find_encoding(v)
end
end
end
Expand All @@ -471,6 +467,15 @@ def tag_multipart_encoding(filename, content_type, name, body)
body.force_encoding(encoding)
end

# Return the related Encoding object. However, because
# enc is submitted by the user, it may be invalid, so
# use a binary encoding in that case.
def find_encoding(enc)
Encoding.find enc
rescue ArgumentError
Encoding::BINARY
end

def handle_empty_content!(content)
if content.nil? || content.empty?
raise EmptyContentError
Expand Down

0 comments on commit e3a9d3a

Please sign in to comment.