mikel / mikel/mail

Issues with handling quotation marks in attachment names

Open
#680 2 comments 0 reactions 0 assignees View on GitHub
Bug Header Fields More Work Needed
Dominant language
Ruby
Stars
3.7k
Forks
934
PR merge metrics
No merged PRs in 30d

Description

I came across to this issue in Ruby On Rails (https://github.com/rails/rails/pull/14158), and have traced it, and it seems that it might be actually a bug in `mail`-gem.

I have tried to fix it, but my dirty fix makes one other test to fail:

``` shell
1) Mail::UnstructuredField displaying encoded field and decoded value should encode additional special characters inside encoded-word-encoded strings
Failure/Error: @field.encoded.should eq result

expected: "Subject: =?UTF-8?Q?Her_er_=C3=A6=28=29<>@,;:\\=22/[]=3F.=3D?=\r\n"
got: "Subject: =?UTF-8?Q?Her_er_=C3=A6=28=29<>@,;:=22/[]=3F.=3D?=\r\n"

(compared using ==)

Diff:
@@ -1,2 +1,2 @@
-Subject: =?UTF-8?Q?Her_er_=C3=A6=28=29<>@,;:\=22/[]=3F.=3D?=
+Subject: =?UTF-8?Q?Her_er_=C3=A6=28=29<>@,;:=22/[]=3F.=3D?=
```

My "fix": https://github.com/jamox/mail/blob/fixing_fail_when_name_has_quotes/lib/mail/encodings.rb#L98-L110

---

Error occurs when in: https://github.com/mikel/mail/blob/master/lib/mail/attachments_list.rb#L40

``` ruby
def []=(name, value)
encoded_name = Mail::Encodings.decode_encode name, :encode
default_values = { :content_type => "#{set_mime_type(name)}; filename=\"#{encoded_name}\"",
:content_transfer_encoding => "#{guess_encoding}",
:content_disposition => "#{@content_disposition_type}; filename=\"#{encoded_name}\"" }
```

we have `name = 'invoice "for you".pdf'` and then we insert it into

``` ruby
default_values = { :content_type => "#{set_mime_type(name)}; filename=\"#{encoded_name}\"",
```

Thus getting

``` ruby
default_values = { :content_type => "#{set_mime_type(name)}; filename=\"invoice \"for you\".pdf\"",
```

And later we get the filename `\"invoice \"for you\".pdf\"` => `"invoice "for you".pdf"`...

Id be happy to fix this (in case this ain't due to misuse of this library) :)

Contributor guide

Open the contributing guide

Research direction

Start with lib/mail/attachments_list.rb around the []= method and lib/mail/encodings.rb around the referenced encoding logic; run the failing Mail::UnstructuredField test shown in the report. The work is done when attachment names containing quotation marks are handled correctly and the existing encoding test passes without the reported regression.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.