-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
73 lines (60 loc) · 2.29 KB
/
proxy.ts
File metadata and controls
73 lines (60 loc) · 2.29 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
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
export function proxy(request: NextRequest) {
const response = NextResponse.next();
// Security Headers
// Content Security Policy (CSP)
// Relaxed policy - balancing security with third-party SDK requirements
const cspHeader = [
"default-src 'self'",
"script-src 'self' 'unsafe-eval' 'unsafe-inline' https:",
"style-src 'self' 'unsafe-inline' https:",
"font-src 'self' data: https:",
"img-src 'self' data: https:",
"connect-src 'self' http://localhost:8545 http://127.0.0.1:8545 https: wss:",
"frame-src 'self' https:",
"object-src 'none'",
"base-uri 'self'",
"form-action 'self'",
].join("; ");
response.headers.set("Content-Security-Policy", cspHeader);
// Prevent clickjacking attacks
response.headers.set("X-Frame-Options", "DENY");
// Prevent MIME type sniffing
response.headers.set("X-Content-Type-Options", "nosniff");
// Enable browser XSS protection
response.headers.set("X-XSS-Protection", "1; mode=block");
// Referrer Policy - limit information sent to other sites
response.headers.set("Referrer-Policy", "strict-origin-when-cross-origin");
// Permissions Policy - restrict browser features
response.headers.set(
"Permissions-Policy",
"camera=(), microphone=(), geolocation=()"
);
// Strict Transport Security - force HTTPS
// Only enable in production with HTTPS
if (process.env.NODE_ENV === "production") {
response.headers.set(
"Strict-Transport-Security",
"max-age=63072000; includeSubDomains; preload"
);
}
// Cross-Origin policies
// Note: COEP require-corp is too strict for third-party resources
response.headers.set("Cross-Origin-Opener-Policy", "same-origin-allow-popups");
response.headers.set("Cross-Origin-Resource-Policy", "cross-origin");
return response;
}
// Apply middleware to all routes except static files and images
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 folder files
*/
"/((?!_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)",
],
};