mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2024-11-29 23:14:52 -05:00
Safely parse numbers from environment vars
This adds the function parseOptionalInt to help parse numbers from environment variables Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
parent
36e721d4a9
commit
52f6310e91
5 changed files with 23 additions and 8 deletions
|
@ -7,7 +7,7 @@
|
||||||
import { registerAs } from '@nestjs/config';
|
import { registerAs } from '@nestjs/config';
|
||||||
import * as Joi from 'joi';
|
import * as Joi from 'joi';
|
||||||
import { Loglevel } from './loglevel.enum';
|
import { Loglevel } from './loglevel.enum';
|
||||||
import { buildErrorMessage, toArrayConfig } from './utils';
|
import { buildErrorMessage, parseOptionalInt, toArrayConfig } from './utils';
|
||||||
|
|
||||||
export interface AppConfig {
|
export interface AppConfig {
|
||||||
domain: string;
|
domain: string;
|
||||||
|
@ -46,11 +46,10 @@ export default registerAs('appConfig', () => {
|
||||||
{
|
{
|
||||||
domain: process.env.HD_DOMAIN,
|
domain: process.env.HD_DOMAIN,
|
||||||
rendererOrigin: process.env.HD_RENDERER_ORIGIN,
|
rendererOrigin: process.env.HD_RENDERER_ORIGIN,
|
||||||
port: parseInt(process.env.PORT) || undefined,
|
port: parseOptionalInt(process.env.PORT),
|
||||||
loglevel: process.env.HD_LOGLEVEL,
|
loglevel: process.env.HD_LOGLEVEL,
|
||||||
forbiddenNoteIds: toArrayConfig(process.env.HD_FORBIDDEN_NOTE_IDS, ','),
|
forbiddenNoteIds: toArrayConfig(process.env.HD_FORBIDDEN_NOTE_IDS, ','),
|
||||||
maxDocumentLength:
|
maxDocumentLength: parseOptionalInt(process.env.HD_MAX_DOCUMENT_LENGTH),
|
||||||
parseInt(process.env.HD_MAX_DOCUMENT_LENGTH) || undefined,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
abortEarly: false,
|
abortEarly: false,
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
import * as Joi from 'joi';
|
import * as Joi from 'joi';
|
||||||
import { DatabaseDialect } from './database-dialect.enum';
|
import { DatabaseDialect } from './database-dialect.enum';
|
||||||
import { registerAs } from '@nestjs/config';
|
import { registerAs } from '@nestjs/config';
|
||||||
import { buildErrorMessage } from './utils';
|
import { buildErrorMessage, parseOptionalInt } from './utils';
|
||||||
|
|
||||||
export interface DatabaseConfig {
|
export interface DatabaseConfig {
|
||||||
username: string;
|
username: string;
|
||||||
|
@ -62,7 +62,7 @@ export default registerAs('databaseConfig', () => {
|
||||||
password: process.env.HD_DATABASE_PASS,
|
password: process.env.HD_DATABASE_PASS,
|
||||||
database: process.env.HD_DATABASE_NAME,
|
database: process.env.HD_DATABASE_NAME,
|
||||||
host: process.env.HD_DATABASE_HOST,
|
host: process.env.HD_DATABASE_HOST,
|
||||||
port: parseInt(process.env.HD_DATABASE_PORT) || undefined,
|
port: parseOptionalInt(process.env.HD_DATABASE_PORT),
|
||||||
storage: process.env.HD_DATABASE_STORAGE,
|
storage: process.env.HD_DATABASE_STORAGE,
|
||||||
dialect: process.env.HD_DATABASE_DIALECT,
|
dialect: process.env.HD_DATABASE_DIALECT,
|
||||||
},
|
},
|
||||||
|
|
|
@ -6,7 +6,7 @@
|
||||||
|
|
||||||
import * as Joi from 'joi';
|
import * as Joi from 'joi';
|
||||||
import { registerAs } from '@nestjs/config';
|
import { registerAs } from '@nestjs/config';
|
||||||
import { buildErrorMessage } from './utils';
|
import { buildErrorMessage, parseOptionalInt } from './utils';
|
||||||
|
|
||||||
export interface HstsConfig {
|
export interface HstsConfig {
|
||||||
enable: boolean;
|
enable: boolean;
|
||||||
|
@ -32,7 +32,7 @@ export default registerAs('hstsConfig', () => {
|
||||||
const hstsConfig = hstsSchema.validate(
|
const hstsConfig = hstsSchema.validate(
|
||||||
{
|
{
|
||||||
enable: process.env.HD_HSTS_ENABLE,
|
enable: process.env.HD_HSTS_ENABLE,
|
||||||
maxAgeSeconds: parseInt(process.env.HD_HSTS_MAX_AGE) || undefined,
|
maxAgeSeconds: parseOptionalInt(process.env.HD_HSTS_MAX_AGE),
|
||||||
includeSubdomains: process.env.HD_HSTS_INCLUDE_SUBDOMAINS,
|
includeSubdomains: process.env.HD_HSTS_INCLUDE_SUBDOMAINS,
|
||||||
preload: process.env.HD_HSTS_PRELOAD,
|
preload: process.env.HD_HSTS_PRELOAD,
|
||||||
},
|
},
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
|
|
||||||
import {
|
import {
|
||||||
needToLog,
|
needToLog,
|
||||||
|
parseOptionalInt,
|
||||||
replaceAuthErrorsWithEnvironmentVariables,
|
replaceAuthErrorsWithEnvironmentVariables,
|
||||||
toArrayConfig,
|
toArrayConfig,
|
||||||
} from './utils';
|
} from './utils';
|
||||||
|
@ -84,4 +85,12 @@ describe('config utils', () => {
|
||||||
expect(needToLog(currentLevel, Loglevel.TRACE)).toBeTruthy();
|
expect(needToLog(currentLevel, Loglevel.TRACE)).toBeTruthy();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
describe('parseOptionalInt', () => {
|
||||||
|
it('returns undefined on undefined parameter', () => {
|
||||||
|
expect(parseOptionalInt(undefined)).toEqual(undefined);
|
||||||
|
});
|
||||||
|
it('correctly parses a string', () => {
|
||||||
|
expect(parseOptionalInt('42')).toEqual(42);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -113,3 +113,10 @@ function transformLoglevelToInt(loglevel: Loglevel): number {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function parseOptionalInt(value?: string): number | undefined {
|
||||||
|
if (value === undefined) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
return parseInt(value);
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue