domainaware / checkdmarc

A parser for SPF and DMARC DNS records
https://domainaware.github.io/checkdmarc
Apache License 2.0
244 stars 76 forks source link

Exception in spf.check_spf if unkown host. #120

Closed Niecke closed 7 months ago

Niecke commented 7 months ago

Hi, I am using checkdmarc version 4.8.5 with python 3.11. In my code I use the checkdmarc.spf.check_spf like this:

spf_record = checkdmarc.check_spf(
        domain=domain,
        nameservers=["1.1.1.1", "8.8.8.8"],
        timeout=2.0,
    )

When the domain for example is test.de everything works fine, but when the domain is set to test.d which does not exist, there is the following error:

Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/checkdmarc.py", line 1663, in query_spf_record
    answers = _query_dns(domain, "TXT", nameservers=nameservers,
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/checkdmarc.py", line 649, in _query_dns
    resolver.resolve(domain, record_type, lifetime=timeout)))
  File "/usr/local/lib/python3.11/site-packages/dns/resolver.py", line 1307, in resolve
    (request, answer) = resolution.next_request()
                        ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/dns/resolver.py", line 749, in next_request
    raise NXDOMAIN(qnames=self.qnames_to_try, responses=self.nxdomain_responses)
dns.resolver.NXDOMAIN: The DNS query name does not exist: test.d.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/checkdmarc.py", line 2743, in check_spf
    spf_query = query_spf_record(
                ^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/checkdmarc.py", line 1684, in query_spf_record
    raise SPFRecordNotFound(f"The domain {domain} does not exist",
checkdmarc.SPFRecordNotFound: ('The domain test.d does not exist', 'test.d')

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/flask/app.py", line 1478, in __call__
    return self.wsgi_app(environ, start_response)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/flask/app.py", line 1458, in wsgi_app
    response = self.handle_exception(e)
               ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/flask/app.py", line 1455, in wsgi_app
    response = self.full_dispatch_request()
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/flask/app.py", line 869, in full_dispatch_request
    rv = self.handle_user_exception(e)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/flask/app.py", line 867, in full_dispatch_request
    rv = self.dispatch_request()
         ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/flask/app.py", line 852, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/app.py", line 126, in api
    check_res = check_recod(req["domain"])
                ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/app.py", line 78, in check_recod
    spf_record = checkdmarc.check_spf(
                 ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/checkdmarc.py", line 2763, in check_spf
    spf_results["error"] = error.args[0].args[0]
                           ^^^^^^^^^^^^^^^^^^^^^

I would have assumed a dict containing the error and valid set to false. Or do I just use the function wrong?

Niecke commented 7 months ago

Ok, when upgrading to 5.1.0 there is no exception any more and everything works as expected.