Dev.to Security 🔐 Cybersecurity 👁 0 📖 4 min read

SPF permerror: the 10-lookup limit, how to count it, and how to get under it

A classic support ticket: "our emails go to spam since we added the new tool". The SPF record looks fine. It has a v=spf1, it has the right include: entries, it ends with ~all. Yet receivers report spf=permerror. The ca

A classic support ticket: "our emails go to spam since we added the new tool". The SPF record looks fine. It has a v=spf1, it has the right include: entries, it ends with ~all. Yet receivers report spf=permerror.

The cause is often a limit most people never read about.

The rule

RFC 7208, section 4.6.4, limits the number of DNS-querying terms in an SPF evaluation to 10. If the evaluation needs more, the result is a permanent error (permerror), which is not a soft fail: the SPF check is simply invalid, and DMARC cannot rely on it.

What counts toward the 10:

Term Counts?
include: Yes, and so does everything inside the included record
a, mx Yes (and mx can trigger further lookups for each host, which have their own limit of 10)
exists: Yes
redirect= Yes
ptr Yes, and the RFC says not to use it
ip4:, ip6: No
all No

The RFC also limits "void lookups" (queries that return nothing) to 2. A dead include: pointing to a domain that no longer exists is enough to hit it.

Count them yourself

You need to follow each include: by hand, or with a small script. By hand:

dig +short TXT example.com | grep spf1
# "v=spf1 include:_spf.google.com include:sendgrid.net include:servers.mcsv.net ~all"

dig +short TXT _spf.google.com
dig +short TXT sendgrid.net
dig +short TXT servers.mcsv.net

Each include: at the top level costs one lookup; each include:, a, mx inside those records costs one more. Write the tree on paper:

example.com
  include:_spf.google.com          (1)
    include:_netblocks.google.com  (2)
    include:_netblocks2.google.com (3)
    include:_netblocks3.google.com (4)
  include:sendgrid.net             (5)
  include:servers.mcsv.net         (6)

The records above are an illustration, not a current snapshot: providers change their records, so run the commands yourself. The point is that a single large provider can consume several of your ten.

For a script, the open source checkdmarc Python package, or the pyspf library, evaluates the record and can report the lookups. Run it in CI against your domains and you will hear about the 11th lookup before your users do.

Four ways to get under the limit

1. Remove what you no longer use. The easiest win. Every migration leaves a trail: the old newsletter tool, the ticketing system from a previous supplier. Check your DMARC reports for sources that send nothing, then delete their include:.

2. Move senders to a subdomain. A marketing platform that sends from news.example.com gets its own SPF record, with its own 10 lookups. This also protects your main domain's reputation. Remember that SPF does not inherit: a subdomain needs its own record.

3. Replace include: with ip4: when the addresses are stable. An ip4: costs no lookup. The risk: if the provider changes its addresses, you break your own email until you notice. Only do this for a server you control, such as your own relay.

4. Rely on DKIM and use SPF for fewer sources. DMARC passes with either an aligned SPF or an aligned DKIM. If a service signs with your domain, you do not need it in SPF for DMARC purposes. Check the service's documentation, enable custom DKIM, then drop its include: and verify with the headers of a test message.

There are "SPF flattening" services that rewrite the record into IP addresses automatically and keep it updated. They solve the count, but you now depend on them to refresh the record on time. If you use one, monitor the result.

Other ways SPF breaks (same ticket, different cause)

  • Two SPF records. A domain must publish only one v=spf1 record. Two records also produce a permanent error. Merge them.
  • +all or a missing all. +all authorizes the whole internet.
  • A very long record. A single TXT string is limited to 255 characters; longer values must be split into several quoted strings inside the same record, and a large DNS response can fall back to TCP, which some old resolvers handle badly.
  • Subdomains. The SPF of example.com does not cover mail.example.com.

Verify after every change

dig +short TXT example.com | grep spf1     # exactly one line

Then send a message to a mailbox on another provider and read the full headers:

Received-SPF: pass ...
Authentication-Results: ... spf=pass smtp.mailfrom=example.com

A permerror here, with its explanation text, will tell you which term exceeded the limit.

A habit worth adopting

Treat the SPF record like code: keep it in version control, require a lookup count in review, and re-check it whenever a new tool wants to send mail "on your behalf". Most teams add the include, see the test email arrive, and move on. The failure shows up weeks later, on the next provider change, when it is hard to link to the cause.

Checklist

  • [ ] One v=spf1 record per domain
  • [ ] Lookup tree counted, at most 10, void lookups at most 2
  • [ ] No ptr, no +all
  • [ ] Unused include: entries removed
  • [ ] DKIM enabled on every sending service, so DMARC does not depend on SPF alone
  • [ ] A recurring check (script or external tool)

Disclosure: I am part of the team behind AttackScout. SPF and DMARC work together, and the free DMARC checker shows what is published for your domain from the outside, no account needed: https://attackscout.com/en/tools/dmarc-check?utm_source=devto&utm_medium=article&utm_campaign=spf-lookups

📰 Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.