Skip to content
代码片段 群组 项目
提交 f14f8132 编辑于 作者: Stan Hu's avatar Stan Hu
浏览文件

Merge branch...

Merge branch 'qmnguyen0711/364326-nomethoderror-undefined-method-find-for-nil-nilclass' into 'master'

Guard email receiver from empty From and To fields

See merge request gitlab-org/gitlab!89296
No related branches found
No related tags found
无相关合并请求
...@@ -32,8 +32,8 @@ def execute ...@@ -32,8 +32,8 @@ def execute
def mail_metadata def mail_metadata
{ {
mail_uid: mail.message_id, mail_uid: mail.message_id,
from_address: mail.from, from_address: from,
to_address: mail.to, to_address: to,
mail_key: mail_key, mail_key: mail_key,
references: Array(mail.references), references: Array(mail.references),
delivered_to: delivered_to.map(&:value), delivered_to: delivered_to.map(&:value),
...@@ -42,7 +42,7 @@ def mail_metadata ...@@ -42,7 +42,7 @@ def mail_metadata
# reduced down to what looks like an email in the received headers # reduced down to what looks like an email in the received headers
received_recipients: recipients_from_received_headers, received_recipients: recipients_from_received_headers,
meta: { meta: {
client_id: "email/#{mail.from.first}", client_id: "email/#{from.first}",
project: handler&.project&.full_path project: handler&.project&.full_path
} }
} }
...@@ -78,7 +78,7 @@ def mail_key ...@@ -78,7 +78,7 @@ def mail_key
end end
def key_from_to_header def key_from_to_header
mail.to.find do |address| to.find do |address|
key = email_class.key_from_address(address) key = email_class.key_from_address(address)
break key if key break key if key
end end
...@@ -112,6 +112,14 @@ def find_key_from_references ...@@ -112,6 +112,14 @@ def find_key_from_references
end end
end end
def from
Array(mail.from)
end
def to
Array(mail.to)
end
def delivered_to def delivered_to
Array(mail[:delivered_to]) Array(mail[:delivered_to])
end end
......
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
let_it_be(:project) { create(:project) } let_it_be(:project) { create(:project) }
let(:handler) { double(:handler, project: project, execute: true, metrics_event: nil, metrics_params: nil) } let(:handler) { double(:handler, project: project, execute: true, metrics_event: nil, metrics_params: nil) }
let(:client_id) { 'email/jake@example.com' }
it 'correctly finds the mail key' do it 'correctly finds the mail key' do
expect(Gitlab::Email::Handler).to receive(:for).with(an_instance_of(Mail::Message), 'gitlabhq/gitlabhq+auth_token').and_return(handler) expect(Gitlab::Email::Handler).to receive(:for).with(an_instance_of(Mail::Message), 'gitlabhq/gitlabhq+auth_token').and_return(handler)
...@@ -33,7 +34,7 @@ ...@@ -33,7 +34,7 @@
metadata = receiver.mail_metadata metadata = receiver.mail_metadata
expect(metadata.keys).to match_array(%i(mail_uid from_address to_address mail_key references delivered_to envelope_to x_envelope_to meta received_recipients)) expect(metadata.keys).to match_array(%i(mail_uid from_address to_address mail_key references delivered_to envelope_to x_envelope_to meta received_recipients))
expect(metadata[:meta]).to include(client_id: 'email/jake@example.com', project: project.full_path) expect(metadata[:meta]).to include(client_id: client_id, project: project.full_path)
expect(metadata[meta_key]).to eq(meta_value) expect(metadata[meta_key]).to eq(meta_value)
end end
end end
...@@ -126,6 +127,49 @@ ...@@ -126,6 +127,49 @@
it_behaves_like 'failed receive' it_behaves_like 'failed receive'
end end
context "when the email's To field is blank" do
before do
stub_incoming_email_setting(enabled: true, address: "incoming+%{key}@appmail.example.com")
end
let(:email_raw) do
<<~EMAIL
Delivered-To: incoming+gitlabhq/gitlabhq+auth_token@appmail.example.com
From: "jake@example.com" <jake@example.com>
Bcc: "support@example.com" <support@example.com>
Email content
EMAIL
end
let(:meta_key) { :delivered_to }
let(:meta_value) { ["incoming+gitlabhq/gitlabhq+auth_token@appmail.example.com"] }
it_behaves_like 'successful receive'
end
context "when the email's From field is blank" do
before do
stub_incoming_email_setting(enabled: true, address: "incoming+%{key}@appmail.example.com")
end
let(:email_raw) do
<<~EMAIL
Delivered-To: incoming+gitlabhq/gitlabhq+auth_token@appmail.example.com
To: "support@example.com" <support@example.com>
Email content
EMAIL
end
let(:meta_key) { :delivered_to }
let(:meta_value) { ["incoming+gitlabhq/gitlabhq+auth_token@appmail.example.com"] }
it_behaves_like 'successful receive' do
let(:client_id) { 'email/' }
end
end
context 'when the email was auto generated with X-Autoreply header' do context 'when the email was auto generated with X-Autoreply header' do
let(:email_raw) { fixture_file('emails/auto_reply.eml') } let(:email_raw) { fixture_file('emails/auto_reply.eml') }
let(:expected_error) { Gitlab::Email::AutoGeneratedEmailError } let(:expected_error) { Gitlab::Email::AutoGeneratedEmailError }
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册