Skip to content

Commit

Permalink
Fix missing Syntax error when ip address and version are not matching (
Browse files Browse the repository at this point in the history
  • Loading branch information
stcz authored Dec 18, 2022
1 parent f36e582 commit 1d74366
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
20 changes: 17 additions & 3 deletions checkdmarc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
14 changes: 14 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand 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"
Expand Down

0 comments on commit 1d74366

Please sign in to comment.