Skip to content

perf: remove unnecessary routes from middleware #21089

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
10 changes: 1 addition & 9 deletions apps/web/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { describe, it, expect } from "vitest";

import { WEBAPP_URL } from "@calcom/lib/constants";

import { checkPostMethod, POST_METHODS_ALLOWED_APP_ROUTES } from "./middleware";
import { checkPostMethod } from "./middleware";

describe("Middleware - POST requests restriction", () => {
const createRequest = (path: string, method: string) => {
Expand All @@ -24,14 +24,6 @@ describe("Middleware - POST requests restriction", () => {
expect(res2).toBeNull();
});

it("should allow POST requests to allowed app routes", async () => {
POST_METHODS_ALLOWED_APP_ROUTES.forEach(async (route) => {
const req = createRequest(route, "POST");
const res = checkPostMethod(req);
expect(res).toBeNull();
});
});

it("should block POST requests to not-allowed app routes", async () => {
const req = createRequest("/team/xyz", "POST");
const res = checkPostMethod(req);
Expand Down
19 changes: 1 addition & 18 deletions apps/web/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,9 @@ const safeGet = async <T = any>(key: string): Promise<T | undefined> => {
};

export const POST_METHODS_ALLOWED_API_ROUTES = ["/api/auth/signup", "/api/trpc/"];
// Some app routes are allowed because "revalidatePath()" is used to revalidate the cache for them
export const POST_METHODS_ALLOWED_APP_ROUTES = [
"/settings/my-account/general",
"/settings/developer/webhooks",
"/settings/developer/api-keys",
"/teams",
];
export function checkPostMethod(req: NextRequest) {
const pathname = req.nextUrl.pathname;
if (
![...POST_METHODS_ALLOWED_API_ROUTES, ...POST_METHODS_ALLOWED_APP_ROUTES].some((route) =>
pathname.startsWith(route)
) &&
req.method === "POST"
) {
if (!POST_METHODS_ALLOWED_API_ROUTES.some((route) => pathname.startsWith(route)) && req.method === "POST") {
return new NextResponse(null, {
status: 405,
statusText: "Method Not Allowed",
Expand Down Expand Up @@ -191,11 +179,6 @@ export const config = {
// API routes
"/api/auth/signup",
"/api/trpc/:path*",
// Routes allowed for POST method (matching `POST_METHODS_ALLOWED_APP_ROUTES` array)
"/settings/my-account/general",
"/settings/developer/webhooks",
"/settings/developer/api-keys",
"/teams",
Comment on lines -194 to -198
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can simply remove these routes from config.matcher and not need to maintain POST_METHODS_ALLOWED_APP_ROUTES array

],
};

Expand Down
Loading