-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathmiddleware.ts
More file actions
67 lines (57 loc) · 1.84 KB
/
Copy pathmiddleware.ts
File metadata and controls
67 lines (57 loc) · 1.84 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
import { NextRequest, NextResponse } from 'next/server';
export function middleware(request: NextRequest) {
if (request.nextUrl.pathname.endsWith('.md')) {
return new NextResponse(null, { status: 404 });
}
const nonce = Buffer.from(crypto.randomUUID()).toString('base64');
const isDev = process.env.NODE_ENV === 'development';
const cspDirectives = {
'default-src': ["'self'"],
'script-src': [
"'self'",
`'nonce-${nonce}'`,
"'strict-dynamic'",
isDev ? "'unsafe-eval'" : "",
].filter(Boolean),
'style-src': ["'self'", `'nonce-${nonce}'`, "'unsafe-inline'"],
'img-src': ["'self'", "blob:", "data:", "https://*"],
'font-src': ["'self'"],
'manifest-src': ["'self'"],
'object-src': ["'none'"],
'base-uri': ["'self'"],
'form-action': ["'self'"],
'frame-ancestors': ["'none'"],
'connect-src': [
"'self'",
"https://*.stellar.org",
"https://*.soroban-rpc.com",
"https://*.vercel-analytics.com",
isDev ? "ws://localhost:*" : ""
].filter(Boolean),
'upgrade-insecure-requests': [],
};
const cspString = Object.entries(cspDirectives)
.map(([key, values]) => {
if (values.length === 0) return key;
return `${key} ${values.join(' ')}`;
})
.join('; ');
const requestHeaders = new Headers(request.headers);
requestHeaders.set('x-nonce', nonce);
const response = NextResponse.next({
request: { headers: requestHeaders },
});
response.headers.set('Content-Security-Policy', cspString);
return response;
}
export const config = {
matcher: [
{
source: '/((?!api|_next/static|_next/image|.*\\.(?:ico|png|jpg|jpeg|svg|webp|gif|woff2?|ttf|otf|map)).*)',
missing: [
{ type: 'header', key: 'next-router-prefetch' },
{ type: 'header', key: 'purpose', value: 'prefetch' },
],
},
],
};