1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-11-01 19:07:38 +01:00
unleash.unleash/src/lib/middleware/secure-headers.ts

61 lines
2.1 KiB
TypeScript
Raw Normal View History

import helmet from 'helmet';
import { RequestHandler } from 'express';
import { IUnleashConfig } from '../types';
import { hoursToSeconds } from 'date-fns';
const secureHeaders: (config: IUnleashConfig) => RequestHandler = (config) => {
2020-10-01 21:47:40 +02:00
if (config.secureHeaders) {
return helmet({
2020-09-18 11:30:30 +02:00
hsts: {
maxAge: hoursToSeconds(24 * 365 * 2), // 2 non-leap years
2020-09-18 11:30:30 +02:00
includeSubDomains: true,
preload: true,
},
contentSecurityPolicy: {
directives: {
defaultSrc: [
"'self'",
'cdn.getunleash.io',
'gravatar.com',
...config.additionalCspAllowedDomains.defaultSrc,
],
2020-10-01 21:47:40 +02:00
fontSrc: [
"'self'",
'cdn.getunleash.io',
'fonts.googleapis.com',
'fonts.gstatic.com',
...config.additionalCspAllowedDomains.fontSrc,
],
styleSrc: [
"'self'",
2020-09-07 09:51:30 +02:00
"'unsafe-inline'",
'cdn.getunleash.io',
'fonts.googleapis.com',
'fonts.gstatic.com',
'data:',
...config.additionalCspAllowedDomains.styleSrc,
],
scriptSrc: [
"'self'",
'cdn.getunleash.io',
...config.additionalCspAllowedDomains.scriptSrc,
],
imgSrc: [
"'self'",
'data:',
'cdn.getunleash.io',
'gravatar.com',
...config.additionalCspAllowedDomains.imgSrc,
],
},
},
crossOriginEmbedderPolicy: false,
});
}
return (req, res, next) => {
next();
};
};
export default secureHeaders;