Skip to content

Commit 8b6c627

Browse files
authored
Fix api v1 block paths (calcom#25554)
1 parent 957d197 commit 8b6c627

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

apps/api/v1/middleware.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type { NextRequest } from "next/server";
2+
import { NextResponse } from "next/server";
3+
4+
export const BLOCKED_ROUTE_SEGMENTS = ["_get", "_post", "_patch", "_delete", "_auth-middleware"] as const;
5+
6+
const pathContainsBlockedSegment = (pathname: string) =>
7+
pathname
8+
.split("/")
9+
.filter(Boolean)
10+
.some((segment) => BLOCKED_ROUTE_SEGMENTS.includes(segment as (typeof BLOCKED_ROUTE_SEGMENTS)[number]));
11+
12+
export function middleware(request: NextRequest) {
13+
if (pathContainsBlockedSegment(request.nextUrl.pathname)) {
14+
return NextResponse.json({ message: "Forbidden" }, { status: 403 });
15+
}
16+
17+
return NextResponse.next();
18+
}
19+
20+
export const config = {
21+
matcher: BLOCKED_ROUTE_SEGMENTS.flatMap((segment) => [
22+
`/:path*/${segment}/:rest*`,
23+
`/:path*/${segment}`,
24+
]),
25+
};

0 commit comments

Comments
 (0)