generated from NHSDigital/nhs-notify-repository-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmiddleware.ts
More file actions
132 lines (111 loc) · 3.71 KB
/
middleware.ts
File metadata and controls
132 lines (111 loc) · 3.71 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { NextResponse, type NextRequest } from 'next/server';
import { getAccessTokenServer } from '@utils/amplify-utils';
import { getBasePath } from '@utils/get-base-path';
const protectedPaths = [
/^\/choose-a-template-type$/,
/^\/copy-template\/[^/]+$/,
/^\/create-email-template$/,
/^\/create-nhs-app-template$/,
/^\/create-text-message-template$/,
/^\/create-letter-template$/,
/^\/delete-template\/[^/]+$/,
/^\/edit-email-template\/[^/]+$/,
/^\/edit-nhs-app-template\/[^/]+$/,
/^\/edit-text-message-template\/[^/]+$/,
/^\/email-template-submitted\/[^/]+$/,
/^\/invalid-template$/,
/^\/manage-templates$/,
/^\/nhs-app-template-submitted\/[^/]+$/,
/^\/preview-email-template\/[^/]+$/,
/^\/preview-letter-template\/[^/]+$/,
/^\/preview-nhs-app-template\/[^/]+$/,
/^\/preview-text-message-template\/[^/]+$/,
/^\/submit-email-template\/[^/]+$/,
/^\/submit-nhs-app-template\/[^/]+$/,
/^\/submit-text-message-template\/[^/]+$/,
/^\/text-message-template-submitted\/[^/]+$/,
/^\/view-submitted-email-template\/[^/]+$/,
/^\/view-submitted-nhs-app-template\/[^/]+$/,
/^\/view-submitted-text-message-template\/[^/]+$/,
];
const publicPaths = [
/^\/create-and-submit-templates$/,
/^\/auth$/,
/^\/auth\/signin$/,
/^\/auth\/signout$/,
/^\/auth\/idle$/,
];
function getContentSecurityPolicy(nonce: string) {
const contentSecurityPolicyDirective = {
'base-uri': [`'self'`],
'default-src': [`'none'`],
'frame-ancestors': [`'none'`],
'font-src': [`'self'`, 'https://assets.nhs.uk'],
'form-action': [`'self'`],
'frame-src': [`'self'`],
'connect-src': [`'self'`, 'https://cognito-idp.eu-west-2.amazonaws.com'],
'img-src': [`'self'`],
'manifest-src': [`'self'`],
'object-src': [`'none'`],
'script-src': [`'self'`, `'nonce-${nonce}'`],
'style-src': [`'self'`, `'nonce-${nonce}'`],
'upgrade-insecure-requests;': [],
};
if (process.env.NODE_ENV === 'development') {
contentSecurityPolicyDirective['script-src'].push(`'unsafe-eval'`);
}
return Object.entries(contentSecurityPolicyDirective)
.map(([key, value]) => `${key} ${value.join(' ')}`)
.join('; ');
}
export async function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
const nonce = Buffer.from(crypto.randomUUID()).toString('base64');
const csp = getContentSecurityPolicy(nonce);
const requestHeaders = new Headers(request.headers);
requestHeaders.set('Content-Security-Policy', csp);
if (publicPaths.some((p) => p.test(pathname))) {
const publicPathResponse = NextResponse.next({
request: {
headers: requestHeaders,
},
});
publicPathResponse.headers.set('Content-Security-Policy', csp);
return publicPathResponse;
}
if (!protectedPaths.some((p) => p.test(pathname))) {
return new NextResponse('Page not found', { status: 404 });
}
const token = await getAccessTokenServer();
if (!token) {
const redirectResponse = NextResponse.redirect(
new URL(
`/auth?redirect=${encodeURIComponent(
`${getBasePath()}${request.nextUrl.pathname}`
)}`,
request.url
)
);
redirectResponse.headers.set('Content-Type', 'text/html');
return redirectResponse;
}
const response = NextResponse.next({
request: {
headers: requestHeaders,
},
});
response.headers.set('Content-Security-Policy', csp);
return response;
}
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)
* - lib/ (our static content)
*/
'/((?!_next/static|_next/image|favicon.ico|lib/).*)',
],
};