Contents · Forum Home · StreamSec Tools 4.1.5.363 documentation

StreamSec.Mail.SpamFilter

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

Classes, interfaces and records

classeSpamFilterError

inherits from Exception

(no public members)

recordtSpamFinding

One check's answer. Points may be NEGATIVE: an allowlist, or a domain whose own DMARC policy passed, is how a deployment keeps its false-positive rate down, and that is worth as much as any positive rule.

Check: string; Points: Integer; Hard: tSpamVerdict; Detail: string; DnsName: string;
A CHECK THAT DNS DID NOT ANSWER. Set when the check could not get its answer - a timeout, SERVFAIL, no reachable server, a zone that refused the query - and went on without it: DnsName is the name it asked for and DnsRelaxed says, in a few words, what was not applied. Full description »
DnsRelaxed: string;

recordtSpamOutcome

What the pipeline concluded. Code and Text are meaningful only for svDefer and svRefuse, and are what the host puts on the wire.

Score: Integer; Verdict: tSpamVerdict; Findings: tSpamFindings; Code: Integer; Text: string;

classtSpamContext

inherits from TObject

EVERYTHING A CHECK IS ALLOWED TO KNOW, filled in by the host as the session progresses. It is a class rather than a record because it lives for a whole connection and is handed to checks by reference; it owns nothing.

Data is set only at sstData and is the message as it ARRIVED. It must not be modified: it is the input to DKIM verification, and anything prepended before that runs invalidates every signature on the message.

procedure Reset; property PeerIP: string read fPeerIP write SetPeerIP;
The peer's address, as text. The one fact no forged header can change.

ASSIGN WHATEVER THE TRANSPORT REPORTED: the setter reduces it to the address alone (see SpamPeerAddress), so a host cannot accidentally hand the checks an address:port pair and quietly defeat every one of them.
property HeloName: string read fHeloName write fHeloName;
What the peer called itself in EHLO/HELO. A claim, not a fact.
property Sender: string read fSender write fSender;
The reverse path, as given in MAIL FROM. Empty is a bounce.
property Recipient: string read fRecipient write fRecipient;
The recipient under consideration at sstRecipient.
property Recipients: Integer read fRecipients write fRecipients;
How many recipients this transaction has asked for so far.
property Secure: Boolean read fSecure write fSecure;
True once the session is inside TLS.
property Authenticated: Boolean read fAuthenticated write fAuthenticated;
True once the session has authenticated - see the unit header.
property LocalDomain: string read fLocalDomain write fLocalDomain;
The domain this host answers for, for checks that compare against it.
property RecipientRefused: Boolean read fRecipientRefused write fRecipientRefused;
SET BEFORE sstRecipient when the recipient about to be judged is one the server is REFUSING - an unknown mailbox, a relay it will not carry. Full description »
property Data: tBytes read fData write fData;
The message, at sstData only, exactly as it arrived. Do not modify.
property ForgedStamps: Integer read fForgedStamps write fForgedStamps;
SET BEFORE sstData: how many fields the host removed from this message because they claimed to come from this host and did not - an Authentication-Results, X-DNS-Warning or X-Stripped-Fields naming it (StreamSec.Mail.MessageAuth, SmtpStripForgedFields). Data still holds them: it is the message as it arrived. The filter itself scores it, see tSpamFilter.ForgedStampPoints; a check may read it too.

interfaceiSpamCheck

ONE CHECK. Implement this and add it to a filter.

Evaluate returns False when the check has nothing to say, which is the common case and costs the pipeline nothing. It must not raise: a check that fails is a check that abstains, and the pipeline says so rather than letting one bad rule refuse the mail stream.

function CheckName: string;
A short, stable, lowercase name. It reaches the stamped header, so it is part of the deployment's interface to its own operators.
function Stages: tSpamStages;
The stages at which this check wants to run.
function Evaluate(aContext: tSpamContext; aStage: tSpamStage; out aFinding: tSpamFinding): Boolean;
False = nothing to report.

classtSpamFilter

inherits from TObject

THE PIPELINE. Not a component: it is configured from code beside the server, the way tSmtpMessageAuthenticator is, because every switch on it is a policy decision that belongs with the deployment's other policy rather than scattered through an Object Inspector.

It is safe to call Evaluate from several connection threads at once. The filter itself holds no per-message state; whether a CHECK is thread-safe is that check's own business, and one that keeps a table must say so.

constructor Create; procedure AddCheck(const aCheck: iSpamCheck);
Adds a check. The order they are added is the order they run and the order they are stamped, so put the cheap ones first.
function CheckCount: Integer;
How many checks are installed.
function Evaluate(aContext: tSpamContext; aStage: tSpamStage): tSpamOutcome;
Runs every check that wants this stage and folds the answers into one outcome. Never raises.
class function Merge(const aFirst, aSecond: tSpamOutcome): tSpamOutcome;
Folds a later stage's outcome into an earlier one, so a host can carry a running total across the conversation: the scores summed, the findings appended, and the harder of the two verdicts with its Code and Text. THAT VERDICT IS NOT THE MESSAGE'S: each stage judged its own points against the thresholds, so points spread over stages cross none of them. Conclude the total before stamping it.
function Conclude(const aOutcome: tSpamOutcome): tSpamOutcome;
THE VERDICT OF A WHOLE MESSAGE, from a Merge of its stages: aOutcome with Verdict decided again - TagScore and FileScore applied to the SUMMED Score, and the hardest verdict any finding asked for. A deferral or refusal aOutcome already carries is kept with its Code and Text; below that Code and Text are cleared. For a single stage's outcome it changes nothing.
function StampVerdict(const aOutcome: tSpamOutcome): tSpamVerdict;
WHAT THE STAMP OF A DELIVERED MESSAGE SAYS: svAccept ("No"), or svTag or svFile ("Yes") - TagScore and FileScore applied to Score, and the hardest svTag or svFile a finding or aOutcome itself asked for. Full description »
function ResultsHeader(const aOutcome: tSpamOutcome): string;
The X-Spam-Status field for an outcome, CRLF-terminated, sanitised. Its verdict is StampVerdict's: No or Yes, never Deferred or Refused.
function StampMessage(const aData: tBytes; const aOutcome: tSpamOutcome): tBytes;
The message with the stamp prepended. aData is returned unchanged when the filter is disabled. MUST be called AFTER DKIM verification.
property Enabled: Boolean read fEnabled write fEnabled;
Off by default: a filter that starts filtering the moment it is linked would change what an existing deployment does at the next rebuild.
property TagScore: Integer read fTagScore write fTagScore;
AT OR ABOVE THIS, THE MESSAGE IS TAGGED. NO DEFAULT: zero or less means never, and the filter only stamps what the score actually was. Full description »
property FileScore: Integer read fFileScore write fFileScore;
AT OR ABOVE THIS, THE VERDICT IS svFile. NO DEFAULT: zero or less means never svFile on score alone. Full description »
property FilterAuthenticated: Boolean read fFilterAuthenticated write fFilterAuthenticated;
Off by default, and the default is the right answer - see the header.
property DeferText: string read fDeferText write fDeferText;
What a deferred or refused peer is told, when a check names nothing.
property RefuseText: string read fRefuseText write fRefuseText; property ForgedStampPoints: Integer read fForgedStampPoints write fForgedStampPoints;
WHAT A MESSAGE THAT CAME WITH A FIELD FORGED IN THIS HOST'S NAME ADDS TO ITS SCORE, at sstData, once whatever the number of fields: a finding named cSpamForgedStampCheck in X-Spam-Status, in the same verdict as every other check of that stage. Full description »

Types (enumerations, sets, aliases)

enumtSpamVerdict

IN ORDER OF SEVERITY, and the order is load-bearing: a pipeline takes the HIGHEST verdict any check asked for, so adding a check can only ever make a message more suspect, never less.

  tSpamVerdict = (
    { Nothing to say. The message is stamped all the same. }
    svAccept,
    { Stamp it and deliver it to the inbox. The recipient's client decides. }
    svTag,
    { Stamp it Yes; a host may file it into the Junk folder, IF the account
      allows that - an account collected over POP3 must not. TsmSMTPServer
      files only a forgery and delivers any other svFile to the inbox, as
      svTag - see the unit header. }
    svFile,
    { Answer 4xx. Greylisting, and any check that could not be run and would
      rather be asked again than guess. }
    svDefer,
    { Answer 5xx. Only from a check that can say why in one line. }
    svRefuse);

enumtSpamStage

WHERE IN THE CONVERSATION a check runs. A check declares the stages it understands and is not called at the others, so a blocklist lookup does not run four times for a message with four recipients. THE ORDER IS THE ORDER FACTS ARRIVE, and each stage is the earliest point at which its own fact exists: the address before the banner, the greeting name after EHLO, the reverse path after MAIL FROM, one visit per recipient, and the message itself at end of data. Judging a fact at the stage it arrives is what makes the cheap checks cheap - an address that will be refused costs a single reply if it is judged at sstConnect, and a whole conversation if it waits for sstData.

  tSpamStage = (sstConnect, sstHelo, sstSender, sstRecipient, sstData);

settSpamStages

  tSpamStages = set of tSpamStage;

arraytSpamFindings

  tSpamFindings = array of tSpamFinding;

Routines