-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
46 lines (40 loc) · 1.55 KB
/
Copy pathproxy.ts
File metadata and controls
46 lines (40 loc) · 1.55 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
import { NextResponse, type NextRequest } from 'next/server';
// Explicit allowlist - never '*': /api/* routes spend paid/rate-limited
// third-party quota (VirusTotal, urlscan.io, Safe Browsing, ...) per request,
// so an open CORS policy would let any site burn through it on our behalf.
const ALLOWED = new Set(
[
'https://localhost', // Capacitor Android WebView (androidScheme: 'https')
'capacitor://localhost', // Capacitor iOS, if the platform is added later
process.env.NEXT_PUBLIC_SITE_ORIGIN, // the web app's own origin
].filter((origin): origin is string => Boolean(origin))
);
function corsHeaders(origin: string): Record<string, string> {
return {
'Access-Control-Allow-Origin': origin,
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, x-health-token',
'Access-Control-Max-Age': '86400',
Vary: 'Origin',
};
}
export function proxy(request: NextRequest) {
const origin = request.headers.get('origin');
const allowed = origin !== null && ALLOWED.has(origin);
if (request.method === 'OPTIONS') {
return new NextResponse(null, {
status: 204,
headers: allowed && origin ? corsHeaders(origin) : {},
});
}
const response = NextResponse.next();
if (allowed && origin) {
for (const [key, value] of Object.entries(corsHeaders(origin))) {
response.headers.set(key, value);
}
}
return response;
}
export const config = {
matcher: '/api/:path*',
};