From b17a9ecc37d0d6ad6ae7bd6907d03390e0a1918c Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Wed, 5 Jul 2023 09:01:40 -0500 Subject: [PATCH] Handle multiple DKIM headers --- lib/interac_email.rb | 8 ++-- test/test_interac_email.rb | 75 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 test/test_interac_email.rb diff --git a/lib/interac_email.rb b/lib/interac_email.rb index b12b045..1a46120 100644 --- a/lib/interac_email.rb +++ b/lib/interac_email.rb @@ -119,17 +119,17 @@ class InteracEmail end def authentication_header - @m["Authentication-Results"]&.value + Array(@m["Authentication-Results"]).map(&:value) end HEADER_REGEX = /\sheader.d=payments.interac.ca\s/.freeze def ensure_authentication_header - auth = authentication_header - + auth = authentication_header.find { |a| + a =~ HEADER_REGEX + } raise Error::NoAuth, @m unless auth raise Error::BadAuth, @m unless auth =~ /\sdkim=pass\s/ - raise Error::BadDomain, @m unless auth =~ HEADER_REGEX end def dkim_header diff --git a/test/test_interac_email.rb b/test/test_interac_email.rb new file mode 100644 index 0000000..b54a60b --- /dev/null +++ b/test/test_interac_email.rb @@ -0,0 +1,75 @@ +# frozen_string_literal: true + +require "interac_email" +require "mail" +require "test_helper" + +class InteracEmailTest < Minitest::Test + def test_authentication_header + @m = Mail.new(<<~MAIL) + Authentication-Results: hai + To: someone@example.com + From: interac@example.com + + body + MAIL + @validator = InteracEmail::Validator.new(@m) + assert_equal ["hai"], @validator.authentication_header + end + + def test_authentication_headers + @m = Mail.new(<<~MAIL) + Authentication-Results: hai + Authentication-Results: hai2 + To: someone@example.com + From: interac@example.com + + body + MAIL + @validator = InteracEmail::Validator.new(@m) + assert_equal ["hai", "hai2"], @validator.authentication_header + end + + def test_ensure_authentication_header + @m = Mail.new(<<~MAIL) + Authentication-Results: stuff header.d=payments.interac.ca dkim=pass and + Authentication-Results: and + To: someone@example.com + From: interac@example.com + + body + MAIL + @validator = InteracEmail::Validator.new(@m) + @validator.ensure_authentication_header + end + + def test_ensure_authentication_header_fail + @m = Mail.new(<<~MAIL) + Authentication-Results: stuff header.d=payments.interac.ca dkim=fail and + Authentication-Results: and + To: someone@example.com + From: interac@example.com + + body + MAIL + @validator = InteracEmail::Validator.new(@m) + assert_raises(InteracEmail::Error::BadAuth) do + @validator.ensure_authentication_header + end + end + + def test_ensure_authentication_header_no_interac + @m = Mail.new(<<~MAIL) + Authentication-Results: stuff header.d=fakey.fake dkim=pass and + Authentication-Results: and dkim=pass stuff + To: someone@example.com + From: interac@example.com + + body + MAIL + @validator = InteracEmail::Validator.new(@m) + assert_raises(InteracEmail::Error::NoAuth) do + @validator.ensure_authentication_header + end + end +end -- 2.45.2