forked from veridatum-labs/earnproof-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
39 lines (32 loc) · 950 Bytes
/
Copy pathmiddleware.ts
File metadata and controls
39 lines (32 loc) · 950 Bytes
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
import { NextRequest, NextResponse } from "next/server";
import { buildSecurityPolicy } from "@/config/security-headers";
function createNonce(): string {
const bytes = new Uint8Array(16);
crypto.getRandomValues(bytes);
let binary = "";
bytes.forEach((byte) => {
binary += String.fromCharCode(byte);
});
return btoa(binary);
}
export function middleware(request: NextRequest) {
const nonce = createNonce();
const policy = buildSecurityPolicy({ nonce });
const requestHeaders = new Headers(request.headers);
requestHeaders.set("x-nonce", nonce);
const response = NextResponse.next({
request: { headers: requestHeaders },
});
for (const header of policy.headers) {
response.headers.set(header.key, header.value);
}
return response;
}
export const config = {
matcher: [
{
source:
"/((?!_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp|ico)$).*)",
},
],
};