Allow Multiple DKIM Signature Headers

Christopher Vollick created

Apparently this is a thing?
Most of the emails I got don't have this, but one that I tested just
happened to, so I have to handle it!

We make sure there's always an array now, and then parse "each" of them,
and fail if none of them are for the interac email address.

I don't really care about the others, but I do worry at some point
they're going to stop supporting the address I like...

Either way, this works today.

Change summary

lib/interac_email.rb | 15 +++++++++------
1 file changed, 9 insertions(+), 6 deletions(-)

Detailed changes

lib/interac_email.rb 🔗

@@ -140,21 +140,24 @@ class InteracEmail
 			raise Error::BadAuth, @m unless auth =~ /\sdkim=pass\s/
 		end
 
-		def dkim_header
-			@m["DKIM-Signature"]
-				&.value
+		def dkim_headers
+			# Apparently there can sometimes be multiple DKIM sigs
+			# And this library returns a scalar if there's one, or array otherwise
+			[@m["DKIM-Signature"]].flatten.compact.map {|h|
+				h.value
 				&.split(/;\s*/)
 				&.each_with_object({}) { |f, h|
 					k, v = f.split("=", 2)
 					h[k.to_sym] = v
 				}
+			}
 		end
 
 		def ensure_dkim
-			dkim = dkim_header
+			dkim = dkim_headers
 
-			raise Error::DKIM, @m unless dkim
-			raise Error::WrongDKIM, @m unless dkim[:d] == "payments.interac.ca"
+			raise Error::DKIM, @m if dkim.empty?
+			raise Error::WrongDKIM, @m unless dkim.any? {|v| v[:d] == "payments.interac.ca" }
 		end
 	end