Skip to content

Open source alternative to Semrush and Ahrefs

Rate-limit email sign-in per IP (#1063)

Ben Senescu0ca244b0dffe
diff --git a/deploy/alchemy/alchemy.run.ts b/deploy/alchemy/alchemy.run.ts
index ea824315..e4ee71ad 100644
--- a/deploy/alchemy/alchemy.run.ts
+++ b/deploy/alchemy/alchemy.run.ts
@@ -490,6 +490,13 @@ export default Alchemy.Stack(
simple: { limit: 5000, period: 60 },
}),
+ // Per-IP throttle for email sign-in (see src/routes/api/auth/$.ts).
+ // Generous enough that no real person or shared office IP hits it.
+ AUTH_RATE_LIMIT: Cloudflare.RateLimit("AUTH_RATE_LIMIT", {
+ namespaceId: 1002,
+ simple: { limit: 20, period: 60 },
+ }),
+
// Durable Objects (the chat agents; the audit scratchpad lives
// privately in the open-seo-audit worker). Alchemy backs new DO
// classes with SQLite storage; wrangler.jsonc's `migrations` only
diff --git a/src/routes/api/auth/$.ts b/src/routes/api/auth/$.ts
index 0e871617..cc5c500b 100644
--- a/src/routes/api/auth/$.ts
+++ b/src/routes/api/auth/$.ts
@@ -16,6 +16,27 @@ async function handleAuthRequest(request: Request) {
});
}
+ // Per-IP throttle on password sign-in so passwords can't be guessed at
+ // machine speed. Better Auth's own limiter is in-memory, which on Workers
+ // is per isolate, so it never engages. The binding is declared in
+ // deploy/alchemy/alchemy.run.ts; the wrangler.jsonc surfaces (local dev,
+ // Docker self-host) don't have it and skip limiting.
+ const ip = request.headers.get("cf-connecting-ip");
+ if (
+ env.AUTH_RATE_LIMIT &&
+ ip &&
+ request.method === "POST" &&
+ new URL(request.url).pathname === "/api/auth/sign-in/email"
+ ) {
+ const { success } = await env.AUTH_RATE_LIMIT.limit({ key: ip });
+ if (!success) {
+ return Response.json(
+ { message: "Too many sign-in attempts. Try again in a minute." },
+ { status: 429 },
+ );
+ }
+ }
+
const auth = getAuth();
return auth.handler(request);
}
diff --git a/src/worker-configuration.d.ts b/src/worker-configuration.d.ts
index c2cb8fcd..d4ff5f9d 100644
--- a/src/worker-configuration.d.ts
+++ b/src/worker-configuration.d.ts
@@ -23,6 +23,7 @@ declare namespace Cloudflare {
// Hand-patched: hosted-prod-only binding declared in deploy/alchemy/alchemy.run.ts,
// absent in the wrangler.jsonc surfaces (local dev, Docker self-host).
MCP_RATE_LIMIT?: RateLimit;
+ AUTH_RATE_LIMIT?: RateLimit;
// Hand-patched: the class moved to src/audit-worker.ts and a full regen pulls
// unrelated runtime-type drift (see cf-typegen); keep this in sync until then.
SITE_AUDIT_WORKFLOW: Workflow<Parameters<import("./audit-worker").SiteAuditWorkflow['run']>[0]['payload']>;