# hieunc229/mailflare — AlpLab Radar Static Code Audit

- Ring: `adopt`
- Category: `other`
- Overall score: `4.3/5`
- Implementation check: `verified`
- Maturity: `active`
- Audited commit: `4bfd81436d2c` (read-only; nothing from the repository was executed)
- Reviewed at: 2026-12-02T05:51:29Z
- Repository: https://github.com/hieunc229/mailflare
- Radar entry: https://radar.alplab.co/#repo/hieunc229/mailflare
- License: `AGPL-3.0`
- CI workflows: `.github/workflows`
- Tags: `email`, `self-hosted`, `cloudflare-workers`, `jmap`, `ai-assistant`, `nextjs`

## Summary

A self-hosted custom-domain email suite — inbound/outbound mail via Cloudflare Email Routing, Resend, SES or a built-in SMTP listener, routing rules, spam/deliverability tooling, JMAP, calendar, Drive, booking, admin console and an approval-gated AI assistant — deployable as a Cloudflare Worker, a single Docker container, or on Vercel.

## Implementation Check

Every README feature row maps to real code I read (src/lib/email/send.ts, inbound.ts, routing.ts, outbound-provider.ts; src/lib/calendar/; src/lib/drive/; src/lib/booking/; src/lib/jmap/handler.ts; src/lib/agent/tools.ts; src/db/with-database.ts for D1/Postgres/MySQL/libSQL switching), and docs/security.md is candid about limits such as CalDAV being discovery-only.

## When to Use

Use it when you want your own Google-Workspace-style mail (plus calendar, Drive, booking) on a domain you control on Cloudflare, Docker or Vercel and can operate the deployment; not when you need write-capable CalDAV/CardDAV sync, built-in antivirus scanning of attachments, or a fully managed service.

## Sub-scores

| Dimension | Score |
|---|---|
| quality | 5.0/5 |
| usability | 4.5/5 |
| reliability | 3.0/5 |
| security | 4.5/5 |
| integration | 4.5/5 |

## Code Evidence

worker.ts implements the Cloudflare Worker entry with fetch/email/queue/scheduled handlers (25 MiB inbound cap, routing decisions resolved on the live ForwardableEmailMessage, queue retry vs permanent-reject semantics); src/lib/email/routing.ts resolveInboundAddress runs reject rules before mailbox lookup, then exact mailboxes/aliases, then catch-alls, and matchesRegex guards user-supplied regexes with isBacktrackingRisk; src/lib/email/send.ts uses claimJob status-transition ('queued'->'sending' with scheduledAt match) so cancel and delivery cannot both win, and re-validates sender authorization and suppression lists at delivery time; src/lib/remote-images/fetch.ts fetchRemoteImage validates hosts against DNS results with isPublicAddress covering IPv4/IPv6/NAT64/6to4 plus a wildcard-DNS suffix blocklist, revalidates every redirect, and checks image signatures; server/runtime/public-fetch.ts guardedFetch pins DNS lookup to public addresses with agent:false (no socket reuse); src/lib/email/html.ts prepareEmailHtml rewrites mail HTML through parse5 + css-tree + DOMPurify, strips frames/forms/media and remote images by default; src/lib/auth/session.ts stores only SHA-256 session-token hashes; tests/api-key-sha256.test.mjs verifies new keys are SHA-256 with legacy-bcrypt upgrade and no fallback on hash mismatch; src/app/api/inbound/route.ts requires HMAC signatures (v1/v2 with timestamp and replay-protection via relay-seen/ objects); src/lib/agent/approvals/utils.ts requestAgentSend/confirmAgentSend enforce revision + payload-hash equality before any AI draft can be sent; server/runtime/smtp.ts deletes sender-written Authentication-Results/Received headers and strips X-SES-* provider trace; .github/workflows/checks.yml runs lint, tsc --noEmit, node --test tests/*.test.mjs and a full build on push/PR, and the 300+ tests (e.g. tests/dav-handler.test.mjs, tests/remote-images-fetch.test.mjs) bundle the real source with esbuild against in-memory SQLite with migrations applied.

## Issues Found

CI actions are pinned to mutable major-version tags, not commit SHAs (14 usages, e.g. actions/checkout@v7 and docker/build-push-action@v7 in .github/workflows/docker-image.yml), a supply-chain hardening gap; JMAP Basic authentication is accepted over plain HTTP for LAN/Docker clients (src/lib/jmap/handler.ts, documented in docs/security.md), so a misconfigured deployment can leak app passwords; server/runtime/mailer.ts honors SMTP_TLS_REJECT_UNAUTHORIZED=false, an operator-controlled TLS-verification bypass.

## Key Risks

Nothing was executed; 'tests exist and run in CI' is asserted from .github/workflows/checks.yml and the test sources, not from a test run. Operating it requires real infrastructure: a Cloudflare account + scoped CF_TOKEN (Workers), or a server with port 25 and DNS MX (Docker), plus a database and object storage on Vercel. JMAP, DAV and IMAP-helper paths have HTTPS caveats; MCP/Resend keys can end up stored in the database (encrypted when the platform key is available, else explicit plaintext consent); DMARC/MTA-STS/TLS-RPT and spam engines were only sampled, not audited end-to-end; no Playwright run was verified beyond playwright.config.ts and package.json test:e2e.

## Reviewed Files

- `README.md`
- `package.json`
- `.github/workflows/checks.yml`
- `.github/workflows/docker-image.yml`
- `.github/workflows/deploy-update.yml`
- `Dockerfile`
- `docker-compose.yml`
- `wrangler.jsonc`
- `worker.ts`
- `server/runtime/smtp.ts`
- `server/runtime/public-fetch.ts`
- `deploy/cloudflare-email-relay/package.json`
- `src/lib/email/inbound.ts`
- `src/lib/email/send.ts`
- `src/lib/email/routing.ts`
- `src/lib/email/html.ts`
- `src/lib/email/outbound-provider.ts`
- `src/lib/email/intake-signature.ts`
- `src/lib/auth/session.ts`
- `src/lib/auth/origin.ts`
- `src/lib/cloudflare-api.ts`
- `src/lib/security/headers.ts`
- `src/lib/remote-images/fetch.ts`
- `src/lib/storage/index.ts`
- `src/db/with-database.ts`
- `src/lib/mailboxes/access.ts`
- `src/lib/jmap/handler.ts`
- `src/lib/agent/tools.ts`
- `src/lib/agent/model.ts`
- `src/lib/agent/mcp/service.ts`
- `src/lib/agent/approvals/utils.ts`
- `src/lib/seed.ts`
- `src/app/api/seed/route.ts`
- `src/app/api/inbound/route.ts`
- `src/app/api/messages/route.ts`
- `src/app/api/send/route.ts`
- `src/lib/api/auth.ts`
- `docs/security.md`
- `tests/dav-handler.test.mjs`
- `tests/api-key-sha256.test.mjs`
