sdgathman / pyspf

Other
52 stars 26 forks source link

dnspython vs python3 dns dns.resolver.resolve lifetime arg bug #30

Closed akhepcat closed 3 years ago

akhepcat commented 3 years ago

under python3, with systems having both dnspython and python3 dns libraries installed, the SPF code fails due to how the resolver dependencies are walked.

The specific bug raised is that the snd.resolver.resolve(name,qtype,lifetime) args are incompatible with python3-dns, and cause pyspf to crash.

Something like this (as seen online elsewhere) seems to resolve the issue:

spf.py @@ -120,10 
 def DNSLookup_dnspython(name, qtype, tcpfallback=True, timeout=30):
+    import dns.version #for compatibility testing
     retVal = []
     try:
         # FIXME: how to disable TCP fallback in dnspython if not tcpfallback?
-        answers = dns.resolver.query(name, qtype, lifetime=timeout)
+        dns_version = dns.version.MAJOR+dns.version.MINOR/100
+        if dns_version<1.16:
+          answers = dns.resolver.query(name, qtype)
+        elif dns_version<2.0:
+          answers = dns.resolver.query(name, qtype, lifetime=timeout)
+        else: # >=2.0
+          answers = dns.resolver.resolve(name, qtype, lifetime=timeout)
         for rdata in answers:
             if qtype == 'A' or qtype == 'AAAA':
                 retVal.append(((name, qtype), rdata.address))

(this is working on my local system, though i have some other local patches which may or may not intertwine with this, but not listed here)

sdgathman commented 3 years ago

Pushed commit 4e82c5aaf3b044f7280ace98edb719f62819db2b with your suggested fix. We do not have a good way to handle test cases using the actual dnspython/py(3)dns. That would require some kind of stub DNS server with the test DNS database.