refactor: Stop using Unkey for IP-based rate limiting (#27674)

* refactor: migrate IP-based rate limiting from Unkey to Cloudflare

Remove Unkey rate limiting for IP-based endpoints that will now be
handled by Cloudflare Enterprise Advanced Rate Limiting:

- Regular booking creation (createBooking:{hashedIP})
- Recurring booking creation (createRecurringBooking:{hashedIP})
- Instant meeting creation (instant.event-{hashedIP})
- Booking cancellation for unauthenticated users (api:cancel-ip:{hashedIP})
- Forgot password (forgotPassword:{hashedIP})
- Reset password (api:reset-password:{hashedIP})
- Signup (api:signup:{hashedIP})
- API v1 requests ({userId} with auto-lock)
- Global proxy rate limiting (common namespace)

Rate limiting that remains in Unkey (user/entity-based):
- Login (hashedEmail)
- Booking cancellation for authenticated users (api:cancel-user:{userId})
- 2FA setup/enable/disable (api:totp-*:{userId})
- SMS sending (team/org/user based)
- Email verification (various patterns)
- Team member operations (userId based)
- Routing forms (formId:responseHash)
- AI phone calls (userId based)

Also includes Cloudflare configuration proposal document with
recommended rules using JA4 fingerprinting for enhanced protection.

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* chore: remove cloudflare proposal doc from PR

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* chore: remove cloudflare comments and keep common rate limiting type

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* refactor: revert global middleware from PR #25080 and restore core rate limits

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* refactor: restore instantMeeting rate limiting

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* fix: restore email fallback in forgot-password rate limiting

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* Remove the static file check

* refactor: add POST_METHODS_ALLOWED_API_ROUTES to proxy matcher

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* revert: restore API v1 rate limiting to original state

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* revert: restore reset-password, cancel, book/event, book/recurring-event to original state

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* refactor: use POST_METHODS_ALLOWED_API_ROUTES spread in matcher instead of hardcoded routes

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* revert: restore signup route to original state

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* test: update proxy matcher tests for POST_METHODS_ALLOWED_API_ROUTES spread

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* Rename var

* refactor: revert matcher to static strings, add sync-check test for POST_METHODS_ALLOWED_API_ROUTES

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* removed dead routing forms rewrite code

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
Keith WilliamsandDevin AI authored and GitHub committed 2026-02-09 15:49:26 -03:00
1 parent edf9cd70dd
commit 8b17df4621
3 files changed
+100 -110

No files matched your search

+43 -59
View File
@@ -1,14 +1,12 @@
// Import mocked functions
import { WEBAPP_URL } from "@calcom/lib/constants";
import { get as edgeConfigGet } from "@vercel/edge-config";
import { NextRequest, NextResponse } from "next/server";
import type { Mock } from "vitest";
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { WEBAPP_URL } from "@calcom/lib/constants";
import { checkPostMethod } from "./proxy";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
// We'll test the wrapped proxy as it would be used in production
import proxy from "./proxy";
import proxy, { checkPostMethod, POST_METHODS_ALLOWED_API_ROUTES } from "./proxy";
import { config } from "./proxy";
// Mock dependencies at module level
@@ -446,7 +444,6 @@ describe("Middleware Integration Tests", () => {
});
describe("Multiple Features", () => {
it("should handle embed route with routing forms rewrite", async () => {
const req = createTestRequest({
url: `${WEBAPP_URL}/apps/routing_forms/form/embed?ui.color-scheme=light`,
@@ -484,66 +481,53 @@ describe("Middleware Integration Tests", () => {
});
});
describe("Middleware Matcher - Comprehensive Coverage", () => {
const matcher = config.matcher[0];
const pattern = matcher.replace(/^\/|\/$/g, "");
const regex = new RegExp(`^/${pattern}`);
describe("Middleware Matcher Configuration", () => {
const matcher: string[] = config.matcher;
const cases = [
// pages & apis
{ path: "/", expected: true, reason: "Root page" },
{ path: "/home", expected: true, reason: "Regular page" },
{ path: "/team/abc", expected: true, reason: "Nested page" },
{ path: "/api/auth/login", expected: true, reason: "API route" },
{ path: "/api/bookings", expected: true, reason: "Top-level API" },
{ path: "/dashboard/settings", expected: true, reason: "Deep nested page" },
{ path: "/user/john/profile", expected: true, reason: "Multiple nested path" },
{ path: "/apps/routing_forms/form", expected: true, reason: "App page under /apps" },
{ path: "/embed?ui.color-scheme=dark", expected: true, reason: "Embed query param" },
it("should include all core middleware routes", () => {
expect(matcher).toContain("/auth/login");
expect(matcher).toContain("/login");
expect(matcher).toContain("/apps/installed");
expect(matcher).toContain("/auth/logout");
expect(matcher).toContain("/:path*/embed");
});
// should be ignored (internal / static / public)
{ path: "/_next/static/chunks/app.js", expected: false, reason: "Internal static asset" },
{ path: "/_next/image?url=%2Flogo.png&w=256&q=75", expected: false, reason: "Internal image handler" },
{ path: "/_next/data/build-id/page.json", expected: false, reason: "Next.js data route" },
{ path: "/favicon.ico", expected: false, reason: "Favicon asset" },
{ path: "/robots.txt", expected: false, reason: "Robots file" },
{ path: "/sitemap.xml", expected: false, reason: "Sitemap file" },
{ path: "/public/images/logo.png", expected: false, reason: "Public folder asset" },
{ path: "/public/fonts/inter.woff2", expected: false, reason: "Public folder font" },
{ path: "/static/js/main.js", expected: false, reason: "Static folder JavaScript" },
{ path: "/static/css/app.css", expected: false, reason: "Static folder stylesheet" },
it("should have no duplicate entries", () => {
const uniqueEntries = new Set(matcher);
expect(uniqueEntries.size).toBe(matcher.length);
});
// edge cases
{ path: "/manifest.json", expected: true, reason: "Manifest is a public page, not ignored" },
{ path: "/_nextsomething", expected: true, reason: "Looks like _next but not reserved" },
{ path: "/nextconfig", expected: true, reason: "Normal route with 'next' in name" },
{ path: "/_NEXT/image", expected: true, reason: "Case-sensitive test (should match)" },
{ path: "/favicon-abc.ico", expected: true, reason: "Favicon variant should still match" },
{ path: "/robots-custom.txt", expected: true, reason: "Custom robots file should match" },
{ path: "/sitemap-other.xml", expected: true, reason: "Custom sitemap file should match" },
{ path: "/api_", expected: true, reason: "Partial match with api underscore" },
{ path: "//double-slash", expected: true, reason: "Double slash URL" },
{ path: "/_next", expected: false, reason: "Bare _next path" },
];
it("should cover every POST_METHODS_ALLOWED_API_ROUTES route via exact match or wildcard", () => {
const wildcardPrefixes = matcher
.filter((entry) => entry.endsWith(":path*"))
.map((entry) => entry.replace(":path*", ""));
it("should match only the intended routes", () => {
for (const { path, expected, reason } of cases) {
const result = regex.test(path);
expect(result, `${path} → ${reason}`).toBe(expected);
for (const route of POST_METHODS_ALLOWED_API_ROUTES) {
const matcherEntry = route.endsWith("/") ? `${route}:path*` : route;
const coveredByWildcard = wildcardPrefixes.some((prefix) => route.startsWith(prefix));
const coveredByExact = matcher.includes(matcherEntry);
expect(
coveredByWildcard || coveredByExact,
`POST route "${route}" is not covered by any matcher entry`
).toBe(true);
}
});
it("should not accidentally match internal Next.js routes", () => {
const internalPaths = ["/_next/static", "/_next/image", "/_next/data"];
for (const path of internalPaths) {
expect(regex.test(path)).toBe(false);
}
});
it("should not contain API matcher entries without corresponding POST_METHODS_ALLOWED_API_ROUTES routes", () => {
const NON_API_ROUTES = ["/auth/login", "/login", "/apps/installed", "/auth/logout", "/:path*/embed"];
const apiMatcherEntries = matcher.filter((entry) => !NON_API_ROUTES.includes(entry));
it("should match all user-facing routes and APIs", () => {
const publicPaths = ["/", "/api/user", "/settings", "/dashboard"];
for (const path of publicPaths) {
expect(regex.test(path)).toBe(true);
for (const entry of apiMatcherEntries) {
if (entry.endsWith(":path*")) {
const prefix = entry.replace(":path*", "");
const hasCoveredRoutes = POST_METHODS_ALLOWED_API_ROUTES.some((route) => route.startsWith(prefix));
expect(hasCoveredRoutes, `Matcher wildcard "${entry}" doesn't cover any POST route`).toBe(true);
} else {
expect(
POST_METHODS_ALLOWED_API_ROUTES,
`Matcher has "${entry}" but it's missing from POST_METHODS_ALLOWED_API_ROUTES`
).toContain(entry);
}
}
});
});