skip to content

An SPF record started returning permerror after a sixth hosted sending service was added; how do you count its DNS lookups by hand?

level: seniorimportance: should knowfreq 26%

answer

  1. resolve it like a receiver
  2. open every include, recursively
  3. read live DNS, not the docs
  4. ten is allowed, eleven is not
  5. first match stops the walk

basics

~20 s

Resolve the record as a receiver would: count each include, a, mx, ptr, exists and redirect, then open every include and add its counted terms, recursively, ignoring ip4, ip6, all and exp. A total above ten is the permerror.

solid answer

~40 s

I fetch the domain's `v=spf1` record from live DNS and walk it left to right, scoring one for every `include:`, `a`, `mx`, `ptr`, `exists` and `redirect=`, and nothing for `ip4:`, `ip6:`, `all` or `exp=`. For each `include:` I fetch the target record and score its counted terms the same way, recursively, because RFC 7208 tracks one global limit of ten for the whole check. Ten is allowed; the eleventh counted term is the `permerror`. In a typical case the sixth service's include is cheap, but an earlier service's include hides three nested includes, which is what tips the total to eleven. The breakage can look partial: a receiver counting as it evaluates stops at the first match, so mail matched by an early term may still pass while mail from the newest service fails.

code

dns · 13 lines
dns
; example.com after the sixth hosted sending service was added
example.com.             TXT "v=spf1 mx ip4:192.0.2.0/25 include:_spf.example.net include:spf.crm.example.org include:send.example.org include:bulk.example.net include:mail.survey.example.org -all"

; what each included name publishes, read from live DNS
_spf.example.net.        TXT "v=spf1 include:_nb1.example.net include:_nb2.example.net include:_nb3.example.net ~all"
_nb1.example.net.        TXT "v=spf1 ip4:198.51.100.0/26 ~all"
_nb2.example.net.        TXT "v=spf1 ip4:198.51.100.64/26 ~all"
_nb3.example.net.        TXT "v=spf1 ip6:2001:db8:20::/48 ~all"
spf.crm.example.org.     TXT "v=spf1 ip4:203.0.113.0/26 ip6:2001:db8:10::/48 -all"
send.example.org.        TXT "v=spf1 a:out.send.example.org include:_ips.send.example.org ~all"
_ips.send.example.org.   TXT "v=spf1 ip4:203.0.113.64/26 ~all"
bulk.example.net.        TXT "v=spf1 ip4:203.0.113.128/26 ~all"
mail.survey.example.org. TXT "v=spf1 ip4:203.0.113.192/27 ~all"

go deeper

for a junior

Recall the procedure: fetch the record, score each counted term, open every include and add its terms. Ten passes; eleven is permerror.

for a middle

Be ready to walk a short record aloud and say why nested includes join your count and why ip4, ip6 and all score nothing.

for a senior

Show that you read the nested records from live DNS, explain why breakage hits the senders listed last, and script the count so it alerts before receivers notice.

for a principal

Use the count as an input to deciding which services may send as the main domain and which belong on their own envelope subdomain.

## The situation A company sends mail as `example.com` from its own servers and from a growing set of **hosted sending services**: a CRM, a ticketing desk, a bulk-mail service and so on. Each service's onboarding page said "add our include". After the sixth was added, receivers began recording SPF `permerror` for some of the company's mail. The suspect is the **10-lookup limit** of RFC 7208 section 4.6.4, and the way to confirm it is to count the record by hand, the way a receiver would. ## The counting procedure 1. **Fetch the published record from live DNS**, not from a ticket or a vendor's setup guide. Only the record beginning `v=spf1` matters. 2. **Walk its terms left to right.** Score 1 for each `include:`, `a`, `mx`, `ptr` and `exists` mechanism and for a `redirect=` modifier. Score 0 for `ip4:`, `ip6:`, `all` and `exp=`. 3. **Open every `include:`** (and a followed `redirect=`): fetch the target's record and score its terms the same way. 4. **Recurse** until every branch ends in records that hold only free terms. 5. **Add everything up.** RFC 7208 says the limit is "a single global limit for all evaluations", so nested terms are part of your total. Ten is within the limit; the eleventh counted term produces `permerror`. 6. **Note the side limits** while you are there: each `mx` or `ptr` term may query at most 10 A or AAAA records, and any name that returns NXDOMAIN or an empty answer is a void lookup, of which verifiers SHOULD allow only two. ## The tally for this record Using the record in the example (the company's record plus what each included name publishes): | Term in the record | Own cost | Nested cost | Running total | |---|---|---|---| | `mx` | 1 | 0 | 1 | | `ip4:192.0.2.0/25` | 0 | 0 | 1 | | `include:_spf.example.net` | 1 | 3 (three nested includes) | 5 | | `include:spf.crm.example.org` | 1 | 0 (addresses only) | 6 | | `include:send.example.org` | 1 | 2 (an `a` and an include) | 9 | | `include:bulk.example.net` | 1 | 0 | 10 | | `include:mail.survey.example.org` | 1 | 0 | **11** | | `-all` | 0 | 0 | 11 | The fifth service brought the record to exactly ten, which is still legal. The sixth service's include is cheap on its own, yet it is the eleventh lookup. The real weight sits earlier, in a service whose include fans out into three more. ## Why the breakage looks partial SPF evaluates mechanisms **left to right and stops at the first match**. A receiver that counts lookups as it evaluates only reaches the eleventh term when nothing before it matched: - Mail from the company's own MX hosts matches at the first term and spends one lookup, so it can still pass. - Mail from the services listed early matches before the count runs out. - Mail from the service at the end of the record needs all eleven lookups, so its check hits the limit and returns `permerror`. - Mail from a host that matches nothing also walks to the end and gets `permerror` rather than the `fail` the `-all` would have given. The symptom therefore lands on the newest sender, which is why teams often blame its include first, when the count is a property of the whole tree. ## Checks that catch it before receivers do - **Re-count on every change** to the sender list, and whenever a service announces new infrastructure, because a service's nested records can grow without any edit to your zone. - **Script the count** against live DNS so it runs on a schedule and alerts at, say, eight or nine, leaving headroom. - **Check the receiving side's evidence**: the SPF result your own test messages receive shows `permerror` before any user notices bounces. - **Watch for void lookups** from names that no longer resolve; they reach a separate limit of two. ## Getting back under ten Once the count is confirmed, the options are, roughly in order of cost: - remove includes for services the company no longer uses; - replace `mx` or `a` for the company's own fixed hosts with `ip4:` and `ip6:`, which RFC 7208 section 10.1.2 recommends anyway; - move one high-volume service to its own envelope subdomain with its own record; - flatten an include into literal ranges, which removes lookups but must then be kept in step with the service's addresses. Which of those a team chooses is a judgment call in its own right. The diagnosis itself is mechanical: count the tree, and the number tells you whether the limit is the cause.

  • Why read the nested records from live DNS instead of each service's setup guide?
    Because the nested records belong to the service and change on its schedule. A guide written when you onboarded may list one include that now fans out into three. Only the records a receiver fetches today decide the count, so the count has to be taken from DNS, and repeated.
  • If one service's mail passes SPF at the same receiver that gives another permerror, does that rule the limit out?
    No. A receiver counting as it evaluates stops at the first matching term, so a sender matched early spends few lookups and passes, while a sender listed after the tenth counted term gets permerror. Mixed results across senders are a typical sign of an over-limit record, not evidence against it.

  • Count only the includes in our own record; the vendors' records are their concern.
  • The ip4 ranges inside an included record add to the lookup count.
  • One service's mail passes, so the record cannot be over the limit.
  • The service's setup guide says its include costs one lookup, so it does.
  • Removing the -all at the end would bring the lookup count down.