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 withBODY.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:
- Server-side search for
FROMsubstrings of the domains andSINCE(the mailbox's period). - Strict check of the domain in the
Fromheader: 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_INTERVALminutes), 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=0switches 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=1or for individually allowed hosts fromCLIPR_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:
- The PDF attachments are sealed with the store's public X25519 key and put into
stores/<uuid>/inbox/. - Sender and subject in the log are sealed too; the result reads “Waiting for vault”.
- 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.