spf-lookups 1.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- spf_lookups-1.0.1/LICENSE +21 -0
- spf_lookups-1.0.1/PKG-INFO +91 -0
- spf_lookups-1.0.1/README.md +70 -0
- spf_lookups-1.0.1/pyproject.toml +32 -0
- spf_lookups-1.0.1/setup.cfg +4 -0
- spf_lookups-1.0.1/src/spf_lookups/__init__.py +2 -0
- spf_lookups-1.0.1/src/spf_lookups/__main__.py +4 -0
- spf_lookups-1.0.1/src/spf_lookups/cli.py +81 -0
- spf_lookups-1.0.1/src/spf_lookups/core.py +171 -0
- spf_lookups-1.0.1/src/spf_lookups.egg-info/PKG-INFO +91 -0
- spf_lookups-1.0.1/src/spf_lookups.egg-info/SOURCES.txt +14 -0
- spf_lookups-1.0.1/src/spf_lookups.egg-info/dependency_links.txt +1 -0
- spf_lookups-1.0.1/src/spf_lookups.egg-info/entry_points.txt +2 -0
- spf_lookups-1.0.1/src/spf_lookups.egg-info/requires.txt +1 -0
- spf_lookups-1.0.1/src/spf_lookups.egg-info/top_level.txt +1 -0
- spf_lookups-1.0.1/tests/test_core.py +73 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Hunkeler Digital Services (Inbox Guard Switzerland)
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: spf-lookups
|
|
3
|
+
Version: 1.0.1
|
|
4
|
+
Summary: Count the DNS lookups of an SPF record including all nested includes (RFC 7208 limit of 10).
|
|
5
|
+
Author-email: Inbox Guard Switzerland <media@igs.ch>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/inboxguard-ch/spf-lookups
|
|
8
|
+
Project-URL: Web version, https://www.igs.ch/?p=tool&t=spf
|
|
9
|
+
Keywords: spf,dmarc,email,dns,rfc7208,email-security
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Topic :: Communications :: Email
|
|
14
|
+
Classifier: Topic :: System :: Networking
|
|
15
|
+
Classifier: Environment :: Console
|
|
16
|
+
Requires-Python: >=3.9
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
License-File: LICENSE
|
|
19
|
+
Requires-Dist: dnspython>=2.0
|
|
20
|
+
Dynamic: license-file
|
|
21
|
+
|
|
22
|
+
# spf-lookups
|
|
23
|
+
|
|
24
|
+
Count the DNS lookups of an SPF record – including every nested `include` and `redirect` – and see which entry breaks the limit.
|
|
25
|
+
|
|
26
|
+
RFC 7208 allows at most **10 DNS lookups** and **2 void lookups** per SPF evaluation. Above that, receivers return `permerror`, SPF no longer counts for DMARC, and mail that relies on SPF alignment starts to fail. Every SaaS sender you add (newsletter, ticketing, CRM, invoicing) usually adds another `include`, so domains cross the limit without anyone noticing.
|
|
27
|
+
|
|
28
|
+
```
|
|
29
|
+
$ spf-lookups example.com
|
|
30
|
+
example.com [9 lookups]
|
|
31
|
+
v=spf1 include:_spf.mail.test include:send.test a mx ip4:192.0.2.1 -all
|
|
32
|
+
include:_spf.mail.test [2 lookups]
|
|
33
|
+
v=spf1 include:_a.mail.test include:_b.mail.test ~all
|
|
34
|
+
include:_a.mail.test [0 lookups]
|
|
35
|
+
v=spf1 ip4:198.51.100.0/24 ~all
|
|
36
|
+
include:_b.mail.test [0 lookups]
|
|
37
|
+
v=spf1 ip6:2001:db8::/32 ~all
|
|
38
|
+
include:send.test [3 lookups]
|
|
39
|
+
v=spf1 exists:%{i}.rbl.send.test redirect=_spf.send.test
|
|
40
|
+
redirect:_spf.send.test [1 lookup]
|
|
41
|
+
v=spf1 a:out.send.test -all
|
|
42
|
+
|
|
43
|
+
DNS lookups: 9 of 10
|
|
44
|
+
Void lookups: 0 of 2
|
|
45
|
+
Result: OK
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Install
|
|
49
|
+
|
|
50
|
+
```
|
|
51
|
+
pip install spf-lookups
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Requires Python 3.9+ and [dnspython](https://www.dnspython.org/).
|
|
55
|
+
|
|
56
|
+
## Usage
|
|
57
|
+
|
|
58
|
+
```
|
|
59
|
+
spf-lookups example.com # tree and totals
|
|
60
|
+
spf-lookups example.com --json # machine-readable
|
|
61
|
+
spf-lookups example.com --timeout 3
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Exit codes, for scripts and CI: `0` within limits · `1` limit exceeded or permerror · `2` no SPF record or DNS error.
|
|
65
|
+
|
|
66
|
+
## What is counted
|
|
67
|
+
|
|
68
|
+
| Term | Lookups |
|
|
69
|
+
|---|---|
|
|
70
|
+
| `include`, `a`, `mx`, `ptr`, `exists`, `redirect` | 1 each, plus everything inside an included or redirected record |
|
|
71
|
+
| `ip4`, `ip6`, `all`, `exp` | 0 |
|
|
72
|
+
| `redirect` when `all` is present | 0 (ignored, RFC 7208 6.1) |
|
|
73
|
+
|
|
74
|
+
It also reports void lookups (NXDOMAIN or empty answers), includes pointing to domains without SPF, more than one SPF record, loops, `ptr` (deprecated) and `mx` targets with more than 10 hosts. Macros such as `%{i}` are counted, but their target can only be resolved by the receiver.
|
|
75
|
+
|
|
76
|
+
## How to get below 10
|
|
77
|
+
|
|
78
|
+
1. Remove services you no longer use.
|
|
79
|
+
2. Take services out of SPF that sign with DKIM in your domain – DMARC passes on aligned DKIM alone.
|
|
80
|
+
3. Move bulk senders to a subdomain with its own SPF record (`news.example.com`).
|
|
81
|
+
4. Flattening (IP addresses instead of `include`) only with monitoring: providers change their ranges without notice.
|
|
82
|
+
|
|
83
|
+
## Web version and DMARC
|
|
84
|
+
|
|
85
|
+
No install needed: the same check runs in the browser at **[igs.ch – SPF lookups](https://www.igs.ch/?p=tool&t=spf)**, next to a DMARC report reader and a DMARC record builder. To check SPF, DKIM, DMARC and MTA-STS of a whole domain at once: **[free DMARC check](https://www.igs.ch/?p=check)**.
|
|
86
|
+
|
|
87
|
+
`spf-lookups` is maintained by [Inbox Guard Switzerland](https://www.igs.ch), a Swiss service that takes domains safely from DMARC `p=none` to `p=reject`.
|
|
88
|
+
|
|
89
|
+
## License
|
|
90
|
+
|
|
91
|
+
MIT
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# spf-lookups
|
|
2
|
+
|
|
3
|
+
Count the DNS lookups of an SPF record – including every nested `include` and `redirect` – and see which entry breaks the limit.
|
|
4
|
+
|
|
5
|
+
RFC 7208 allows at most **10 DNS lookups** and **2 void lookups** per SPF evaluation. Above that, receivers return `permerror`, SPF no longer counts for DMARC, and mail that relies on SPF alignment starts to fail. Every SaaS sender you add (newsletter, ticketing, CRM, invoicing) usually adds another `include`, so domains cross the limit without anyone noticing.
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
$ spf-lookups example.com
|
|
9
|
+
example.com [9 lookups]
|
|
10
|
+
v=spf1 include:_spf.mail.test include:send.test a mx ip4:192.0.2.1 -all
|
|
11
|
+
include:_spf.mail.test [2 lookups]
|
|
12
|
+
v=spf1 include:_a.mail.test include:_b.mail.test ~all
|
|
13
|
+
include:_a.mail.test [0 lookups]
|
|
14
|
+
v=spf1 ip4:198.51.100.0/24 ~all
|
|
15
|
+
include:_b.mail.test [0 lookups]
|
|
16
|
+
v=spf1 ip6:2001:db8::/32 ~all
|
|
17
|
+
include:send.test [3 lookups]
|
|
18
|
+
v=spf1 exists:%{i}.rbl.send.test redirect=_spf.send.test
|
|
19
|
+
redirect:_spf.send.test [1 lookup]
|
|
20
|
+
v=spf1 a:out.send.test -all
|
|
21
|
+
|
|
22
|
+
DNS lookups: 9 of 10
|
|
23
|
+
Void lookups: 0 of 2
|
|
24
|
+
Result: OK
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Install
|
|
28
|
+
|
|
29
|
+
```
|
|
30
|
+
pip install spf-lookups
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Requires Python 3.9+ and [dnspython](https://www.dnspython.org/).
|
|
34
|
+
|
|
35
|
+
## Usage
|
|
36
|
+
|
|
37
|
+
```
|
|
38
|
+
spf-lookups example.com # tree and totals
|
|
39
|
+
spf-lookups example.com --json # machine-readable
|
|
40
|
+
spf-lookups example.com --timeout 3
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
Exit codes, for scripts and CI: `0` within limits · `1` limit exceeded or permerror · `2` no SPF record or DNS error.
|
|
44
|
+
|
|
45
|
+
## What is counted
|
|
46
|
+
|
|
47
|
+
| Term | Lookups |
|
|
48
|
+
|---|---|
|
|
49
|
+
| `include`, `a`, `mx`, `ptr`, `exists`, `redirect` | 1 each, plus everything inside an included or redirected record |
|
|
50
|
+
| `ip4`, `ip6`, `all`, `exp` | 0 |
|
|
51
|
+
| `redirect` when `all` is present | 0 (ignored, RFC 7208 6.1) |
|
|
52
|
+
|
|
53
|
+
It also reports void lookups (NXDOMAIN or empty answers), includes pointing to domains without SPF, more than one SPF record, loops, `ptr` (deprecated) and `mx` targets with more than 10 hosts. Macros such as `%{i}` are counted, but their target can only be resolved by the receiver.
|
|
54
|
+
|
|
55
|
+
## How to get below 10
|
|
56
|
+
|
|
57
|
+
1. Remove services you no longer use.
|
|
58
|
+
2. Take services out of SPF that sign with DKIM in your domain – DMARC passes on aligned DKIM alone.
|
|
59
|
+
3. Move bulk senders to a subdomain with its own SPF record (`news.example.com`).
|
|
60
|
+
4. Flattening (IP addresses instead of `include`) only with monitoring: providers change their ranges without notice.
|
|
61
|
+
|
|
62
|
+
## Web version and DMARC
|
|
63
|
+
|
|
64
|
+
No install needed: the same check runs in the browser at **[igs.ch – SPF lookups](https://www.igs.ch/?p=tool&t=spf)**, next to a DMARC report reader and a DMARC record builder. To check SPF, DKIM, DMARC and MTA-STS of a whole domain at once: **[free DMARC check](https://www.igs.ch/?p=check)**.
|
|
65
|
+
|
|
66
|
+
`spf-lookups` is maintained by [Inbox Guard Switzerland](https://www.igs.ch), a Swiss service that takes domains safely from DMARC `p=none` to `p=reject`.
|
|
67
|
+
|
|
68
|
+
## License
|
|
69
|
+
|
|
70
|
+
MIT
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "spf-lookups"
|
|
7
|
+
version = "1.0.1"
|
|
8
|
+
description = "Count the DNS lookups of an SPF record including all nested includes (RFC 7208 limit of 10)."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = {text = "MIT"}
|
|
11
|
+
authors = [{name = "Inbox Guard Switzerland", email = "media@igs.ch"}]
|
|
12
|
+
requires-python = ">=3.9"
|
|
13
|
+
dependencies = ["dnspython>=2.0"]
|
|
14
|
+
keywords = ["spf", "dmarc", "email", "dns", "rfc7208", "email-security"]
|
|
15
|
+
classifiers = [
|
|
16
|
+
"Programming Language :: Python :: 3",
|
|
17
|
+
"License :: OSI Approved :: MIT License",
|
|
18
|
+
"Operating System :: OS Independent",
|
|
19
|
+
"Topic :: Communications :: Email",
|
|
20
|
+
"Topic :: System :: Networking",
|
|
21
|
+
"Environment :: Console",
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
[project.urls]
|
|
25
|
+
Homepage = "https://github.com/inboxguard-ch/spf-lookups"
|
|
26
|
+
"Web version" = "https://www.igs.ch/?p=tool&t=spf"
|
|
27
|
+
|
|
28
|
+
[project.scripts]
|
|
29
|
+
spf-lookups = "spf_lookups.cli:main"
|
|
30
|
+
|
|
31
|
+
[tool.setuptools.packages.find]
|
|
32
|
+
where = ["src"]
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"""Command line interface: spf-lookups example.com"""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import argparse
|
|
5
|
+
import json
|
|
6
|
+
import sys
|
|
7
|
+
|
|
8
|
+
from . import __version__
|
|
9
|
+
from .core import LOOKUP_LIMIT, VOID_LIMIT, Node, check, dns_resolver
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def _tree(node: Node, prefix: str = "", out=None) -> list:
|
|
13
|
+
out = [] if out is None else out
|
|
14
|
+
label = node.domain if not node.via else f"{node.via}:{node.domain}"
|
|
15
|
+
out.append(f"{prefix}{label} [{node.total()} lookup{'s' if node.total() != 1 else ''}]")
|
|
16
|
+
child_prefix = prefix + " "
|
|
17
|
+
if node.record:
|
|
18
|
+
out.append(f"{child_prefix}{node.record}")
|
|
19
|
+
for c in node.children:
|
|
20
|
+
_tree(c, child_prefix, out)
|
|
21
|
+
return out
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _as_dict(node: Node) -> dict:
|
|
25
|
+
return {
|
|
26
|
+
"domain": node.domain,
|
|
27
|
+
"via": node.via or None,
|
|
28
|
+
"record": node.record,
|
|
29
|
+
"lookups": node.total(),
|
|
30
|
+
"own_lookups": node.lookups,
|
|
31
|
+
"void_lookups": node.total_void(),
|
|
32
|
+
"terms": [{"term": t, "cost": c, "note": n or None} for t, c, n in node.terms],
|
|
33
|
+
"problems": node.problems,
|
|
34
|
+
"children": [_as_dict(c) for c in node.children],
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def main(argv=None) -> int:
|
|
39
|
+
p = argparse.ArgumentParser(
|
|
40
|
+
prog="spf-lookups",
|
|
41
|
+
description="Count the DNS lookups of an SPF record including all nested includes (RFC 7208 limit: 10).",
|
|
42
|
+
epilog="Exit codes: 0 = within limits, 1 = limit exceeded or permerror, 2 = no SPF record or DNS error. "
|
|
43
|
+
"Free web version and DMARC check: https://www.igs.ch/?p=tool&t=spf",
|
|
44
|
+
)
|
|
45
|
+
p.add_argument("domain", help="domain to check, e.g. example.com")
|
|
46
|
+
p.add_argument("--json", action="store_true", help="output JSON")
|
|
47
|
+
p.add_argument("--timeout", type=float, default=5.0, help="DNS timeout per query in seconds (default 5)")
|
|
48
|
+
p.add_argument("--version", action="version", version=f"%(prog)s {__version__}")
|
|
49
|
+
a = p.parse_args(argv)
|
|
50
|
+
|
|
51
|
+
root = check(a.domain, dns_resolver(a.timeout))
|
|
52
|
+
total, void = root.total(), root.total_void()
|
|
53
|
+
problems = root.all_problems()
|
|
54
|
+
if not root.record:
|
|
55
|
+
code = 2
|
|
56
|
+
elif total > LOOKUP_LIMIT or void > VOID_LIMIT or any("permerror" in x or "loop" in x for x in problems):
|
|
57
|
+
code = 1
|
|
58
|
+
else:
|
|
59
|
+
code = 0
|
|
60
|
+
|
|
61
|
+
if a.json:
|
|
62
|
+
print(json.dumps({"domain": root.domain, "lookups": total, "limit": LOOKUP_LIMIT,
|
|
63
|
+
"void_lookups": void, "void_limit": VOID_LIMIT, "ok": code == 0,
|
|
64
|
+
"problems": problems, "tree": _as_dict(root)}, indent=2))
|
|
65
|
+
return code
|
|
66
|
+
|
|
67
|
+
if not root.record:
|
|
68
|
+
print(f"{root.domain}: {problems[0] if problems else 'no SPF record found'}")
|
|
69
|
+
return code
|
|
70
|
+
print("\n".join(_tree(root)))
|
|
71
|
+
print()
|
|
72
|
+
print(f"DNS lookups: {total} of {LOOKUP_LIMIT}" + (" -> LIMIT EXCEEDED (permerror)" if total > LOOKUP_LIMIT else ""))
|
|
73
|
+
print(f"Void lookups: {void} of {VOID_LIMIT}" + (" -> LIMIT EXCEEDED (permerror)" if void > VOID_LIMIT else ""))
|
|
74
|
+
for x in dict.fromkeys(problems):
|
|
75
|
+
print(f"! {x}")
|
|
76
|
+
print("Result: OK" if code == 0 else "Result: FAIL – receivers will treat SPF as permerror")
|
|
77
|
+
return code
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
if __name__ == "__main__":
|
|
81
|
+
sys.exit(main())
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
"""Count the DNS lookups of an SPF record (RFC 7208, section 4.6.4)."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
from dataclasses import dataclass, field
|
|
5
|
+
from typing import Callable, Optional
|
|
6
|
+
|
|
7
|
+
LOOKUP_LIMIT = 10 # RFC 7208 4.6.4: mechanisms/modifiers that cause DNS queries
|
|
8
|
+
VOID_LIMIT = 2 # RFC 7208 4.6.4: lookups returning NXDOMAIN or no answer
|
|
9
|
+
MX_LIMIT = 10 # RFC 7208 4.6.4: at most 10 MX hosts per mx mechanism
|
|
10
|
+
MAX_RECORDS = 40 # safety stop for very large trees
|
|
11
|
+
|
|
12
|
+
LOOKUP_MECHS = ("include", "a", "mx", "ptr", "exists")
|
|
13
|
+
|
|
14
|
+
# A resolver returns (status, answers). status: "ok", "nxdomain", "nodata", "error".
|
|
15
|
+
Resolver = Callable[[str, str], "tuple[str, list[str]]"]
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@dataclass
|
|
19
|
+
class Node:
|
|
20
|
+
domain: str
|
|
21
|
+
via: str = "" # "include", "redirect" or "" for the root
|
|
22
|
+
record: Optional[str] = None
|
|
23
|
+
lookups: int = 0 # lookups caused by terms of this record (not children)
|
|
24
|
+
void: int = 0
|
|
25
|
+
terms: list = field(default_factory=list) # (term, cost, note)
|
|
26
|
+
children: list = field(default_factory=list)
|
|
27
|
+
problems: list = field(default_factory=list)
|
|
28
|
+
|
|
29
|
+
def total(self) -> int:
|
|
30
|
+
return self.lookups + sum(c.total() for c in self.children)
|
|
31
|
+
|
|
32
|
+
def total_void(self) -> int:
|
|
33
|
+
return self.void + sum(c.total_void() for c in self.children)
|
|
34
|
+
|
|
35
|
+
def all_problems(self) -> list:
|
|
36
|
+
out = list(self.problems)
|
|
37
|
+
for c in self.children:
|
|
38
|
+
out += c.all_problems()
|
|
39
|
+
return out
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def dns_resolver(timeout: float = 5.0) -> Resolver:
|
|
43
|
+
import dns.resolver
|
|
44
|
+
import dns.exception
|
|
45
|
+
|
|
46
|
+
r = dns.resolver.Resolver()
|
|
47
|
+
r.lifetime = timeout
|
|
48
|
+
|
|
49
|
+
def resolve(name: str, rtype: str):
|
|
50
|
+
try:
|
|
51
|
+
ans = r.resolve(name, rtype)
|
|
52
|
+
except dns.resolver.NXDOMAIN:
|
|
53
|
+
return "nxdomain", []
|
|
54
|
+
except dns.resolver.NoAnswer:
|
|
55
|
+
return "nodata", []
|
|
56
|
+
except (dns.exception.DNSException, OSError):
|
|
57
|
+
return "error", []
|
|
58
|
+
if rtype == "TXT":
|
|
59
|
+
return "ok", [b"".join(rd.strings).decode("utf-8", "replace") for rd in ans]
|
|
60
|
+
if rtype == "MX":
|
|
61
|
+
return "ok", [str(rd.exchange).rstrip(".") for rd in ans]
|
|
62
|
+
return "ok", [rd.to_text() for rd in ans]
|
|
63
|
+
|
|
64
|
+
return resolve
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def _spf_records(txts: list) -> list:
|
|
68
|
+
return [t.strip() for t in txts if t.strip().lower() == "v=spf1" or t.strip().lower().startswith("v=spf1 ")]
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def _target(arg: str, current: str) -> str:
|
|
72
|
+
return (arg or current).split("/")[0].rstrip(".").lower()
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
class Counter:
|
|
76
|
+
def __init__(self, resolve: Resolver):
|
|
77
|
+
self.resolve = resolve
|
|
78
|
+
self.seen: set = set()
|
|
79
|
+
self.records = 0
|
|
80
|
+
|
|
81
|
+
def check(self, domain: str) -> Node:
|
|
82
|
+
return self._walk(domain.rstrip(".").lower(), "", 0)
|
|
83
|
+
|
|
84
|
+
def _void(self, node: Node, status: str, what: str):
|
|
85
|
+
if status in ("nxdomain", "nodata"):
|
|
86
|
+
node.void += 1
|
|
87
|
+
node.problems.append(f"void lookup: {what} ({status})")
|
|
88
|
+
elif status == "error":
|
|
89
|
+
node.problems.append(f"DNS error: {what}")
|
|
90
|
+
|
|
91
|
+
def _walk(self, domain: str, via: str, depth: int) -> Node:
|
|
92
|
+
node = Node(domain, via)
|
|
93
|
+
if domain in self.seen:
|
|
94
|
+
node.problems.append(f"loop: {domain} is referenced again")
|
|
95
|
+
return node
|
|
96
|
+
if depth > 10 or self.records >= MAX_RECORDS:
|
|
97
|
+
node.problems.append("tree too large, counting stopped")
|
|
98
|
+
return node
|
|
99
|
+
self.seen.add(domain)
|
|
100
|
+
self.records += 1
|
|
101
|
+
|
|
102
|
+
status, txts = self.resolve(domain, "TXT")
|
|
103
|
+
recs = _spf_records(txts)
|
|
104
|
+
if not recs:
|
|
105
|
+
if status in ("nxdomain", "nodata", "ok"):
|
|
106
|
+
if depth > 0:
|
|
107
|
+
node.void += 1 if status != "ok" else 0
|
|
108
|
+
node.problems.append(f"{domain} has no SPF record" + (" (permerror)" if depth > 0 else ""))
|
|
109
|
+
else:
|
|
110
|
+
node.problems.append(f"DNS error reading TXT of {domain}")
|
|
111
|
+
return node
|
|
112
|
+
if len(recs) > 1:
|
|
113
|
+
node.problems.append(f"{domain} has {len(recs)} SPF records (permerror)")
|
|
114
|
+
node.record = recs[0]
|
|
115
|
+
|
|
116
|
+
terms = recs[0].split()[1:]
|
|
117
|
+
has_all = any(t.lstrip("+-~?").lower() == "all" for t in terms)
|
|
118
|
+
redirect = None
|
|
119
|
+
for raw in terms:
|
|
120
|
+
t = raw.lower()
|
|
121
|
+
mech = t.lstrip("+-~?")
|
|
122
|
+
name, sep, arg = mech.partition(":") if ":" in mech.split("/")[0] else (mech.split("/")[0], "", "")
|
|
123
|
+
if mech.startswith("redirect="):
|
|
124
|
+
redirect = raw.split("=", 1)[1]
|
|
125
|
+
continue
|
|
126
|
+
if name not in LOOKUP_MECHS:
|
|
127
|
+
node.terms.append((raw, 0, ""))
|
|
128
|
+
continue
|
|
129
|
+
node.lookups += 1
|
|
130
|
+
if "%" in arg:
|
|
131
|
+
node.terms.append((raw, 1, "macro, target known only at the receiver"))
|
|
132
|
+
continue
|
|
133
|
+
if name == "include":
|
|
134
|
+
node.terms.append((raw, 1, ""))
|
|
135
|
+
node.children.append(self._walk(_target(arg, domain), "include", depth + 1))
|
|
136
|
+
elif name == "a":
|
|
137
|
+
tgt = _target(arg, domain)
|
|
138
|
+
st4, a4 = self.resolve(tgt, "A")
|
|
139
|
+
st6, a6 = self.resolve(tgt, "AAAA")
|
|
140
|
+
if not a4 and not a6:
|
|
141
|
+
self._void(node, st4 if st4 != "ok" else st6, f"a → {tgt}")
|
|
142
|
+
node.terms.append((raw, 1, ""))
|
|
143
|
+
elif name == "mx":
|
|
144
|
+
tgt = _target(arg, domain)
|
|
145
|
+
st, hosts = self.resolve(tgt, "MX")
|
|
146
|
+
note = ""
|
|
147
|
+
if not hosts:
|
|
148
|
+
self._void(node, st, f"mx → {tgt}")
|
|
149
|
+
elif len(hosts) > MX_LIMIT:
|
|
150
|
+
node.problems.append(f"mx → {tgt} has {len(hosts)} MX hosts (more than {MX_LIMIT}, permerror)")
|
|
151
|
+
else:
|
|
152
|
+
note = f"{len(hosts)} MX host(s)"
|
|
153
|
+
node.terms.append((raw, 1, note))
|
|
154
|
+
elif name == "ptr":
|
|
155
|
+
node.terms.append((raw, 1, "ptr is deprecated (RFC 7208 5.5)"))
|
|
156
|
+
node.problems.append("ptr mechanism should not be used")
|
|
157
|
+
elif name == "exists":
|
|
158
|
+
node.terms.append((raw, 1, ""))
|
|
159
|
+
if redirect is not None:
|
|
160
|
+
if has_all:
|
|
161
|
+
node.terms.append(("redirect=" + redirect, 0, "ignored because 'all' is present"))
|
|
162
|
+
else:
|
|
163
|
+
node.lookups += 1
|
|
164
|
+
node.terms.append(("redirect=" + redirect, 1, ""))
|
|
165
|
+
if "%" not in redirect:
|
|
166
|
+
node.children.append(self._walk(_target(redirect, domain), "redirect", depth + 1))
|
|
167
|
+
return node
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def check(domain: str, resolve: Optional[Resolver] = None) -> Node:
|
|
171
|
+
return Counter(resolve or dns_resolver()).check(domain)
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: spf-lookups
|
|
3
|
+
Version: 1.0.1
|
|
4
|
+
Summary: Count the DNS lookups of an SPF record including all nested includes (RFC 7208 limit of 10).
|
|
5
|
+
Author-email: Inbox Guard Switzerland <media@igs.ch>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/inboxguard-ch/spf-lookups
|
|
8
|
+
Project-URL: Web version, https://www.igs.ch/?p=tool&t=spf
|
|
9
|
+
Keywords: spf,dmarc,email,dns,rfc7208,email-security
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Topic :: Communications :: Email
|
|
14
|
+
Classifier: Topic :: System :: Networking
|
|
15
|
+
Classifier: Environment :: Console
|
|
16
|
+
Requires-Python: >=3.9
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
License-File: LICENSE
|
|
19
|
+
Requires-Dist: dnspython>=2.0
|
|
20
|
+
Dynamic: license-file
|
|
21
|
+
|
|
22
|
+
# spf-lookups
|
|
23
|
+
|
|
24
|
+
Count the DNS lookups of an SPF record – including every nested `include` and `redirect` – and see which entry breaks the limit.
|
|
25
|
+
|
|
26
|
+
RFC 7208 allows at most **10 DNS lookups** and **2 void lookups** per SPF evaluation. Above that, receivers return `permerror`, SPF no longer counts for DMARC, and mail that relies on SPF alignment starts to fail. Every SaaS sender you add (newsletter, ticketing, CRM, invoicing) usually adds another `include`, so domains cross the limit without anyone noticing.
|
|
27
|
+
|
|
28
|
+
```
|
|
29
|
+
$ spf-lookups example.com
|
|
30
|
+
example.com [9 lookups]
|
|
31
|
+
v=spf1 include:_spf.mail.test include:send.test a mx ip4:192.0.2.1 -all
|
|
32
|
+
include:_spf.mail.test [2 lookups]
|
|
33
|
+
v=spf1 include:_a.mail.test include:_b.mail.test ~all
|
|
34
|
+
include:_a.mail.test [0 lookups]
|
|
35
|
+
v=spf1 ip4:198.51.100.0/24 ~all
|
|
36
|
+
include:_b.mail.test [0 lookups]
|
|
37
|
+
v=spf1 ip6:2001:db8::/32 ~all
|
|
38
|
+
include:send.test [3 lookups]
|
|
39
|
+
v=spf1 exists:%{i}.rbl.send.test redirect=_spf.send.test
|
|
40
|
+
redirect:_spf.send.test [1 lookup]
|
|
41
|
+
v=spf1 a:out.send.test -all
|
|
42
|
+
|
|
43
|
+
DNS lookups: 9 of 10
|
|
44
|
+
Void lookups: 0 of 2
|
|
45
|
+
Result: OK
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Install
|
|
49
|
+
|
|
50
|
+
```
|
|
51
|
+
pip install spf-lookups
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Requires Python 3.9+ and [dnspython](https://www.dnspython.org/).
|
|
55
|
+
|
|
56
|
+
## Usage
|
|
57
|
+
|
|
58
|
+
```
|
|
59
|
+
spf-lookups example.com # tree and totals
|
|
60
|
+
spf-lookups example.com --json # machine-readable
|
|
61
|
+
spf-lookups example.com --timeout 3
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Exit codes, for scripts and CI: `0` within limits · `1` limit exceeded or permerror · `2` no SPF record or DNS error.
|
|
65
|
+
|
|
66
|
+
## What is counted
|
|
67
|
+
|
|
68
|
+
| Term | Lookups |
|
|
69
|
+
|---|---|
|
|
70
|
+
| `include`, `a`, `mx`, `ptr`, `exists`, `redirect` | 1 each, plus everything inside an included or redirected record |
|
|
71
|
+
| `ip4`, `ip6`, `all`, `exp` | 0 |
|
|
72
|
+
| `redirect` when `all` is present | 0 (ignored, RFC 7208 6.1) |
|
|
73
|
+
|
|
74
|
+
It also reports void lookups (NXDOMAIN or empty answers), includes pointing to domains without SPF, more than one SPF record, loops, `ptr` (deprecated) and `mx` targets with more than 10 hosts. Macros such as `%{i}` are counted, but their target can only be resolved by the receiver.
|
|
75
|
+
|
|
76
|
+
## How to get below 10
|
|
77
|
+
|
|
78
|
+
1. Remove services you no longer use.
|
|
79
|
+
2. Take services out of SPF that sign with DKIM in your domain – DMARC passes on aligned DKIM alone.
|
|
80
|
+
3. Move bulk senders to a subdomain with its own SPF record (`news.example.com`).
|
|
81
|
+
4. Flattening (IP addresses instead of `include`) only with monitoring: providers change their ranges without notice.
|
|
82
|
+
|
|
83
|
+
## Web version and DMARC
|
|
84
|
+
|
|
85
|
+
No install needed: the same check runs in the browser at **[igs.ch – SPF lookups](https://www.igs.ch/?p=tool&t=spf)**, next to a DMARC report reader and a DMARC record builder. To check SPF, DKIM, DMARC and MTA-STS of a whole domain at once: **[free DMARC check](https://www.igs.ch/?p=check)**.
|
|
86
|
+
|
|
87
|
+
`spf-lookups` is maintained by [Inbox Guard Switzerland](https://www.igs.ch), a Swiss service that takes domains safely from DMARC `p=none` to `p=reject`.
|
|
88
|
+
|
|
89
|
+
## License
|
|
90
|
+
|
|
91
|
+
MIT
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
src/spf_lookups/__init__.py
|
|
5
|
+
src/spf_lookups/__main__.py
|
|
6
|
+
src/spf_lookups/cli.py
|
|
7
|
+
src/spf_lookups/core.py
|
|
8
|
+
src/spf_lookups.egg-info/PKG-INFO
|
|
9
|
+
src/spf_lookups.egg-info/SOURCES.txt
|
|
10
|
+
src/spf_lookups.egg-info/dependency_links.txt
|
|
11
|
+
src/spf_lookups.egg-info/entry_points.txt
|
|
12
|
+
src/spf_lookups.egg-info/requires.txt
|
|
13
|
+
src/spf_lookups.egg-info/top_level.txt
|
|
14
|
+
tests/test_core.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
dnspython>=2.0
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
spf_lookups
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import json
|
|
2
|
+
|
|
3
|
+
from spf_lookups import cli
|
|
4
|
+
from spf_lookups.core import check
|
|
5
|
+
|
|
6
|
+
ZONE = {
|
|
7
|
+
("example.com", "TXT"): ("ok", ["google-site-verification=abc", "v=spf1 include:_spf.mail.test include:send.test a mx ip4:192.0.2.1 -all"]),
|
|
8
|
+
("_spf.mail.test", "TXT"): ("ok", ["v=spf1 include:_a.mail.test include:_b.mail.test ~all"]),
|
|
9
|
+
("_a.mail.test", "TXT"): ("ok", ["v=spf1 ip4:198.51.100.0/24 ~all"]),
|
|
10
|
+
("_b.mail.test", "TXT"): ("ok", ["v=spf1 ip6:2001:db8::/32 ~all"]),
|
|
11
|
+
("send.test", "TXT"): ("ok", ["v=spf1 exists:%{i}.rbl.send.test redirect=_spf.send.test"]),
|
|
12
|
+
("_spf.send.test", "TXT"): ("ok", ["v=spf1 a:out.send.test -all"]),
|
|
13
|
+
("out.send.test", "A"): ("ok", ["203.0.113.5"]),
|
|
14
|
+
("out.send.test", "AAAA"): ("nodata", []),
|
|
15
|
+
("example.com", "A"): ("ok", ["192.0.2.10"]),
|
|
16
|
+
("example.com", "AAAA"): ("nodata", []),
|
|
17
|
+
("example.com", "MX"): ("ok", ["mx1.example.com", "mx2.example.com"]),
|
|
18
|
+
# too many
|
|
19
|
+
("big.test", "TXT"): ("ok", ["v=spf1 " + " ".join(f"include:i{n}.test" for n in range(11)) + " -all"]),
|
|
20
|
+
# void + loop + missing
|
|
21
|
+
("bad.test", "TXT"): ("ok", ["v=spf1 include:gone.test include:nospf.test a:nohost.test include:bad.test ptr -all"]),
|
|
22
|
+
("gone.test", "TXT"): ("nxdomain", []),
|
|
23
|
+
("nospf.test", "TXT"): ("ok", ["something else"]),
|
|
24
|
+
("nohost.test", "A"): ("nxdomain", []),
|
|
25
|
+
("nohost.test", "AAAA"): ("nxdomain", []),
|
|
26
|
+
("two.test", "TXT"): ("ok", ["v=spf1 -all", "v=spf1 ~all"]),
|
|
27
|
+
("redir.test", "TXT"): ("ok", ["v=spf1 redirect=_spf.send.test ~all"]),
|
|
28
|
+
}
|
|
29
|
+
for n in range(11):
|
|
30
|
+
ZONE[(f"i{n}.test", "TXT")] = ("ok", ["v=spf1 ip4:192.0.2.0/24 ~all"])
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def fake(name, rtype):
|
|
34
|
+
return ZONE.get((name, rtype), ("nxdomain", []))
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def test_counts_nested_includes_redirect_and_macros():
|
|
38
|
+
r = check("example.com", fake)
|
|
39
|
+
# include, include, a, mx (4) + _spf.mail.test: 2 + send.test: exists + redirect (2) + _spf.send.test: a (1)
|
|
40
|
+
assert r.total() == 9
|
|
41
|
+
assert r.total_void() == 0
|
|
42
|
+
assert [c.domain for c in r.children] == ["_spf.mail.test", "send.test"]
|
|
43
|
+
assert r.children[1].children[0].via == "redirect"
|
|
44
|
+
assert r.all_problems() == []
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def test_over_limit():
|
|
48
|
+
r = check("big.test", fake)
|
|
49
|
+
assert r.total() == 11
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def test_void_loop_missing_ptr():
|
|
53
|
+
r = check("bad.test", fake)
|
|
54
|
+
p = " | ".join(r.all_problems())
|
|
55
|
+
assert r.total_void() == 2 # gone.test (nxdomain) and a:nohost.test
|
|
56
|
+
assert "loop" in p and "ptr" in p and "nospf.test has no SPF record" in p
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def test_multiple_records_and_redirect_ignored_with_all():
|
|
60
|
+
assert "2 SPF records" in check("two.test", fake).problems[0]
|
|
61
|
+
r = check("redir.test", fake)
|
|
62
|
+
assert r.total() == 0 and r.children == []
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def test_cli_exit_codes_and_json(monkeypatch, capsys):
|
|
66
|
+
monkeypatch.setattr(cli, "dns_resolver", lambda timeout: fake)
|
|
67
|
+
assert cli.main(["example.com"]) == 0
|
|
68
|
+
assert "DNS lookups: 9 of 10" in capsys.readouterr().out
|
|
69
|
+
assert cli.main(["big.test"]) == 1
|
|
70
|
+
capsys.readouterr()
|
|
71
|
+
assert cli.main(["nothing.test", "--json"]) == 2
|
|
72
|
+
d = json.loads(capsys.readouterr().out)
|
|
73
|
+
assert d["ok"] is False and d["lookups"] == 0
|