diff --git a/checkdmarc.py b/checkdmarc.py index 24462dd..570ab43 100755 --- a/checkdmarc.py +++ b/checkdmarc.py @@ -1667,11 +1667,25 @@ def parse_spf_record(record, domain, parked=False, seen=None, recursion=None, value = match[2] try: - if mechanism in ["ip4", "ip6"]: + if mechanism == "ip4": try: - ipaddress.ip_network(value, strict=False) + if not isinstance(ipaddress.ip_network(value, + strict=False), + ipaddress.IPv4Network): + raise SPFSyntaxError("{0} is not a valid ipv4 value. " + "Looks like ipv6".format(value)) except ValueError: - raise SPFSyntaxError("{0} is not a valid ipv4/ipv6 " + raise SPFSyntaxError("{0} is not a valid ipv4 " + "value".format(value)) + elif mechanism == "ip6": + try: + if not isinstance(ipaddress.ip_network(value, + strict=False), + ipaddress.IPv6Network): + raise SPFSyntaxError("{0} is not a valid ipv6 value. " + "Looks like ipv4".format(value)) + except ValueError: + raise SPFSyntaxError("{0} is not a valid ipv6 " "value".format(value)) if mechanism == "a": diff --git a/tests.py b/tests.py index 334ce0c..d0c4b60 100755 --- a/tests.py +++ b/tests.py @@ -111,6 +111,13 @@ def testSPFInvalidIPv4(self): self.assertRaises(checkdmarc.SPFSyntaxError, checkdmarc.parse_spf_record, spf_record, domain) + def testSPFInvalidIPv6inIPv4(self): + """Invalid ipv4 SPF mechanism values raise SPFSyntaxError""" + spf_record = "v=spf1 ip4:1200:0000:AB00:1234:0000:2552:7777:1313 ~all" + domain = "surftown.dk" + self.assertRaises(checkdmarc.SPFSyntaxError, + checkdmarc.parse_spf_record, spf_record, domain) + def testSPFInvalidIPv4Range(self): """Invalid ipv4 SPF mechanism values raise SPFSyntaxError""" spf_record = "v=spf1 ip4:78.46.96.236/99 ~all" @@ -125,6 +132,13 @@ def testSPFInvalidIPv6(self): self.assertRaises(checkdmarc.SPFSyntaxError, checkdmarc.parse_spf_record, spf_record, domain) + def testSPFInvalidIPv4inIPv6(self): + """Invalid ipv6 SPF mechanism values raise SPFSyntaxError""" + spf_record = "v=spf1 ip6:78.46.96.236 ~all" + domain = "surftown.dk" + self.assertRaises(checkdmarc.SPFSyntaxError, + checkdmarc.parse_spf_record, spf_record, domain) + def testSPFInvalidIPv6Range(self): """Invalid ipv6 SPF mechanism values raise SPFSyntaxError""" record = "v=spf1 ip6:1200:0000:AB00:1234:0000:2552:7777:1313/130 ~all"