Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions server/runtime/scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -27,6 +28,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);
Expand Down
2 changes: 1 addition & 1 deletion src/app/api/accounts/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
5 changes: 3 additions & 2 deletions src/app/api/accounts/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
});
}
Expand Down
7 changes: 7 additions & 0 deletions src/app/api/accounts/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends { resetEmail?: string | null }>(item: T): Omit<T, "resetEmail"> {
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);
Expand Down
4 changes: 2 additions & 2 deletions src/app/api/domains/check/route.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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 });
Expand Down
2 changes: 2 additions & 0 deletions src/app/api/messages/[messageId]/star/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion src/components/messages/message-folder-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ function MessageListRowComponent({
size="sm"
className="relative z-10"
onClick={() => {
void toggleMessageStar(message.id, !starred).then((result) => setStarred(result.starred));
void toggleMessageStar(message.id, !starred).then((result) => setStarred(result.starred)).catch(() => alertDialog(t("message.error.star")));
}}
aria-label={t("message.star")}
aria-pressed={starred}
Expand Down
3 changes: 2 additions & 1 deletion src/lib/api/key-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ const ADMIN_SCOPES = new Set<string>(ADMIN_API_KEY_SCOPES);
export async function authenticateApiKeyValue(env: CloudflareEnv, key: string): Promise<ApiAuthResult | null> {
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);
Expand Down
37 changes: 37 additions & 0 deletions src/lib/auth/purge.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
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 });
}
}
}
70 changes: 61 additions & 9 deletions src/lib/cloudflare-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {
CfEmailRoutingRule,
CfEmailRoutingRuleChange,
CfResponse,
CfResultInfo,
CfSendingSubdomain,
} from "@/lib/cloudflare-api.types";
import {
Expand All @@ -16,21 +17,33 @@ import { CloudflareApiError } from "@/lib/cloudflare-api-error";
import { getZoneLookupCandidates } from "@/lib/domains/utils";
export type { CfDnsRecord } from "@/lib/cloudflare-api.types";

export async function cfRequest<T>(
const CF_REQUEST_TIMEOUT_MS = 30_000;

async function cfRequestWithInfo<T>(
env: CloudflareEnv,
path: string,
init?: RequestInit,
): Promise<T> {
): 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),
...init,
headers: {
...getCloudflareAuthHeaders(auth),
"Content-Type": "application/json",
...(init?.headers ?? {}),
},
});
const json = (await res.json()) as CfResponse<T>;
// 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<T> | 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(
Expand All @@ -40,7 +53,15 @@ export async function cfRequest<T>(
json.errors ?? [],
);
}
return json.result;
return { result: json.result, resultInfo: json.result_info };
}

export async function cfRequest<T>(
env: CloudflareEnv,
path: string,
init?: RequestInit,
): Promise<T> {
return (await cfRequestWithInfo<T>(env, path, init)).result;
}

export async function getZone(
Expand Down Expand Up @@ -161,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<CfEmailRoutingRule[]>(
env,
`/zones/${zoneId}/email/routing/rules`,
);
const rules: CfEmailRoutingRule[] = [];
for (let page = 1; page <= MAX_RULE_PAGES; page += 1) {
const { result, resultInfo } = await cfRequestWithInfo<CfEmailRoutingRule[]>(
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(
Expand Down Expand Up @@ -215,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<string, Promise<CfEmailRoutingRule[]>>;

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;
Expand Down
9 changes: 9 additions & 0 deletions src/lib/cloudflare-api.types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ export type CfResponse<T> = {
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 =
Expand Down
17 changes: 14 additions & 3 deletions src/lib/domains/dns-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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({
Expand Down
44 changes: 43 additions & 1 deletion src/lib/domains/mx-records.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
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<never> {
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;
}
5 changes: 3 additions & 2 deletions src/lib/domains/preflight.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -7,7 +8,7 @@ export async function preflightDomain(
env: CloudflareEnv,
hostname: string,
): Promise<DomainPreflightResult> {
const normalized = hostname.toLowerCase().trim();
const normalized = normalizeHostname(hostname);
const zone = await findZoneByHostname(env, normalized);
if (!zone) {
throw new Error(
Expand All @@ -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 } };
}

Expand Down
Loading
Loading