Skip to content
CliprWiki

Mail retrieval

How Clipr fetches mailboxes – read-only, with sender filter, SSRF protection and a vault inbox.

Code: services/mail.py (fetching, scheduler), services/inbox.py (sealed inbox), api/mail.py (endpoints).

Course of a fetch

sequenceDiagram
  participant S as Scheduler
  participant W as Worker
  participant I as IMAP server
  participant P as Pipeline
  S->>W: mailbox due
  W->>I: connect (TLS), login
  W->>I: EXAMINE folder (read-only)
  W->>I: UID SEARCH FROM … SINCE …
  loop for each new UID
    W->>I: UID FETCH (BODY.PEEK[])
    W->>W: check From domain strictly
    W->>P: PDF attachments → ingest()
    W->>W: result into the log
  end
  W->>I: LOGOUT

Read-only

  • The folder is opened with EXAMINE (read-only), messages are loaded with BODY.PEEK[]. Clipr marks nothing as read, moves and deletes nothing. A test against a local IMAP server verifies that no writing commands are sent.
  • Clipr recognises already processed messages by (UIDVALIDITY, UID) in the log mail_log.

Sender filter

Two stages:

  1. Server-side search for FROM substrings of the domains and SINCE (the mailbox's period).
  2. Strict check of the domain in the From header: exactly the domain or a subdomain of it.

Only PDF attachments go into the import pipeline. Terms and invoices are sorted out there by classification.

Scheduling

  • A scheduler checks every minute which mailboxes are due. Fetching happens at system-wide times (CLIPR_IMAP_INTERVAL minutes), slightly offset per account.
  • A worker pool (CLIPR_IMAP_WORKERS, default 2) processes the fetches. At most one fetch runs per mailbox at a time.
  • “Fetch now” triggers a fetch immediately, throttled by a minimum pause.
  • CLIPR_IMAP_ENABLED=0 switches the automatic times off; fetching by hand remains possible.

SSRF protection

Before every connection Clipr resolves the host name and checks all addresses (check_host):

  • Always blocked: link-local (including cloud metadata 169.254.169.254), multicast, reserved and unspecified addresses.
  • Loopback and private networks only with CLIPR_IMAP_ALLOW_PRIVATE=1 or for individually allowed hosts from CLIPR_IMAP_ALLOW_HOSTS (name and port, if given, exactly).

TLS is always verified. For own certificate authorities (such as the Proton Bridge) there is CLIPR_IMAP_CA_FILE – see Mailboxes.

Locked vault

If the store is a locked vault, the worker cannot open it. Then:

  1. The PDF attachments are sealed with the store's public X25519 key and put into stores/<uuid>/inbox/.
  2. Sender and subject in the log are sealed too; the result reads “Waiting for vault”.
  3. On the next unlock Clipr imports the inbox through the normal pipeline.

Limits

  • Emails above a maximum size are skipped.
  • The mailbox password is stored encrypted and only leaves the app towards the IMAP server.