A platform for community discussion. Free, open, simple.
FIX: Don't crash on a no-result `Authentication-Results` header
Régis Hanol6ee30b6d6ae8
diff --git a/lib/email/authentication_results.rb b/lib/email/authentication_results.rbindex 05bea8df78d..8df6f06676c 100644--- a/lib/email/authentication_results.rb+++ b/lib/email/authentication_results.rb@@ -60,7 +60,7 @@ module Emailvalue = /(?:"([^"]*)")|(?:([^\s";]*))/authserv_id = valueauthres_version = /\d+#{cfws}?/- no_result = /#{cfws}?;#{cfws}?none/+ no_result = /#{cfws}?;#{cfws}?none#{cfws}?\z/ikeyword = /([a-zA-Z0-9-]*[a-zA-Z0-9])/authres_payload =/\A#{cfws}?#{authserv_id}(?:#{cfws}#{authres_version})?(?:#{no_result}|([\S\s]*))/@@ -97,7 +97,7 @@ module Emailendend- { authserv_id: parsed_authserv_id, resinfo: parsed_resinfo }+ { authserv_id: parsed_authserv_id, resinfo: parsed_resinfo || [] }endendenddiff --git a/spec/lib/email/authentication_results_spec.rb b/spec/lib/email/authentication_results_spec.rbindex 539ee645bcf..c53d591ccdb 100644--- a/spec/lib/email/authentication_results_spec.rb+++ b/spec/lib/email/authentication_results_spec.rb@@ -8,7 +8,7 @@ RSpec.describe Email::AuthenticationResults do# https://tools.ietf.org/html/rfc8601#appendix-B.2results = described_class.new(" example.org 1; none").resultsexpect(results[0][:authserv_id]).to eq "example.org"- expect(results[0][:resinfo]).to be nil+ expect(results[0][:resinfo]).to eq []endit "parses 'Service Provided, Authentication Done' correctly" do@@ -145,6 +145,11 @@ RSpec.describe Email::AuthenticationResults doexpect(results[0][:resinfo][0][:props][0][:pvalue]).to eq "1362471462"end+ it "doesn't mistake a method starting with 'none' for a no-result" do+ results = described_class.new("example.com; nonexistent=pass").results+ expect(results[0][:resinfo][0][:method]).to eq "nonexistent"+ end+it "parses header with no props correctly" doresults = described_class.new(" example.com; dmarc=pass").resultsexpect(results[0][:authserv_id]).to eq "example.com"@@ -226,6 +231,24 @@ RSpec.describe Email::AuthenticationResults doinclude_examples "is verdict", :grayend+ context "with a valid no-result" do+ let(:headers) { "valid.com; none" }++ include_examples "is verdict", :gray+ end++ context "with a valid versioned no-result" do+ let(:headers) { "valid.com 1; NONE (no checks run)" }++ include_examples "is verdict", :gray+ end++ context "with a fail following a misplaced 'none'" do+ let(:headers) { "valid.com; none; dmarc=fail" }++ include_examples "is verdict", :fail+ end+context "with no email_in_authserv_id set" dobefore { SiteSetting.email_in_authserv_id = "" }@@ -256,6 +279,12 @@ RSpec.describe Email::AuthenticationResults doinclude_examples "is verdict", :failend+ context "with a valid fail, and a valid no-result" do+ let(:headers) { ["valid.com; dmarc=fail", "valid.com; none"] }++ include_examples "is verdict", :fail+ end+context "with a valid error, and a valid pass" dolet(:headers) { ["valid.com; dmarc=foobar", "valid.com; dmarc=pass"] }