git » spf » commit a1b76d0

ptr: Case-insensitive domain comparison

author Alberto Bertogli
2019-10-14 11:11:04 UTC
committer Alberto Bertogli
2019-10-14 12:35:33 UTC
parent e7194370b9d9d1bd261aa2a7250fc284159a1595

ptr: Case-insensitive domain comparison

The given and returned domains could be in any case, but the comparison
should be case-insensitive. However, the code does a case-sensitive
comparison today.

This patch fixes the bug by enforcing a lowercase comparison.

Found by the standard test suite.

spf.go +8 -2

diff --git a/spf.go b/spf.go
index 72677f7..760ed9e 100644
--- a/spf.go
+++ b/spf.go
@@ -370,7 +370,7 @@ func (r *resolution) ptrField(res Result, field, domain string) (bool, Result, e
 
 	if r.ipNames == nil {
 		r.count++
-		n, err := lookupAddr(r.ip.String())
+		ns, err := lookupAddr(r.ip.String())
 		if err != nil {
 			// https://tools.ietf.org/html/rfc7208#section-5
 			if isTemporary(err) {
@@ -378,10 +378,16 @@ func (r *resolution) ptrField(res Result, field, domain string) (bool, Result, e
 			}
 			return false, "", err
 		}
-		r.ipNames = n
+		for _, n := range ns {
+			// Append the lower-case variants so we do a case-insensitive
+			// lookup below.
+			r.ipNames = append(r.ipNames, strings.ToLower(n))
+		}
 	}
 
+	ptrDomain = strings.ToLower(ptrDomain)
 	for _, n := range r.ipNames {
+		trace("ptr evaluating %q in %q", n, ptrDomain)
 		if strings.HasSuffix(n, ptrDomain+".") {
 			return true, res, errMatchedPTR
 		}