-
Notifications
You must be signed in to change notification settings - Fork 942
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
Gracefully handle fields whose encoding is unknown to Ruby #580
Comments
👍 |
(I'm primarily working on the reading mails part) module UnknownEncoding
# try to guess encoding if we cannot find it
def q_value_decode(str)
super
rescue ArgumentError
encoding = CharlockHolmes::EncodingDetector.new.detect(str).try(:[], :encoding) || "UTF-8"
warn "Invalid encoding caught #{$!} using #{encoding}"
super(str.sub(/^=?.*?\?/, "=?#{encoding}?Q?"))
end
end
class << Mail::Encodings
prepend UnknownEncoding
end |
We've had to special-case charset -> encoding mappings in other cases too. We'd be better off exposing a uniform way to let users do the lookup and manage fallbacks. For example, returning 'ascii-8bit' as a fallback encoding, doing charset detection on the decoded content, or raising an |
If the value is later used for headers raising or returning ascii are not very helpful since then the header cannot be parsed and we just end up with other issues, having a detection built in could work, but maybe better to just have some callback in place that can be overwritten. |
For most apps, an exception is much better than getting wrongly-encoded data, or a binary string. Both will just cause exceptions when the content is manipulated in any way, but in a much harder to debug way. For now, 👍 to raising a more specific subclass of For later, give users a better way to manage charset -> Ruby encoding lookup, including failures. |
I think returning header values in a fallback encoding of That said, there could be an option to |
I've been successfully using this monkey patch for the last 6 months: # Mail::Encodings.value_decode raises ArgumentError if any encoded word
# specifies an unknown encoding. Monkey-patch Ruby19.{b,q}_value_decode to
# fall back on an ASCII-8BIT encoding.
# Cf. <https://github.com/mikel/mail/issues/580>
###############################################################################
module Mail
class Ruby19
class << self
def b_value_decode_with_fallback_encoding(str)
b_value_decode_without_fallback_encoding(str)
rescue ArgumentError
b_value_decode_without_fallback_encoding(str.sub(/^=\?.*?\?/, "=?ASCII-8BIT?"))
end
alias_method_chain :b_value_decode, :fallback_encoding
def q_value_decode_with_fallback_encoding(str)
q_value_decode_without_fallback_encoding(str)
rescue ArgumentError
q_value_decode_without_fallback_encoding(str.sub(/^=\?.*?\?/, "=?ASCII-8BIT?"))
end
alias_method_chain :q_value_decode, :fallback_encoding
end
end
end Is there value in turning this into a pull request? |
we have a encoder now, does that take care of it ? https://github.com/mikel/mail/blob/master/lib/mail/version_specific/ruby_1_9.rb#L5-L29 |
Encoder? Isn't this issue about |
I think it might be handleable in the encoder, since you see an encoding coming in and can rescue the ArgumentError there ... might be wrong ... can you update to master, reproduce or add a failing test case ? |
I'll try to reproduce this on master. In my dev & prod environment I can't update to master, since we're using the mail gem as part of the Rails framework, and we're currently stuck on Rails 3.2, which depends on mail 2.5.4. All I can do is monkey-patch 2.5.4. The reason I'm pushing for this is that I'd rather monkey-patch it in a way that's consistent with any fixes going into master. |
just monkey patch the version down :) gem "mail", :git => '[email protected]:zendesk/mail', :ref => On Sun, Mar 15, 2015 at 10:25 AM, Julian Mehnle [email protected]
|
@grosser, did you have any compatibility issues with using 2.6 with Rails? |
nope, works fine (and we are doing a ton of email ...) :) |
We're now using our own fork with some fixes pulled in from other forks that haven't been merged into master (most notably https://github.com/okkez/mail/tree/handle-invalid-date), patching the version down to 2.5.999. Apparently this issue (#580) has been resolved in master. Kind of a ridiculous solution, but like @grosser, we're exposed to a lot of email garbage and need to handle it all in our Rails app. Thanks, @grosser! |
Glad that helps, keep making PRs, at least we have a common place for On Thu, Mar 19, 2015 at 11:54 PM, Julian Mehnle [email protected]
|
Ruby19.b_value_decode (and probably q_value_decode) may cause an
ArgumentError: unknown encoding XXXX
exception if they call#force_encoding
with an encoding Ruby doesn't know about.We should either silently fail to decode the field or raise a more specific exception.
The text was updated successfully, but these errors were encountered: