The SPF 10 DNS Lookup Limit: Permerror and a Lasting Fix

Why does an SPF record give a “too many DNS lookups” error? The 10 lookup and void lookup limits in RFC 7208, counting nested includes, the effect of permerror on DMARC and lasting fixes.
Technical summary
Technology
SPF, RFC 7208, DMARC, DNS, dnspython
Topic
Diagnosing and fixing permerror caused by the SPF lookup limit
Level
Intermediate
Prerequisites
  • Knowledge of DNS record types and TXT records
  • Access to the domain’s DNS management
  • A list of services that send email
In this article8 sections

An include is added to the SPF record for every new SaaS service, and one day the record silently breaks: evaluation exceeds 10 DNS lookups and receiving servers return permerror. It looks as if nobody has touched DNS, because a change in the provider’s own record increased the lookup count. This guide explains the lookup and void lookup limits in RFC 7208, how to calculate the count, and lasting ways to fix an error caused by an include chain.

How SPF evaluation works

The receiving server evaluates SPF not against the From header the user sees but against the envelope sender in the SMTP session (MAIL FROM, Return-Path in the headers). If MAIL FROM is empty (bounce messages), the HELO/EHLO name is used. The order is:

  1. The TXT records of the MAIL FROM domain are queried, and the record starting with v=spf1 is selected. If there’s more than one SPF record, the result is directly permerror.
  2. Mechanisms are evaluated left to right; the connecting IP gets its result at the first matching mechanism (+ pass, - fail, ~ softfail, ? neutral).
  3. If no mechanism matches and there’s a redirect=, evaluation continues with the target domain’s record; otherwise the result is neutral.

include: runs the target domain’s SPF record as a separate evaluation; if the target returns pass, the include counts as matched. The overlooked detail: if the included domain has no SPF record, the result isn’t none but permerror. The include of a provider whose contract has ended and who has shut down its domain breaks your whole record. One line, the whole record.

The 10 lookup and void lookup limits

Section 4.6.4 of RFC 7208 sets three limits to cap the receiver’s resource consumption:

Limit Scope When exceeded
10 DNS lookups include, a, mx, ptr, exists, redirect (including nested ones) permerror
10 address lookups per mechanism A/AAAA lookups for MX/PTR names returned in an mx or ptr evaluation permerror for mx; for ptr, names beyond the first 10 are ignored
2 void lookups Lookups returning an empty answer or NXDOMAIN (the RFC recommends this at “SHOULD” level) permerror

ip4, ip6 and all don’t require a DNS lookup and don’t count. The exp= modifier is used only for explanation text on fail and doesn’t count toward the limit. The source of the problem is that the limit covers nested includes too. You see four includes in your own record; each one may call other includes internally, and you can’t see that count in the DNS panel.

The void lookup limit is usually exceeded by forgotten records: an a:old-server.example.com pointing to a removed server, or an include to a domain that no longer exists. RFC 7208 explicitly discourages the ptr mechanism; it’s slow and unreliable. If it’s in your record, remove it.

Calculating the lookup count

Let’s look at the example record below (the domain names are illustrative):

example.com.  IN  TXT  "v=spf1 mx a include:spf.protection.outlook.com include:_spf.crm.example.net include:mail.marketing.example.net include:spf.invoice.example.net ip4:203.0.113.10 ~all"

There are 6 lookups at the top level: mx, a and four includes. Then you go down into the contents of each include separately. In the example calculation, if the CRM record calls two more includes and the marketing record three more (assuming the others have no extra lookups), the total is 6 + 2 + 3 = 11 and the record produces permerror. To trace the chain by hand, query them one by one:

dig +short TXT example.com | grep -i spf1
dig +short TXT _spf.crm.example.net
Resolve-DnsName -Name mail.marketing.example.net -Type TXT

For regular auditing, a small script is more practical. The Python example below uses the dnspython package, prints the SPF chain and adds up the counted lookups; it counts targets containing macros (such as %{i}) but doesn’t try to resolve them:

import sys
import dns.resolver

def spf_record(domain):
    for r in dns.resolver.resolve(domain, "TXT"):
        text = b"".join(r.strings).decode()
        if text.lower().startswith("v=spf1"):
            return text
    raise ValueError(domain + ": no SPF record")

def count(domain, depth=0):
    record = spf_record(domain)
    print("  " * depth + domain + " -> " + record)
    total = 0
    for term in record.split()[1:]:
        t = term.lstrip("+-~?").lower()
        if t.startswith(("include:", "redirect=")):
            target = t.split(":", 1)[1] if t.startswith("include:") else t.split("=", 1)[1]
            total += 1
            if "%" not in target:
                total += count(target, depth + 1)
        elif t in ("a", "mx", "ptr") or t.startswith(("a:", "a/", "mx:", "mx/", "ptr:", "exists:")):
            total += 1
    return total

print("Total counted DNS lookups:", count(sys.argv[1]))

Compare the result with an external SPF validation tool; two different methods giving the same number is a good cross-check. Providers’ records change over time; a count of 9 today may be 11 in a few months. Measurement isn’t a one-off job. Put it on the calendar.

The effect of permerror on delivery and DMARC

What a server that receives a permerror does depends on its local policy. Some reject the message, some raise the spam score, some ignore the SPF result. That’s why the symptoms arrive scattered: an invoice doesn’t reach one customer but reaches another. The support team looks first at the user’s mailbox, then at the recipient’s filter; DNS comes to mind last.

From a DMARC standpoint, permerror means SPF didn’t return pass. If the message carries a valid DKIM signature aligned with the From domain, DMARC still passes; if not, DMARC fails and the p=quarantine or p=reject policy is applied. That’s why permerror has much more visible consequences in organizations that have moved DMARC to enforcement. If you see permerror in the SPF result in aggregate reports, you’re already experiencing the problem. For the general plan of the policy migration, see the moving DMARC from p=none to p=reject guide.

Comparing the fix options

Before starting a fix, one question: is this include really needed? SPF is evaluated against the MAIL FROM domain. If a marketing service sends messages with its own bounce domain, the include in your record has no effect at all on that service’s SPF result; it just eats your lookup budget. This kind of include comes up often in records audited in the field. The Return-Path value in the header of a real message from the service answers the question.

Method When it fits Limitation
Cleaning up unnecessary includes Unused services, or services that use their own Return-Path Requires an inventory and header checks
ip4/ip6 instead of a/mx Your own servers with fixed IPs The record must be updated when the IP changes
Splitting off to a subdomain If the service supports a custom Return-Path (e.g. bounce.example.com) Each subdomain’s SPF is managed separately
Manual flattening Small services whose IP ranges rarely change If you miss a provider change, the record goes stale
Automatic flattening / macro services Many services, frequently changing ranges Your SPF becomes dependent on a third-party service

In most organizations, the cleanest fix is splitting off to a subdomain. When the marketing service uses a subdomain such as bounce.example.com as the Return-Path, SPF is evaluated against that subdomain’s own record; it doesn’t touch the main record’s budget. Because the subdomain counts as aligned with the main domain in DMARC relaxed alignment (aspf=r), DMARC passes too:

example.com.         IN  TXT  "v=spf1 include:spf.protection.outlook.com ip4:203.0.113.10 ~all"
bounce.example.com.  IN  TXT  "v=spf1 include:mail.marketing.example.net -all"

One more detail: a single TXT string can’t exceed 255 characters. Long records are split into multiple quoted strings, and the receiver joins them without inserting spaces. If you don’t leave a space at the split point, two mechanisms stick together and a new error is born.

Choosing ~all or -all

-all (fail) states definitively that sources not on the list are unauthorized; ~all (softfail) says “most likely unauthorized”. In environments without DMARC, this difference was decisive. When DMARC is at enforcement, the difference narrows because DMARC makes the final decision, but one side effect remains: some receivers reject the message when they see SPF fail, before getting to DMARC. A legitimate message that was forwarded with an intact DKIM signature is lost this way; RFC 7489 section 10.1 explicitly mentions this risk.

A practical approach: ~all together with DMARC p=reject on domains that send email, and v=spf1 -all on domains that never send email. Whichever you choose, don’t use +all or ?all. The first authorizes everyone. The second leaves SPF useless as an enterprise cybersecurity control.

SPF maintenance checklist

  • Verify there’s a single v=spf1 record per domain.
  • Measure the total lookup count including nested ones; for records approaching 10, make room before adding a new service.
  • Record the owning department and the service’s Return-Path behavior for each include.
  • Remove the includes of services that use their own Return-Path.
  • Clean up a:, mx: and include targets that return NXDOMAIN or an empty answer.
  • Remove the ptr mechanism.
  • Delete the include and DKIM records of services whose contracts have ended in the same change.
  • Monitor SPF permerror and temperror results in DMARC reports weekly.
  • For flattened records, assign someone to follow the provider’s IP range announcements.

To interpret the SPF result in a single suspicious message reported by a user, see the Authentication-Results section in the suspicious email header analysis guide. The reference text for the protocol is RFC 7208. In setups with multiple domains and many sending services, bulk auditing of the records can be handled as part of an email security and phishing protection project.

Frequently asked questions

Do the ip4 and ip6 mechanisms count toward the 10 lookup limit?

No. ip4, ip6 and all don’t require a DNS lookup and don’t count toward the limit. What counts are the include, a, mx, ptr and exists mechanisms and the redirect modifier. These terms inside included records are added to the total too.

If I remove a service’s include from SPF, will its emails be affected?

If the service sends messages with its own bounce domain, that is, its own Return-Path, SPF is evaluated against its record; removing the include doesn’t affect delivery. If the Return-Path is your domain, the include is needed. Before deciding, look at the Return-Path value in the header of a real message from the service. If delivery problems continue for messages leaving through Microsoft 365, compare the message trace and the rejection code using the Exchange Online mail flow diagnosis steps.

Is SPF flattening safe?

It’s technically valid. But when the provider changes its IP range, your record goes stale and legitimate emails fail SPF. Manual flattening is only suitable for services whose ranges rarely change and that announce changes. With automatic flattening, a tool updates your DNS record; you become dependent on that tool’s reliability.

Can DMARC still pass when there’s a permerror?

Yes. For DMARC, it’s enough for either the aligned SPF or the aligned DKIM result to be pass. Even if SPF returns permerror, DMARC passes if the message carries a valid DKIM signature aligned with the From domain. That’s why a permerror sometimes goes unnoticed for months and only surfaces as a delivery problem for sources that don’t sign with DKIM.

Support

Contact us for support or a discovery session on this topic.

Contact form
[email protected]

Eretek Cybersecurity

Firewall, VPN, endpoint security and threat prevention. We reduce the attack surface and measure and report the risks.

Knowledge Center

Other articles