-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
72 lines (61 loc) · 2.32 KB
/
proxy.ts
File metadata and controls
72 lines (61 loc) · 2.32 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
import type { NextRequest } from 'next/server';
import { NextResponse } from 'next/server';
/**
* Security proxy that adds Content Security Policy and other security headers
* to protect against XSS, clickjacking, and other common web vulnerabilities.
*
* Note: Monaco Editor requires 'unsafe-eval' and 'unsafe-inline' to function.
* This is a known limitation of the editor's architecture.
*/
export function proxy(request: NextRequest) {
const response = NextResponse.next();
// Content Security Policy
// Monaco Editor requires unsafe-eval and unsafe-inline for its TypeScript/JavaScript features
const cspDirectives = [
"default-src 'self'",
"script-src 'self' 'unsafe-inline' 'unsafe-eval'", // unsafe-eval needed for Monaco
"style-src 'self' 'unsafe-inline'", // unsafe-inline needed for Tailwind and Monaco
"img-src 'self' data: https:",
"font-src 'self' data:",
"connect-src 'self'",
"frame-ancestors 'none'", // Prevent clickjacking
"base-uri 'self'",
"form-action 'self'",
];
response.headers.set('Content-Security-Policy', cspDirectives.join('; '));
// Prevent clickjacking
response.headers.set('X-Frame-Options', 'DENY');
// Prevent MIME type sniffing
response.headers.set('X-Content-Type-Options', 'nosniff');
// Control referrer information
response.headers.set('Referrer-Policy', 'strict-origin-when-cross-origin');
// Disable unnecessary browser features
response.headers.set(
'Permissions-Policy',
'geolocation=(), microphone=(), camera=(), payment=(), usb=()'
);
// XSS Protection (legacy but still useful for older browsers)
response.headers.set('X-XSS-Protection', '1; mode=block');
// Strict Transport Security (HSTS) - enforce HTTPS
// Only enable in production
if (process.env.NODE_ENV === 'production') {
response.headers.set(
'Strict-Transport-Security',
'max-age=31536000; includeSubDomains; preload'
);
}
return response;
}
// Apply proxy to all routes
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
* - public assets (images, etc.)
*/
'/((?!_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)',
],
};