Karta.Scan (Karta v1.0.0)

Spam and virus scanning of a received message, before the final reply to DATA (Karta.SMTP.Policy.Inbound).

The scanners. Karta.Scan.Rspamd asks rspamd's HTTP API for a spam score, an action and the symbols that made it; Karta.Scan.ClamAV streams the spool file to clamd. Both are optional: with neither configured nothing is scanned and no header is added. They run one after the other in a process of its own, and the whole scan has one time budget (:timeout, default 5 seconds): a scanner that has not answered by then is killed and reported as failed, so the SMTP session is never held longer than that.

Fail-closed. A scanner that is configured but cannot answer (down, too slow, an unreadable answer, or a message larger than :max_size) is a scan failure. Every recipient's route says what then (inbound_routes.fail_closed, default on): when any of them asks to be closed the message is refused with 451 4.7.1, so the sender keeps it and tries again; when every one of them is open the message is accepted and marked (X-Spam-Scanned: no, X-Virus-Scanned: no). A scanner that is not configured is not a failure.

The routes' spam modes (inbound_routes.spam_mode): a message whose score reached a route's threshold (spam_threshold, default 6.0) is spam for that route. :mark (the default) delivers it, :hold stores it with held deliveries that an owner releases, and :reject refuses the whole message with 550 5.7.1. A virus is always refused (554 5.7.1). A message that was over some routes' threshold and under others' is spam for the first: those routes hold or reject, the others get their mail.

Arguments. run/2: :timeout, :max_size, :rspamd and :clamav (theirs, as keyword lists; by default the configured config :karta, Karta.Scan), and :envelope (%{ip:, helo:, mail_from:, rcpt:}). verdict/2: the scan and the recipients with their routes ([{address, route}], the route as Karta.Inbound.Directory holds it).

headers/1 gives the fields added to the stored message; spam_status/1, score/1 and report/1 read a stored scan (the messages.scan map) for the formats that carry spam fields.

Summary

Types

What the scan decided for one message: the addresses whose route holds it, whether it is spam for any of them, and what is stored with it.

t()

Functions

The size beyond which a message is not scanned, in bytes.

The scan budget of one message, in milliseconds.

The fields added on top of the stored message. A scanner that is not configured adds nothing; one that is configured but did not answer says so. Values are Karta's own: a score, one of rspamd's actions, symbol names and scores. Nothing a scanner sent reaches a header as text.

The descriptions of a stored scan's symbols, for a report field.

Scans the message at path. See the module doc. Never raises: a scanner that fails is a result like another. A scan with a scanner configured is measured (Karta.Metrics.scan/2); one with neither is not.

The score of a stored scan, or nil when it was not scanned.

A score as the headers and the formats write it: two decimals.

The spam status of a stored scan, in Postal's words: "Spam", "NotSpam", or "NotChecked" when no scanner answered.

The scan as it is stored with the message (messages.scan).

One line about what the scanners said, for the log: spam 15.00 (reject), virus Eicar-Test-Signature, spam unavailable (econnrefused), virus clean. Only Karta's own words and numbers: nothing a scanner sent as text (a virus name is cut to letters, digits, _, - and . already).

Checks the scanners' settings where Karta starts: a wrong one stops it at boot with a readable message. config defaults to the configured config :karta, Karta.Scan.

What happens to the message, for its recipients and their routes: {:ok, assessment}, or {:reject, reply} for a virus, spam a route rejects, or a scan failure on a route that is fail-closed.

Types

assessment()

@type assessment() :: %{hold: MapSet.t(String.t()), spam: boolean(), scan: map()}

What the scan decided for one message: the addresses whose route holds it, whether it is spam for any of them, and what is stored with it.

t()

@type t() :: %Karta.Scan{spam: map(), virus: map()}

Functions

default_max_size()

@spec default_max_size() :: pos_integer()

The size beyond which a message is not scanned, in bytes.

default_timeout()

@spec default_timeout() :: pos_integer()

The scan budget of one message, in milliseconds.

headers(scan)

@spec headers(t()) :: iodata()

The fields added on top of the stored message. A scanner that is not configured adds nothing; one that is configured but did not answer says so. Values are Karta's own: a score, one of rspamd's actions, symbol names and scores. Nothing a scanner sent reaches a header as text.

report(arg1)

@spec report(map() | nil) :: String.t()

The descriptions of a stored scan's symbols, for a report field.

run(path, opts \\ [])

@spec run(Path.t(), keyword()) :: t()

Scans the message at path. See the module doc. Never raises: a scanner that fails is a result like another. A scan with a scanner configured is measured (Karta.Metrics.scan/2); one with neither is not.

score(arg1)

@spec score(map() | nil) :: float() | nil

The score of a stored scan, or nil when it was not scanned.

score_text(score)

@spec score_text(number()) :: String.t()

A score as the headers and the formats write it: two decimals.

spam_status(arg1)

@spec spam_status(map() | nil) :: String.t()

The spam status of a stored scan, in Postal's words: "Spam", "NotSpam", or "NotChecked" when no scanner answered.

stored(scan, is_spam)

@spec stored(t(), boolean()) :: map()

The scan as it is stored with the message (messages.scan).

summary(scan)

@spec summary(t()) :: String.t()

One line about what the scanners said, for the log: spam 15.00 (reject), virus Eicar-Test-Signature, spam unavailable (econnrefused), virus clean. Only Karta's own words and numbers: nothing a scanner sent as text (a virus name is cut to letters, digits, _, - and . already).

validate!(config \\ nil)

@spec validate!(keyword() | nil) :: :ok

Checks the scanners' settings where Karta starts: a wrong one stops it at boot with a readable message. config defaults to the configured config :karta, Karta.Scan.

verdict(scan, routed)

@spec verdict(t(), [{String.t(), map()}]) ::
  {:ok, assessment()} | {:reject, Karta.SMTP.Reply.t()}

What happens to the message, for its recipients and their routes: {:ok, assessment}, or {:reject, reply} for a virus, spam a route rejects, or a scan failure on a route that is fail-closed.