forked from SmartDropLabs/smartdrop-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
73 lines (60 loc) · 1.94 KB
/
Copy pathmiddleware.ts
File metadata and controls
73 lines (60 loc) · 1.94 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 createMiddleware from 'next-intl/middleware';
import { NextRequest, NextResponse } from 'next/server';
import { routing } from './i18n/routing';
const intlMiddleware = createMiddleware(routing);
function generateNonce(): string {
const bytes = new Uint8Array(16);
crypto.getRandomValues(bytes);
return btoa(String.fromCharCode(...bytes));
}
const backendApiOrigin = (() => {
try {
return new URL(
process.env.NEXT_PUBLIC_BACKEND_API_URL ?? "http://localhost:4000/api/v1"
).origin;
} catch {
return "http://localhost:4000";
}
})();
function buildCsp(nonce: string): string {
return [
"default-src 'self'",
`script-src 'self' 'nonce-${nonce}' 'unsafe-eval'`,
"style-src 'self' 'unsafe-inline'",
`connect-src 'self' https://horizon.stellar.org https://horizon-testnet.stellar.org https://soroban-testnet.stellar.org https://soroban.stellar.org https://stellar.expert ${backendApiOrigin}`,
"img-src 'self' data: https:",
"font-src 'self'",
"frame-src 'none'",
"object-src 'none'",
"base-uri 'self'",
"form-action 'self'",
].join('; ');
}
export function middleware(request: NextRequest) {
const nonce = generateNonce();
const cspHeader = buildCsp(nonce);
const intlResponse = intlMiddleware(request);
if (intlResponse.headers.has('location')) {
intlResponse.headers.set('Content-Security-Policy', cspHeader);
return intlResponse;
}
const requestHeaders = new Headers(request.headers);
requestHeaders.set('x-nonce', nonce);
const response = NextResponse.next({
request: { headers: requestHeaders },
});
for (const [key, value] of intlResponse.headers.entries()) {
if (key.toLowerCase() !== 'content-security-policy') {
response.headers.set(key, value);
}
}
response.headers.set('Content-Security-Policy', cspHeader);
return response;
}
export const config = {
matcher: [
'/',
'/(en|es)/:path*',
'/((?!_next|_vercel|.*\\..*).*)',
]
};