Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

quoted printable decode swallows character when having 2 quoted values #741

Open
grosser opened this issue Jul 21, 2014 · 3 comments
Open

Comments

@grosser
Copy link
Contributor

grosser commented Jul 21, 2014

>> Mail.new('Subject: "Subject:=?utf-8?B?0J/QvtC80L7Qs9C40YLQtSDQstC+0YHRgdGC0LDQvdC+0LLQ?= =?utf-8?B?uNGC0Ywg0L/QsNGA0L7Qu9GM?="')['Subject'].to_s
=> "\"Subject:Помогите восстановть пароль\"" # second word ends in ть
>> Mail.new('Subject: "Subject:=?utf-8?B?0J/QvtC80L7Qs9C40YLQtSDQstC+0YHRgdGC0LDQvdC+0LLQuNGC0Ywg0L/QsNGA0L7Qu9GM?="')['Subject'].to_s
=> "\"Subject:Помогите восстановить пароль\"" # second word ends in ить
@grosser
Copy link
Contributor Author

grosser commented Jul 21, 2014

fix:

Mail::Encoding.prepend(Module.new {
  def collapse_adjacent_encodings(str)
    super(str.gsub(/(\=\?[^?]+\?[QB]\?)([^?]+?)\?\=\s*(\1([^?]+?)\?\=\s*)+/) do |all|
      $1 + all.scan(/\=\?[^?]+\?[QB]\?([^?]+?)\?\=/).join("") + "?="
    end)
  end
})

... this worked for a few encodings, but not with this:

=?windows-1251?B?0uXx8g==?= =?windows-1251?B?IOHo6+Xy?= =?windows-1251?B?IPE=?= =?windows-1251?B?IO3o6uDq4ug=?= =?windows-1251?B?IO/u5+3g7ej/?= =?windows-1251?B?IO/u?= =?windows-1251?B?IODt4+vo6fHq6A==?=

so no idea what to do now :(

@grosser
Copy link
Contributor Author

grosser commented Jul 21, 2014

ok, this works:

# https://github.com/mikel/mail/issues/741
module MultiByteB64
  def value_decode(str)
    result = super
    result.gsub!("\0", "") # = to A replacement below leaves 0 bytes
    result
  end

  # join multiple of the same encoding to avoid split-bytes issue 
  def collapse_adjacent_encodings(str)
    super(str.gsub(/(\=\?[^?]+\?([QB])\?)([^?]+?)\?\=\s*(\1([^?]+?)\?\=\s*)+/xmi) do |all|
      header = $1
      type = $2
      inner = all.scan(/\=\?[^?]+\?[QB]\?([^?]+?)\?\=/xmi).join("")
      inner.gsub!("=", "A") if type.upcase == "B"
      header + inner + "?="
    end)
  end
end
class << Mail::Encodings
  prepend MultiByteB64
end

@jeremy
Copy link
Collaborator

jeremy commented May 17, 2017

Current master:

>> Mail.new('Subject: "Subject:=?utf-8?B?0J/QvtC80L7Qs9C40YLQtSDQstC+0YHRgdGC0LDQvdC+0LLQ?= =?utf-8?B?uNGC0Ywg0L/QsNGA0L7Qu9GM?="')['Subject'].to_s
=> "\"Subject:Помогите восстанов��ть пароль\""
>> Mail.new('Subject: "Subject:=?utf-8?B?0J/QvtC80L7Qs9C40YLQtSDQstC+0YHRgdGC0LDQvdC+0LLQuNGC0Ywg0L/QsNGA0L7Qu9GM?="')['Subject'].to_s
=> "\"Subject:Помогите восстановить пароль\""

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants