Skip to content

Commit

Permalink
Add security headers. (XSS, CSP) (leerob#310)
Browse files Browse the repository at this point in the history
  • Loading branch information
leerob committed Apr 11, 2021
1 parent 6889988 commit ee5f0a1
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ module.exports = {
'pbs.twimg.com' // Twitter Profile Picture
]
},
async headers() {
return [
{
source: '/',
headers: securityHeaders
},
{
source: '/:path*',
headers: securityHeaders
}
];
},
webpack: (config, { dev, isServer }) => {
if (isServer) {
require('./scripts/generate-sitemap');
Expand All @@ -27,3 +39,53 @@ module.exports = {
return config;
}
};

// https://securityheaders.com
const ContentSecurityPolicy = `
default-src 'self';
script-src 'self' 'unsafe-eval' 'unsafe-inline' *.youtube.com *.twitter.com cdn.usefathom.com;
child-src *.youtube.com *.google.com *.twitter.com;
style-src 'self' 'unsafe-inline' *.googleapis.com;
img-src * blob: data:;
media-src 'none';
connect-src *;
font-src 'self';
`;

const securityHeaders = [
// https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP
{
key: 'Content-Security-Policy',
value: ContentSecurityPolicy.replace(/\n/g, '')
},
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy
{
key: 'Referrer-Policy',
value: 'origin-when-cross-origin'
},
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Frame-Options
{
key: 'X-Frame-Options',
value: 'DENY'
},
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-Content-Type-Options
{
key: 'X-Content-Type-Options',
value: 'nosniff'
},
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/X-DNS-Prefetch-Control
{
key: 'X-DNS-Prefetch-Control',
value: 'on'
},
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Strict-Transport-Security
{
key: 'Strict-Transport-Security',
value: 'max-age=31536000; includeSubDomains; preload'
},
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Feature-Policy
{
key: 'Permissions-Policy',
value: 'camera=(), microphone=(), geolocation=()'
}
];

0 comments on commit ee5f0a1

Please sign in to comment.