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