Skip to content

Accept parameter with whitespace around equal # Content-Disposition #1592

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion lib/mail/fields/content_disposition_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ def initialize(value = nil, charset = nil)
end

def element
@element ||= Mail::ContentDispositionElement.new(value)
@element ||=
begin
Mail::ContentDispositionElement.new(value)
rescue Mail::Field::ParseError
# Sanitize the value, handle special cases
Mail::ContentDispositionElement.new(sanitize(value))
end
end

def disposition_type
Expand All @@ -40,5 +46,14 @@ def decoded
p = "; #{parameters.decoded}" if parameters.length > 0
"#{disposition_type}#{p}"
end

private

def sanitize(val)
val.
gsub(/\s*=\s*/,'='). # remove whitespaces around equal sign
gsub(/[; ]+/, '; '). # use '; ' as a separator (or EOL)
gsub(/;\s*$/,'') # remove trailing to keep examples below
end
end
end
16 changes: 8 additions & 8 deletions lib/mail/fields/content_type_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,6 @@ def element
end
end

def attempt_to_clean
# Sanitize the value, handle special cases
Mail::ContentTypeElement.new(sanitize(value))
rescue Mail::Field::ParseError
# All else fails, just get the MIME media type
Mail::ContentTypeElement.new(get_mime_type(value))
end

def main_type
@main_type ||= element.main_type
end
Expand Down Expand Up @@ -103,6 +95,14 @@ def decoded

private

def attempt_to_clean
# Sanitize the value, handle special cases
Mail::ContentTypeElement.new(sanitize(value))
rescue Mail::Field::ParseError
# All else fails, just get the MIME media type
Mail::ContentTypeElement.new(get_mime_type(value))
end

def method_missing(name, *args, &block)
if name.to_s =~ /(\w+)=/
self.parameters[$1] = args.first
Expand Down
6 changes: 6 additions & 0 deletions spec/mail/fields/content_disposition_field_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 +109,11 @@
c = Mail::ContentDispositionField.new(string)
expect(c.filename).to eq "Eelanalüüsi päring.jpg"
end

it "should accept with whitespace around equal sign" do
string = %q{attachment; name = mikel.jpg}
c = Mail::ContentDispositionField.new(string)
expect(c.filename).to eq 'mikel.jpg'
end
end
end
5 changes: 5 additions & 0 deletions spec/mail/fields/content_type_field_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,11 @@
expect(c.encoded).to eq result
end

it "should accept with whitespace around equal sign" do
string = %q{application/octet-stream; filename = mikel.jpg}
c = Mail::ContentTypeField.new(string)
expect(c.filename).to eq 'mikel.jpg'
end
end

describe "handling badly formated content-type fields" do
Expand Down