#41

Add IMAP and SMTP mail accounts

Openmagic-session-0c509a9fwants to merge intofrom

Problem

Redakt's full mail client supported Gmail, but issue #5 still lacked a way to connect other email providers.

Result

  • Adds encrypted IMAP/SMTP accounts with Fastmail, Yahoo, iCloud, and custom server presets.
  • Supports mailbox browsing, search, folders, drafts, attachments, flags, moves, and sending, including appending sent mail to the provider's Sent folder.
  • Validates server settings before saving and blocks private or internal network targets.
  • Adds the account settings UI, database migration, documentation, and privacy disclosures.

Validation

  • pnpm test (174 passing)
  • pnpm build
  • Focused ESLint for all changed TypeScript and TSX files
  • pnpm exec drizzle-kit check
  • git diff --check
  • Desktop and 320px browser checks, including accessible labels and horizontal-overflow verification

Repository-wide pnpm lint continues to report the two pre-existing @typescript-eslint/no-require-imports errors in babel.config.js and postcss.config.js; changed source files pass lint.

Known limitations

  • The generic connector uses username and app-password authentication; OAuth-only providers need provider-specific connectors. Gmail continues to use its native OAuth integration.
  • IMAP refreshes when mailbox views load or refresh rather than using a long-running IDLE or push connection.
  • Message bodies and attachments remain with the provider and are not imported into Postgres.
  • IMAP messages are displayed individually rather than reconstructed into provider-specific conversation threads.

Closes #5.

Activity

4
mikeyreviewed2mo ago
mikey2mo ago

Summary

Reviewed the full diff against 2f48341..f84e0dd (23 files, +2430/-31), focused on the IMAP/SMTP connector: lib/imap/config.ts, lib/imap/provider.ts, lib/imap/mime.ts, the account API routes, lib/data/mail-accounts.ts, lib/mail/get-provider.ts/accounts.ts, the settings UI, and the schema migration.

No blocking issues found. Specifically checked and confirmed sound:

  • SSRF guard (resolvePublicMailHost in lib/imap/config.ts): rejects IP literals and bare hostnames, resolves via DNS, rejects if any returned address is non-unicast (blocks private/loopback/link-local/ULA/reserved), and — importantly — connects using the resolved IP literal as host with servername set separately for TLS SNI, so there's no DNS-rebinding TOCTOU between the check and the actual connection. This same check runs both at connect-time (connectImapAccount) and on every subsequent use (ImapProvider.withClient/send), so a host can't be validated once and swapped later.
  • Authorization/IDOR: all stored-account reads/writes (findStoredMailAccount, listStoredMailAccounts, delete) are scoped by userId, and connectImapAccount only reuses an existing row ID after filtering the caller's own accounts by email — no path lets one user target another user's mailAccounts row.
  • Credential handling: passwords are encrypted at rest via the existing encryptSecret/decryptSecret keyring, and storedMailAccountToDescriptor (the shape returned to the client/API) never includes encryptedPassword.
  • XSS: no dangerouslySetInnerHTML introduced; React escaping covers the new settings form and account list rendering.

Non-blocking follow-up

  • Test coverage gap: lib/imap/config.ts adds the SSRF allowlist (resolvePublicMailHost, publicAddress, HOST_PATTERN) and input normalization (normalizeImapAccountInput) — the most security-sensitive new logic in this PR — but no unit tests were added for either (only lib/imap/mime.test.mjs was added, covering MIME parsing/ID encoding). Given this function is the sole barrier against an authenticated user pointing the server at internal infrastructure, I'd want direct coverage for: private/loopback/link-local rejection, mixed public+private A/AAAA records, IP-literal hostnames, and the "no dot" bare-hostname case, before this logic is refactored by someone unfamiliar with the threat model.

Verification performed: read the full diff, cross-referenced getMailProvider, saveMailAccount/findStoredMailAccount scoping, and the account descriptor serialization to confirm no credential leakage or cross-user access path.

niku2676reviewed2mo ago