-
Notifications
You must be signed in to change notification settings - Fork 936
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
Encoding messages introduces LF characters. #1190
Comments
I just ran into another side effect of #1113: The LF line endings are now encoded as Browsers submit text area fields with \r\n endings in Rails and Ruby uses \n endings by default so there's a going to be a lot of effort to ensure we don't get any mixed line endings in there, I would be in favour of having a way to get the old behaviour back. |
@paho-outreach Could you show an example message or code snippet that demonstrates this?
@Fjan Could you provide an example? I've added a pull request which restores \n normalization at #1210. Please give it a shot as well. |
This is what is affecting me, it seems to be related to this issue:
After parsing and encoding this specific message two times it ends up with two \r\r that the browser inteprets as two lines. Also DKIM and S/MIME signatures get invalidated as the message is effectively changed. |
Hi Jeremy,
This is an excerpt of an email generated by mail 2.7.0:
We can remove the extra |
Here's a simple example:
Outputs:
Pre 2.7.0, the line-wrapping characters would be |
Hi, I had opened this within Rails at rails/rails#32836 but was helped to realize this was a separate gem. I used the default mail version 2.7 that comes with |
FYI I opened bug in Ruby https://bugs.ruby-lang.org/issues/14741 - correct me if I'm wrong, but [].pack('M') should return CRLF based on the spec, not just LF |
So what's the best solution for mail gem? |
@nicosticht I think we have to wait for #1210 to get merged, but until then downgrading the mail gem to version 2.6.6 works for me |
It is an unresolved issue with 'mail' <mikel/mail#1190> that it started to contain carriage returns. It's a little unclear why they don't fix it, but it's a minor problem which we can work around.
It is an unresolved issue with 'mail' <mikel/mail#1190> that it started to contain carriage returns. It's a little unclear why they don't fix it, but it's a minor problem which we can work around.
* Mailer specs were failing, due to \r being added to lines. See: mikel/mail#1190
* Update rails to 5.0.7.2 * Update locale modules/**/*.gemspec to point to rails 5.0.7.1 * Update modules meta information to point to va.gov * Update activerecord-postgis-adapter version to >= 4.0.x * Update nokogiri version to >= 1.10.1 * Add rails 5.0 initializers * Disable config.action_controller.raise_on_unfiltered_parameters setting * Use relative requires in rails 5 way * Add generated binstubs for update and setup * Disable raise on missing callback to fix specs not running * Update db/schema.rb with rails 5 style * Add ApplicationRecord model * Update models to inherit from ApplicationRecord * fix in progress form cast * update database type cast for clean uuid * dont send strings for hash attributes * add deep transform parameters method in app controller * fix supporting evidence upload swagger spec * Add utf8-cleaner gem * Rails 5.0.0 introduced code in ActionDispatch to catch invalid UTF-8 querystring params, and raise a ActionController::BadRequest error when present. For more information, see the commit at: rails/rails@59ab2d1#diff-a2247cc200a3f9cd3d0e5ef8d76c4df8 * utf8-cleaner gem introduces a middleware layer to rack requests that will sanitize rack input, avoiding UTF8 errors altogether * Remove #safe_encoded_params from GI::InstitutionsController * Update specs to expect a successful request if sent invalid UTF8 string, as it should be sanitized by rack * Update module callbacks to declare raise: false * Render header only responses * Refactor UsersController test to use consistent matcher * Assign id param in V0::Facilities::CcpController#validate_id instead of mutating it * Pin mail gem to 2.6.6 to avoid generating LF characters * Mailer specs were failing, due to \r being added to lines. See: mikel/mail#1190 * Remove unneeded assets initializer * Uptick rubocop target rails version * DRY up appeals_api request spec * move deep transform parameters out of app controller (#2842) * Update migrations to specify from which Rails version they were spawned * turn on belongs_to_required_by_default * fix async stale scope spec * fix rails 5.0 deprecation warnings (#2856) * move deep transform parameters out of app controller * remove raise in transactional callbacks * dont use strings for middleware * permit all on MessagingPreferencesController * dont need to return false if error is added * permit params in pciu controller * permit in health records ctrl * permit params for military ranks ctrl * fix async base spec * use `#to_hash` in prescriptions finder * permit in prescrip pref ctrl * permit pciu address fields * dont permit all in health records ctrl * dont permit all in messaging pref ctrl * send array to health records ctrl * dont permit all in filterable * dont raise on deprecation * dont halt callbacks on return false * turn on raise_on_unfiltered_parameters * remove raise_on_unfiltered_parameters config as it's causing console errors and will have not effect once we're >= rails 5.1 (#2926) * Match SessionsController spec to master * Filter HashHelpers coverage
It is an unresolved issue with 'mail' <mikel/mail#1190> that it started to contain carriage returns. It's a little unclear why they don't fix it, but it's a minor problem which we can work around.
As far as I can tell the encoded LF produced by the Quoted Printable output of
and
The original implementation did this correctly, so I would suggest either reverting or doing |
@jeremy We have been confronted with exactly this issue when implementing S/MIME signatures for Gitlab. When Unicode characters and Our basic approach is an interceptor that does something like below (the incoming message is multipart with text and html parts, file samples available in a link at the end): def delivering_email(message)
signed_message = Gitlab::Email::Smime::Signer.sign(
cert: certificate.cert,
key: certificate.key,
data: message.encoded)
signed_email = Mail.new(signed_message)
overwrite_body(message, signed_email)
overwrite_headers(message, signed_email)
end
def overwrite_body(message, signed_email)
# since this is a multipart email, assignment to nil is important,
# otherwise Message#body will add a new mail part
message.body = nil
message.body = signed_email.body.encoded
end
def overwrite_headers(message, signed_email)
message.content_disposition = signed_email.content_disposition if signed_email.content_disposition
message.content_transfer_encoding = signed_email.content_transfer_encoding
message.content_type = signed_email.content_type
end I've traced down this to exactly the extra I also tried some of the patches listed in this ticket, #1325, and your PR #1210. It improves things but does not solve all cases; some of those patches solve the For the record, I've checked older email notifications from gitlab.com, and this issue with In the corresponding Gitlab issue there's also sample message files pre- and post 2.7: https://gitlab.com/gitlab-org/gitlab/issues/197386#note_273117603 Would be really grateful if you could give us your assessment. Thanks! |
A sample of the failing behaviour: require 'mail'
email = Mail.new do
to 'nicolas@test.lindsaar.net.au'
from 'Mikel Lindsaar <mikel@test.lindsaar.net.au>'
subject 'This will break encoded'
content_type 'text/plain; charset=UTF-8'
content_transfer_encoding 'quoted-printable'
body "0123456789012345678901234567890123456789012345678901234567890123456789\n"
end
email.body.raw_source
=> "0123456789012345678901234567890123456789012345678901234567890123456789\r\n"
email.body.encoded
=> "0123456789012345678901234567890123456789012345678901234567890123456789=0D=\n\n"
Mail.new(email).body.encoded
=> "0123456789012345678901234567890123456789012345678901234567890123456789=0D=\n=0D\n" This would be encoded to 71 chars, though with
The endlines for each that should be |
I ran into the same issue sending mails with rails and the mail gem in version 2.7.1. Mails containing special chars are sent as quoted printable and all This additional By debugging I found out that the raw souce of the body changed between versions 2.7.0 and 2.7.1: This happens because of calling May be this conversion needs to be undone in the |
This issue is fairly simple: this gem switched to using
|
@Fjan Thank you for sharing your monkey patch and explanations. I converted it to an module that can be prepended to QuotedPrintable and reuses the
This module is applied as an core extension during initialization of my app. |
Fixed by #1210.
@dlouzan Could you share the cases that #1210 does not solve for you? Perhaps as separate new issues. |
@jeremy can we see a release of 2.7.2, please? We are looking at using the monkey patch above in the mean time. |
Hi, |
@elalemanyo That's normal for 7bit encoding, it can't be changed without breaking decoding at the recipient side |
@Fjan ok, but was not like this without the monkey patch, and now I have some failing tests 🤔 |
In #1113,
QuotedPrintable.encode
was changed fromto
The problem I'm running into is that
pack
introduces LF characters, so encoding a message > 998 characters creates something that is not valid per rfc2822, even if you only had CRLF line-endings before.I see that #1157 prevents attachments from being forced into quoted-printable, and attachment corruption was the motivation for this change in #1113. Would you be open to restoring the old behavior, or does more need to be done in attachment land first?
The text was updated successfully, but these errors were encountered: