diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-06-20 14:10:13 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-06-20 14:10:13 +0300 |
commit | 0ea3fcec397b69815975647f5e2aa5fe944a8486 (patch) | |
tree | 7979381b89d26011bcf9bdc989a40fcc2f1ed4ff /lib/gitlab/email | |
parent | 72123183a20411a36d607d70b12d57c484394c8e (diff) |
Add latest changes from gitlab-org/gitlab@15-1-stable-eev15.1.0-rc42
Diffstat (limited to 'lib/gitlab/email')
-rw-r--r-- | lib/gitlab/email/receiver.rb | 20 | ||||
-rw-r--r-- | lib/gitlab/email/reply_parser.rb | 22 |
2 files changed, 33 insertions, 9 deletions
diff --git a/lib/gitlab/email/receiver.rb b/lib/gitlab/email/receiver.rb index 4da112bc5a0..ba84be6e8ca 100644 --- a/lib/gitlab/email/receiver.rb +++ b/lib/gitlab/email/receiver.rb @@ -32,8 +32,8 @@ module Gitlab def mail_metadata { mail_uid: mail.message_id, - from_address: mail.from, - to_address: mail.to, + from_address: from, + to_address: to, mail_key: mail_key, references: Array(mail.references), delivered_to: delivered_to.map(&:value), @@ -42,7 +42,7 @@ module Gitlab # reduced down to what looks like an email in the received headers received_recipients: recipients_from_received_headers, meta: { - client_id: "email/#{mail.from.first}", + client_id: "email/#{from.first}", project: handler&.project&.full_path } } @@ -63,6 +63,8 @@ module Gitlab end def build_mail + # See https://github.com/mikel/mail/blob/641060598f8f4be14d79bad8d703e9f2967e1cdb/spec/mail/message_spec.rb#L569 + # for mail structure Mail::Message.new(@raw) rescue Encoding::UndefinedConversionError, Encoding::InvalidByteSequenceError => e @@ -76,7 +78,7 @@ module Gitlab end def key_from_to_header - mail.to.find do |address| + to.find do |address| key = email_class.key_from_address(address) break key if key end @@ -110,6 +112,14 @@ module Gitlab end end + def from + Array(mail.from) + end + + def to + Array(mail.to) + end + def delivered_to Array(mail[:delivered_to]) end @@ -148,8 +158,6 @@ module Gitlab end def find_first_key_from_received_headers - return unless ::Feature.enabled?(:use_received_header_for_incoming_emails) - recipients_from_received_headers.find do |email| key = email_class.key_from_address(email) break key if key diff --git a/lib/gitlab/email/reply_parser.rb b/lib/gitlab/email/reply_parser.rb index d39fa139abb..c2d645138d7 100644 --- a/lib/gitlab/email/reply_parser.rb +++ b/lib/gitlab/email/reply_parser.rb @@ -33,10 +33,10 @@ module Gitlab l.strip.empty? || (!allow_only_quotes && l.start_with?('>')) end - encoded_body = body.force_encoding(encoding).encode("UTF-8") + encoded_body = force_utf8(body.force_encoding(encoding)) return encoded_body unless @append_reply - [encoded_body, stripped_text.force_encoding(encoding).encode("UTF-8")] + [encoded_body, force_utf8(stripped_text.force_encoding(encoding))] end private @@ -70,13 +70,29 @@ module Gitlab return if object.nil? if object.charset - object.body.decoded.force_encoding(object.charset.gsub(/utf8/i, "UTF-8")).encode("UTF-8").to_s + # A part of a multi-part may have a different encoding. Its encoding + # is denoted in its header. For example: + # + # ``` + # ------=_Part_2192_32400445.1115745999735 + # Content-Type: text/plain; charset=ISO-8859-1 + # Content-Transfer-Encoding: 7bit + # + # Plain email. + # ``` + # So, we had to force its part to corresponding encoding before able + # to convert it to UTF-8 + force_utf8(object.body.decoded.force_encoding(object.charset.gsub(/utf8/i, "UTF-8"))) else object.body.to_s end rescue StandardError nil end + + def force_utf8(str) + Gitlab::EncodingHelper.encode_utf8(str).to_s + end end end end |