Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

fix(auth): allow 10 sign-in attempts per 5 minutes instead of 5 per hour

Sidwebworks1bf1597da69f
diff --git a/.claude/rules/authentication-system.md b/.claude/rules/authentication-system.md
index 06c6f17d97..253be58691 100644
--- a/.claude/rules/authentication-system.md
+++ b/.claude/rules/authentication-system.md
@@ -34,7 +34,7 @@ Export subpaths (`package.json`): `.` (`index.ts`), `./auth.server`, `./session.
## Login (`apps/erp/app/routes/_public+/login.tsx`)
Magic link is the primary flow. The action rate-limits by IP (Upstash via `@carbon/kv`,
-`RATE_LIMIT` env default **5 / hour**), runs the bot check (`verifyBotProtection`), then:
+`RATE_LIMIT` env default **10 per 5 minutes**, shared by login, MFA and unlock), runs the bot check (`verifyBotProtection`), then:
**Bot protection.** `botProtection` (`bot-protection.server.ts`, re-exported from
`auth.server.ts`) picks ONE provider per process from `BOT_PROTECTION`: `"botid"` (only
diff --git a/.env.example b/.env.example
index 8ad94d57f9..2a274e7db5 100644
--- a/.env.example
+++ b/.env.example
@@ -10,7 +10,7 @@
AUTH_PROVIDERS="email,google,passkey" # "email" | "google" | "azure" | "passkey"
CARBON_EDITION="community" # "community" | "cloud" | "enterprise"
SESSION_SECRET="************************"
-RATE_LIMIT="5"
+RATE_LIMIT="10"
# MAINTENANCE_MODE="true" # every request except /health answers 503 with a maintenance screen; needs a restart
# SYSTEM_STATUS_URL="https://example.com/status.json" # optional; { latestVersion?, announcement? } shown as a bar in the app
# Feature flags — optional; unset means every flag answers its registry default (packages/env/src/flags.ts).
@@ -132,7 +132,7 @@ GOOGLE_CLIENT_ID=""
GOOGLE_CLIENT_SECRET=""
GOOGLE_OAUTH_REDIRECT_URL=""
-RATE_LIMIT="5"
+RATE_LIMIT="10"
# Enterprise SAML SSO (Enterprise edition only). Generate the key with:
# openssl genpkey -algorithm RSA -out /tmp/pk.pem -quiet && \
diff --git a/apps/academy/app/routes/_auth+/login.tsx b/apps/academy/app/routes/_auth+/login.tsx
index ff8318339e..8640390a15 100644
--- a/apps/academy/app/routes/_auth+/login.tsx
+++ b/apps/academy/app/routes/_auth+/login.tsx
@@ -67,7 +67,7 @@ export async function action({ request }: ActionFunctionArgs) {
const ip = getClientIp(request) ?? "127.0.0.1";
const ratelimit = new Ratelimit({
redis,
- limiter: Ratelimit.slidingWindow(RATE_LIMIT, "1 h"),
+ limiter: Ratelimit.slidingWindow(RATE_LIMIT, "5 m"),
analytics: true
});
const { success } = await ratelimit.limit(ip);
diff --git a/apps/academy/app/routes/_auth+/mfa.tsx b/apps/academy/app/routes/_auth+/mfa.tsx
index d2b242f744..32cda54bef 100644
--- a/apps/academy/app/routes/_auth+/mfa.tsx
+++ b/apps/academy/app/routes/_auth+/mfa.tsx
@@ -74,7 +74,7 @@ export async function action({ request }: ActionFunctionArgs) {
const ratelimit = new Ratelimit({
redis,
- limiter: Ratelimit.slidingWindow(RATE_LIMIT, "1 h"),
+ limiter: Ratelimit.slidingWindow(RATE_LIMIT, "5 m"),
analytics: true
});
diff --git a/apps/erp/app/routes/_public+/login.tsx b/apps/erp/app/routes/_public+/login.tsx
index 5c1a5006e9..7880ce85f2 100644
--- a/apps/erp/app/routes/_public+/login.tsx
+++ b/apps/erp/app/routes/_public+/login.tsx
@@ -128,7 +128,7 @@ export async function action({ request }: ActionFunctionArgs) {
const ip = getClientIp(request) ?? "127.0.0.1";
const ratelimit = new Ratelimit({
redis,
- limiter: Ratelimit.slidingWindow(RATE_LIMIT, "1 h"),
+ limiter: Ratelimit.slidingWindow(RATE_LIMIT, "5 m"),
analytics: true
});
const { success } = await ratelimit.limit(ip);
diff --git a/apps/erp/app/routes/_public+/mfa.tsx b/apps/erp/app/routes/_public+/mfa.tsx
index e64ac6c5c8..86c00286d2 100644
--- a/apps/erp/app/routes/_public+/mfa.tsx
+++ b/apps/erp/app/routes/_public+/mfa.tsx
@@ -78,7 +78,7 @@ export async function action({ request }: ActionFunctionArgs) {
const ratelimit = new Ratelimit({
redis,
- limiter: Ratelimit.slidingWindow(RATE_LIMIT, "1 h"),
+ limiter: Ratelimit.slidingWindow(RATE_LIMIT, "5 m"),
analytics: true
});
diff --git a/apps/erp/app/routes/_public+/unlock.tsx b/apps/erp/app/routes/_public+/unlock.tsx
index 38b4c90c27..b73b53320d 100644
--- a/apps/erp/app/routes/_public+/unlock.tsx
+++ b/apps/erp/app/routes/_public+/unlock.tsx
@@ -272,7 +272,7 @@ export async function action({ request }: ActionFunctionArgs) {
const ratelimit = new Ratelimit({
redis,
- limiter: Ratelimit.slidingWindow(RATE_LIMIT, "1 h"),
+ limiter: Ratelimit.slidingWindow(RATE_LIMIT, "5 m"),
analytics: true
});
diff --git a/apps/erp/app/routes/_public+/verify.tsx b/apps/erp/app/routes/_public+/verify.tsx
index 65e88d2718..1c97588896 100644
--- a/apps/erp/app/routes/_public+/verify.tsx
+++ b/apps/erp/app/routes/_public+/verify.tsx
@@ -69,7 +69,7 @@ export async function action({ request }: ActionFunctionArgs) {
const ratelimit = new Ratelimit({
redis,
- limiter: Ratelimit.slidingWindow(RATE_LIMIT, "1 h"),
+ limiter: Ratelimit.slidingWindow(RATE_LIMIT, "5 m"),
analytics: true
});
diff --git a/apps/erp/app/routes/api+/login.verify-code.ts b/apps/erp/app/routes/api+/login.verify-code.ts
index ae88af7009..611e9e26c7 100644
--- a/apps/erp/app/routes/api+/login.verify-code.ts
+++ b/apps/erp/app/routes/api+/login.verify-code.ts
@@ -53,7 +53,7 @@ export async function action({ request }: ActionFunctionArgs) {
const ratelimit = new Ratelimit({
redis,
- limiter: Ratelimit.slidingWindow(RATE_LIMIT, "1 h"),
+ limiter: Ratelimit.slidingWindow(RATE_LIMIT, "5 m"),
analytics: true,
prefix: "@carbon/ratelimit-code"
});
diff --git a/apps/erp/app/routes/api+/mfa.unenroll.ts b/apps/erp/app/routes/api+/mfa.unenroll.ts
index 3959948b8d..2313af6964 100644
--- a/apps/erp/app/routes/api+/mfa.unenroll.ts
+++ b/apps/erp/app/routes/api+/mfa.unenroll.ts
@@ -22,7 +22,7 @@ export async function action({ request }: ActionFunctionArgs) {
const ip = getClientIp(request) ?? "127.0.0.1";
const ratelimit = new Ratelimit({
redis,
- limiter: Ratelimit.slidingWindow(RATE_LIMIT, "1 h"),
+ limiter: Ratelimit.slidingWindow(RATE_LIMIT, "5 m"),
analytics: true
});
const { success: withinLimit } = await ratelimit.limit(ip);
diff --git a/apps/erp/app/routes/api+/mfa.verify.ts b/apps/erp/app/routes/api+/mfa.verify.ts
index 3560673459..b928f34a2a 100644
--- a/apps/erp/app/routes/api+/mfa.verify.ts
+++ b/apps/erp/app/routes/api+/mfa.verify.ts
@@ -24,7 +24,7 @@ export async function action({ request }: ActionFunctionArgs) {
const ip = getClientIp(request) ?? "127.0.0.1";
const ratelimit = new Ratelimit({
redis,
- limiter: Ratelimit.slidingWindow(RATE_LIMIT, "1 h"),
+ limiter: Ratelimit.slidingWindow(RATE_LIMIT, "5 m"),
analytics: true
});
const { success: withinLimit } = await ratelimit.limit(ip);
diff --git a/apps/mes/app/routes/_public+/login.tsx b/apps/mes/app/routes/_public+/login.tsx
index 563d5c28ac..4e4d6a23ee 100644
--- a/apps/mes/app/routes/_public+/login.tsx
+++ b/apps/mes/app/routes/_public+/login.tsx
@@ -121,7 +121,7 @@ export async function action({ request }: ActionFunctionArgs) {
const ip = getClientIp(request) ?? "127.0.0.1";
const ratelimit = new Ratelimit({
redis,
- limiter: Ratelimit.slidingWindow(RATE_LIMIT, "1 h"),
+ limiter: Ratelimit.slidingWindow(RATE_LIMIT, "5 m"),
analytics: true
});
const { success } = await ratelimit.limit(ip);
diff --git a/apps/mes/app/routes/_public+/mfa.tsx b/apps/mes/app/routes/_public+/mfa.tsx
index 0f51050495..0223deaa65 100644
--- a/apps/mes/app/routes/_public+/mfa.tsx
+++ b/apps/mes/app/routes/_public+/mfa.tsx
@@ -76,7 +76,7 @@ export async function action({ request }: ActionFunctionArgs) {
const ratelimit = new Ratelimit({
redis,
- limiter: Ratelimit.slidingWindow(RATE_LIMIT, "1 h"),
+ limiter: Ratelimit.slidingWindow(RATE_LIMIT, "5 m"),
analytics: true
});
diff --git a/apps/mes/app/routes/_public+/unlock.tsx b/apps/mes/app/routes/_public+/unlock.tsx
index daf4266821..b5fed44343 100644
--- a/apps/mes/app/routes/_public+/unlock.tsx
+++ b/apps/mes/app/routes/_public+/unlock.tsx
@@ -274,7 +274,7 @@ export async function action({ request }: ActionFunctionArgs) {
const ratelimit = new Ratelimit({
redis,
- limiter: Ratelimit.slidingWindow(RATE_LIMIT, "1 h"),
+ limiter: Ratelimit.slidingWindow(RATE_LIMIT, "5 m"),
analytics: true
});
diff --git a/apps/mes/app/routes/api+/login.verify-code.ts b/apps/mes/app/routes/api+/login.verify-code.ts
index ae88af7009..611e9e26c7 100644
--- a/apps/mes/app/routes/api+/login.verify-code.ts
+++ b/apps/mes/app/routes/api+/login.verify-code.ts
@@ -53,7 +53,7 @@ export async function action({ request }: ActionFunctionArgs) {
const ratelimit = new Ratelimit({
redis,
- limiter: Ratelimit.slidingWindow(RATE_LIMIT, "1 h"),
+ limiter: Ratelimit.slidingWindow(RATE_LIMIT, "5 m"),
analytics: true,
prefix: "@carbon/ratelimit-code"
});
diff --git a/apps/starter/app/routes/_public+/login.tsx b/apps/starter/app/routes/_public+/login.tsx
index 60322c2512..a39c3eced3 100644
--- a/apps/starter/app/routes/_public+/login.tsx
+++ b/apps/starter/app/routes/_public+/login.tsx
@@ -67,7 +67,7 @@ export async function action({ request }: ActionFunctionArgs) {
const ip = getClientIp(request) ?? "127.0.0.1";
const ratelimit = new Ratelimit({
redis,
- limiter: Ratelimit.slidingWindow(RATE_LIMIT, "1 h"),
+ limiter: Ratelimit.slidingWindow(RATE_LIMIT, "5 m"),
analytics: true
});
const { success } = await ratelimit.limit(ip);
diff --git a/apps/starter/app/routes/_public+/mfa.tsx b/apps/starter/app/routes/_public+/mfa.tsx
index 4c9f718104..4ad7f07d6f 100644
--- a/apps/starter/app/routes/_public+/mfa.tsx
+++ b/apps/starter/app/routes/_public+/mfa.tsx
@@ -74,7 +74,7 @@ export async function action({ request }: ActionFunctionArgs) {
const ratelimit = new Ratelimit({
redis,
- limiter: Ratelimit.slidingWindow(RATE_LIMIT, "1 h"),
+ limiter: Ratelimit.slidingWindow(RATE_LIMIT, "5 m"),
analytics: true
});
diff --git a/docs/content/docs/platform/self-hosting/environment-variables.mdx b/docs/content/docs/platform/self-hosting/environment-variables.mdx
index 68a51f9634..c18fd2c4aa 100644
--- a/docs/content/docs/platform/self-hosting/environment-variables.mdx
+++ b/docs/content/docs/platform/self-hosting/environment-variables.mdx
@@ -18,7 +18,7 @@ Platform-wide behavior and edition.
<EnvVar name="ERP_URL" type="url">Public URL of the ERP app. Required for the MES: it sends shop-floor postings (material issues, completions, picking) to this address.</EnvVar>
<EnvVar name="MES_URL" type="url">Public URL of the MES app.</EnvVar>
<EnvVar name="DEFAULT_LANGUAGE" type="string">Default UI language.</EnvVar>
-<EnvVar name="RATE_LIMIT" type="number">Requests per window allowed against the API.</EnvVar>
+<EnvVar name="RATE_LIMIT" type="number">Sign-in, MFA and unlock attempts allowed per IP every 5 minutes (default 10).</EnvVar>
</EnvVars>
## Database — Supabase
diff --git a/packages/env/src/index.ts b/packages/env/src/index.ts
index e566236e42..30c95731fc 100644
--- a/packages/env/src/index.ts
+++ b/packages/env/src/index.ts
@@ -335,7 +335,7 @@ export const DEFAULT_LANGUAGE = getEnv("DEFAULT_LANGUAGE") ?? "en";
// browser prod: warning), so an invalid/absent value never throws.
export const LOG_LEVEL = getEnv("LOG_LEVEL");
-export const RATE_LIMIT = parseInt(getEnv("RATE_LIMIT") || "5", 10);
+export const RATE_LIMIT = parseInt(getEnv("RATE_LIMIT") || "10", 10);
// Vercel's own VERCEL_URL is a bare host; an APP_URL set by hand is an origin.
const previewUrl = () =>
diff --git a/packages/env/src/schema.ts b/packages/env/src/schema.ts
index 678f372324..0872507f46 100644
--- a/packages/env/src/schema.ts
+++ b/packages/env/src/schema.ts
@@ -295,7 +295,7 @@ export const schema = define({
},
RATE_LIMIT: {
group: "auth",
- description: "Sign-in attempts allowed per window (5)",
+ description: "Sign-in attempts allowed per IP every 5 minutes (10)",
type: number
},
TRUSTED_PROXIES: {
diff --git a/packages/kv/src/lockout/lockout.test.ts b/packages/kv/src/lockout/lockout.test.ts
index 8e53bcff9c..d74d9c5b73 100644
--- a/packages/kv/src/lockout/lockout.test.ts
+++ b/packages/kv/src/lockout/lockout.test.ts
@@ -276,8 +276,8 @@ describe("AccountLockout", () => {
});
describe("maxAttemptsForRateLimit", () => {
- it("is a quarter of the hourly RATE_LIMIT, never below the default", () => {
- expect(maxAttemptsForRateLimit(5)).toBe(DEFAULT_MAX_ATTEMPTS);
+ it("is a quarter of RATE_LIMIT, never below the default", () => {
+ expect(maxAttemptsForRateLimit(10)).toBe(DEFAULT_MAX_ATTEMPTS);
expect(maxAttemptsForRateLimit(1000)).toBe(250);
expect(maxAttemptsForRateLimit(30)).toBe(8);
expect(maxAttemptsForRateLimit(Number.NaN)).toBe(DEFAULT_MAX_ATTEMPTS);
diff --git a/packages/kv/src/lockout/lockout.ts b/packages/kv/src/lockout/lockout.ts
index 2c1e36bb2b..9006bf1601 100644
--- a/packages/kv/src/lockout/lockout.ts
+++ b/packages/kv/src/lockout/lockout.ts
@@ -32,17 +32,17 @@ export const DEFAULT_MAX_LOCK_SECONDS = 60 * 60; // cap: 1 hour
export const DEFAULT_LEVEL_TTL_SECONDS = 24 * 60 * 60; // 1 day
/**
- * The per-account allowance for a login route, from its per-IP hourly
- * RATE_LIMIT: a quarter of the hour's budget per 15-minute window, never
- * fewer than DEFAULT_MAX_ATTEMPTS. At the default RATE_LIMIT of 5 that is
+ * The per-account allowance for a login route, from its per-IP RATE_LIMIT
+ * (attempts per 5 minutes): a quarter of it per 15-minute window, never
+ * fewer than DEFAULT_MAX_ATTEMPTS. At the default RATE_LIMIT of 10 that is
* the default; a deployment that raises RATE_LIMIT raises this with it,
* one knob for both.
*/
-export function maxAttemptsForRateLimit(ratePerHour: number): number {
- if (!Number.isFinite(ratePerHour) || ratePerHour <= 0) {
+export function maxAttemptsForRateLimit(rateLimit: number): number {
+ if (!Number.isFinite(rateLimit) || rateLimit <= 0) {
return DEFAULT_MAX_ATTEMPTS;
}
- return Math.max(DEFAULT_MAX_ATTEMPTS, Math.ceil(ratePerHour / 4));
+ return Math.max(DEFAULT_MAX_ATTEMPTS, Math.ceil(rateLimit / 4));
}
const DEFAULT_PREFIX = "@carbon/lockout";