[Python-modules-commits] [pyspf] 01/04: Catch ValueError due to improper IP address in connect IP or in ip4/ip6 mechanisms

Scott Kitterman kitterman at moszumanska.debian.org
Wed Aug 3 05:27:15 UTC 2016


This is an automated email from the git hooks/post-receive script.

kitterman pushed a commit to branch master
in repository pyspf.

commit 082368f15ec51b243fbdbc7b22e0373d22723337
Author: Scott Kitterman <scott at kitterman.com>
Date:   Wed Aug 3 01:20:45 2016 -0400

    Catch ValueError due to improper IP address in connect IP or in ip4/ip6 mechanisms
---
 CHANGELOG |  4 ++++
 spf.py    | 56 ++++++++++++++++++++++++++++++++++----------------------
 2 files changed, 38 insertions(+), 22 deletions(-)

diff --git a/CHANGELOG b/CHANGELOG
index a3be370..8116328 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -1,3 +1,7 @@
+Version 2.0.13 - UNRELEASED
+  * Catch ValueError due to improper IP address in connect IP or in ip4/ip6
+    mechanisms
+
 Version 2.0.12 - August 5, 2015
   * Reset void_lookups at top of check()
   * Ignore permerror for best_guess()
diff --git a/spf.py b/spf.py
index 7ea41d7..703188f 100755
--- a/spf.py
+++ b/spf.py
@@ -421,9 +421,12 @@ class query(object):
             ip6 = True
         else:
             try:
-                self.ipaddr = ipaddress.ip_address(i)
-            except AttributeError:
-                self.ipaddr = ipaddress.IPAddress(i)
+                try:
+                    self.ipaddr = ipaddress.ip_address(i)
+                except AttributeError:
+                    self.ipaddr = ipaddress.IPAddress(i)
+            except ValueError as x:
+                raise PermError(str(x))
             if self.ipaddr.version == 6:
                 if self.ipaddr.ipv4_mapped:
                     self.ipaddr = ipaddress.IPv4Address(self.ipaddr.ipv4_mapped)
@@ -512,6 +515,12 @@ class query(object):
     >>> q.check(spf='v=spf1 ip4:192.0.0.0/8 ?all moo')
     ('permerror', 550, 'SPF Permanent Error: Unknown mechanism found: moo')
 
+    >>> q.check(spf='v=spf1 ip4:192.0.0.n ?all')
+    ('permerror', 550, 'SPF Permanent Error: Invalid IP4 address: ip4:192.0.0.n')
+
+    >>> q.check(spf='v=spf1 ip6:2001:db8:ZZZZ:: ?all')
+    ('permerror', 550, 'SPF Permanent Error: Invalid IP6 address: ip6:2001:db8:ZZZZ::')
+
     >>> q.check(spf='v=spf1 =a ?all moo')
     ('permerror', 550, 'SPF Permanent Error: Unknown qualifier, RFC 4408 para 4.6.1, found in: =a')
 
@@ -1376,27 +1385,30 @@ class query(object):
         True
         """
         try:
-            for netwrk in [ipaddress.ip_network(ip) for ip in ipaddrs]:
-                network = netwrk.supernet(new_prefix=n)
-                if isinstance(self.iplist, bool):
-                    if network.__contains__(self.ipaddr):
-                        return True
-                else:
-                    if n < self.cidrmax:
-                        self.iplist.append(network)
+            try:
+                for netwrk in [ipaddress.ip_network(ip) for ip in ipaddrs]:
+                    network = netwrk.supernet(new_prefix=n)
+                    if isinstance(self.iplist, bool):
+                        if network.__contains__(self.ipaddr):
+                            return True
                     else:
-                        self.iplist.append(network.ip)
-        except AttributeError:
-            for netwrk in [ipaddress.IPNetwork(ip,strict=False) for ip in ipaddrs]:
-                network = netwrk.supernet(new_prefix=n)
-                if isinstance(self.iplist, bool):
-                    if network.__contains__(self.ipaddr):
-                        return True
-                else:
-                    if n < self.cidrmax:
-                        self.iplist.append(network)
+                        if n < self.cidrmax:
+                            self.iplist.append(network)
+                        else:
+                            self.iplist.append(network.ip)
+            except AttributeError:
+                for netwrk in [ipaddress.IPNetwork(ip,strict=False) for ip in ipaddrs]:
+                    network = netwrk.supernet(new_prefix=n)
+                    if isinstance(self.iplist, bool):
+                        if network.__contains__(self.ipaddr):
+                            return True
                     else:
-                        self.iplist.append(network.ip)
+                        if n < self.cidrmax:
+                            self.iplist.append(network)
+                        else:
+                            self.iplist.append(network.ip)
+        except ValueError as x:
+            raise PermError(str(x))
         return False
 
     def parse_header_ar(self, val):

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/pyspf.git



More information about the Python-modules-commits mailing list