Skip to content

Commit

Permalink
Handle question mark in encoded text (Backports mikel#1452)
Browse files Browse the repository at this point in the history
  • Loading branch information
lewispb authored and jeremy committed Oct 20, 2021
1 parent 51c05e0 commit 873aa38
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Bugs:
* Fix duplicate dots at beginning of lines. (c960657)
* Don't alter attachment filenames when Content-Type parsing fails. (nbianca)
* Fix decoding fields ending in question marks. (jeremy)
* Fix decoding quoted-printable fields containing question marks. (lewispb)


== Version 2.7.1 (2018-10-13)
Expand Down
24 changes: 22 additions & 2 deletions lib/mail/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,28 @@ module Constants
ATOM_UNSAFE = /[#{Regexp.quote aspecial}#{control}#{sp}]/n
PHRASE_UNSAFE = /[#{Regexp.quote aspecial}#{control}]/n
TOKEN_UNSAFE = /[#{Regexp.quote tspecial}#{control}#{sp}]/n
ENCODED_VALUE = /\=\?([^?]+)\?([QB])\?[^?]*?\?+\=/mi
FULL_ENCODED_VALUE = /(\=\?[^?]+\?[QB]\?[^?]*?\?+\=)/mi

ENCODED_VALUE = %r{
\=\? # literal =?
([^?]+) #
\? # literal ?
([QB]) # either a "Q" or a "B"
\? # literal ?
.*? # lazily match all characters
\?\= # literal ?=
}mix # m is multi-line, i is case-insensitive, x is free-spacing

FULL_ENCODED_VALUE = %r{ # Identical to ENCODED_VALUE but captures the whole rather than components of
(
\=\? # literal =?
[^?]+ #
\? # literal ?
[QB] # either a "Q" or a "B"
\? # literal ?
.*? # lazily match all characters
\?\= # literal ?=
)
}mix # m is multi-line, i is case-insensitive, x is free-spacing

EMPTY = ''
SPACE = ' '
Expand Down
10 changes: 7 additions & 3 deletions spec/mail/encodings_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,10 @@
expect(Mail::Encodings.value_decode(encoded)).to eq expected
end

it "should handle a question mark in the text" do
encoded = "=?iso-8859-1?Q?Hello World?_-_How are you??="
expect(Mail::Encodings.value_decode(encoded)).to eq "Hello World? - How are you?"
end
end

describe "pre encoding non usascii text" do
Expand Down Expand Up @@ -939,7 +943,7 @@ def convert(from, to)
it "splits adjacent encodings into separate parts" do
convert "A=?iso-2022-jp?B?X=?==?iso-2022-jp?B?Y=?=B", ["A", "=?iso-2022-jp?B?X=?=", "=?iso-2022-jp?B?Y=?=", "B"]
end

it "splits adjacent encodings without unencoded into separate parts" do
convert "=?iso-2022-jp?B?X=?==?iso-2022-jp?B?Y=?=", ["=?iso-2022-jp?B?X=?=", "=?iso-2022-jp?B?Y=?="]
end
Expand All @@ -951,10 +955,10 @@ def convert(from, to)
it "does not join different encodings" do
convert "A=?iso-2022-jp?B?X=?==?utf-8?B?Y=?=B", ["A", "=?iso-2022-jp?B?X=?=", "=?utf-8?B?Y=?=", "B"]
end

it "does not keep the separator character between two different encodings" do
rfc_1342_newline_separators = ["\x0A", "\x20"]

rfc_1342_newline_separators.each do |rfc_1342_separator|
convert "=?iso-2022-jp?B?X=?=#{rfc_1342_separator}=?utf-8?Q?Y=?=", ["=?iso-2022-jp?B?X=?=", "=?utf-8?Q?Y=?="]
end
Expand Down

0 comments on commit 873aa38

Please sign in to comment.