2021-01-15 10:57:04 -05:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
import * as Joi from 'joi';
|
|
|
|
import { registerAs } from '@nestjs/config';
|
2021-01-19 09:47:05 -05:00
|
|
|
import { buildErrorMessage } from './utils';
|
2021-01-15 10:57:04 -05:00
|
|
|
|
|
|
|
export interface HstsConfig {
|
|
|
|
enable: boolean;
|
|
|
|
maxAgeSeconds: number;
|
|
|
|
includeSubdomains: boolean;
|
|
|
|
preload: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
const hstsSchema = Joi.object({
|
2021-01-19 09:47:05 -05:00
|
|
|
enable: Joi.boolean().default(true).optional().label('HD_HSTS_ENABLE'),
|
2021-01-15 10:57:04 -05:00
|
|
|
maxAgeSeconds: Joi.number()
|
|
|
|
.default(60 * 60 * 24 * 365)
|
2021-01-19 09:47:05 -05:00
|
|
|
.optional()
|
|
|
|
.label('HD_HSTS_MAX_AGE'),
|
|
|
|
includeSubdomains: Joi.boolean()
|
|
|
|
.default(true)
|
|
|
|
.optional()
|
|
|
|
.label('HD_HSTS_INCLUDE_SUBDOMAINS'),
|
|
|
|
preload: Joi.boolean().default(true).optional().label('HD_HSTS_PRELOAD'),
|
2021-01-15 10:57:04 -05:00
|
|
|
});
|
|
|
|
|
2021-02-24 14:29:39 -05:00
|
|
|
export default registerAs('hstsConfig', () => {
|
2021-01-15 10:57:04 -05:00
|
|
|
const hstsConfig = hstsSchema.validate(
|
|
|
|
{
|
|
|
|
enable: process.env.HD_HSTS_ENABLE,
|
|
|
|
maxAgeSeconds: parseInt(process.env.HD_HSTS_MAX_AGE) || undefined,
|
|
|
|
includeSubdomains: process.env.HD_HSTS_INCLUDE_SUBDOMAINS,
|
|
|
|
preload: process.env.HD_HSTS_PRELOAD,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
abortEarly: false,
|
|
|
|
presence: 'required',
|
|
|
|
},
|
|
|
|
);
|
|
|
|
if (hstsConfig.error) {
|
2021-02-24 14:29:39 -05:00
|
|
|
const errorMessages = hstsConfig.error.details.map(
|
2021-01-19 09:47:05 -05:00
|
|
|
(detail) => detail.message,
|
|
|
|
);
|
|
|
|
throw new Error(buildErrorMessage(errorMessages));
|
2021-01-15 10:57:04 -05:00
|
|
|
}
|
2021-02-24 14:29:39 -05:00
|
|
|
return hstsConfig.value as HstsConfig;
|
2021-01-15 10:57:04 -05:00
|
|
|
});
|