-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathproxy.ts
More file actions
186 lines (156 loc) · 5.51 KB
/
Copy pathproxy.ts
File metadata and controls
186 lines (156 loc) · 5.51 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import { NextResponse } from 'next/server';
import type { NextFetchEvent, NextRequest } from 'next/server';
import {
ADMIN_SESSION_TTL_SECONDS,
shouldRotateAdminSession,
signAdminSessionJwt,
verifyAdminSessionJwt,
} from '@/lib/auth/admin-jwt';
const ADMIN_SESSION_COOKIE = 'aq_admin_session';
const ADMIN_UI_MODULES = new Set(['tokens', 'bridge-config', 'audit-logs']);
const ADMIN_API_POLICIES: Record<string, Set<string>> = {
'/api/admin/tokens': new Set(['GET', 'POST', 'PUT', 'DELETE']),
'/api/admin/bridge-config': new Set(['GET', 'POST', 'PUT', 'DELETE']),
'/api/admin/audit-logs': new Set(['GET']),
};
function getInternalAuditSecret(): string {
if (process.env.INTERNAL_AUDIT_SECRET) {
return process.env.INTERNAL_AUDIT_SECRET;
}
if (process.env.NODE_ENV !== 'production') {
return 'dev-only-internal-audit-secret';
}
throw new Error('INTERNAL_AUDIT_SECRET is required in production');
}
function normalizeAddress(value: string): string {
return value.toLowerCase();
}
function isAddressSegment(value: string): boolean {
return /^0x[a-fA-F0-9]{40}$/.test(value);
}
function extractAddressFromPath(pathname: string): string | null {
const segments = pathname.split('/').filter(Boolean);
const found = segments.find(isAddressSegment);
return found ? normalizeAddress(found) : null;
}
function getAdminUiModule(pathname: string): string {
const segments = pathname.split('/').filter(Boolean);
if (segments.length < 2) {
return '';
}
return segments[1] || '';
}
async function withSlidingCookie(response: NextResponse, token: string) {
const claims = await verifyAdminSessionJwt(token);
if (!claims) {
return response;
}
if (!shouldRotateAdminSession(claims)) {
return response;
}
const nextExpiresAt = new Date(Date.now() + ADMIN_SESSION_TTL_SECONDS * 1000);
const rotatedToken = await signAdminSessionJwt({
sessionId: claims.sessionId,
userId: claims.userId,
address: claims.address,
role: claims.role,
expiresAt: nextExpiresAt,
});
response.cookies.set(ADMIN_SESSION_COOKIE, rotatedToken, {
httpOnly: true,
secure: process.env.NODE_ENV === 'production',
sameSite: 'strict',
path: '/',
expires: nextExpiresAt,
});
return response;
}
function isAdminApiRouteAllowed(pathname: string, method: string): boolean {
const allowedMethods = ADMIN_API_POLICIES[pathname];
if (!allowedMethods) {
return false;
}
return allowedMethods.has(method.toUpperCase());
}
async function logSecurityEvent(request: NextRequest, payload: {
action: string;
detail: string;
adminAddress?: string;
metadata?: unknown;
}) {
try {
const url = new URL('/api/internal/security-event', request.nextUrl.origin);
await fetch(url, {
method: 'POST',
headers: {
'content-type': 'application/json',
'x-internal-audit-secret': getInternalAuditSecret(),
},
body: JSON.stringify(payload),
});
} catch {
// Ignore logging failures to avoid blocking request flow.
}
}
export async function proxy(request: NextRequest, event: NextFetchEvent) {
const { pathname } = request.nextUrl;
const token = request.cookies.get(ADMIN_SESSION_COOKIE)?.value;
const validSession = token ? await verifyAdminSessionJwt(token) : null;
const hasValidAdminSession = Boolean(validSession && validSession.role === 'admin');
const pathAddress = extractAddressFromPath(pathname);
if (hasValidAdminSession && pathAddress && validSession && normalizeAddress(validSession.address) !== pathAddress) {
event.waitUntil(
logSecurityEvent(request, {
action: 'SECURITY_ADDRESS_MISMATCH',
detail: 'Address in path does not match authenticated admin claim',
adminAddress: validSession.address,
metadata: {
pathname,
method: request.method,
pathAddress,
claimAddress: validSession.address,
ip: request.headers.get('x-forwarded-for') || 'unknown',
userAgent: request.headers.get('user-agent') || 'unknown',
},
}),
);
if (pathname.startsWith('/api/admin/')) {
return NextResponse.json({ error: 'Forbidden: address claim mismatch' }, { status: 403 });
}
const redirectUrl = request.nextUrl.clone();
redirectUrl.pathname = '/admin';
redirectUrl.search = '';
return NextResponse.redirect(redirectUrl);
}
if (pathname.startsWith('/api/admin/')) {
if (!isAdminApiRouteAllowed(pathname, request.method)) {
return NextResponse.json({ error: 'Forbidden: admin API policy denied' }, { status: 403 });
}
if (!hasValidAdminSession) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
const response = NextResponse.next();
return token ? withSlidingCookie(response, token) : response;
}
if (pathname.startsWith('/admin') && pathname !== '/admin') {
if (!hasValidAdminSession) {
const redirectUrl = request.nextUrl.clone();
redirectUrl.pathname = '/admin';
redirectUrl.search = '';
return NextResponse.redirect(redirectUrl);
}
const moduleName = getAdminUiModule(pathname);
if (moduleName && !ADMIN_UI_MODULES.has(moduleName)) {
const redirectUrl = request.nextUrl.clone();
redirectUrl.pathname = '/admin';
redirectUrl.search = '';
return NextResponse.redirect(redirectUrl);
}
const response = NextResponse.next();
return token ? withSlidingCookie(response, token) : response;
}
return NextResponse.next();
}
export const config = {
matcher: ['/admin/:path*', '/api/admin/:path*'],
};