debian-mirror-gitlab/lib/gitlab/email/receiver.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

211 lines
5.7 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
2016-09-13 17:45:13 +05:30
require_dependency 'gitlab/email/handler'
2015-09-25 12:07:36 +05:30
# Inspired in great part by Discourse's Email::Receiver
module Gitlab
module Email
2016-09-13 17:45:13 +05:30
class Receiver
2021-06-08 01:23:25 +05:30
include Gitlab::Utils::StrongMemoize
2022-05-07 20:08:51 +05:30
RECEIVED_HEADER_REGEX = /for\s+\<(.+)\>/.freeze
2023-01-13 00:05:48 +05:30
# Errors that are purely from users and not anything we can control
USER_ERRORS = [
Gitlab::Email::AutoGeneratedEmailError, Gitlab::Email::ProjectNotFound, Gitlab::Email::EmptyEmailError,
Gitlab::Email::UserNotFoundError, Gitlab::Email::UserBlockedError, Gitlab::Email::UserNotAuthorizedError,
Gitlab::Email::NoteableNotFoundError, Gitlab::Email::InvalidAttachment, Gitlab::Email::InvalidRecordError,
Gitlab::Email::EmailTooLarge
].freeze
2015-09-25 12:07:36 +05:30
def initialize(raw)
@raw = raw
end
def execute
raise EmptyEmailError if @raw.blank?
2021-06-08 01:23:25 +05:30
ignore_auto_reply!
2015-09-25 12:07:36 +05:30
2016-09-13 17:45:13 +05:30
raise UnknownIncomingEmail unless handler
2015-09-25 12:07:36 +05:30
2019-12-04 20:38:33 +05:30
handler.execute.tap do
2021-09-04 01:27:46 +05:30
Gitlab::Metrics::BackgroundTransaction.current&.add_event(handler.metrics_event, handler.metrics_params)
2019-12-04 20:38:33 +05:30
end
2023-01-13 00:05:48 +05:30
rescue *USER_ERRORS => e
# do not send a metric event since these are purely user errors that we can't control
raise e
2021-09-30 23:02:18 +05:30
rescue StandardError => e
Gitlab::Metrics::BackgroundTransaction.current&.add_event('email_receiver_error', error: e.class.name)
raise e
2015-09-25 12:07:36 +05:30
end
2021-06-08 01:23:25 +05:30
def mail_metadata
{
mail_uid: mail.message_id,
2022-07-23 23:45:48 +05:30
from_address: from,
to_address: to,
2021-06-08 01:23:25 +05:30
mail_key: mail_key,
references: Array(mail.references),
delivered_to: delivered_to.map(&:value),
envelope_to: envelope_to.map(&:value),
2021-09-30 23:02:18 +05:30
x_envelope_to: x_envelope_to.map(&:value),
2022-05-07 20:08:51 +05:30
# reduced down to what looks like an email in the received headers
received_recipients: recipients_from_received_headers,
2021-09-30 23:02:18 +05:30
meta: {
2022-07-23 23:45:48 +05:30
client_id: "email/#{from.first}",
2021-09-30 23:02:18 +05:30
project: handler&.project&.full_path
}
2021-06-08 01:23:25 +05:30
}
end
2021-12-11 22:18:48 +05:30
def mail
strong_memoize(:mail) { build_mail }
end
2017-08-17 22:00:37 +05:30
private
2021-06-08 01:23:25 +05:30
def handler
strong_memoize(:handler) { find_handler }
end
def find_handler
2020-04-08 14:13:33 +05:30
Handler.for(mail, mail_key)
end
2016-09-13 17:45:13 +05:30
def build_mail
2022-07-23 23:45:48 +05:30
# See https://github.com/mikel/mail/blob/641060598f8f4be14d79bad8d703e9f2967e1cdb/spec/mail/message_spec.rb#L569
# for mail structure
2016-09-13 17:45:13 +05:30
Mail::Message.new(@raw)
rescue Encoding::UndefinedConversionError,
Encoding::InvalidByteSequenceError => e
2015-09-25 12:07:36 +05:30
raise EmailUnparsableError, e
end
2021-06-08 01:23:25 +05:30
def mail_key
strong_memoize(:mail_key) do
key_from_to_header || key_from_additional_headers
end
2016-06-02 11:05:42 +05:30
end
2021-06-08 01:23:25 +05:30
def key_from_to_header
2022-07-23 23:45:48 +05:30
to.find do |address|
2022-01-26 12:08:38 +05:30
key = email_class.key_from_address(address)
2016-09-13 17:45:13 +05:30
break key if key
2016-06-02 11:05:42 +05:30
end
end
2021-06-08 01:23:25 +05:30
def key_from_additional_headers
find_key_from_references ||
find_key_from_delivered_to_header ||
find_key_from_envelope_to_header ||
2022-05-07 20:08:51 +05:30
find_key_from_x_envelope_to_header ||
find_first_key_from_received_headers
2017-08-17 22:00:37 +05:30
end
def ensure_references_array(references)
case references
when Array
references
when String
# Handle emails from clients which append with commas,
# example clients are Microsoft exchange and iOS app
Gitlab::IncomingEmail.scan_fallback_references(references)
when nil
[]
end
end
2021-06-08 01:23:25 +05:30
def find_key_from_references
2017-08-17 22:00:37 +05:30
ensure_references_array(mail.references).find do |mail_id|
2022-01-26 12:08:38 +05:30
key = email_class.key_from_fallback_message_id(mail_id)
2016-09-13 17:45:13 +05:30
break key if key
2015-09-25 12:07:36 +05:30
end
end
2017-08-17 22:00:37 +05:30
2022-07-23 23:45:48 +05:30
def from
Array(mail.from)
end
def to
Array(mail.to)
end
2021-06-08 01:23:25 +05:30
def delivered_to
Array(mail[:delivered_to])
end
def envelope_to
Array(mail[:envelope_to])
end
def x_envelope_to
Array(mail[:x_envelope_to])
end
2022-05-07 20:08:51 +05:30
def received
Array(mail[:received])
end
2021-06-08 01:23:25 +05:30
def find_key_from_delivered_to_header
delivered_to.find do |header|
2022-01-26 12:08:38 +05:30
key = email_class.key_from_address(header.value)
2017-08-17 22:00:37 +05:30
break key if key
end
end
2017-09-10 17:25:29 +05:30
2021-06-08 01:23:25 +05:30
def find_key_from_envelope_to_header
envelope_to.find do |header|
2022-01-26 12:08:38 +05:30
key = email_class.key_from_address(header.value)
2020-03-13 15:44:24 +05:30
break key if key
end
end
2021-06-08 01:23:25 +05:30
def find_key_from_x_envelope_to_header
x_envelope_to.find do |header|
2022-01-26 12:08:38 +05:30
key = email_class.key_from_address(header.value)
2020-11-24 15:15:51 +05:30
break key if key
end
end
2022-05-07 20:08:51 +05:30
def find_first_key_from_received_headers
recipients_from_received_headers.find do |email|
key = email_class.key_from_address(email)
break key if key
end
end
def recipients_from_received_headers
strong_memoize :emails_from_received_headers do
received.map { |header| header.value[RECEIVED_HEADER_REGEX, 1] }.compact
end
end
2021-06-08 01:23:25 +05:30
def ignore_auto_reply!
if auto_submitted? || auto_replied?
2019-12-21 20:55:43 +05:30
raise AutoGeneratedEmailError
end
end
2021-06-08 01:23:25 +05:30
def auto_submitted?
2017-09-10 17:25:29 +05:30
# Mail::Header#[] is case-insensitive
auto_submitted = mail.header['Auto-Submitted']&.value
# Mail::Field#value would strip leading and trailing whitespace
2023-03-04 22:38:38 +05:30
# See also https://www.rfc-editor.org/rfc/rfc3834
2019-12-21 20:55:43 +05:30
auto_submitted && auto_submitted != 'no'
end
2021-06-08 01:23:25 +05:30
def auto_replied?
2019-12-21 20:55:43 +05:30
autoreply = mail.header['X-Autoreply']&.value
autoreply && autoreply == 'yes'
2017-09-10 17:25:29 +05:30
end
2022-01-26 12:08:38 +05:30
def email_class
Gitlab::IncomingEmail
end
2015-09-25 12:07:36 +05:30
end
end
end