Karta.MTASTS (Karta v1.0.0)

The MTA-STS policy of a recipient domain (RFC 8461): discovered through DNS, fetched over HTTPS, kept in Postgres, refreshed.

policy/2 answers the policy in force for a delivery (RFC 8461 §5.1):

  1. The domain's _mta-sts TXT record is looked up (Karta.MTASTS.TXT).
  2. A cached policy that has not expired and has the announced id is used as it is.
  3. Otherwise, when a record announces an id, the policy is fetched (Karta.MTASTS.Fetcher) and parsed (Karta.MTASTS.Policy); a valid one replaces the cached policy and is used. It expires max_age seconds after the fetch.
  4. When there is no record, the lookup fails, or the fetch or the policy fails, the cached policy is used if it has not expired (an attacker who blocks DNS or the policy host cannot turn a policy off); without one the domain has no policy.

A fetch of a domain's policy version is tried at most once in five minutes (RFC 8461 §3.3), on each node (Karta.RateLimiter). Every failed fetch is recorded for TLS reports (Karta.TLSReport: sts-policy-fetch-error, sts-webpki-invalid, sts-policy-invalid).

A policy in none mode is cached like any other (it is how a domain withdraws its policy) but is not in force: policy/2 answers nil, as for a domain without a policy. Address literals have no policy.

refresh/2 fetches a cached domain's policy again before it expires (RFC 8461 §3.3 and §10.2; Karta.MTASTS.Refresh, daily), so that a policy that is in use never lapses.

Options: :dns (for Karta.DNS.lookup/3), :limiter (the Karta.RateLimiter table, default Karta.RateLimiter), :now, and the options of Karta.MTASTS.Fetcher.fetch/2.

Summary

Functions

The cached policy of domain (ASCII) if it has not expired at now, else nil.

Removes the policies that have expired at now. Returns how many.

The domains whose policy was fetched before before and has not expired, oldest fetch first, at most limit.

The policy in force for domain (enforce or testing), or nil. See the module doc.

Fetches the policy of domain again if its TXT record announces one, whether or not the id changed; a valid policy replaces the cached one and runs from now. :refreshed, or :kept when nothing new was stored.

Functions

cached(domain, now \\ DateTime.utc_now())

@spec cached(String.t(), DateTime.t()) :: Karta.MTASTS.Policy.t() | nil

The cached policy of domain (ASCII) if it has not expired at now, else nil.

delete_expired(now \\ DateTime.utc_now())

@spec delete_expired(DateTime.t()) :: non_neg_integer()

Removes the policies that have expired at now. Returns how many.

due_for_refresh(before, limit, now \\ DateTime.utc_now())

@spec due_for_refresh(DateTime.t(), pos_integer(), DateTime.t()) :: [String.t()]

The domains whose policy was fetched before before and has not expired, oldest fetch first, at most limit.

policy(domain, opts \\ [])

@spec policy(String.t(), keyword()) :: Karta.MTASTS.Policy.t() | nil

The policy in force for domain (enforce or testing), or nil. See the module doc.

refresh(domain, opts \\ [])

@spec refresh(String.t(), keyword()) :: :refreshed | :kept

Fetches the policy of domain again if its TXT record announces one, whether or not the id changed; a valid policy replaces the cached one and runs from now. :refreshed, or :kept when nothing new was stored.