What do SPF macros such as %{i}, %{s} and %{d} let the exists mechanism and the exp= modifier do, and what do they cost?
answer
- names built from the envelope
- any A record means match
- text only on a fail
- per-sender data defeats caching
- queries reveal who sends
basics
~20 sSPF macros insert values such as the client IP (%{i}), sender (%{s}) and domain (%{d}) into names. exists matches when the built name has any A record; exp= fetches explanation text for a fail. Sender macros defeat caching and expose senders.
solid answer
~40 sA macro such as `%{i}` (client IP), `%{s}` (sender), `%{l}` (local-part), `%{o}` (sender domain), `%{d}` (current domain) or `%{h}` (HELO name) is expanded inside a domain-spec before the lookup. `exists:` turns the expanded name into an A query and matches if any A record comes back, even on IPv6, so a domain can keep a per-address or per-sender allow list in DNS. `exp=` names a TXT record whose macro-expanded text is offered for the rejection when the result is `fail` from a matching mechanism. RFC 7208 warns that `%{s}`, `%{l}`, `%{o}` and `%{h}` in mechanisms stop receivers caching results by domain and IP, that `exists` queries expose who is sending to the record owner's DNS, and that a receiver must treat expanded content as untrusted.
code
dns · 8 lines; policy: gateway by address, other hosts by an A-record allow list, explanation on fail
example.com. TXT "v=spf1 ip4:192.0.2.25 exists:%{i}._allow.%{d} -all exp=why._spf.%{d}"
; allow one office NAT address without editing the policy
198.51.100.61._allow.example.com. A 127.0.0.2
; explanation text, expanded only for a fail result
why._spf.example.com. TXT "%{i} is not authorised to send mail for %{d}"go deeper
Recall that SPF macros insert values such as the client IP and the domain into names, and that exists matches when the built name has an A record.
Explain the main macro letters, the r and digit transformers, and when exp= text is fetched and used.
Judge when an exists-based allow list is worth its caching and privacy costs, and treat expanded sender data as untrusted input on the receiving side.
Decide whether per-sender SPF logic belongs in DNS at all, or whether submission controls and simpler address lists carry the same intent at lower cost.
## Macros in a paragraph A **macro** is a `%{...}` sequence inside a domain-spec or an explanation that the checker replaces before it queries DNS (RFC 7208 section 7). The common letters: | Macro | Expands to | |---|---| | `%{s}` | the sender, for example `[email protected]` | | `%{l}` | the local-part of the sender, `alice` | | `%{o}` | the domain of the sender | | `%{d}` | the domain currently being evaluated | | `%{i}` | the client IP address (dotted-quad for IPv4, dot-separated nibbles for IPv6) | | `%{h}` | the HELO or EHLO name the client gave | | `%{v}` | `in-addr` for IPv4, `ip6` for IPv6 | Three more, `%{c}` (a readable client IP), `%{r}` (the checking host's name) and `%{t}` (a timestamp), are allowed only in explanation text. `%{p}`, the validated reverse name, carries the same SHOULD NOT as the `ptr` mechanism. **Transformers** reshape a value: `r` reverses its parts (`%{ir}` turns 192.0.2.1 into `1.2.0.192`), and a digit keeps that many right-hand parts (`%{d2}` keeps the last two labels). An uppercase letter expands the same value URL-escaped. A `%` not followed by `{`, `%`, `_` or `-` is a syntax error, which makes the whole record `permerror`. ## exists: a lookup you design `exists:` expands its domain-spec, issues an **A query** for the result (even when the client connected over IPv6) and matches if **any** A record is returned. The address in the answer does not matter. That makes DNS itself the policy database: - **Per-host allow list.** `exists:%{i}._allow.%{d}` matches a client at 198.51.100.61 only if `198.51.100.61._allow.example.com` has an A record. Adding a printer's NAT address is a single new record, with no edit to the TXT policy. For IPv6 clients, `%{ir}.%{v}._allow.%{d}` builds a nibble-reversed name ending in `ip6._allow.example.com`, so one naming scheme can serve both address families. - **Per-sender rules.** Folding `%{l}` into the name lets a domain authorise specific local-parts from specific hosts, the scheme RFC 7208 Appendix A.3 sketches for remote users. Section 11.4 limits the expectation: SPF maps domains to authorised MTAs, and `%{l}` gives only a limited way to describe individual addresses. Stopping one user of a shared server from sending as another is the submission server's job, through SMTP AUTH. ## exp=: a message in the rejection `exp=` names a domain whose TXT record holds an **explanation string**. It is used only when the check returns `fail` because a mechanism matched, such as `-all`. The checker fetches that TXT record, joins its strings and expands its macros. If the lookup errors, returns nothing, returns more than one record or the text has a syntax error, it proceeds as if there were no `exp=`. The result is limited to US-ASCII, because it is meant for an SMTP reply. RFC 7208 asks receivers to make clear the text comes from the sender's domain, for example by prefixing `%{o} explains:`. ## What macros cost 1. **Caching.** If no evaluated directive uses `%{s}`, `%{l}`, `%{o}` or `%{h}`, a receiver can cache the result on domain and IP alone. Using them in mechanisms forces fresh evaluations and reduces the effectiveness of DNS caches, which is why section 7.3 advises senders to avoid them. 2. **Privacy.** Every `exists` query lands in the record owner's DNS logs. Section 11.6 notes such queries can reveal who is sending mail and, likely, to which receiving MTA. 3. **Label limits.** A local-part can exceed the 63-octet DNS label limit, and an expanded name beyond 253 characters is truncated from the left. 4. **Untrusted input.** The sender and HELO name come from the connecting client and the record from the publishing domain, so receivers cannot trust the expanded text. Section 11.5.3 tells receivers to be prepared for hostile or unexpected content, and section 11.5.2 notes that explanation text can carry misleading links. ## A defender's use: auditing your own senders Appendix C describes a **tracking `exists`**: a term that embeds `%{h}`, `%{l}`, `%{o}` and `%{i}` in a query under the domain's own zone, so the owner's DNS logs show which hosts send with the domain before the record is tightened. It is a measurement step on the owner's own record, and it carries every cost above, the privacy one included.
- Does the address returned by the exists lookup have to equal the client's IP?No. RFC 7208 section 5.7 says the mechanism matches if any A record is returned for the expanded name. The client's address is usually already inside the name through `%{i}`, so the answer value is only a marker; a loopback-range value such as `127.0.0.2` is a common convention.
- Why is exists:%{l}.%{ir}._spf.%{d} expensive for receivers compared with exists:%{ir}._spf.%{d}?The first embeds the local-part, so the result depends on who is sending as well as from where. RFC 7208 section 7.3 says results can be cached on domain and IP alone only when no directive uses `%{s}`, `%{l}`, `%{o}` or `%{h}`; the per-sender form forces a fresh query for each sender, and DNS caches help less.
- exists matches only if the A record's value equals the client IP.
- The exp= text is attached to every softfail and neutral result.
- Macros are expanded on the sending side before the record is published.
- %{c}, %{r} and %{t} can be used inside any mechanism.
- A per-sender exists rule stops one user of a shared server forging another.