Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SPF checks throw exceptions if ipaddress module is installed beside ipaddr #7

Open
jahlives opened this issue Sep 23, 2018 · 1 comment

Comments

@jahlives
Copy link
Contributor

If using pyspf on python2.7.5 (though think that all python2 is affected) the execution fails if spf.py finds ipaddress package and imports it. In my case ipaddress came as dependency of salt-minion

import spf
spf.check2('XX.YY.ZZ.YY', '[email protected]', 'example.com')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/site-packages/spf.py", line 297, in check2
    receiver=receiver,timeout=timeout,verbose=verbose,querytime=querytime).check()
  File "/usr/lib/python2.7/site-packages/spf.py", line 378, in __init__
    self.set_ip(i)
  File "/usr/lib/python2.7/site-packages/spf.py", line 405, in set_ip
    self.ipaddr = ipaddress.ip_address(i)
  File "/usr/lib/python2.7/site-packages/ipaddress.py", line 163, in ip_address
    ' a unicode object?' % address)
ipaddress.AddressValueError: 'XX.YY.ZZ.YY' does not appear to be an IPv4 or IPv6 address. Did you pass in a bytes (str in Python 2) instead of a unicode object?

I tried to make it unicode

import spf
spf.check2(unicode('XX.YY.ZZ.YY'), '[email protected]', 'example.com')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/site-packages/spf.py", line 297, in check2
    receiver=receiver,timeout=timeout,verbose=verbose,querytime=querytime).check()
  File "/usr/lib/python2.7/site-packages/spf.py", line 547, in check
    rc = self.check1(spf, self.d, 0)
  File "/usr/lib/python2.7/site-packages/spf.py", line 586, in check1
    return self.check0(spf, recursion)
  File "/usr/lib/python2.7/site-packages/spf.py", line 871, in check0
    res, code, txt = self.check1(d,arg, recursion + 1)
  File "/usr/lib/python2.7/site-packages/spf.py", line 586, in check1
    return self.check0(spf, recursion)
  File "/usr/lib/python2.7/site-packages/spf.py", line 895, in check0
    if self.cidrmatch(self.dns_a(arg,self.A), cidrlength):
  File "/usr/lib/python2.7/site-packages/spf.py", line 1348, in cidrmatch
    for netwrk in [ipaddress.ip_network(ip) for ip in ipaddrs]:
  File "/usr/lib/python2.7/site-packages/ipaddress.py", line 199, in ip_network
    ' a unicode object?' % address)
ipaddress.AddressValueError: 'XX.XX.XX.XX' does not appear to be an IPv4 or IPv6 address. Did you pass in a bytes (str in Python 2) instead of a unicode object?

note that using unicode() the reported IP address is NOT the one tested by spf.check2() but an ipv4 address from the senders domain spf record.

After trying some I found that if I use an inport like this

try:
    import ipaddr as ipadress

in /usr/lib/python2.7/site-packages/spf.py "solves" the issue and spf.check2() works as expected again

@jahlives jahlives mentioned this issue Sep 23, 2018
@niftylettuce
Copy link
Contributor

I ran sudo rm -rf /usr/lib/python2.7/dist-packages/ipaddress* and it fixed the issue.

niftylettuce added a commit to forwardemail/python-spfcheck2 that referenced this issue Dec 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants