Day 3 · Sender authentication

SPF explained: syntax, the 10-lookup limit and common mistakes

lesson 08 / 1916 min1 labpublished
Course contents
On this page
  1. What SPF checks, and what it doesn’t
  2. Reading an SPF record
  3. The 10-lookup limit
  4. Macros, briefly
  5. Forwarding breaks SPF
  6. Common SPF mistakes
  7. What the receiver writes

SMTP lets anyone claim any sender address. SPF (Sender Policy Framework, RFC 7208) is the first of three mechanisms that fix this: a domain publishes, in DNS, the list of IP addresses that are allowed to send mail for it, and a receiver checks the connecting IP against that list. It is simple, almost universal, and widely misunderstood, mostly because of one detail: which domain it checks.

What SPF checks, and what it doesn’t

When a server connects and says MAIL FROM:<bounces@mail.shop.example>, the receiver:

  1. takes the domain of the envelope sender (mail.shop.example),
  2. looks up its SPF record (a TXT record starting with v=spf1),
  3. checks whether the connecting IP address is on the list,
  4. records the result, typically in an Authentication-Results or Received-SPF header.

Three consequences people miss:

  • SPF checks the envelope sender (MAIL FROM, which becomes Return-Path), not the From: header the reader sees. A phisher can pass SPF perfectly with their own domain in MAIL FROM while showing your domain in From:. Tying the two together is DMARC’s job (lesson 10).
  • For bounces, the envelope sender is empty (<>), so SPF checks the domain from the EHLO/HELO name instead. That’s one more reason a server’s HELO name should be a real name in a domain with SPF.
  • SPF is about the last hop: the IP that connected to the receiver. When a message is forwarded, the forwarding server’s IP isn’t in the original sender’s SPF, and SPF fails. That’s normal and expected, and it’s why DKIM exists.

Reading an SPF record

v=spf1 ip4:192.0.2.0/24 ip6:2001:db8::/32 include:_spf.google.com include:sendgrid.net -all

A record is v=spf1 followed by terms, read left to right; the first mechanism that matches the IP decides the result.

MechanismMatches if the IP…DNS lookup?
ip4:192.0.2.0/24, ip6:2001:db8::/32is in this rangeno
a, a:host.example.comis an A/AAAA address of the domain or hostyes
mx, mx:example.comis an address of one of the domain’s MX hostsyes (plus one per MX host)
include:other.examplegets a pass from other.example’s SPF recordyes (plus whatever that record costs)
exists:%{i}.spf.examplemakes this name resolve (used with macros, below)yes
ptrhas reverse DNS in the domain. Deprecated: slow and unreliable, don’t useyes
allalways matches. Goes last, as the defaultno

Each mechanism can have a qualifier in front, which says what a match means:

QualifierResultMeaning
+ (the default)passAuthorized
-failNot authorized: reject or treat as forged
~softfailProbably not authorized: accept, but mark as suspicious
?neutralNo statement either way

Two modifiers complete the syntax: redirect=other.example (if nothing matched, use that domain’s record instead; Gmail uses it: v=spf1 redirect=_spf.google.com) and the rarely used exp= (an explanation text for failures).

The possible results of a check are pass, fail, softfail, neutral, none (no SPF record), temperror (a DNS timeout; the receiver may defer) and permerror (a broken record: a syntax error, two SPF records, or too many lookups). A permerror is treated like having no SPF at all, or worse, so a broken record is a real outage for your authentication.

~all or -all?

The end of the record says what to do with every IP not listed:

  • -all (fail) is the strict statement: “nothing else sends for us”. Receivers may reject outright.
  • ~all (softfail) says “nothing else should, but don’t reject on SPF alone”. Google Workspace’s own instructions use ~all, Microsoft 365’s use -all.
  • ?all and +all are useless and dangerous respectively: +all authorizes every IP on the Internet.

Once a domain has DMARC at enforcement, the DMARC policy is what decides, and the difference between ~all and -all matters less. There is one argument for ~all even then: a few receivers reject on an SPF -all failure before they evaluate DKIM and DMARC, which loses forwarded mail that DKIM would have saved. A sensible path: start with ~all while you are discovering all your senders, and move to -all when you are confident the list is complete, or stay on ~all with DMARC p=reject doing the enforcement.

The 10-lookup limit

Evaluating SPF must not turn into a DNS storm (or a denial-of-service tool), so RFC 7208 limits it: the mechanisms and modifiers that need DNS (include, a, mx, ptr, exists, redirect) may cause at most 10 DNS lookups in total, counting everything inside included records. One more lookup and the result is permerror. There is a second, quieter limit: at most two “void” lookups (names that don’t exist or have no records), to catch records that point at dead domains.

ip4 and ip6 cost nothing, which is why big providers publish their ranges directly, and why every include: you add for a new SaaS tool is a small budget decision. A record for a company with Microsoft 365, Google, a helpdesk, a CRM, a newsletter tool and an ESP is easily at the limit, as the lab below shows with a real one.

How to stay under 10:

  1. Remove what’s unused. Old ESPs and tools you no longer use are the most common waste. Also remove a and mx unless those hosts really send mail.
  2. Move senders to subdomains. If your marketing tool sends as news.example.com and your app as mail.example.com, each subdomain has its own SPF record and its own 10 lookups. This is the clean solution, and it isolates reputation too.
  3. Flatten carefully. Replacing an include: with the IP ranges it currently contains saves lookups, but providers change their IPs without telling you, and a stale flattened record silently breaks. Only flatten with automation that re-checks daily, or not at all.

Macros, briefly

exists: and a few other places accept macros that the receiver expands: %{i} the sending IP, %{s} the envelope sender, %{l} its local part, %{d} the domain, %{h} the HELO name. They allow per-IP or even per-user policies served by a special DNS server. You will mostly meet them in large providers’ records; you rarely need to write one.

Forwarding breaks SPF

Bob has bob@university.example forwarded to his Gmail. A message from alice@example.com arrives at the university, whose server forwards it to Gmail unchanged. Gmail sees: envelope sender alice@example.com, connecting IP the university’s server. That IP is not in example.com’s SPF record, so SPF fails, even though nothing is wrong.

There are two fixes, both on the forwarder’s side, covered in lesson 11: SRS (the forwarder rewrites the envelope sender to its own domain, so SPF is checked against the forwarder) and ARC (the forwarder records and signs the results it saw before forwarding). And one fix on your side: make sure all your mail is DKIM-signed, because a DKIM signature survives plain forwarding.

Common SPF mistakes

MistakeWhat happensFix
Two TXT records starting with v=spf1permerror, no SPF at allMerge them into one
More than 10 lookupspermerrorRemove unused includes, move senders to subdomains
include: of a domain that has no SPF recordpermerror (and a void lookup)Check every include with dig; follow the provider’s documentation exactly
+all, or a huge shared range such as a whole cloud providerAnyone on that network can send as youList only what you use; use the ESP’s include, not raw cloud ranges
Adding the ESP’s include when the ESP sends with its own envelope senderYour SPF is never checked for that mail; it doesn’t help DMARCSet up a custom return-path / MAIL FROM domain at the ESP (bounces.example.com with a CNAME or MX they give you)
Forgetting IPv6Mail from an IPv6 address of your own server failsAdd ip6: ranges, or send over IPv4 only
Using ptrSlow, may be ignored, counts against the limitReplace with ip4/ip6
Typos: ip4:192.0.2.0/33, include: _spf.google.com (space), smart quotespermerrorValidate after every change
An SPF record only on the root, but mail sent from a subdomainnone for the subdomainEvery domain that appears in MAIL FROM (or HELO) needs its own record

What the receiver writes

After checking, the receiver records the result. A modern receiver uses Authentication-Results (RFC 8601), often with the older Received-SPF as well:

Received-SPF: pass (google.com: domain of bounces+8f2@mail.shop.example designates 198.51.100.25
    as permitted sender) client-ip=198.51.100.25;
Authentication-Results: mx.google.com;
       spf=pass (google.com: domain of bounces+8f2@mail.shop.example designates 198.51.100.25
       as permitted sender) smtp.mailfrom=bounces+8f2@mail.shop.example

smtp.mailfrom names the domain that was checked. When you investigate a DMARC failure, compare that domain with the From: header: if they don’t match, SPF can pass and still not help DMARC. That comparison, alignment, is the center of lesson 10.