-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
53 lines (45 loc) · 1.68 KB
/
proxy.ts
File metadata and controls
53 lines (45 loc) · 1.68 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
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
/**
* CORS + origin enforcement for API routes.
* Blocks cross-origin requests unless the Origin matches the app's own host.
*/
export function proxy(request: NextRequest) {
const origin = request.headers.get('origin')
const host = request.headers.get('host')
// Determine the allowed origin from the Host header (same-origin)
const proto = request.headers.get('x-forwarded-proto') ?? 'https'
const allowedOrigin = host ? `${proto}://${host}` : null
// For preflight OPTIONS requests, return CORS headers immediately
if (request.method === 'OPTIONS') {
return new NextResponse(null, {
status: 204,
headers: {
'Access-Control-Allow-Origin': allowedOrigin ?? '',
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Max-Age': '86400',
},
})
}
// For actual requests: block cross-origin calls
// (origin is null for same-origin navigations and server-to-server calls)
if (origin && allowedOrigin && origin !== allowedOrigin) {
return NextResponse.json(
{ error: 'Cross-origin requests are not allowed' },
{ status: 403 }
)
}
const response = NextResponse.next()
// Set CORS headers on the response
if (allowedOrigin) {
response.headers.set('Access-Control-Allow-Origin', allowedOrigin)
}
response.headers.set('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
response.headers.set('Access-Control-Allow-Headers', 'Content-Type')
return response
}
// Only run middleware on API routes
export const config = {
matcher: '/api/:path*',
}