Contents · Forum Home · StreamSec Tools 4.1.5.363 documentation

StreamSec.Mail.Spf

WHAT SPF ACTUALLY ANSWERS, AND WHAT IT DOES NOT.

  check_host() answers one narrow question: did the domain that owns this
  envelope sender authorise THIS IP to send for it? That is all. In particular:

  - IT SAYS NOTHING ABOUT THE MESSAGE. SPF authorises a connection, not content;
    the From: header a human sees is not the identity checked here, and a
    "pass" on a throwaway domain is a pass. Treating an SPF pass as evidence the
    mail is genuine is the single most common misuse of it - that is what DKIM
    and DMARC are for, and DMARC is what ties this identity to the header one.
  - A FAIL IS THE DOMAIN OWNER'S POLICY, NOT A VERDICT ON THE SENDER. Forwarded
    mail fails SPF routinely, because the forwarder is not in the original
    domain's policy and has not rewritten the envelope. A receiver that rejects
    on fail alone breaks mailing lists and forwarding; the result belongs in a
    policy decision (and in a Received-SPF header), not in an automatic 5xx.

  THE LIMITS ARE PART OF THE PROTOCOL, NOT AN OPTIMISATION. sec.4.6.4 caps a
  single evaluation at cSpfMaxLookups DNS-querying terms and cSpfMaxVoid void
  answers. Those caps exist because 'include' and 'redirect' recurse into
  someone else's record: without them, a hostile - or merely careless - policy
  turns one inbound message into an unbounded fan-out of queries issued BY THIS
  HOST AT SOMEONE ELSE'S DIRECTION. Exceeding a limit is permerror, and that is
  a security property; it is not something to raise "if it gets in the way".

  EVERY ANSWER HERE IS UNAUTHENTICATED. There is no DNSSEC below this (see
  StreamSec.Mail.Dns), so an attacker who can forge DNS can manufacture a pass.
  SPF is a filter, not an authentication mechanism.

  Resolution goes through iDnsResolver, so the whole evaluator runs against a
  scripted resolver with no network at all - which is how the vectors at the
  bottom pin the limit and macro behaviour that live traffic would never
  exercise reproducibly.

Feature notes: Mail subsystem notes (SMTP / POP3 / IMAP)

Classes, interfaces and records

classeSpfError

inherits from Exception

(no public members)

recordtSpfTerm

Kind: tSpfTermKind; Qualifier: tSpfQualifier; Spec: string;
domain-spec, or the literal for ip4/ip6
Bits4: Integer;
prefix length, or -1 for "not given"
Bits6: Integer; Net: tBytes;
parsed network for stkIp4/stkIp6

classtSpfEvaluator

ONE EVALUATION PER INSTANCE. The lookup and void budgets are per-instance state, so reusing an evaluator across two messages would let the first one spend the second one's budget. Check() resets them; nothing else does.

constructor Create(const aResolver: iDnsResolver); function Check(const aClientIP, aHeloName, aMailFrom: string): tSpfResult;
The full check for a MAIL FROM identity. An EMPTY aMailFrom is a bounce, and RFC 7208 sec.2.4 says the HELO name stands in as the identity with a local part of 'postmaster' - not that the check is skipped.
function CheckHelo(const aClientIP, aHeloName: string): tSpfResult;
The HELO identity on its own (sec.2.3). Worth running even when the MAIL FROM check passed: it is the only identity a bounce has.
property Explanation: string read fExplanation;
The domain owner's own words for a fail, from an 'exp' modifier. Empty unless the result was srFail and the record carried one. IT IS ATTACKER-SUPPLIED TEXT: it comes from the DNS of the domain being checked. Never put it in a log or an SMTP reply without sanitising - the expander below strips controls, but the content is still not yours.
property Lookups: Integer read fLookups;
How much of the budget the last Check() spent.
property VoidLookups: Integer read fVoids; property TempErrorName: string read fTempErrorName;
The DNS name whose lookup did not answer when the last Check() came to srTempError - the name asked for, a reverse name for a ptr lookup. Empty for every other result.
property ReceivingDomain: string read fReceivingDomain write fReceivingDomain;
This host's own name, for the %{r} macro.
property Resolver: iDnsResolver read fResolver write fResolver;

Types (enumerations, sets, aliases)

enumtSpfResult

RFC 7208 sec.2.6. The two error results are NOT interchangeable:
srTempError says "ask again later" (a 4xx if anything), srPermError says "this policy is broken and will stay broken" (never a retry).

  tSpfResult = (
    { No policy published - the domain has said nothing, which is not the same
      as saying "not authorised". }
    srNone,
    { Published, but explicitly declines to assert anything about this IP. }
    srNeutral,
    { The IP is authorised. }
    srPass,
    { The domain says this IP is NOT authorised. }
    srFail,
    { The domain says it is probably not authorised but is still transitioning
      ('~all'). Treat as a weaker fail; do not reject on it. }
    srSoftFail,
    { A DNS failure. Transient. }
    srTempError,
    { The policy could not be evaluated: bad syntax, or a limit exceeded. }
    srPermError);

enumtSpfQualifier

  tSpfQualifier = (sqPass, sqFail, sqSoftFail, sqNeutral);

enumtSpfTermKind

  tSpfTermKind = (stkAll, stkInclude, stkA, stkMx, stkPtr, stkIp4, stkIp6,
                  stkExists, stkRedirect, stkExp, stkUnknown);

arraytSpfTermArray

  tSpfTermArray = array of tSpfTerm;

Routines