- mail-oci: OCI Email Delivery client (signed REST), per-customer domain/DKIM/sender onboarding, suppression mirror, sending limits - mail-mta: RelayPool transport with OCI primary, optional secondary, health/back-off/quota failover; direct MTA kept but off - mail-inbound: DSN/ARF/DMARC/TLS-RPT parsing + HMAC-signed POST /inbound/raw ingest (no port 25) - workers/inbound-forwarder: Cloudflare Email Routing worker forwarding raw mail over HTTPS Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
81 lines
5.6 KiB
Markdown
81 lines
5.6 KiB
Markdown
# inbound-forwarder (Cloudflare Email Worker)
|
|
|
|
Scribase Mail never listens on port 25. Cloudflare Email Routing is the MX for
|
|
our inbound domains; this Worker receives each message through the `email()`
|
|
handler, buffers `message.raw`, signs it (HMAC-SHA256 over a shared secret and
|
|
a timestamp) and POSTs it to `mail-inbound`'s HTTPS ingest
|
|
(`POST /inbound/raw`). From there the message takes exactly the same path as
|
|
one received over SMTP: DSN → bounce, ARF → complaint, `dmarc@` → DMARC
|
|
aggregate, `tls-rpt@` → TLS-RPT, mailbox → stored and handed to the sink.
|
|
|
|
Independent implementation, written without reference to useSend source. MIT.
|
|
|
|
## Request contract (mirrors `crates/mail-inbound/src/ingest/auth.rs`)
|
|
|
|
| Header | Value |
|
|
|---|---|
|
|
| `X-Scribase-Timestamp` | unix seconds; server accepts ±300 s |
|
|
| `X-Scribase-Envelope-From` | SMTP MAIL FROM (may be empty for DSNs) |
|
|
| `X-Scribase-Envelope-To` | SMTP RCPT TO |
|
|
| `X-Scribase-Signature` | `v1=<hex HMAC-SHA256(secret, "v1\n{ts}\n{from}\n{to}\n{sha256hex(body)}")>` |
|
|
| body | raw RFC 5322 message, `Content-Type: message/rfc822` |
|
|
|
|
Ingest answers → what the sending MTA sees:
|
|
|
|
| Ingest | Worker action | Sender sees |
|
|
|---|---|---|
|
|
| `202 {accepted, id, kind}` | nothing | 250 |
|
|
| `404 unknown_recipient` | `setReject("550 5.1.1 …")` | permanent bounce |
|
|
| `403 relay_denied` | `setReject("550 5.7.1 …")` | permanent bounce |
|
|
| `413 / 422` | `setReject` 552 / 501 | permanent bounce |
|
|
| `401`, `5xx`, network | throw | temporary failure, sender retries |
|
|
|
|
Replayed signatures are refused for 15 minutes; a signature is only valid
|
|
for the exact body hash, so a message cannot be re-targeted.
|
|
|
|
## Files
|
|
|
|
- `src/index.ts` — `email()` handler + `forwardToIngest()`; `fetch()` returns 404 so the Worker hostname is not an HTTP surface.
|
|
- `src/sign.ts` — WebCrypto HMAC, same signing string as the Rust side.
|
|
- `scripts/local-check.mjs` — runs the handler in Node against a local ingest with a fake message.
|
|
- `wrangler.jsonc` — vars `INGEST_URL`, `ALLOWED_RCPT_DOMAINS`; secret `INGEST_SECRET`.
|
|
|
|
## Local verification (done 2026-11-13)
|
|
|
|
```sh
|
|
# 1. ingest + SMTP MX locally (dir storage, no Postgres needed)
|
|
INBOUND_DOMAINS=dev.test,reports.dev.test INBOUND_RECEIVE_DOMAINS=dev.test \
|
|
INBOUND_STORE_DIR=/tmp/scribase-mail-dev/inbound-store \
|
|
INBOUND_HTTP_LISTEN=127.0.0.1:8585 INBOUND_HTTP_SECRET=<32+ chars> \
|
|
cargo run -p mail-inbound --example serve
|
|
|
|
# 2. Rust feeder: DSN/ARF/DMARC/TLS-RPT valid → 202; bad HMAC/stale/replay → 401; relay → 403; unknown → 404
|
|
INBOUND_HTTP_SECRET=<same> cargo run -p mail-inbound --example ingest_feed
|
|
|
|
# 3. Worker handler in Node against the same ingest
|
|
cd workers/inbound-forwarder && INGEST_SECRET=<same> node scripts/local-check.mjs
|
|
|
|
# 4. Worker under wrangler dev, fed through Cloudflare's local email endpoint
|
|
cp .dev.vars.example .dev.vars # set INGEST_SECRET + INGEST_URL=http://127.0.0.1:8585
|
|
npx wrangler dev --port 8787
|
|
curl -s -X POST 'http://localhost:8787/cdn-cgi/handler/email?from=MAILER-DAEMON@mx.example.net&to=bounces+wd1@dev.test' \
|
|
-H 'Content-Type: message/rfc822' --data-binary @/tmp/scribase-mail-dev/dsn.eml
|
|
```
|
|
|
|
## Deploy step (NOT done; owner/operator action)
|
|
|
|
1. `wrangler secret put INGEST_SECRET` (same value as `INBOUND_HTTP_SECRET` on the mail-inbound host) and set `INGEST_URL` to the public ingest URL (e.g. `https://inbound.scribase.com`, behind the box's reverse proxy with TLS).
|
|
2. `npx wrangler deploy`.
|
|
3. Enable Email Routing on the zone(s): `npx wrangler email routing enable scribase.com` (or Dashboard → Email Service → Email Routing). Cloudflare then **sets the MX records itself** on the zone: `route1.mx.cloudflare.net` (priority 69), `route2.mx.cloudflare.net` (86), `route3.mx.cloudflare.net` (3) plus a TXT `v=spf1 include:_spf.mx.cloudflare.net ~all` on the routed name. For subdomains (`bounce.scribase.com`, `inbound.scribase.com`) Email Routing for subdomains must be enabled on the zone, and Cloudflare adds the same MX set on each subdomain. Do **not** hand-write these; let `wrangler email routing dns get <domain>` show them.
|
|
4. Routing rules (Dashboard or `wrangler email routing rules create`):
|
|
- **catch-all on `bounce.scribase.com`** → Send to Worker `scribase-mail-inbound-forwarder` (VERP `bounces+<id>@bounce.scribase.com` lands here).
|
|
- **catch-all on `inbound.scribase.com`** → same Worker (customer inbound mailboxes, when `INBOUND_RECEIVE_DOMAINS` includes it).
|
|
- `dmarc@scribase.com` → Worker (DMARC aggregate reports; `_dmarc` `rua=mailto:dmarc@scribase.com`).
|
|
- `tls-rpt@scribase.com` → Worker (`_smtp._tls` TXT `v=TLSRPTv1; rua=mailto:tls-rpt@scribase.com`).
|
|
- `postmaster@scribase.com`, `abuse@scribase.com` → Worker (role accounts, stored).
|
|
Any address without a rule is rejected by Cloudflare itself.
|
|
5. On the ingest host set `INBOUND_DOMAINS=bounce.scribase.com,inbound.scribase.com,scribase.com`, `INBOUND_RECEIVE_DOMAINS=inbound.scribase.com`, `INBOUND_HTTP_LISTEN=127.0.0.1:8585`, `INBOUND_HTTP_SECRET`, and proxy `https://inbound.scribase.com/inbound/*` to it. Restrict the proxy to Cloudflare's egress ranges if desired; the HMAC is the real gate.
|
|
6. Customer domains: the customer's DMARC `rua` can point at `dmarc@<their-domain>` only if we are their MX; by default we give them `rua=mailto:dmarc@scribase.com` style collector addresses so reports arrive through this Worker.
|
|
|
|
Relevant Cloudflare facts (checked 2026-11-13): Email Routing is free; `message.raw` is a single-use stream (buffer first); `setReject` yields a permanent SMTP error; an unhandled throw yields a temporary error so the sender retries; `wrangler dev` exposes `POST /cdn-cgi/handler/email?from=&to=` for local tests (message must carry `Message-ID`).
|