Skip to content

Commit f997569

Browse files
committed
CCM-9916: preload fonts with cross origin directive
1 parent 44a4b09 commit f997569

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

frontend/src/__tests__/middleware.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ describe('middleware function', () => {
165165
]);
166166
});
167167

168-
it('when running in development mode, CSP script-src allows unsafe-eval', async () => {
168+
it('when running in development mode, CSP script-src allows unsafe-eval and does not upgrade insecure requests', async () => {
169169
// @ts-expect-error assignment to const
170170
process.env.NODE_ENV = 'development';
171171

@@ -189,8 +189,6 @@ describe('middleware function', () => {
189189
/^script-src 'self' 'nonce-[\dA-Za-z]+' 'unsafe-eval'$/
190190
),
191191
expect.stringMatching(/^style-src 'self' 'nonce-[\dA-Za-z]+'$/),
192-
'upgrade-insecure-requests',
193-
'',
194192
]);
195193
});
196194
});

frontend/src/app/layout.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,22 @@ export default function RootLayout({
5757
return (
5858
<html lang='en'>
5959
<head>
60+
<link
61+
rel='preload'
62+
as='font'
63+
href='https://assets.nhs.uk/fonts/FrutigerLTW01-55Roman.woff2'
64+
type='font/woff2'
65+
crossOrigin='anonymous'
66+
/>
67+
68+
<link
69+
rel='preload'
70+
as='font'
71+
href='https://assets.nhs.uk/fonts/FrutigerLTW01-65Bold.woff2'
72+
type='font/woff2'
73+
crossOrigin='anonymous'
74+
/>
75+
6076
<script
6177
src={`${getBasePath()}/lib/nhsuk-frontend-10.0.0.min.js`}
6278
defer

frontend/src/middleware.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ const publicPaths = [
5151
];
5252

5353
function getContentSecurityPolicy(nonce: string) {
54-
const contentSecurityPolicyDirective = {
54+
const contentSecurityPolicyDirective: Record<string, string[]> = {
5555
'base-uri': [`'self'`],
5656
'default-src': [`'none'`],
5757
'frame-ancestors': [`'none'`],
@@ -64,16 +64,18 @@ function getContentSecurityPolicy(nonce: string) {
6464
'object-src': [`'none'`],
6565
'script-src': [`'self'`, `'nonce-${nonce}'`],
6666
'style-src': [`'self'`, `'nonce-${nonce}'`],
67-
'upgrade-insecure-requests;': [],
6867
};
6968

7069
if (process.env.NODE_ENV === 'development') {
7170
contentSecurityPolicyDirective['script-src'].push(`'unsafe-eval'`);
71+
} else {
72+
contentSecurityPolicyDirective['upgrade-insecure-requests'] = [];
7273
}
7374

7475
return Object.entries(contentSecurityPolicyDirective)
7576
.map(([key, value]) => `${key} ${value.join(' ')}`)
76-
.join('; ');
77+
.join('; ')
78+
.concat(';');
7779
}
7880

7981
export async function middleware(request: NextRequest) {

0 commit comments

Comments
 (0)