chore: Block POST requests to all page routes except a whitelist (#19346)
* chore: block POST requests to next.js routes except a whitelist * update config.matcher --------- Co-authored-by: Keith Williams <[email protected]>
This commit is contained in:
1 parent
1be002c33c
commit
01fc501cb9
4 files changed
+83
-75
No files matched your search
+28
-4
@@ -6,9 +6,7 @@ import { NextResponse } from "next/server";
|
||||
import { getLocale } from "@calcom/features/auth/lib/getLocale";
|
||||
import { extendEventData, nextCollectBasicSettings } from "@calcom/lib/telemetry";
|
||||
|
||||
import { csp } from "@lib/csp";
|
||||
|
||||
import { abTestMiddlewareFactory } from "./abTest/middlewareFactory";
|
||||
import { csp } from "./lib/csp";
|
||||
|
||||
const safeGet = async <T = any>(key: string): Promise<T | undefined> => {
|
||||
try {
|
||||
@@ -18,7 +16,33 @@ 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"];
|
||||
|
||||
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"
|
||||
) {
|
||||
return new NextResponse(null, {
|
||||
status: 405,
|
||||
statusText: "Method Not Allowed",
|
||||
headers: {
|
||||
Allow: "GET",
|
||||
},
|
||||
});
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
const middleware = async (req: NextRequest): Promise<NextResponse<unknown>> => {
|
||||
const postCheckResult = checkPostMethod(req);
|
||||
if (postCheckResult) return postCheckResult;
|
||||
|
||||
const url = req.nextUrl;
|
||||
const requestHeaders = new Headers(req.headers);
|
||||
requestHeaders.set("x-url", req.url);
|
||||
@@ -174,7 +198,7 @@ export const config = {
|
||||
};
|
||||
|
||||
export default collectEvents({
|
||||
middleware: abTestMiddlewareFactory(middleware),
|
||||
middleware,
|
||||
...nextCollectBasicSettings,
|
||||
cookieName: "__clnds",
|
||||
extend: extendEventData,
|
||||
|
||||
Reference in new issue
Block a user