-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
97 lines (87 loc) · 2.8 KB
/
Copy pathproxy.ts
File metadata and controls
97 lines (87 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { NextResponse, type NextRequest } from "next/server";
import { getGlobalLimiter, getApiLimiter, getClientIp } from "@/lib/rate-limit";
import {
verifySessionToken,
readSessionTokenFromCookies,
resolveSessionCookieName,
isSecureRequestUrl,
SESSION_COOKIE_DEV,
SESSION_COOKIE_PROD,
} from "@/lib/session-token";
export async function proxy(request: NextRequest) {
const { pathname } = request.nextUrl;
const ip = getClientIp(request.headers);
// 1. API Rate Limiting (Tier 4)
if (pathname.startsWith("/api/")) {
const apiLimiter = getApiLimiter();
if (apiLimiter) {
const { success, limit, reset, remaining } = await apiLimiter.limit(ip);
if (!success) {
return NextResponse.json(
{ error: "Too many requests. Please try again later." },
{
status: 429,
headers: {
"X-RateLimit-Limit": limit.toString(),
"X-RateLimit-Remaining": remaining.toString(),
"Retry-After": Math.ceil((reset - Date.now()) / 1000).toString(),
},
},
);
}
}
} else {
// 2. Global Rate Limiting (Tier 1) for all non-API paths
const globalLimiter = getGlobalLimiter();
if (globalLimiter) {
const { success, limit, reset, remaining } =
await globalLimiter.limit(ip);
if (!success) {
return new NextResponse("Too Many Requests", {
status: 429,
headers: {
"X-RateLimit-Limit": limit.toString(),
"X-RateLimit-Remaining": remaining.toString(),
"Retry-After": Math.ceil((reset - Date.now()) / 1000).toString(),
},
});
}
}
}
// 3. Admin Authentication Logic
if (!pathname.startsWith("/admin")) {
return NextResponse.next();
}
if (pathname === "/admin/error") {
return NextResponse.next();
}
const isSecure = isSecureRequestUrl(request.nextUrl.protocol);
const token = readSessionTokenFromCookies(request.cookies, isSecure);
if (!token) {
if (pathname !== "/admin/login") {
return NextResponse.redirect(new URL("/admin/login", request.url));
}
return NextResponse.next();
}
try {
await verifySessionToken(token);
if (pathname === "/admin/login") {
return NextResponse.redirect(new URL("/admin", request.url));
}
return NextResponse.next();
} catch (_) {
if (pathname !== "/admin/login") {
const response = NextResponse.redirect(
new URL("/admin/login", request.url),
);
response.cookies.delete(resolveSessionCookieName(isSecure));
response.cookies.delete(SESSION_COOKIE_PROD);
response.cookies.delete(SESSION_COOKIE_DEV);
return response;
}
return NextResponse.next();
}
}
export const config = {
matcher: ["/((?!_next/static|_next/image|favicon.ico).*)"],
};