From c50775b3e62095f9a29c1857a47a1500559fe420 Mon Sep 17 00:00:00 2001 From: bobmcdonald0 <266144304+bobmcdonald0@users.noreply.github.com> Date: Sat, 14 Nov 2026 01:39:03 +0530 Subject: [PATCH 01/13] fix(auth): lowercase and trim the email on web and device login 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 --- src/lib/validators.ts | 4 ++-- tests/auth-email-case.test.mjs | 35 ++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 tests/auth-email-case.test.mjs diff --git a/src/lib/validators.ts b/src/lib/validators.ts index c274a90d5..28b246f74 100644 --- a/src/lib/validators.ts +++ b/src/lib/validators.ts @@ -93,7 +93,7 @@ export const addDomainSchema = z.object({ }); export const loginSchema = z.object({ - email: z.string().email(), + email: z.string().trim().toLowerCase().email(), password: z.string().min(1), }); @@ -108,7 +108,7 @@ export const passwordResetConfirmSchema = z.object({ /** `/api/auth/device`: either email + password, or a challenge + code after MFA. */ export const deviceLoginSchema = z.union([ - z.object({ email: z.string().trim().email(), password: z.string().min(1), deviceName: z.string().trim().min(1).max(100) }), + z.object({ email: z.string().trim().toLowerCase().email(), password: z.string().min(1), deviceName: z.string().trim().min(1).max(100) }), z.object({ challengeToken: z.string().min(8).max(200), code: z.string().trim().min(6).max(32), deviceName: z.string().trim().min(1).max(100) }), ]); diff --git a/tests/auth-email-case.test.mjs b/tests/auth-email-case.test.mjs new file mode 100644 index 000000000..34ee13004 --- /dev/null +++ b/tests/auth-email-case.test.mjs @@ -0,0 +1,35 @@ +import assert from "node:assert/strict"; +import { mkdtempSync, rmSync } from "node:fs"; +import { dirname, join } from "node:path"; +import test, { after } from "node:test"; +import { fileURLToPath, pathToFileURL } from "node:url"; +import { build } from "esbuild"; + +const root = join(dirname(fileURLToPath(import.meta.url)), ".."); +const directory = mkdtempSync(join(root, "tests", ".tmp-auth-email-case-")); +after(() => rmSync(directory, { recursive: true, force: true })); +await build({ + stdin: { + contents: `export { loginSchema, deviceLoginSchema } from "./src/lib/validators.ts";`, + resolveDir: root, + sourcefile: "auth-email-case-entry.ts", + }, + outfile: join(directory, "entry.mjs"), + bundle: true, + platform: "node", + format: "esm", + target: "node24", + tsconfig: join(root, "tsconfig.json"), + packages: "external", + logLevel: "silent", +}); +const { loginSchema, deviceLoginSchema } = await import(pathToFileURL(join(directory, "entry.mjs")).href); + +test("web login matches the lowercase stored email", () => { + assert.equal(loginSchema.parse({ email: " Alice@Example.COM ", password: "x" }).email, "alice@example.com"); +}); + +test("device login matches the lowercase stored email", () => { + const parsed = deviceLoginSchema.parse({ email: "Alice@Example.COM", password: "x", deviceName: "Phone" }); + assert.equal(parsed.email, "alice@example.com"); +}); From f8fb38850f8501ca50bc282a2041f5f03e9a58a8 Mon Sep 17 00:00:00 2001 From: bobmcdonald0 <266144304+bobmcdonald0@users.noreply.github.com> Date: Sat, 14 Nov 2026 01:43:16 +0530 Subject: [PATCH 02/13] fix(domains): use the setup preflight for the domain check route 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 --- src/app/api/domains/check/route.ts | 4 ++-- tests/domains-check-route.test.mjs | 38 ++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 tests/domains-check-route.test.mjs diff --git a/src/app/api/domains/check/route.ts b/src/app/api/domains/check/route.ts index b5d9e486c..181042f92 100644 --- a/src/app/api/domains/check/route.ts +++ b/src/app/api/domains/check/route.ts @@ -1,7 +1,7 @@ import { NextResponse } from "next/server"; import { requireUser } from "@/lib/auth/cookies"; import { getEnv } from "@/lib/cloudflare"; -import { preflightDomain } from "@/lib/domains/preflight"; +import { preflightSetupDomain } from "@/lib/domains/preflight"; import { setupDomainSchema } from "@/lib/validators"; export async function POST(request: Request) { @@ -13,7 +13,7 @@ export async function POST(request: Request) { } try { - return NextResponse.json({ domain: await preflightDomain(env, parsed.data.hostname) }); + return NextResponse.json({ domain: await preflightSetupDomain(env, parsed.data.hostname, { skipDns: parsed.data.skipDns }) }); } catch (error) { const message = error instanceof Error ? error.message : "Domain check failed"; return NextResponse.json({ error: message }, { status: 502 }); diff --git a/tests/domains-check-route.test.mjs b/tests/domains-check-route.test.mjs new file mode 100644 index 000000000..e1b769054 --- /dev/null +++ b/tests/domains-check-route.test.mjs @@ -0,0 +1,38 @@ +import assert from "node:assert/strict"; +import { mkdtempSync, rmSync } from "node:fs"; +import { dirname, join } from "node:path"; +import { fileURLToPath, pathToFileURL } from "node:url"; +import test, { after } from "node:test"; +import { build } from "esbuild"; + +const project = join(dirname(fileURLToPath(import.meta.url)), ".."); +const temporary = mkdtempSync(join(project, "tests", ".tmp-domains-check-")); +const realFetch = globalThis.fetch; +after(() => { rmSync(temporary, { recursive: true, force: true }); delete globalThis.checkEnv; globalThis.fetch = realFetch; }); +const mocks = { + "@/lib/cloudflare": "export function getEnv(){return globalThis.checkEnv}", + "@/lib/auth/cookies": "export async function requireUser(){return {id:'user'}}", + "next/server": "export const NextResponse = { json: (body, init) => Response.json(body, init) }", +}; +await build({ entryPoints: [join(project, "src/app/api/domains/check/route.ts")], outfile: join(temporary, "route.mjs"), bundle: true, platform: "node", format: "esm", packages: "external", alias: { "@": join(project, "src") }, logLevel: "silent", plugins: [{ name: "route-fixtures", setup(builder) { + builder.onResolve({ filter: /^@\/lib\/(cloudflare|auth\/cookies)$|^next\/server$/ }, args => ({ path: args.path, namespace: "mock" })); + builder.onLoad({ filter: /.*/, namespace: "mock" }, args => ({ contents: mocks[args.path], loader: "js" })); +} }] }); +const { POST } = await import(pathToFileURL(join(temporary, "route.mjs")).href); + +const post = (body) => POST(new Request("http://localhost/api/domains/check", { method: "POST", body: JSON.stringify(body) })); + +test("without Cloudflare credentials the check answers with a manual zone instead of failing", async () => { + globalThis.checkEnv = {}; + globalThis.fetch = async () => { throw new Error("must not call the Cloudflare API"); }; + const response = await post({ hostname: "Example.com" }); + assert.equal(response.status, 200); + assert.deepEqual((await response.json()).domain.zone.name, "example.com"); +}); + +test("skipDns answers with a manual zone even when credentials exist", async () => { + globalThis.checkEnv = { CF_TOKEN: "token" }; + globalThis.fetch = async () => { throw new Error("must not call the Cloudflare API"); }; + const response = await post({ hostname: "example.com", skipDns: true }); + assert.equal(response.status, 200); +}); From 31cf7b691872a8e277c286405ef10f63edea0891 Mon Sep 17 00:00:00 2001 From: bobmcdonald0 <266144304+bobmcdonald0@users.noreply.github.com> Date: Sat, 14 Nov 2026 01:43:30 +0530 Subject: [PATCH 03/13] fix(domains): keep MX records safe when replacing another provider's 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 --- src/lib/domains/dns-setup.ts | 17 +++- src/lib/domains/mx-records.ts | 44 +++++++++- src/lib/domains/receiving-dns.ts | 4 +- src/lib/domains/receiving.ts | 27 +++++- tests/domain-mx-restore.test.mjs | 115 +++++++++++++++++++++++++ tests/domain-receiving-status.test.mjs | 2 +- 6 files changed, 199 insertions(+), 10 deletions(-) create mode 100644 tests/domain-mx-restore.test.mjs diff --git a/src/lib/domains/dns-setup.ts b/src/lib/domains/dns-setup.ts index 72ddf20a8..ac1139d9a 100644 --- a/src/lib/domains/dns-setup.ts +++ b/src/lib/domains/dns-setup.ts @@ -9,6 +9,8 @@ import { isManualZone, shouldBindEmailCatchAllToWorker, } from "@/lib/domains/provision"; +import { restoreMxRecordsAfterFailure } from "@/lib/domains/mx-records"; +import type { CfDnsRecord } from "@/lib/cloudflare-api.types"; import { ensureSendingSubdomain } from "@/lib/domains/sending-subdomain"; import type { DomainRow } from "@/lib/domains/types"; import { isZoneApex } from "@/lib/domains/utils"; @@ -58,9 +60,18 @@ export async function setupDomainDnsRecord( // The stored tag can be stale or empty even though the subdomain exists // on Cloudflare, so reuse what is there instead of creating a duplicate // (which Cloudflare rejects with "Subdomain already exists"). - const { subdomain } = await ensureSendingSubdomain(env, domain.zoneId, domain.hostname, { - replaceMx: options.replaceMx, - }); + const deletedMx: CfDnsRecord[] = []; + let subdomain; + try { + ({ subdomain } = await ensureSendingSubdomain(env, domain.zoneId, domain.hostname, { + replaceMx: options.replaceMx, + deletedMx, + })); + } catch (error) { + // Replaced MX records come back if the sending subdomain still could not be created; + // if they cannot, that is reported rather than logged. + return restoreMxRecordsAfterFailure(env, domain.zoneId, deletedMx, error); + } await getDb(env) .update(domains) .set({ diff --git a/src/lib/domains/mx-records.ts b/src/lib/domains/mx-records.ts index c34532a36..dc460b49f 100644 --- a/src/lib/domains/mx-records.ts +++ b/src/lib/domains/mx-records.ts @@ -59,12 +59,54 @@ export async function hasConflictingMxRecords( return records.some((record) => !isCloudflareEmailRoutingMx(record)); } +/** Re-creates every record it can, then reports the ones it could not. */ export async function restoreMxRecords( env: CloudflareEnv, zoneId: string, records: CfDnsRecord[], ): Promise { + const failed: CfDnsRecord[] = []; + const reasons: unknown[] = []; for (const record of records) { - await createDnsRecord(env, zoneId, toCreateInput(record)); + try { + await createDnsRecord(env, zoneId, toCreateInput(record)); + } catch (error) { + failed.push(record); + reasons.push(error); + } } + if (failed.length > 0) throw new MxRestoreError(failed, reasons); +} + +/** The MX records a failed change deleted could not be put back; the domain may have no MX. */ +export class MxRestoreError extends AggregateError { + constructor(readonly records: CfDnsRecord[], reasons: unknown[], failure?: string) { + super( + reasons, + `${failure ? `${failure}. ` : ""}The previous MX records (${records.map((record) => `${record.priority ?? "?"} ${record.content ?? "?"}`).join(", ")}) could not be restored. Add them back at your DNS provider.`, + ); + this.name = "MxRestoreError"; + } +} + +/** + * Cleanup for a change that deleted MX records and then failed: put them back and + * rethrow the original error. When they cannot be restored the failure is raised + * instead (with the original error as `cause`), so it is never only a log line. + */ +export async function restoreMxRecordsAfterFailure( + env: CloudflareEnv, + zoneId: string, + deleted: CfDnsRecord[], + original: unknown, +): Promise { + if (deleted.length > 0) { + try { + await restoreMxRecords(env, zoneId, deleted); + } catch (restoreError) { + const failure = restoreError as MxRestoreError; + throw new MxRestoreError(failure.records ?? deleted, [...(failure.errors ?? [restoreError]), original], original instanceof Error ? original.message : String(original)); + } + } + throw original; } diff --git a/src/lib/domains/receiving-dns.ts b/src/lib/domains/receiving-dns.ts index 24f47f20e..8547e4455 100644 --- a/src/lib/domains/receiving-dns.ts +++ b/src/lib/domains/receiving-dns.ts @@ -59,10 +59,12 @@ export async function publishMx( if (others.length > 0 && !options.replace) { throw new MxConflictError(others.map((record) => ({ content: record.content ?? "", priority: record.priority ?? 0 }))); } - for (const record of others) if (record.id) await deleteDnsRecord(env, domain.zoneId, record.id); + // Create the new record first: if it is rejected nothing has been deleted, so the + // domain keeps receiving mail where it did. if (!existing.some((record) => same(record.content ?? "", content))) { await createDnsRecord(env, domain.zoneId, { type: "MX", name: domain.hostname, content, priority: options.priority ?? 10, ttl: 3600 }); } + for (const record of others) if (record.id) await deleteDnsRecord(env, domain.zoneId, record.id); return others; } diff --git a/src/lib/domains/receiving.ts b/src/lib/domains/receiving.ts index 7bc9b9085..37246962d 100644 --- a/src/lib/domains/receiving.ts +++ b/src/lib/domains/receiving.ts @@ -4,7 +4,7 @@ import { domains, mailboxes } from "@/db/schema"; import { disableEmailRouting } from "@/lib/cloudflare-api"; import { deleteDnsRecord } from "@/lib/cloudflare-dns"; import { deleteEmailRoutingRulesForDomain } from "@/lib/domains/cloudflare-cleanup"; -import { removeMxRecords } from "@/lib/domains/mx-records"; +import { removeMxRecords, restoreMxRecordsAfterFailure } from "@/lib/domains/mx-records"; import { setupDomainDnsRecord } from "@/lib/domains/dns-setup"; import { adoptCloudflareZone } from "@/lib/domains/adopt-zone"; import { isManualZone } from "@/lib/domains/provision"; @@ -12,6 +12,7 @@ import { listDomainMx, MxConflictError } from "@/lib/domains/receiving-dns"; import { removeResendReceiving, setupResendReceiving, hasResendReceivingConfig } from "@/lib/domains/resend-receiving"; import { hasSesReceivingConfig, removeSesReceiving, setupSesReceiving } from "@/lib/aws/ses-receiving"; import { ensureMailboxDomainRouting } from "@/lib/mailboxes/domain-addresses"; +import type { CfDnsRecord } from "@/lib/cloudflare-api.types"; import type { DomainRow } from "@/lib/domains/types"; export type ReceivingProviderId = "none" | "cloudflare" | "resend" | "ses"; @@ -23,12 +24,24 @@ const isCloudflareMx = (content: string) => content.toLowerCase().replace(/\.$/, export async function setupCloudflareReceiving(env: CloudflareEnv, skippedDomain: DomainRow, options: { replaceMx: boolean }): Promise { const domain = await adoptCloudflareZone(env, skippedDomain); if (isManualZone(domain.zoneId)) throw new Error("Cloudflare Email Routing needs a domain on a Cloudflare zone"); - const foreign = (await listDomainMx(env, domain)).filter((record) => !isCloudflareMx(record.content ?? "")); + const mxBefore = await listDomainMx(env, domain); + const foreign = mxBefore.filter((record) => !isCloudflareMx(record.content ?? "")); if (foreign.length > 0 && !options.replaceMx) { throw new MxConflictError(foreign.map((record) => ({ content: record.content ?? "", priority: record.priority ?? 0 }))); } - if (foreign.length > 0) await removeMxRecords(env, domain.zoneId, domain.hostname, []); - await setupDomainDnsRecord(env, domain, "mx"); + const deletedMx: CfDnsRecord[] = []; + const knownMxIds = new Set(mxBefore.map((record) => record.id)); + try { + // Like provisioning, remove the other service's MX before enabling routing; they + // are put back below if the rest of the setup fails. + if (foreign.length > 0) await removeMxRecords(env, domain.zoneId, domain.hostname, deletedMx); + await setupDomainDnsRecord(env, domain, "mx"); + } catch (error) { + if (deletedMx.length === 0) throw error; + // Enabling routing can fail after Cloudflare created its MX; drop those so the old records are not left beside them. + await removeCloudflareMxCreatedSince(env, domain, knownMxIds).catch((cleanupError) => console.warn("Removing partial Cloudflare MX failed", cleanupError)); + return restoreMxRecordsAfterFailure(env, domain.zoneId, deletedMx, error); + } // Cleaning up Cloudflare receiving deletes the per-address Worker routes; put them back. const db = getDb(env); @@ -50,6 +63,12 @@ export async function removeCloudflareReceiving(env: CloudflareEnv, domain: Doma await db.update(domains).set({ routingEnabled: false }).where(eq(domains.id, domain.id)); } +async function removeCloudflareMxCreatedSince(env: CloudflareEnv, domain: DomainRow, knownIds: Set): Promise { + for (const record of await listDomainMx(env, domain)) { + if (record.id && !knownIds.has(record.id) && isCloudflareMx(record.content ?? "")) await deleteDnsRecord(env, domain.zoneId, record.id); + } +} + async function removeCloudflareMx(env: CloudflareEnv, domain: DomainRow): Promise { for (const record of await listDomainMx(env, domain)) { if (record.id && isCloudflareMx(record.content ?? "")) await deleteDnsRecord(env, domain.zoneId, record.id); diff --git a/tests/domain-mx-restore.test.mjs b/tests/domain-mx-restore.test.mjs new file mode 100644 index 000000000..680c1acd2 --- /dev/null +++ b/tests/domain-mx-restore.test.mjs @@ -0,0 +1,115 @@ +import assert from "node:assert/strict"; +import { mkdtempSync, rmSync } from "node:fs"; +import { dirname, join } from "node:path"; +import test, { after, beforeEach } from "node:test"; +import { fileURLToPath, pathToFileURL } from "node:url"; +import { build } from "esbuild"; + +// Replacing another service's MX records must not leave the domain without MX: +// Cloudflare-managed paths delete first and restore on failure (and report a failed +// restore); publishMx creates the new record before deleting anything. + +const root = join(dirname(fileURLToPath(import.meta.url)), ".."); +const outDir = mkdtempSync(join(root, "tests", ".tmp-domain-mx-restore-")); +after(() => rmSync(outDir, { recursive: true, force: true })); + +async function bundle(name, entry, mocks) { + await build({ + stdin: { contents: entry, resolveDir: root, loader: "ts" }, + outfile: join(outDir, `${name}.mjs`), + bundle: true, + platform: "node", + format: "esm", + packages: "external", + alias: { "@": join(root, "src") }, + plugins: [{ + name: "mock-deps", + setup(builder) { + const names = Object.keys(mocks).map((key) => key.replace(/[/.-]/g, "\\$&")).join("|"); + builder.onResolve({ filter: new RegExp(`^(${names})$`) }, (args) => ({ path: args.path, namespace: "mock" })); + builder.onLoad({ filter: /.*/, namespace: "mock" }, (args) => ({ contents: mocks[args.path], loader: "js" })); + }, + }], + logLevel: "silent", + }); + return import(pathToFileURL(join(outDir, `${name}.mjs`)).href); +} + +const oldMx = { id: "mx1", type: "MX", name: "example.com", content: "mx.old-provider.test", priority: 10, ttl: 3600 }; + +const flow = await bundle( + "flow", + "export { setupCloudflareReceiving } from './src/lib/domains/receiving';export { setupDomainDnsRecord } from './src/lib/domains/dns-setup';", + { + "drizzle-orm": "export const eq=(c,v)=>({eq:[c,v]});export const and=(...a)=>({and:a});export const or=(...a)=>({or:a});export const ne=(c,v)=>({ne:[c,v]});", + "@/db/schema": "export const domains={id:'domains.id'};export const mailboxes={};", + "@/db": "export function getDb(){return {select(){throw new Error('unexpected')},update(){return {set(){return {where(){}}}}}}}", + "@/lib/cloudflare-api": "export async function enableEmailRouting(){globalThis.__mx.push({id:'cfmx',content:'route1.mx.cloudflare.net',priority:1});throw new Error('routing failed')}export async function getZone(){return {name:'example.com'}}export async function disableEmailRouting(){}", + "@/lib/cloudflare-dns": "export async function createDnsRecord(env,zone,input){if(globalThis.__failRestore)throw new Error('create rejected');globalThis.__restored.push(input)}export async function listDnsRecords(){return []}export async function listMxRecords(){return globalThis.__mx}export async function deleteDnsRecord(env,zone,id){globalThis.__deleted.push(id);globalThis.__mx=globalThis.__mx.filter((r)=>r.id!==id)}", + "@/lib/domains/catch-all-routing": "export async function ensureEmailRoutingCatchAllToWorker(){}", + "@/lib/domains/provision": "export const isManualZone=(z)=>z==='manual';export const shouldBindEmailCatchAllToWorker=()=>false;", + "@/lib/domains/sending-subdomain": "export async function ensureSendingSubdomain(env,zoneId,hostname,options){const mx=globalThis.__mx.filter((r)=>!r.content.endsWith('.mx.cloudflare.net'));for(const r of mx){options.deletedMx?.push(r);globalThis.__mx=globalThis.__mx.filter((x)=>x.id!==r.id)}throw new Error('sending failed')}", + "@/lib/domains/cloudflare-cleanup": "export async function deleteEmailRoutingRulesForDomain(){}", + "@/lib/domains/adopt-zone": "export async function adoptCloudflareZone(env,d){return d;}", + "@/lib/domains/receiving-dns": "export async function listDomainMx(){return globalThis.__mx}export class MxConflictError extends Error{}", + "@/lib/domains/resend-receiving": "export async function setupResendReceiving(){}export async function removeResendReceiving(){}export async function hasResendReceivingConfig(){return false}", + "@/lib/aws/ses-receiving": "export async function setupSesReceiving(){}export async function removeSesReceiving(){}export async function hasSesReceivingConfig(){return false}", + "@/lib/mailboxes/domain-addresses": "export async function ensureMailboxDomainRouting(){}", + }, +); + +const publish = await bundle( + "publish", + "export { publishMx } from './src/lib/domains/receiving-dns';", + { + "@/lib/dns-query": "export async function queryDns(){return []}", + "@/lib/cloudflare-dns": "export async function listMxRecords(){return [globalThis.__oldMx]}export async function deleteDnsRecord(env,zone,id){globalThis.__log.push('delete '+id)}export async function createDnsRecord(env,zone,input){globalThis.__log.push('create '+input.content);if(globalThis.__createFails)throw new Error('create rejected')}", + "@/lib/domains/provision": "export const isManualZone=(z)=>z==='manual';", + }, +); + +const domain = { id: "d1", userId: "u1", hostname: "example.com", zoneId: "zone-1", routingEnabled: false, routingStatus: null }; + +beforeEach(() => { + globalThis.__oldMx = oldMx; + globalThis.__mx = [oldMx]; + globalThis.__restored = []; + globalThis.__deleted = []; + globalThis.__log = []; + globalThis.__failRestore = false; + globalThis.__createFails = false; +}); + +test("replacing MX restores the old records and drops the half-created Cloudflare MX when routing fails", async () => { + await assert.rejects(flow.setupCloudflareReceiving({}, domain, { replaceMx: true }), /^Error: routing failed$/); + assert.deepEqual(globalThis.__deleted, ["mx1", "cfmx"]); + assert.deepEqual(globalThis.__restored.map((record) => record.content), ["mx.old-provider.test"]); + assert.deepEqual(globalThis.__mx.map((record) => record.id), []); +}); + +test("a failed restore is raised, naming the records and carrying the original error", async () => { + globalThis.__failRestore = true; + await assert.rejects(flow.setupCloudflareReceiving({}, domain, { replaceMx: true }), (error) => { + assert.equal(error.name, "MxRestoreError"); + assert.match(error.message, /routing failed/); + assert.match(error.message, /mx\.old-provider\.test/); + assert.ok(error.errors.some((reason) => reason.message === "routing failed")); + return true; + }); +}); + +test("replacing MX restores the old records when the sending subdomain cannot be created", async () => { + await assert.rejects(flow.setupDomainDnsRecord({}, domain, "dkim", { replaceMx: true }), /sending failed/); + assert.deepEqual(globalThis.__restored.map((record) => record.id ?? record.content), ["mx.old-provider.test"]); +}); + +test("publishMx creates the new MX before deleting the old one", async () => { + await publish.publishMx({}, domain, "inbound.new-provider.test", { replace: true }); + assert.deepEqual(globalThis.__log, ["create inbound.new-provider.test", "delete mx1"]); +}); + +test("publishMx deletes nothing when the new record is rejected", async () => { + globalThis.__createFails = true; + await assert.rejects(publish.publishMx({}, domain, "inbound.new-provider.test", { replace: true }), /create rejected/); + assert.deepEqual(globalThis.__log, ["create inbound.new-provider.test"]); +}); diff --git a/tests/domain-receiving-status.test.mjs b/tests/domain-receiving-status.test.mjs index 13bd5e487..913055e20 100644 --- a/tests/domain-receiving-status.test.mjs +++ b/tests/domain-receiving-status.test.mjs @@ -22,7 +22,7 @@ const mocks = { "@/lib/domains/provision": "export const isManualZone=(z)=>z==='manual';export const shouldBindEmailCatchAllToWorker=()=>false;", "@/lib/domains/sending-subdomain": "export async function ensureSendingSubdomain(){return {subdomain:globalThis.__subdomain};}", "@/lib/domains/cloudflare-cleanup": "export async function deleteEmailRoutingRulesForDomain(){}", - "@/lib/domains/mx-records": "export async function removeMxRecords(){}", + "@/lib/domains/mx-records": "export async function removeMxRecords(){}export async function restoreMxRecords(){}export async function restoreMxRecordsAfterFailure(){}", "@/lib/domains/adopt-zone": "export async function adoptCloudflareZone(env,d){return d;}", "@/lib/domains/receiving-dns": "export async function listDomainMx(){return [];}export class MxConflictError extends Error{}", "@/lib/domains/resend-receiving": "export async function setupResendReceiving(){}export async function removeResendReceiving(){}export async function hasResendReceivingConfig(){return false;}", From 2e8c66a419ce1bc47f63c6e58fd27b1030b74d6e Mon Sep 17 00:00:00 2001 From: bobmcdonald0 <266144304+bobmcdonald0@users.noreply.github.com> Date: Sat, 14 Nov 2026 01:45:23 +0530 Subject: [PATCH 04/13] fix(domains): normalise and validate domain names on input MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- src/lib/domains/preflight.ts | 5 +-- src/lib/domains/provision.ts | 4 +-- src/lib/domains/service.ts | 3 +- src/lib/domains/utils.ts | 23 ++++++++++++ src/lib/validators.ts | 15 +++++--- tests/domain-input-validation.test.mjs | 50 ++++++++++++++++++++++++++ 6 files changed, 91 insertions(+), 9 deletions(-) create mode 100644 tests/domain-input-validation.test.mjs diff --git a/src/lib/domains/preflight.ts b/src/lib/domains/preflight.ts index 6fd906d93..78210450e 100644 --- a/src/lib/domains/preflight.ts +++ b/src/lib/domains/preflight.ts @@ -1,3 +1,4 @@ +import { normalizeHostname } from "@/lib/domains/utils"; import { findZoneByHostname } from "@/lib/cloudflare-api"; import { MANUAL_ZONE_ID } from "@/lib/domains/provision"; import type { DomainPreflightResult } from "@/lib/domains/types"; @@ -7,7 +8,7 @@ export async function preflightDomain( env: CloudflareEnv, hostname: string, ): Promise { - const normalized = hostname.toLowerCase().trim(); + const normalized = normalizeHostname(hostname); const zone = await findZoneByHostname(env, normalized); if (!zone) { throw new Error( @@ -20,7 +21,7 @@ export async function preflightDomain( /** A domain this install will not manage on Cloudflare. Same zone id provisionDomainOnCloudflare records. */ export function manualDomainPreflight(hostname: string): DomainPreflightResult { - const normalized = hostname.toLowerCase().trim(); + const normalized = normalizeHostname(hostname); return { hostname: normalized, zone: { id: MANUAL_ZONE_ID, name: normalized } }; } diff --git a/src/lib/domains/provision.ts b/src/lib/domains/provision.ts index c47ace421..16cabb750 100644 --- a/src/lib/domains/provision.ts +++ b/src/lib/domains/provision.ts @@ -7,7 +7,7 @@ import { ensureEmailRoutingCatchAllToWorker, getEmailRoutingCatchAll, } from "@/lib/domains/catch-all-routing"; -import { isZoneApex } from "@/lib/domains/utils"; +import { isZoneApex, normalizeHostname } from "@/lib/domains/utils"; import { hasCloudflareCredentials, isNodeRuntime } from "@/lib/runtime"; import type { DomainProvisioningChanges, DomainProvisioningResult } from "@/lib/domains/types"; import { removeMxRecords } from "@/lib/domains/mx-records"; @@ -37,7 +37,7 @@ export async function provisionDomainOnCloudflare( hostname: string, options?: { enableRouting?: boolean; enableSending?: boolean; replaceMxRecords?: boolean; skipDns?: boolean }, ): Promise { - const normalized = hostname.toLowerCase().trim(); + const normalized = normalizeHostname(hostname); if (options?.skipDns || !hasCloudflareCredentials(env)) { // Nothing to provision: the operator points MX at this server themselves, // or chose to skip DNS for now. diff --git a/src/lib/domains/service.ts b/src/lib/domains/service.ts index 861a24502..8ce1dec61 100644 --- a/src/lib/domains/service.ts +++ b/src/lib/domains/service.ts @@ -19,6 +19,7 @@ import { rollbackDomainProvisioning } from "@/lib/domains/rollback"; import type { DomainProvisioningChanges } from "@/lib/domains/types"; import { findSendingSubdomain } from "@/lib/domains/sending-status"; import { preflightDomain } from "@/lib/domains/preflight"; +import { normalizeHostname } from "@/lib/domains/utils"; import { hasCloudflareCredentials } from "@/lib/runtime"; export type DomainDnsView = { @@ -48,7 +49,7 @@ export async function addDomainForUser( changes: DomainProvisioningChanges; }> { const db = getDb(env); - const normalizedHostname = hostname.toLowerCase().trim(); + const normalizedHostname = normalizeHostname(hostname); const [claimedHostname] = await db.select({ userId: domains.userId }).from(domains).where(eq(domains.hostname, normalizedHostname)).limit(1); if (claimedHostname && claimedHostname.userId !== userId) { throw new Error("Domain is already registered"); diff --git a/src/lib/domains/utils.ts b/src/lib/domains/utils.ts index 907f08a47..0f08128fc 100644 --- a/src/lib/domains/utils.ts +++ b/src/lib/domains/utils.ts @@ -1,3 +1,26 @@ +/** + * Canonical form of a domain name: trimmed, lowercase, no trailing dot, and + * punycode for internationalised names (Cloudflare zone names are punycode). + * Input the URL parser cannot read is returned trimmed and lowercased so a + * validity check can reject it. + */ +export function normalizeHostname(hostname: string): string { + const trimmed = hostname.trim().toLowerCase().replace(/\.+$/, ""); + if (!trimmed || /[\s/@:?#\\]/.test(trimmed)) return trimmed; + try { + return new URL(`http://${trimmed}`).hostname.replace(/\.+$/, ""); + } catch { + return trimmed; + } +} + +const HOSTNAME_PATTERN = /^(?=.{1,253}$)([a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?$/; + +/** A domain name, not an IP address: top-level domains are never all digits, which also catches forms the URL parser turns into IPv4 ("0x7f.1"). */ +export function isValidHostname(hostname: string): boolean { + return HOSTNAME_PATTERN.test(hostname) && !/^\d+$/.test(hostname.slice(hostname.lastIndexOf(".") + 1)); +} + export function getZoneLookupCandidates(hostname: string): string[] { const labels = hostname.split(".").filter(Boolean); const candidates: string[] = []; diff --git a/src/lib/validators.ts b/src/lib/validators.ts index 28b246f74..323e3e114 100644 --- a/src/lib/validators.ts +++ b/src/lib/validators.ts @@ -1,5 +1,6 @@ import { z } from "zod"; import { isPublicHttpsUrl, PUBLIC_HTTPS_URL_MESSAGE } from "@/lib/security/public-url"; +import { isValidHostname, normalizeHostname } from "@/lib/domains/utils"; import { getEmailAddress, splitEmailAddressList } from "@/lib/email/address"; import { DEFAULT_FOLDER_COLOR, FOLDER_COLOR_VALUES } from "@/lib/folders/colors"; import { MAX_FOLLOW_UP_DAYS } from "@/lib/follow-ups/follow-up-utils"; @@ -53,9 +54,15 @@ export const registerSchema = z.object({ name: z.string().min(1), }); +const hostnameField = z + .string() + .min(3) + .transform(normalizeHostname) + .refine(isValidHostname, { message: "Enter a valid domain name such as example.com" }); + export const firstRunRegisterSchema = z .object({ - domain: z.string().min(3), + domain: hostnameField, enableSending: z.boolean().optional(), replaceMxRecords: z.boolean().optional(), skipDns: z.boolean().optional(), @@ -74,13 +81,13 @@ export const primaryDomainRegisterSchema = z.object({ }); export const setupDomainSchema = z.object({ - hostname: z.string().min(3), + hostname: hostnameField, /** Record the domain without looking it up on Cloudflare. The operator sets DNS by hand. */ skipDns: z.boolean().optional(), }); export const addDomainSchema = z.object({ - hostname: z.string().min(3), + hostname: hostnameField, enableRouting: z.boolean().optional(), enableSending: z.boolean().optional(), replaceMxRecords: z.boolean().optional(), @@ -218,7 +225,7 @@ export const mfaDisableSchema = z .refine((value) => value.password || value.stepUpToken, { message: "Verify your identity first" }); export const domainSchema = z.object({ - hostname: z.string().min(3), + hostname: hostnameField, }); export const mailboxSchema = z.object({ diff --git a/tests/domain-input-validation.test.mjs b/tests/domain-input-validation.test.mjs new file mode 100644 index 000000000..7650df50b --- /dev/null +++ b/tests/domain-input-validation.test.mjs @@ -0,0 +1,50 @@ +import assert from "node:assert/strict"; +import { mkdtempSync, rmSync } from "node:fs"; +import { dirname, join } from "node:path"; +import test, { after } from "node:test"; +import { fileURLToPath, pathToFileURL } from "node:url"; +import { build } from "esbuild"; + +const root = join(dirname(fileURLToPath(import.meta.url)), ".."); +const directory = mkdtempSync(join(root, "tests", ".tmp-domain-input-")); +after(() => rmSync(directory, { recursive: true, force: true })); +await build({ + stdin: { + contents: `export { addDomainSchema, setupDomainSchema } from "./src/lib/validators.ts";`, + resolveDir: root, + sourcefile: "domain-input-entry.ts", + }, + outfile: join(directory, "entry.mjs"), + bundle: true, + platform: "node", + format: "esm", + target: "node24", + tsconfig: join(root, "tsconfig.json"), + packages: "external", + logLevel: "silent", +}); +const { addDomainSchema, setupDomainSchema } = await import(pathToFileURL(join(directory, "entry.mjs")).href); + +test("hostnames lose case, spaces and the trailing dot", () => { + assert.equal(addDomainSchema.parse({ hostname: " Example.COM. " }).hostname, "example.com"); + assert.equal(setupDomainSchema.parse({ hostname: "mail.example.com." }).hostname, "mail.example.com"); +}); + +test("internationalised hostnames become punycode", () => { + assert.equal(addDomainSchema.parse({ hostname: "münchen.de" }).hostname, "xn--mnchen-3ya.de"); +}); + +test("IP addresses are not domain names, including forms the URL parser normalises", () => { + for (const hostname of ["1.2.3.4", "127.0.0.1", "0x7f.1", "0x7f.0.0.1", "2130706433", "192.168.1.1."]) { + assert.equal(addDomainSchema.safeParse({ hostname }).success, false, hostname); + } + assert.equal(addDomainSchema.safeParse({ hostname: "mail1.example.com" }).success, true); + assert.equal(addDomainSchema.safeParse({ hostname: "example.co" }).success, true); +}); + +test("hostnames with a scheme or spaces are rejected", () => { + assert.equal(addDomainSchema.safeParse({ hostname: "https://example.com" }).success, false); + assert.equal(addDomainSchema.safeParse({ hostname: "a b.com" }).success, false); + assert.equal(addDomainSchema.safeParse({ hostname: "example" }).success, false); +}); + From 92bda708b5d964703084c7e113bc38e3a9d64c0a Mon Sep 17 00:00:00 2001 From: bobmcdonald0 <266144304+bobmcdonald0@users.noreply.github.com> Date: Sat, 14 Nov 2026 01:45:28 +0530 Subject: [PATCH 05/13] fix(mailboxes): restrict mailbox local parts to address-safe characters 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 --- src/lib/validators.ts | 2 +- tests/mailbox-local-part-schema.test.mjs | 33 ++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 tests/mailbox-local-part-schema.test.mjs diff --git a/src/lib/validators.ts b/src/lib/validators.ts index 323e3e114..e380464ba 100644 --- a/src/lib/validators.ts +++ b/src/lib/validators.ts @@ -231,7 +231,7 @@ export const domainSchema = z.object({ export const mailboxSchema = z.object({ domainId: z.string().min(1), ownerUserId: z.string().min(1).optional(), - localPart: z.string().min(1).max(64), + localPart: z.string().min(1).max(64).regex(/^[a-zA-Z0-9._%+-]+$/), displayName: z.string().optional(), type: z.enum(["personal", "shared"]).optional(), }); diff --git a/tests/mailbox-local-part-schema.test.mjs b/tests/mailbox-local-part-schema.test.mjs new file mode 100644 index 000000000..df057cf03 --- /dev/null +++ b/tests/mailbox-local-part-schema.test.mjs @@ -0,0 +1,33 @@ +import assert from "node:assert/strict"; +import { mkdtempSync, rmSync } from "node:fs"; +import { dirname, join } from "node:path"; +import test, { after } from "node:test"; +import { fileURLToPath, pathToFileURL } from "node:url"; +import { build } from "esbuild"; + +const root = join(dirname(fileURLToPath(import.meta.url)), ".."); +const directory = mkdtempSync(join(root, "tests", ".tmp-mailbox-local-part-")); +after(() => rmSync(directory, { recursive: true, force: true })); +await build({ + stdin: { + contents: `export { mailboxSchema } from "./src/lib/validators.ts";`, + resolveDir: root, + sourcefile: "mailbox-local-part-entry.ts", + }, + outfile: join(directory, "entry.mjs"), + bundle: true, + platform: "node", + format: "esm", + target: "node24", + tsconfig: join(root, "tsconfig.json"), + packages: "external", + logLevel: "silent", +}); +const { mailboxSchema } = await import(pathToFileURL(join(directory, "entry.mjs")).href); + +test("mailbox local parts use the same characters as the other schemas", () => { + const input = (localPart) => ({ domainId: "d", localPart }); + assert.equal(mailboxSchema.safeParse(input("john.doe+tag")).success, true); + assert.equal(mailboxSchema.safeParse(input("bob smith")).success, false); + assert.equal(mailboxSchema.safeParse(input("a@b")).success, false); +}); From 6893d053e5069a68d56eb4a794327abdc2fbcbe8 Mon Sep 17 00:00:00 2001 From: bobmcdonald0 <266144304+bobmcdonald0@users.noreply.github.com> Date: Sat, 14 Nov 2026 01:46:01 +0530 Subject: [PATCH 06/13] fix(domains): pass the MX replacement consent to the sending step on add 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 --- src/lib/domains/service.ts | 3 +- tests/domain-add-sending-replace.test.mjs | 51 +++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 tests/domain-add-sending-replace.test.mjs diff --git a/src/lib/domains/service.ts b/src/lib/domains/service.ts index 8ce1dec61..13f295ccf 100644 --- a/src/lib/domains/service.ts +++ b/src/lib/domains/service.ts @@ -75,7 +75,8 @@ export async function addDomainForUser( ...options, enableSending: requestedSending === "cloudflare", enableRouting: receivingProvider === "cloudflare" ? (options?.enableRouting ?? true) : false, - replaceMxRecords: receivingProvider === "cloudflare" ? options?.replaceMxRecords : false, + // Routing is skipped for other receivers, so this only reaches the sending step then. + replaceMxRecords: options?.replaceMxRecords, }); let insertedDomainId: string | null = null; let domain: typeof domains.$inferSelect; diff --git a/tests/domain-add-sending-replace.test.mjs b/tests/domain-add-sending-replace.test.mjs new file mode 100644 index 000000000..1844ebacd --- /dev/null +++ b/tests/domain-add-sending-replace.test.mjs @@ -0,0 +1,51 @@ +import assert from "node:assert/strict"; +import { mkdtempSync, rmSync } from "node:fs"; +import { dirname, join } from "node:path"; +import test, { after } from "node:test"; +import { fileURLToPath, pathToFileURL } from "node:url"; +import { build } from "esbuild"; + +const root = join(dirname(fileURLToPath(import.meta.url)), ".."); +const outDir = mkdtempSync(join(root, "tests", ".tmp-domain-add-sending-")); +after(() => rmSync(outDir, { recursive: true, force: true })); + +const mocks = { + "drizzle-orm": "export const eq=(c,v)=>({eq:[c,v]});export const and=(...a)=>({and:a});export const ne=(c,v)=>({ne:[c,v]});export const inArray=(c,v)=>({inArray:[c,v]});", + "@/db/schema": "export const domains={};export const mailboxes={};", + "@/db": "const rows={from(){return rows},where(){return rows},limit(){return Promise.resolve([])}};export function getDb(){return {select(){return rows}}}", + "@/lib/mailboxes/domain-addresses": "export async function ensureMailboxDomainRouting(){}", + "@/lib/ids": "export const newId=(p)=>p", + "@/lib/cloudflare-api": "export const disableEmailRouting=async()=>{},getEmailRoutingDns=async()=>{},getEmailRoutingSettings=async()=>{},getSendingSubdomainDns=async()=>{},deleteSendingSubdomain=async()=>{},listSendingSubdomains=async()=>{};", + "@/lib/domains/cloudflare-cleanup": "export async function deleteEmailRoutingRulesForDomain(){}", + "@/lib/domains/provision": "export const isManualZone=()=>false;export async function provisionDomainOnCloudflare(env,hostname,options){globalThis.__provisionOptions=options;throw new Error('stop')}", + "@/lib/domains/manual-dns": "export const getManualDomainDns=()=>{}", + "@/lib/domains/rollback": "export async function rollbackDomainProvisioning(){}", + "@/lib/domains/sending-status": "export const findSendingSubdomain=()=>null", + "@/lib/domains/preflight": "export async function preflightDomain(){}", +}; +await build({ + stdin: { contents: "export { addDomainForUser } from './src/lib/domains/service';", resolveDir: root, loader: "ts" }, + outfile: join(outDir, "service.mjs"), + bundle: true, + platform: "node", + format: "esm", + packages: "external", + alias: { "@": join(root, "src") }, + plugins: [{ + name: "mock-deps", + setup(builder) { + const names = Object.keys(mocks).map((name) => name.replace(/[/.-]/g, "\\$&")).join("|"); + builder.onResolve({ filter: new RegExp(`^(${names})$`) }, (args) => ({ path: args.path, namespace: "mock" })); + builder.onLoad({ filter: /.*/, namespace: "mock" }, (args) => ({ contents: mocks[args.path], loader: "js" })); + }, + }], + logLevel: "silent", +}); +const { addDomainForUser } = await import(pathToFileURL(join(outDir, "service.mjs")).href); + +test("confirming the MX replacement reaches the sending step when another service receives mail", async () => { + await assert.rejects(addDomainForUser({}, "u1", "example.com", { receivingProvider: "resend", sendingProvider: "cloudflare", replaceMxRecords: true }), /stop/); + assert.equal(globalThis.__provisionOptions.enableSending, true); + assert.equal(globalThis.__provisionOptions.enableRouting, false); + assert.equal(globalThis.__provisionOptions.replaceMxRecords, true); +}); From c4a31010d1ecb47947e23c70583a60ff0e14a8de Mon Sep 17 00:00:00 2001 From: bobmcdonald0 <266144304+bobmcdonald0@users.noreply.github.com> Date: Sat, 14 Nov 2026 01:48:03 +0530 Subject: [PATCH 07/13] fix(cloudflare): report non-JSON API responses as API errors and add 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 --- src/lib/cloudflare-api.ts | 14 +++++++++- tests/cloudflare-api-request.test.mjs | 40 +++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 tests/cloudflare-api-request.test.mjs diff --git a/src/lib/cloudflare-api.ts b/src/lib/cloudflare-api.ts index a401672f4..726166da8 100644 --- a/src/lib/cloudflare-api.ts +++ b/src/lib/cloudflare-api.ts @@ -16,6 +16,8 @@ import { CloudflareApiError } from "@/lib/cloudflare-api-error"; import { getZoneLookupCandidates } from "@/lib/domains/utils"; export type { CfDnsRecord } from "@/lib/cloudflare-api.types"; +const CF_REQUEST_TIMEOUT_MS = 30_000; + export async function cfRequest( env: CloudflareEnv, path: string, @@ -23,6 +25,7 @@ export async function cfRequest( ): Promise { const auth = getCloudflareAuth(env); const res = await fetch(`https://api.cloudflare.com/client/v4${path}`, { + signal: AbortSignal.timeout(CF_REQUEST_TIMEOUT_MS), ...init, headers: { ...getCloudflareAuthHeaders(auth), @@ -30,7 +33,16 @@ export async function cfRequest( ...(init?.headers ?? {}), }, }); - const json = (await res.json()) as CfResponse; + // Cloudflare answers incidents with an HTML page; report it as an API error, not a parse error. + const json = (await res.json().catch(() => null)) as CfResponse | null; + if (!json) { + throw new CloudflareApiError( + `Cloudflare API ${path} returned ${res.status} ${res.statusText} without a JSON body`.trim(), + res.status, + path, + [], + ); + } if (!json.success) { throw new CloudflareApiError( diff --git a/tests/cloudflare-api-request.test.mjs b/tests/cloudflare-api-request.test.mjs new file mode 100644 index 000000000..d07470bd8 --- /dev/null +++ b/tests/cloudflare-api-request.test.mjs @@ -0,0 +1,40 @@ +import assert from "node:assert/strict"; +import { mkdtempSync, rmSync } from "node:fs"; +import { dirname, join } from "node:path"; +import test, { after } from "node:test"; +import { fileURLToPath, pathToFileURL } from "node:url"; +import { build } from "esbuild"; + +const root = join(dirname(fileURLToPath(import.meta.url)), ".."); +const outDir = mkdtempSync(join(root, "tests", ".tmp-cloudflare-api-")); +const realFetch = globalThis.fetch; +after(() => { rmSync(outDir, { recursive: true, force: true }); globalThis.fetch = realFetch; }); +await build({ + stdin: { contents: "export * from './src/lib/cloudflare-api';export { CloudflareApiError } from './src/lib/cloudflare-api-error';", resolveDir: root, loader: "ts" }, + outfile: join(outDir, "api.mjs"), + bundle: true, + platform: "node", + format: "esm", + packages: "external", + alias: { "@": join(root, "src") }, + logLevel: "silent", +}); +const api = await import(pathToFileURL(join(outDir, "api.mjs")).href); +const env = { CF_TOKEN: "token" }; + +const reply = (body, init = {}) => new Response(typeof body === "string" ? body : JSON.stringify(body), init); + +test("a non-JSON error page becomes a CloudflareApiError", async () => { + globalThis.fetch = async () => reply("502 Bad Gateway", { status: 502, statusText: "Bad Gateway" }); + await assert.rejects(api.getZone(env, "zone"), (error) => error instanceof api.CloudflareApiError && error.status === 502); +}); + +test("requests carry a timeout signal", async () => { + let signal; + globalThis.fetch = async (_url, init) => { + signal = init.signal; + return reply({ success: true, errors: [], result: { id: "zone", name: "example.com" } }); + }; + await api.getZone(env, "zone"); + assert.ok(signal instanceof AbortSignal); +}); From b1f7571d7c61778ea00e528ffdf0664fb146dbbe Mon Sep 17 00:00:00 2001 From: bobmcdonald0 <266144304+bobmcdonald0@users.noreply.github.com> Date: Sat, 14 Nov 2026 01:48:43 +0530 Subject: [PATCH 08/13] fix(cloudflare): list every page of email routing rules 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 --- src/lib/cloudflare-api.ts | 38 ++++++++++++++++++++++----- src/lib/cloudflare-api.types.d.ts | 9 +++++++ tests/cloudflare-api-request.test.mjs | 24 +++++++++++++++++ 3 files changed, 64 insertions(+), 7 deletions(-) diff --git a/src/lib/cloudflare-api.ts b/src/lib/cloudflare-api.ts index 726166da8..073102647 100644 --- a/src/lib/cloudflare-api.ts +++ b/src/lib/cloudflare-api.ts @@ -3,6 +3,7 @@ import type { CfEmailRoutingRule, CfEmailRoutingRuleChange, CfResponse, + CfResultInfo, CfSendingSubdomain, } from "@/lib/cloudflare-api.types"; import { @@ -18,11 +19,11 @@ export type { CfDnsRecord } from "@/lib/cloudflare-api.types"; const CF_REQUEST_TIMEOUT_MS = 30_000; -export async function cfRequest( +async function cfRequestWithInfo( env: CloudflareEnv, path: string, init?: RequestInit, -): Promise { +): Promise<{ result: T; resultInfo?: CfResultInfo }> { const auth = getCloudflareAuth(env); const res = await fetch(`https://api.cloudflare.com/client/v4${path}`, { signal: AbortSignal.timeout(CF_REQUEST_TIMEOUT_MS), @@ -52,7 +53,15 @@ export async function cfRequest( json.errors ?? [], ); } - return json.result; + return { result: json.result, resultInfo: json.result_info }; +} + +export async function cfRequest( + env: CloudflareEnv, + path: string, + init?: RequestInit, +): Promise { + return (await cfRequestWithInfo(env, path, init)).result; } export async function getZone( @@ -173,11 +182,26 @@ export async function getEmailRoutingSettings( ); } +const RULES_PER_PAGE = 50; +const MAX_RULE_PAGES = 100; + +/** + * Every routing rule in the zone; the API returns them one page at a time. + * Throws when the page cap is hit: callers decide whether a rule exists from + * this list, and a partial list would make them create duplicates. + */ export async function listEmailRoutingRules(env: CloudflareEnv, zoneId: string) { - return cfRequest( - env, - `/zones/${zoneId}/email/routing/rules`, - ); + const rules: CfEmailRoutingRule[] = []; + for (let page = 1; page <= MAX_RULE_PAGES; page += 1) { + const { result, resultInfo } = await cfRequestWithInfo( + env, + `/zones/${zoneId}/email/routing/rules?page=${page}&per_page=${RULES_PER_PAGE}`, + ); + rules.push(...result); + const lastPage = resultInfo?.total_pages !== undefined ? page >= resultInfo.total_pages : result.length < RULES_PER_PAGE; + if (lastPage || result.length === 0) return rules; + } + throw new Error(`Email routing rules for zone ${zoneId} span more than ${MAX_RULE_PAGES} pages`); } export async function deleteEmailRoutingRule( diff --git a/src/lib/cloudflare-api.types.d.ts b/src/lib/cloudflare-api.types.d.ts index 077c84790..5ed295323 100644 --- a/src/lib/cloudflare-api.types.d.ts +++ b/src/lib/cloudflare-api.types.d.ts @@ -22,6 +22,15 @@ export type CfResponse = { errors: CfApiError[]; messages?: CfApiError[]; result: T; + result_info?: CfResultInfo; +}; + +export type CfResultInfo = { + page?: number; + per_page?: number; + total_pages?: number; + count?: number; + total_count?: number; }; export type CfAuth = diff --git a/tests/cloudflare-api-request.test.mjs b/tests/cloudflare-api-request.test.mjs index d07470bd8..45fc87b34 100644 --- a/tests/cloudflare-api-request.test.mjs +++ b/tests/cloudflare-api-request.test.mjs @@ -24,6 +24,22 @@ const env = { CF_TOKEN: "token" }; const reply = (body, init = {}) => new Response(typeof body === "string" ? body : JSON.stringify(body), init); +test("listEmailRoutingRules reads every page", async () => { + const rules = Array.from({ length: 120 }, (_, index) => ({ id: `rule${index}` })); + const urls = []; + globalThis.fetch = async (url) => { + urls.push(String(url)); + const parsed = new URL(String(url)); + const page = Number(parsed.searchParams.get("page") ?? 1); + const perPage = Number(parsed.searchParams.get("per_page") ?? 20); + return reply({ success: true, errors: [], result: rules.slice((page - 1) * perPage, page * perPage), result_info: { page, per_page: perPage, total_pages: Math.ceil(rules.length / perPage) } }); + }; + const result = await api.listEmailRoutingRules(env, "zone"); + assert.equal(result.length, 120); + assert.equal(new Set(result.map((rule) => rule.id)).size, 120); + assert.ok(urls.every((url) => url.includes("per_page=50"))); +}); + test("a non-JSON error page becomes a CloudflareApiError", async () => { globalThis.fetch = async () => reply("502 Bad Gateway", { status: 502, statusText: "Bad Gateway" }); await assert.rejects(api.getZone(env, "zone"), (error) => error instanceof api.CloudflareApiError && error.status === 502); @@ -38,3 +54,11 @@ test("requests carry a timeout signal", async () => { await api.getZone(env, "zone"); assert.ok(signal instanceof AbortSignal); }); + +test("a list longer than the page cap throws instead of returning a partial list", async () => { + globalThis.fetch = async (url) => { + const page = Number(new URL(String(url)).searchParams.get("page")); + return reply({ success: true, errors: [], result: [{ id: `rule${page}` }], result_info: { page, per_page: 50, total_pages: 1000 } }); + }; + await assert.rejects(api.listEmailRoutingRules(env, "zone"), /more than \d+ pages/); +}); From 96d16b83fdd4dfe3a35d1c8dd7bddc0dc430511f Mon Sep 17 00:00:00 2001 From: bobmcdonald0 <266144304+bobmcdonald0@users.noreply.github.com> Date: Sat, 14 Nov 2026 01:49:24 +0530 Subject: [PATCH 09/13] perf(domains): list a zone's routing rules once per operation 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 --- src/lib/cloudflare-api.ts | 18 ++++++++++++- src/lib/domains/receiving.ts | 5 ++-- src/lib/domains/service.ts | 4 ++- src/lib/mailboxes/domain-addresses.ts | 5 ++-- tests/routing-rule-list-cache.test.mjs | 37 ++++++++++++++++++++++++++ 5 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 tests/routing-rule-list-cache.test.mjs diff --git a/src/lib/cloudflare-api.ts b/src/lib/cloudflare-api.ts index 073102647..fa9c78472 100644 --- a/src/lib/cloudflare-api.ts +++ b/src/lib/cloudflare-api.ts @@ -251,16 +251,32 @@ function isWorkerRouteForAddress( return Boolean(routesAddress && sendsToWorker); } +/** One rules listing per zone, shared by the calls of a single operation. */ +export type EmailRoutingRuleCache = Map>; + +function listEmailRoutingRulesCached(env: CloudflareEnv, zoneId: string, cache?: EmailRoutingRuleCache) { + if (!cache) return listEmailRoutingRules(env, zoneId); + let listing = cache.get(zoneId); + if (!listing) { + listing = listEmailRoutingRules(env, zoneId); + cache.set(zoneId, listing); + // A failed listing must not stay cached for the next address. + listing.catch(() => cache.delete(zoneId)); + } + return listing; +} + export async function ensureEmailRoutingRuleToWorker( env: CloudflareEnv, zoneId: string, address: string, changes?: CfEmailRoutingRuleChange[], + ruleCache?: EmailRoutingRuleCache, ) { if (zoneId === "manual") return; const normalized = address.toLowerCase(); const workerName = getEmailWorkerName(); - const rules = await listEmailRoutingRules(env, zoneId); + const rules = await listEmailRoutingRulesCached(env, zoneId, ruleCache); const existing = rules.find((rule) => isWorkerRouteForAddress(rule, normalized, workerName)); if (existing?.enabled) return existing; diff --git a/src/lib/domains/receiving.ts b/src/lib/domains/receiving.ts index 37246962d..37bcda1fe 100644 --- a/src/lib/domains/receiving.ts +++ b/src/lib/domains/receiving.ts @@ -1,7 +1,7 @@ import { and, eq, ne, or } from "drizzle-orm"; import { getDb } from "@/db"; import { domains, mailboxes } from "@/db/schema"; -import { disableEmailRouting } from "@/lib/cloudflare-api"; +import { disableEmailRouting, type EmailRoutingRuleCache } from "@/lib/cloudflare-api"; import { deleteDnsRecord } from "@/lib/cloudflare-dns"; import { deleteEmailRoutingRulesForDomain } from "@/lib/domains/cloudflare-cleanup"; import { removeMxRecords, restoreMxRecordsAfterFailure } from "@/lib/domains/mx-records"; @@ -50,7 +50,8 @@ export async function setupCloudflareReceiving(env: CloudflareEnv, skippedDomain .from(mailboxes) .innerJoin(domains, eq(mailboxes.domainId, domains.id)) .where(and(eq(domains.userId, domain.userId), or(eq(mailboxes.domainId, domain.id), eq(mailboxes.useAllDomains, true)))); - await Promise.allSettled(mailboxRows.map((mailbox) => ensureMailboxDomainRouting(env, db, { ...mailbox, useAllDomains: true }))); + const ruleCache: EmailRoutingRuleCache = new Map(); + await Promise.allSettled(mailboxRows.map((mailbox) => ensureMailboxDomainRouting(env, db, { ...mailbox, useAllDomains: true }, undefined, ruleCache))); } export async function removeCloudflareReceiving(env: CloudflareEnv, domain: DomainRow): Promise { diff --git a/src/lib/domains/service.ts b/src/lib/domains/service.ts index 13f295ccf..fecabfb13 100644 --- a/src/lib/domains/service.ts +++ b/src/lib/domains/service.ts @@ -11,6 +11,7 @@ import { deleteSendingSubdomain, listSendingSubdomains, type CfDnsRecord, + type EmailRoutingRuleCache, } from "@/lib/cloudflare-api"; import { deleteEmailRoutingRulesForDomain } from "@/lib/domains/cloudflare-cleanup"; import { isManualZone, provisionDomainOnCloudflare } from "@/lib/domains/provision"; @@ -116,8 +117,9 @@ export async function addDomainForUser( .from(mailboxes) .innerJoin(domains, eq(mailboxes.domainId, domains.id)) .where(and(eq(domains.userId, userId), eq(mailboxes.useAllDomains, true))); + const ruleCache: EmailRoutingRuleCache = new Map(); const routingResults = await Promise.allSettled( - aliasMailboxes.map((mailbox) => ensureMailboxDomainRouting(env, db, mailbox)), + aliasMailboxes.map((mailbox) => ensureMailboxDomainRouting(env, db, mailbox, undefined, ruleCache)), ); for (const result of routingResults) { if (result.status === "rejected") console.warn("ensureMailboxDomainRouting", result.reason); diff --git a/src/lib/mailboxes/domain-addresses.ts b/src/lib/mailboxes/domain-addresses.ts index d31fa2f87..6056c4177 100644 --- a/src/lib/mailboxes/domain-addresses.ts +++ b/src/lib/mailboxes/domain-addresses.ts @@ -1,7 +1,7 @@ import { and, eq, inArray } from "drizzle-orm"; import type { AppDatabase } from "@/db"; import { domains, mailboxAliases, mailboxes } from "@/db/schema"; -import { deleteEmailRoutingRuleForAddress, ensureEmailRoutingRuleToWorker } from "@/lib/cloudflare-api"; +import { deleteEmailRoutingRuleForAddress, ensureEmailRoutingRuleToWorker, type EmailRoutingRuleCache } from "@/lib/cloudflare-api"; import { normalizeRecipientLocalPart } from "@/lib/email/recipient-address"; import type { MailboxDomainAddressInput } from "./domain-addresses-types"; import type { CfEmailRoutingRuleChange } from "@/lib/cloudflare-api.types"; @@ -82,6 +82,7 @@ export async function ensureMailboxDomainRouting( db: AppDatabase, mailbox: MailboxDomainAddressInput, changes?: CfEmailRoutingRuleChange[], + ruleCache: EmailRoutingRuleCache = new Map(), ): Promise { const addresses = await getMailboxDomainAddresses(db, mailbox); if (addresses.length === 0) return; @@ -103,7 +104,7 @@ export async function ensureMailboxDomainRouting( const hostname = address.slice(address.lastIndexOf("@") + 1); const domain = domainsByHostname.get(hostname); // Worker routes only matter when Cloudflare is what receives this domain's mail. - if (domain && domain.receivingProvider === "cloudflare") await ensureEmailRoutingRuleToWorker(env, domain.zoneId, address, changes); + if (domain && domain.receivingProvider === "cloudflare") await ensureEmailRoutingRuleToWorker(env, domain.zoneId, address, changes, ruleCache); }), ); const failure = results.find((result) => result.status === "rejected"); diff --git a/tests/routing-rule-list-cache.test.mjs b/tests/routing-rule-list-cache.test.mjs new file mode 100644 index 000000000..7c7a779d0 --- /dev/null +++ b/tests/routing-rule-list-cache.test.mjs @@ -0,0 +1,37 @@ +import assert from "node:assert/strict"; +import { mkdtempSync, rmSync } from "node:fs"; +import { dirname, join } from "node:path"; +import test, { after } from "node:test"; +import { fileURLToPath, pathToFileURL } from "node:url"; +import { build } from "esbuild"; + +const root = join(dirname(fileURLToPath(import.meta.url)), ".."); +const outDir = mkdtempSync(join(root, "tests", ".tmp-routing-rule-cache-")); +const realFetch = globalThis.fetch; +after(() => { rmSync(outDir, { recursive: true, force: true }); globalThis.fetch = realFetch; }); +await build({ + stdin: { contents: "export * from './src/lib/cloudflare-api';", resolveDir: root, loader: "ts" }, + outfile: join(outDir, "api.mjs"), + bundle: true, + platform: "node", + format: "esm", + packages: "external", + alias: { "@": join(root, "src") }, + logLevel: "silent", +}); +const api = await import(pathToFileURL(join(outDir, "api.mjs")).href); + +test("addresses of one operation share a single rules listing per zone", async () => { + const calls = []; + globalThis.fetch = async (url, init = {}) => { + const method = init.method ?? "GET"; + calls.push(`${method} ${new URL(String(url)).pathname}`); + const result = method === "GET" ? [] : { id: "new-rule" }; + return new Response(JSON.stringify({ success: true, errors: [], result, result_info: { total_pages: 1 } })); + }; + const cache = new Map(); + const env = { CF_TOKEN: "token" }; + await Promise.all(["a", "b", "c", "d"].map((name) => api.ensureEmailRoutingRuleToWorker(env, "zone", `${name}@example.com`, undefined, cache))); + assert.equal(calls.filter((call) => call.startsWith("GET")).length, 1); + assert.equal(calls.filter((call) => call.startsWith("POST")).length, 4); +}); From 2f0bdb0b4a780653a2f2aabfad6153cd5322055d Mon Sep 17 00:00:00 2001 From: bobmcdonald0 <266144304+bobmcdonald0@users.noreply.github.com> Date: Sat, 14 Nov 2026 01:49:47 +0530 Subject: [PATCH 10/13] fix(auth): read API key lookups from the primary database 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 --- src/lib/api/key-auth.ts | 3 +- src/lib/mcp/auth.ts | 3 +- tests/api-key-primary-read.test.mjs | 57 +++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 tests/api-key-primary-read.test.mjs diff --git a/src/lib/api/key-auth.ts b/src/lib/api/key-auth.ts index 30beb7a24..92d7406fe 100644 --- a/src/lib/api/key-auth.ts +++ b/src/lib/api/key-auth.ts @@ -17,7 +17,8 @@ const ADMIN_SCOPES = new Set(ADMIN_API_KEY_SCOPES); export async function authenticateApiKeyValue(env: CloudflareEnv, key: string): Promise { const trimmed = key.trim(); if (!trimmed) return null; - const db = getDb(env); + // Authentication reads the primary: a lagging read replica could still honour a key that was just revoked. + const db = getDb({ DB: env.DB_PRIMARY ?? env.DB }); const candidate = await findApiKeyForSecret(db, trimmed); if (!candidate) return null; const [user] = await db.select().from(users).where(eq(users.id, candidate.userId)).limit(1); diff --git a/src/lib/mcp/auth.ts b/src/lib/mcp/auth.ts index a4003863b..3e67276c3 100644 --- a/src/lib/mcp/auth.ts +++ b/src/lib/mcp/auth.ts @@ -15,7 +15,8 @@ export async function authenticateMcpRequest(env: CloudflareEnv, request: Reques if (!authorization.startsWith("Bearer ")) return null; const key = authorization.slice(7).trim(); if (!key.startsWith("ep_") || key.length > 256) return null; - const db = getDb(env); + // Authentication reads the primary: a lagging read replica could still honour a key that was just revoked. + const db = getDb({ DB: env.DB_PRIMARY ?? env.DB }); const candidate = await findApiKeyForSecret(db, key, { kind: "mcp" }); if (candidate) { const allScopes = parseScopes(candidate.scopes); diff --git a/tests/api-key-primary-read.test.mjs b/tests/api-key-primary-read.test.mjs new file mode 100644 index 000000000..10a106970 --- /dev/null +++ b/tests/api-key-primary-read.test.mjs @@ -0,0 +1,57 @@ +import assert from "node:assert/strict"; +import { mkdtempSync, rmSync } from "node:fs"; +import { dirname, join } from "node:path"; +import test, { after, beforeEach } from "node:test"; +import { fileURLToPath, pathToFileURL } from "node:url"; +import { build } from "esbuild"; + +// Authentication reads go to the primary so a revoked key or disabled account +// is not honoured by a lagging read replica (same rule as session auth). + +const root = join(dirname(fileURLToPath(import.meta.url)), ".."); +const outDir = mkdtempSync(join(root, "tests", ".tmp-api-key-primary-")); +after(() => rmSync(outDir, { recursive: true, force: true })); + +const mocks = { + "drizzle-orm": "export const eq=(c,v)=>({eq:[c,v]});export const and=(...a)=>({and:a});export const or=(...a)=>({or:a});export const lt=(c,v)=>({lt:[c,v]});export const isNull=(c)=>({isNull:c});", + "@/db/schema": "export const apiKeys={};export const mcpKeyMailboxes={};export const users={};", + "@/db": "const rows={from(){return rows},where(){return rows},limit(){return Promise.resolve([globalThis.__user])},then(resolve){resolve([])}};export function getDb(env){globalThis.__dbEnvs.push(env.DB);return {select(){return rows},update(){return {set(){return {where(){return Promise.resolve()}}}}}}}", + "@/lib/api/find-api-key": "export async function findApiKeyForSecret(db){return {id:'key1',userId:'u1',kind:'api',scopes:'[]',mailboxScopeEnabled:false}}", + "@/lib/api-keys": "export const parseScopes=()=>[]", + "@/lib/api/scopes": "export const ADMIN_API_KEY_SCOPES=[]", +}; +await build({ + stdin: { contents: "export { authenticateApiKeyValue } from './src/lib/api/key-auth';", resolveDir: root, loader: "ts" }, + outfile: join(outDir, "auth.mjs"), + bundle: true, + platform: "node", + format: "esm", + packages: "external", + alias: { "@": join(root, "src") }, + plugins: [{ + name: "mock-deps", + setup(builder) { + const names = Object.keys(mocks).map((name) => name.replace(/[/.-]/g, "\\$&")).join("|"); + builder.onResolve({ filter: new RegExp(`^(${names})$`) }, (args) => ({ path: args.path, namespace: "mock" })); + builder.onLoad({ filter: /.*/, namespace: "mock" }, (args) => ({ contents: mocks[args.path], loader: "js" })); + }, + }], + logLevel: "silent", +}); +const { authenticateApiKeyValue } = await import(pathToFileURL(join(outDir, "auth.mjs")).href); + +beforeEach(() => { + globalThis.__dbEnvs = []; + globalThis.__user = { id: "u1", email: "a@example.com", disabled: false }; +}); + +test("API key authentication reads through the primary database", async () => { + const result = await authenticateApiKeyValue({ DB: "replica-session", DB_PRIMARY: "primary" }, "ep_key"); + assert.equal(result?.userId, "u1"); + assert.deepEqual([...new Set(globalThis.__dbEnvs)], ["primary"]); +}); + +test("without a separate primary binding the normal database is used", async () => { + await authenticateApiKeyValue({ DB: "only" }, "ep_key"); + assert.deepEqual([...new Set(globalThis.__dbEnvs)], ["only"]); +}); From eaf4657f4b29a96fc745c5ac073860542d25ecc3 Mon Sep 17 00:00:00 2001 From: bobmcdonald0 <266144304+bobmcdonald0@users.noreply.github.com> Date: Sat, 14 Nov 2026 01:50:28 +0530 Subject: [PATCH 11/13] fix(auth): purge expired sessions, login challenges and reset tokens 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 --- server/runtime/scheduler.ts | 2 + src/lib/auth/purge.ts | 37 ++++++++++++++++ tests/auth-expired-purge.test.mjs | 73 +++++++++++++++++++++++++++++++ worker.ts | 2 + 4 files changed, 114 insertions(+) create mode 100644 src/lib/auth/purge.ts create mode 100644 tests/auth-expired-purge.test.mjs diff --git a/server/runtime/scheduler.ts b/server/runtime/scheduler.ts index db3739fc8..2594d5cff 100644 --- a/server/runtime/scheduler.ts +++ b/server/runtime/scheduler.ts @@ -3,6 +3,7 @@ import { runAgentMaintenance } from "@/lib/agent/maintenance"; import { runTrashRetention } from "@/lib/email/trash-retention"; import { runDriveTrashRetention } from "@/lib/drive/retention"; import { runAccountRetention } from "@/lib/accounts/deletion"; +import { purgeExpiredAuthRows } from "@/lib/auth/purge"; import { runJmapChangeRetention } from "@/lib/jmap/changes"; import { runFollowUpReminders } from "@/lib/follow-ups/service"; import { runCalendarReminders } from "@/lib/calendar/reminders"; @@ -25,6 +26,7 @@ export function startScheduler(env: CloudflareEnv) { runScheduledDatabaseBackup(env, now).catch((error) => console.error("Scheduled backup failed", error)); runDriveTrashRetention(env, now).catch((error) => console.error("Drive trash retention failed", error)); runAccountRetention(env, now).catch((error) => console.error("Account retention failed", error)); + purgeExpiredAuthRows(env, now).catch((error) => console.error("Expired auth row purge failed", error)); runJmapChangeRetention(env, now).catch((error) => console.error("JMAP change retention failed", error)); sweepOrphanedRawMail(env, now).catch((error) => console.error("Raw mail sweep failed", error)); }, 60_000); diff --git a/src/lib/auth/purge.ts b/src/lib/auth/purge.ts new file mode 100644 index 000000000..2cdb1511b --- /dev/null +++ b/src/lib/auth/purge.ts @@ -0,0 +1,37 @@ +import { inArray, lt } from "drizzle-orm"; +import { getDb } from "@/db"; +import { loginChallenges, passkeyEnrollmentGrants, passwordResetTokens, sessions } from "@/db/schema"; + +/** Under D1's 100 bound parameters per statement. */ +const BATCH_SIZE = 90; +/** Bounds one run: the first run after deploy may face years of expired rows; the next days continue. */ +const MAX_BATCHES_PER_TABLE = 50; + +/** + * Deletes sessions, login challenges, password reset tokens and passkey + * enrolment links that expired before `now`. Nothing reads them after expiry, + * and without this every login not ended by a logout leaves a row forever. + * Deleting a session also removes its push subscription (cascade), as logout does. + * Runs from the daily scheduled handler; one table failing does not stop the rest. + */ +export async function purgeExpiredAuthRows(env: CloudflareEnv, now = new Date()): Promise { + const db = getDb(env); + const tables = [ + ["sessions", sessions], + ["login_challenges", loginChallenges], + ["password_reset_tokens", passwordResetTokens], + ["passkey_enrollment_grants", passkeyEnrollmentGrants], + ] as const; + for (const [name, table] of tables) { + try { + for (let batch = 0; batch < MAX_BATCHES_PER_TABLE; batch += 1) { + const expired = await db.select({ id: table.id }).from(table).where(lt(table.expiresAt, now)).limit(BATCH_SIZE); + if (expired.length === 0) break; + await db.delete(table).where(inArray(table.id, expired.map((row) => row.id))); + if (expired.length < BATCH_SIZE) break; + } + } catch (error) { + console.error("Expired auth row purge failed", { table: name, error }); + } + } +} diff --git a/tests/auth-expired-purge.test.mjs b/tests/auth-expired-purge.test.mjs new file mode 100644 index 000000000..1f5a4de69 --- /dev/null +++ b/tests/auth-expired-purge.test.mjs @@ -0,0 +1,73 @@ +import assert from "node:assert/strict"; +import { mkdtempSync, rmSync } from "node:fs"; +import { dirname, join } from "node:path"; +import test, { after } from "node:test"; +import { fileURLToPath, pathToFileURL } from "node:url"; +import { build } from "esbuild"; + +const root = join(dirname(fileURLToPath(import.meta.url)), ".."); +const outDir = mkdtempSync(join(root, "tests", ".tmp-auth-purge-")); +after(() => rmSync(outDir, { recursive: true, force: true })); +const mocks = { + "drizzle-orm": "export const lt=(c,v)=>({lt:[c,v]});export const inArray=(c,v)=>({inArray:[c,v]});", + "@/db/schema": "const t=(n)=>({__name:n,id:n+'.id',expiresAt:n+'.expiresAt'});export const sessions=t('sessions'),loginChallenges=t('login_challenges'),passwordResetTokens=t('password_reset_tokens'),passkeyEnrollmentGrants=t('passkey_enrollment_grants');", + "@/db": `export function getDb(){return { + select(){return {from(table){return {where(condition){return {limit(n){ + globalThis.__selects.push({table:table.__name,condition,limit:n}); + const left=globalThis.__expired[table.__name]??0; + const take=Math.min(left,n); + globalThis.__expired[table.__name]=left-take; + return Promise.resolve(Array.from({length:take},(_,i)=>({id:table.__name+i}))); + }}}}}}}, + delete(table){return {where(condition){globalThis.__deletes.push({table:table.__name,ids:condition.inArray[1]});if(globalThis.__failOn===table.__name)return Promise.reject(new Error('boom'));return Promise.resolve()}}} + }}`, +}; +await build({ + stdin: { contents: "export { purgeExpiredAuthRows } from './src/lib/auth/purge';", resolveDir: root, loader: "ts" }, + outfile: join(outDir, "purge.mjs"), + bundle: true, + platform: "node", + format: "esm", + packages: "external", + alias: { "@": join(root, "src") }, + plugins: [{ + name: "mock-deps", + setup(builder) { + const names = Object.keys(mocks).map((name) => name.replace(/[/.-]/g, "\\$&")).join("|"); + builder.onResolve({ filter: new RegExp(`^(${names})$`) }, (args) => ({ path: args.path, namespace: "mock" })); + builder.onLoad({ filter: /.*/, namespace: "mock" }, (args) => ({ contents: mocks[args.path], loader: "js" })); + }, + }], + logLevel: "silent", +}); +const { purgeExpiredAuthRows } = await import(pathToFileURL(join(outDir, "purge.mjs")).href); + +const setup = (expired) => { globalThis.__selects = []; globalThis.__deletes = []; globalThis.__expired = { ...expired }; globalThis.__failOn = undefined; }; +const rowsDeleted = (table) => globalThis.__deletes.filter((entry) => entry.table === table).reduce((sum, entry) => sum + entry.ids.length, 0); + +test("only rows that expired before now are selected, per table", async () => { + setup({ sessions: 3, login_challenges: 1, password_reset_tokens: 2, passkey_enrollment_grants: 1 }); + const now = new Date("2026-01-01T00:00:00Z"); + await purgeExpiredAuthRows({}, now); + assert.deepEqual([...new Set(globalThis.__selects.map((entry) => entry.table))], ["sessions", "login_challenges", "password_reset_tokens", "passkey_enrollment_grants"]); + for (const { table, condition } of globalThis.__selects) assert.deepEqual(condition, { lt: [`${table}.expiresAt`, now] }); + assert.equal(rowsDeleted("sessions"), 3); + assert.equal(rowsDeleted("passkey_enrollment_grants"), 1); +}); + +test("deletes go in batches of at most 90 ids and stop at the per-run cap", async () => { + setup({ sessions: 1_000_000 }); + await purgeExpiredAuthRows({}, new Date()); + const sessionDeletes = globalThis.__deletes.filter((entry) => entry.table === "sessions"); + assert.equal(sessionDeletes.length, 50); + assert.ok(sessionDeletes.every((entry) => entry.ids.length === 90)); +}); + +test("a failing table does not stop the others", async () => { + setup({ sessions: 5, login_challenges: 5 }); + globalThis.__failOn = "sessions"; + const originalError = console.error; + console.error = () => {}; + try { await purgeExpiredAuthRows({}, new Date()); } finally { console.error = originalError; } + assert.equal(rowsDeleted("login_challenges"), 5); +}); diff --git a/worker.ts b/worker.ts index 9e45e5ada..e23002e12 100644 --- a/worker.ts +++ b/worker.ts @@ -34,6 +34,7 @@ import { runAgentMaintenance } from "./src/lib/agent/maintenance"; import { runTrashRetention } from "./src/lib/email/trash-retention"; import { runDriveTrashRetention } from "./src/lib/drive/retention"; import { runAccountRetention } from "./src/lib/accounts/deletion"; +import { purgeExpiredAuthRows } from "./src/lib/auth/purge"; import { runJmapChangeRetention } from "./src/lib/jmap/changes"; import { runFollowUpReminders } from "./src/lib/follow-ups/service"; import { runCalendarReminders } from "./src/lib/calendar/reminders"; @@ -162,6 +163,7 @@ export default { ctx.waitUntil(runCalendarReminders(env, new Date(controller.scheduledTime))); ctx.waitUntil(sweepImportJobs(env, new Date(controller.scheduledTime))); if (controller.cron === "0 2 * * *") ctx.waitUntil(runAccountRetention(env, new Date(controller.scheduledTime))); + if (controller.cron === "0 2 * * *") ctx.waitUntil(purgeExpiredAuthRows(env, new Date(controller.scheduledTime))); // Drive trash is emptied once a day, with the 02:00 UTC cron; the 5-minute cron is for queue-like upkeep. if (controller.cron === "0 2 * * *") ctx.waitUntil(runDriveTrashRetention(env, new Date(controller.scheduledTime))); if (controller.cron === "0 2 * * *") ctx.waitUntil(runJmapChangeRetention(env, new Date(controller.scheduledTime))); From 14f88bcee18c691a8af4ccdf0b11fb33b6bb7b8d Mon Sep 17 00:00:00 2001 From: bobmcdonald0 <266144304+bobmcdonald0@users.noreply.github.com> Date: Sat, 14 Nov 2026 01:51:25 +0530 Subject: [PATCH 12/13] fix(accounts): limit personal addresses in account responses to user 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 --- src/app/api/accounts/[id]/route.ts | 2 +- src/app/api/accounts/route.ts | 5 ++- src/app/api/accounts/utils.ts | 7 +++ tests/account-detail-contacts.test.mjs | 61 ++++++++++++++++++++++++++ tests/accounts-list-contacts.test.mjs | 60 +++++++++++++++++++++++++ 5 files changed, 132 insertions(+), 3 deletions(-) create mode 100644 tests/account-detail-contacts.test.mjs create mode 100644 tests/accounts-list-contacts.test.mjs diff --git a/src/app/api/accounts/[id]/route.ts b/src/app/api/accounts/[id]/route.ts index e5dc06c40..f518122e3 100644 --- a/src/app/api/accounts/[id]/route.ts +++ b/src/app/api/accounts/[id]/route.ts @@ -37,7 +37,7 @@ export async function GET(request: Request, { params }: AccountRouteParams) { canManageUsers: account.canManageUsers, calendarEnabled: account.calendarEnabled, driveEnabled: account.driveEnabled, - forwardingEmail: account.forwardingEmail, + ...(canManageUsers(actor) ? { forwardingEmail: account.forwardingEmail } : {}), canForwardEmail: (await getLicenseEntitlements(access.env)).canForwardEmail, hasAvatar: !!account.avatarKey, passwordSet: hasPasswordHash(account.passwordHash), diff --git a/src/app/api/accounts/route.ts b/src/app/api/accounts/route.ts index a462cc13f..19475135c 100644 --- a/src/app/api/accounts/route.ts +++ b/src/app/api/accounts/route.ts @@ -6,15 +6,16 @@ import { getLicenseEntitlements } from "@/lib/licenses/service"; import { countUsedAccountSeats } from "@/lib/licenses/seats"; import { deleteAccounts } from "@/lib/accounts/deletion"; import { createAccountResponse } from "./create"; -import { accountListItemFromUser, listAccountsForAdmin, requireTeamAdmin } from "./utils"; +import { accountListItemFromUser, listAccountsForAdmin, requireTeamAdmin, withoutAccountContacts } from "./utils"; export async function GET(request: Request) { const access = await requireTeamAdmin(request); if (access.error) return access.error; + const includeContacts = canManageUsers(access.user!); const rows = await listAccountsForAdmin(getDb(access.env)); const [{ accountSeatLimit }, seatsUsed] = await Promise.all([getLicenseEntitlements(access.env), countUsedAccountSeats(access.env)]); return NextResponse.json({ - accounts: rows.map((row) => accountListItemFromUser(row)), + accounts: rows.map((row) => (includeContacts ? accountListItemFromUser(row) : withoutAccountContacts(accountListItemFromUser(row)))), seats: { limit: accountSeatLimit, used: seatsUsed }, }); } diff --git a/src/app/api/accounts/utils.ts b/src/app/api/accounts/utils.ts index 8f65930e8..1afe4a18b 100644 --- a/src/app/api/accounts/utils.ts +++ b/src/app/api/accounts/utils.ts @@ -107,6 +107,13 @@ export function accountListItemFromUser(user: { }; } +/** Recovery addresses are personal; the list shows them only to an admin who manages users. */ +export function withoutAccountContacts(item: T): Omit { + const rest = { ...item }; + delete rest.resetEmail; + return rest; +} + /** Admin session on an installation licensed for Team-only mailbox sharing. */ export async function requireTeamSharingAdmin(request: Request) { const access = await requireTeamAdmin(request); diff --git a/tests/account-detail-contacts.test.mjs b/tests/account-detail-contacts.test.mjs new file mode 100644 index 000000000..e8c0473de --- /dev/null +++ b/tests/account-detail-contacts.test.mjs @@ -0,0 +1,61 @@ +import assert from "node:assert/strict"; +import { mkdtempSync, rmSync } from "node:fs"; +import { dirname, join } from "node:path"; +import test, { after } from "node:test"; +import { fileURLToPath, pathToFileURL } from "node:url"; +import { build } from "esbuild"; + +// The account detail response includes the forwarding address only for admins who manage users. + +const root = join(dirname(fileURLToPath(import.meta.url)), ".."); +const outDir = mkdtempSync(join(root, "tests", ".tmp-account-detail-")); +after(() => rmSync(outDir, { recursive: true, force: true })); +const mocks = { + "next/server": "export const NextResponse = { json: (body, init) => Response.json(body, init) }", + "drizzle-orm": "export const eq=()=>0;", + "@/db/schema": "const c=new Proxy({}, {get:(_,k)=>String(k)});export const users=c;", + "@/db": "export function getDb(){return {}}", + "../utils": "export async function requireTeamAdmin(){return {env:{},user:globalThis.__actor}}", + "./utils": "export const selectAccountById=async()=>({id:'u2',email:'b@example.com',name:'B',role:'user',isPrimaryAdmin:false,disabled:false,forwardingEmail:'private@example.org',passwordHash:'x'});export class AccountSecurityChangedError extends Error{};export class PasskeyOnlyPasswordError extends Error{};export const updateAccountCredentials=async()=>{};", + "@/lib/licenses/seats": "export async function getAccountSeatError(){return null}", + "@/lib/licenses/service": "export async function getLicenseEntitlements(){return {canForwardEmail:true}}", + "@/lib/auth/session": "export async function deleteUserSessions(){}export function getSessionTokenFromRequestHeaders(){return null}", + "@/lib/auth/password": "export const hasPasswordHash=()=>true;", + "@/lib/auth/webauthn/grants": "export async function consumeStepUpGrant(){}", + "@/lib/mailboxes/audit": "export async function createAuditLog(){}", + "@/lib/http/auth-error": "export const authError=()=>new Response(null,{status:400});", +}; +await build({ + entryPoints: [join(root, "src/app/api/accounts/[id]/route.ts")], + outfile: join(outDir, "route.mjs"), + bundle: true, + platform: "node", + format: "esm", + packages: "external", + alias: { "@": join(root, "src") }, + plugins: [{ + name: "mock-deps", + setup(builder) { + const names = Object.keys(mocks).map((name) => name.replace(/[/.-]/g, "\\$&")).join("|"); + builder.onResolve({ filter: new RegExp(`^(${names})$`) }, (args) => ({ path: args.path, namespace: "mock" })); + builder.onLoad({ filter: /.*/, namespace: "mock" }, (args) => ({ contents: mocks[args.path], loader: "js" })); + }, + }], + logLevel: "silent", +}); +const { GET } = await import(pathToFileURL(join(outDir, "route.mjs")).href); +const detail = async (actor) => { + globalThis.__actor = actor; + return (await GET(new Request("http://localhost/api/accounts/u2"), { params: Promise.resolve({ id: "u2" }) })).json(); +}; + +test("an admin without the manage-users permission does not get the forwarding address", async () => { + const body = await detail({ id: "a1", role: "admin", isPrimaryAdmin: false, canManageUsers: false }); + assert.equal(body.account.id, "u2"); + assert.equal("forwardingEmail" in body.account, false); +}); + +test("an admin who manages users still gets it", async () => { + const body = await detail({ id: "a1", role: "admin", isPrimaryAdmin: false, canManageUsers: true }); + assert.equal(body.account.forwardingEmail, "private@example.org"); +}); diff --git a/tests/accounts-list-contacts.test.mjs b/tests/accounts-list-contacts.test.mjs new file mode 100644 index 000000000..2c2ba70bf --- /dev/null +++ b/tests/accounts-list-contacts.test.mjs @@ -0,0 +1,60 @@ +import assert from "node:assert/strict"; +import { mkdtempSync, rmSync } from "node:fs"; +import { dirname, join } from "node:path"; +import test, { after } from "node:test"; +import { fileURLToPath, pathToFileURL } from "node:url"; +import { build } from "esbuild"; + +// An admin who cannot manage users still gets the account list (it feeds +// pickers) but not other people's recovery addresses. + +const root = join(dirname(fileURLToPath(import.meta.url)), ".."); +const outDir = mkdtempSync(join(root, "tests", ".tmp-accounts-list-")); +after(() => rmSync(outDir, { recursive: true, force: true })); +const mocks = { + "next/server": "export const NextResponse = { json: (body, init) => Response.json(body, init) }", + "drizzle-orm": "export const eq=()=>0,and=()=>0,desc=()=>0,inArray=()=>0;", + "@/db/schema": "const c=new Proxy({}, {get:(_,k)=>String(k)});export const users=c,domains=c,mailboxes=c;", + "@/db": "const rows={from(){return rows},orderBy(){return Promise.resolve([{id:'u2',email:'b@example.com',name:'B',resetEmail:'private@example.org',role:'user',disabled:false,createdAt:new Date(0)}])}};export function getDb(){return {select(){return rows}}}", + "@/lib/auth/cookies": "export async function requireUser(){return globalThis.__actor}", + "@/lib/cloudflare": "export function getEnv(){return {}}", + "@/lib/licenses/service": "export async function getLicenseEntitlements(){return {canManageAccounts:true,accountSeatLimit:null}}", + "@/lib/licenses/seats": "export async function countUsedAccountSeats(){return 1}", + "@/lib/accounts/deletion": "export async function deleteAccounts(){}", + "./create": "export async function createAccountResponse(){}", +}; +await build({ + entryPoints: [join(root, "src/app/api/accounts/route.ts")], + outfile: join(outDir, "route.mjs"), + bundle: true, + platform: "node", + format: "esm", + packages: "external", + alias: { "@": join(root, "src") }, + plugins: [{ + name: "mock-deps", + setup(builder) { + const names = Object.keys(mocks).map((name) => name.replace(/[/.-]/g, "\\$&")).join("|"); + builder.onResolve({ filter: new RegExp(`^(${names})$`) }, (args) => ({ path: args.path, namespace: "mock" })); + builder.onLoad({ filter: /.*/, namespace: "mock" }, (args) => ({ contents: mocks[args.path], loader: "js" })); + }, + }], + logLevel: "silent", +}); +const { GET } = await import(pathToFileURL(join(outDir, "route.mjs")).href); +const list = async (actor) => { + globalThis.__actor = actor; + return (await GET(new Request("http://localhost/api/accounts"))).json(); +}; + +test("an admin without the manage-users permission does not see recovery addresses", async () => { + const body = await list({ id: "a1", role: "admin", isPrimaryAdmin: false, canManageUsers: false }); + assert.equal(body.accounts.length, 1); + assert.equal("resetEmail" in body.accounts[0], false); +}); + +test("an admin who manages users and the primary admin still see them", async () => { + for (const actor of [{ id: "a1", role: "admin", isPrimaryAdmin: false, canManageUsers: true }, { id: "a0", role: "admin", isPrimaryAdmin: true, canManageUsers: false }]) { + assert.equal((await list(actor)).accounts[0].resetEmail, "private@example.org"); + } +}); From 104fa99b4bb5a58700d4e3f58594f4eb874ba3f8 Mon Sep 17 00:00:00 2001 From: bobmcdonald0 <266144304+bobmcdonald0@users.noreply.github.com> Date: Sat, 14 Nov 2026 01:51:47 +0530 Subject: [PATCH 13/13] fix(messages): require write access to change the star flag 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 --- .../api/messages/[messageId]/star/route.ts | 2 + .../messages/message-folder-page.tsx | 2 +- tests/message-star-permission.test.mjs | 54 +++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 tests/message-star-permission.test.mjs diff --git a/src/app/api/messages/[messageId]/star/route.ts b/src/app/api/messages/[messageId]/star/route.ts index b62c4124c..598d42f93 100644 --- a/src/app/api/messages/[messageId]/star/route.ts +++ b/src/app/api/messages/[messageId]/star/route.ts @@ -25,6 +25,8 @@ export async function POST( const access = await getMailboxAccessLevel(db, user, message.mailboxId); if (!access?.canRead) return NextResponse.json({ error: "Message not found" }, { status: 404 }); + // The flag is stored on the shared message, so it needs the same floor as JMAP: any permission above read-only. + if (!access.canSendOnBehalf) return NextResponse.json({ error: "You cannot change messages in this mailbox" }, { status: 403 }); const starred = !message.starred; await db.update(messages).set({ starred }).where(eq(messages.id, message.id)); diff --git a/src/components/messages/message-folder-page.tsx b/src/components/messages/message-folder-page.tsx index fe45e380a..b23694984 100644 --- a/src/components/messages/message-folder-page.tsx +++ b/src/components/messages/message-folder-page.tsx @@ -257,7 +257,7 @@ function MessageListRowComponent({ size="sm" className="relative z-10" onClick={() => { - void toggleMessageStar(message.id).then((result) => setStarred(result.starred)); + void toggleMessageStar(message.id).then((result) => setStarred(result.starred)).catch(() => alertDialog(t("message.error.star"))); }} aria-label={t("message.star")} aria-pressed={starred} diff --git a/tests/message-star-permission.test.mjs b/tests/message-star-permission.test.mjs new file mode 100644 index 000000000..f1685db00 --- /dev/null +++ b/tests/message-star-permission.test.mjs @@ -0,0 +1,54 @@ +import assert from "node:assert/strict"; +import { mkdtempSync, rmSync } from "node:fs"; +import { dirname, join } from "node:path"; +import test, { after, beforeEach } from "node:test"; +import { fileURLToPath, pathToFileURL } from "node:url"; +import { build } from "esbuild"; + +const root = join(dirname(fileURLToPath(import.meta.url)), ".."); +const outDir = mkdtempSync(join(root, "tests", ".tmp-message-star-")); +after(() => rmSync(outDir, { recursive: true, force: true })); +const mocks = { + "next/server": "export const NextResponse = { json: (body, init) => Response.json(body, init) }", + "drizzle-orm": "export const eq=()=>0;", + "@/db/schema": "export const messages={id:'id',mailboxId:'mailboxId',starred:'starred'};", + "@/db": "const rows={from(){return rows},where(){return rows},limit(){return Promise.resolve([{id:'m1',mailboxId:'mb1',starred:false}])}};export function getDb(){return {select(){return rows},update(){return {set(values){return {where(){globalThis.__updates.push(values)}}}}}}}", + "@/lib/auth/cookies": "export async function getCurrentUser(){return {id:'u1'}}", + "@/lib/cloudflare": "export function getEnv(){return {}}", + "@/lib/mailboxes/access": "export async function getMailboxAccessLevel(){return globalThis.__access}", +}; +await build({ + entryPoints: [join(root, "src/app/api/messages/[messageId]/star/route.ts")], + outfile: join(outDir, "route.mjs"), + bundle: true, + platform: "node", + format: "esm", + packages: "external", + alias: { "@": join(root, "src") }, + plugins: [{ + name: "mock-deps", + setup(builder) { + const names = Object.keys(mocks).map((name) => name.replace(/[/.-]/g, "\\$&")).join("|"); + builder.onResolve({ filter: new RegExp(`^(${names})$`) }, (args) => ({ path: args.path, namespace: "mock" })); + builder.onLoad({ filter: /.*/, namespace: "mock" }, (args) => ({ contents: mocks[args.path], loader: "js" })); + }, + }], + logLevel: "silent", +}); +const { POST } = await import(pathToFileURL(join(outDir, "route.mjs")).href); +const toggle = () => POST(new Request("http://localhost/star", { method: "POST" }), { params: Promise.resolve({ messageId: "m1" }) }); + +beforeEach(() => { globalThis.__updates = []; }); + +test("a read-only delegate cannot change the shared star flag", async () => { + globalThis.__access = { canRead: true, canSendOnBehalf: false, canManage: false }; + assert.equal((await toggle()).status, 403); + assert.deepEqual(globalThis.__updates, []); +}); + +test("a delegate with write access can star", async () => { + globalThis.__access = { canRead: true, canSendOnBehalf: true, canManage: false }; + const response = await toggle(); + assert.equal(response.status, 200); + assert.deepEqual(globalThis.__updates, [{ starred: true }]); +});