Skip to content

fix: domain setup, Cloudflare API paging and errors, login and auth cleanup - #322

Merged
hieunc229 merged 14 commits into
hieunc229:mainfrom
bobmcdonald0:fix/domains-auth
Nov 18, 2026
Merged

hieunc229 merged 14 commits into
hieunc229:mainfrom
bobmcdonald0:fix/domains-auth

Conversation

@bobmcdonald0

Copy link
Copy Markdown
Contributor

Fixes for adding domains, Cloudflare API handling and login, plus two small permission-consistency changes.

Domains and Cloudflare

  • Domain check without Cloudflare credentials: the check route now uses the setup preflight, so a manual zone (or Skip DNS) no longer fails with 502 "CF_TOKEN or CF_API_KEY is not configured".
  • MX replacement is safe if a later step fails. For Resend/SES the new MX is created before the old ones are deleted. For Cloudflare routing and the sending subdomain, the deleted records are recreated on failure, and the error names any that couldn't be.
  • MX consent on add: the confirmed "replace MX" flag now reaches the sending step when receiving isn't Cloudflare, instead of answering 409 again.
  • Hostname input: one normalizeHostname (trim, lowercase, strip trailing dot, punycode), and the value must look like a domain. example.com., münchen.de and https://example.com now behave; IP addresses are rejected.
  • Mailbox names: POST /api/mailboxes uses the same character check as accounts and aliases, so bob smith or a@b can't be created. Dot and +tag collisions are not changed here.
  • Cloudflare API: an HTML 5xx page is raised as CloudflareApiError with its status instead of a JSON SyntaxError, and requests time out after 30 s.
  • Routing rules: the list follows every page (per_page=50, total_pages), so rules past page 1 are no longer missed when creating or deleting mailboxes. It is fetched once per zone per operation instead of once per address.

Auth

  • Login email is trimmed and lowercased on web and device login, matching password reset.
  • API and MCP key lookups read from the primary database, as session lookup already does, so replica lag doesn't affect them.
  • Daily cleanup of expired sessions, login challenges, reset tokens and passkey enrolment links: batched (90 ids × 50 batches per table per run), no schema change.

Permission consistency

  • Account list/detail responses include recovery and forwarding addresses only for admins who can manage users. The list itself is unchanged.
  • Starring a message needs write access, matching JMAP. Read-only delegates get 403, and the list shows an error.

Rebased on current main: tsc is clean, lint has 0 errors, and node --test passes 709 of 711 (2 skipped).

🤖 Generated with Claude Code

bobmcdonald0 and others added 14 commits November 16, 2026 17:44
Ids: bugs-login-email-case

Emails are stored lowercase, so signing in with "Alice@Example.com" (for
example a keyboard that capitalises the first letter) returned invalid
credentials while password reset for the same text worked.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Ids: bugs-domains-check-no-cf-creds

Without Cloudflare credentials (or with Skip DNS ticked) the domain check
answered 502 "CF_TOKEN or CF_API_KEY is not configured" and the domain
could not be added, although the server adds it as a manual zone.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Ids: bugs-mx-delete-no-restore

When an operator confirmed replacing another provider's MX records and a
later step failed, the old MX was already deleted and the domain had no MX.

- Resend/SES receiving: the new MX is created first and the old ones are
  deleted afterwards, so a rejected create deletes nothing.
- Cloudflare Email Routing and the sending subdomain: provisioning needs the
  old records removed first, so they are deleted and then recreated if the
  next step fails. Any Cloudflare MX created by a half-finished routing
  setup is removed first. If the records cannot be recreated the error says
  which ones, instead of only logging it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Ids: bugs-hostname-validation

"example.com." was stored with the trailing dot and treated as a subdomain
of its own zone, "münchen.de" failed with "Zone not found" because zone
names are punycode, and values such as "https://example.com" or "a b.com"
were stored as given. Hostnames now go through one normalizeHostname
(trim, lowercase, strip trailing dot, punycode) and must look like a domain.
IP addresses, including forms the URL parser rewrites such as "0x7f.1",
are rejected.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Ids: bugs-mailbox-localpart-validation

POST /api/mailboxes accepted any text, so "bob smith" or "a@b" created a
mailbox that could never receive mail. The schema now uses the same
character check as the account and alias schemas. Collisions between
names that differ only by dots or +tag are not changed here.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Ids: bugs-add-domain-mx-replace-ignored

Adding a domain with Cloudflare sending and another receiving provider
answered 409 MX conflict again after the user confirmed, because the
confirmation was dropped whenever receiving was not Cloudflare. The
routing step is already skipped in that case, so the flag only affects
the sending subdomain.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…a timeout

Ids: bugs-cfrequest-non-json

During Cloudflare incidents the API answers with an HTML 5xx page; the
request helper threw a JSON SyntaxError instead of CloudflareApiError, so
the status-based handling never saw it. A hung connection also waited
forever. Requests now time out after 30 s and a body that is not JSON is
raised as a CloudflareApiError carrying the HTTP status.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Ids: bugs-cf-rules-pagination

The rules list was read without paging, so in a zone with more rules than
one page, rules past the first page were invisible: creating a mailbox
could try to add a duplicate rule, deleting a mailbox or domain left the
Worker route behind. The list now requests per_page=50 and follows
result_info.total_pages. If the list is longer than the page cap it throws
instead of returning a partial list.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Ids: perf-routing-rule-list-per-address

Adding a domain or enabling Cloudflare receiving re-fetched the zone's
routing rules for every address of every mailbox at the same time, so a
user with many mailboxes issued dozens of identical requests and could hit
request limits, leaving some mailboxes without a route. The listing is now
shared per zone within one operation.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Ids: bugs-apikey-replica-read (read from primary)

Session lookup already reads the primary database so it is not affected by
replica lag; API key and MCP key lookups used the replica-capable session.
They now read the key row and its user from the primary like session
lookup.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…daily

Ids: db-expired-auth-rows-never-purged

Sessions not ended by a logout, abandoned MFA logins and reset requests
left rows behind forever, growing the tables and every backup. The daily
scheduled run (Worker cron and Node scheduler) now deletes rows whose
expiry has passed, including expired passkey enrolment links. Deletes run
in batches of 90 ids with a cap of 50 batches per table per run, so a
first run over a large backlog stays bounded and continues the next day.
Deleting a session also removes its push subscription, as logout does.
No schema change.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…managers

Ids: accounts-list-contacts (permission consistency)

The accounts list and detail responses included each account's recovery
and forwarding address for any admin, while changing accounts requires the
manage-users permission. Those fields are now returned only to admins who
can manage users; the list itself is unchanged so account pickers keep
working.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Ids: star-read-only (permission consistency)

The star toggle only checked read access, although the flag is stored on
the shared message and JMAP already requires a permission above read-only
to change it. Read-only delegates now get a 403, matching JMAP, and the
message list shows an error when the toggle fails.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
# Conflicts:
#	src/components/messages/message-folder-page.tsx
@hieunc229
hieunc229 merged commit aa6d267 into hieunc229:main Nov 18, 2026
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants