Skip to content

Commit adb069b

Browse files
committed
Added content security policy.
1 parent 2b7da33 commit adb069b

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

middleware.js

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,6 @@ function customScriptName(req) {
3333
}
3434
}
3535

36-
function forceSSL(req, res) {
37-
if (process.env.FORCE_SSL && req.nextUrl.protocol === 'http:') {
38-
res.headers.set('Strict-Transport-Security', 'max-age=31536000; includeSubDomains');
39-
}
40-
41-
return res;
42-
}
43-
4436
export default function middleware(req) {
4537
const fns = [customCollectEndpoint, customScriptName];
4638

@@ -51,5 +43,5 @@ export default function middleware(req) {
5143
}
5244
}
5345

54-
return forceSSL(req, NextResponse.next());
46+
return NextResponse.next();
5547
}

next.config.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,37 @@
11
require('dotenv').config();
22
const pkg = require('./package.json');
33

4+
const contentSecurityPolicy = `
5+
default-src 'self';
6+
img-src *;
7+
script-src 'self' 'unsafe-eval';
8+
style-src 'self' 'unsafe-inline';
9+
connect-src 'self' api.umami.is;
10+
frame-ancestors 'self';
11+
`;
12+
13+
const headers = [
14+
{
15+
key: 'X-DNS-Prefetch-Control',
16+
value: 'on',
17+
},
18+
{
19+
key: 'X-Frame-Options',
20+
value: 'SAMEORIGIN',
21+
},
22+
{
23+
key: 'Content-Security-Policy',
24+
value: contentSecurityPolicy.replace(/\s{2,}/g, ' ').trim(),
25+
},
26+
];
27+
28+
if (process.env.FORCE_SSL) {
29+
headers.push({
30+
key: 'Strict-Transport-Security',
31+
value: 'max-age=63072000; includeSubDomains; preload',
32+
});
33+
}
34+
435
module.exports = {
536
env: {
637
currentVersion: pkg.version,
@@ -24,6 +55,10 @@ module.exports = {
2455
},
2556
async headers() {
2657
return [
58+
{
59+
source: '/:path*',
60+
headers,
61+
},
2762
{
2863
source: `/(.*\\.js)`,
2964
headers: [

0 commit comments

Comments
 (0)