1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-30 10:36:35 +02:00

Server: Improved env variable validation and support true-false as boolean values

This commit is contained in:
Laurent Cozic 2022-01-11 15:01:37 +00:00
parent 09ed92bc26
commit fd322edaab
2 changed files with 68 additions and 12 deletions

View File

@ -0,0 +1,46 @@
import { afterAllTests, beforeAllDb, beforeEachDb, expectThrow } from './utils/testing/testUtils';
import { parseEnv } from './env';
describe('env', function() {
beforeAll(async () => {
await beforeAllDb('env');
});
afterAll(async () => {
await afterAllTests();
});
beforeEach(async () => {
await beforeEachDb();
});
it('should parse env values', async function() {
const result = parseEnv({
DB_CLIENT: 'pg',
POSTGRES_PORT: '123',
MAILER_ENABLED: 'true',
SIGNUP_ENABLED: 'false',
TERMS_ENABLED: '0',
ACCOUNT_TYPES_ENABLED: '1',
});
expect(result.DB_CLIENT).toBe('pg');
expect(result.POSTGRES_PORT).toBe(123);
expect(result.MAILER_ENABLED).toBe(true);
expect(result.SIGNUP_ENABLED).toBe(false);
expect(result.TERMS_ENABLED).toBe(false);
expect(result.ACCOUNT_TYPES_ENABLED).toBe(true);
});
it('should overrides default values', async function() {
expect(parseEnv({}).POSTGRES_USER).toBe('joplin');
expect(parseEnv({}, { POSTGRES_USER: 'other' }).POSTGRES_USER).toBe('other');
});
it('should validate values', async function() {
await expectThrow(async () => parseEnv({ POSTGRES_PORT: 'notanumber' }));
await expectThrow(async () => parseEnv({ MAILER_ENABLED: 'TRUE' }));
});
});

View File

@ -140,7 +140,13 @@ export interface EnvVariables {
STRIPE_WEBHOOK_SECRET: string;
}
export function parseEnv(rawEnv: any, defaultOverrides: any = null): EnvVariables {
const parseBoolean = (s: string): boolean => {
if (s === 'true' || s === '1') return true;
if (s === 'false' || s === '0') return false;
throw new Error(`Invalid boolean value: "${s}" (Must be one of "true", "false", "0, "1")`);
};
export function parseEnv(rawEnv: Record<string, string>, defaultOverrides: any = null): EnvVariables {
const output: EnvVariables = {
...defaultEnvValues,
...defaultOverrides,
@ -151,17 +157,21 @@ export function parseEnv(rawEnv: any, defaultOverrides: any = null): EnvVariable
if (rawEnvValue === undefined) continue;
if (typeof value === 'number') {
const v = Number(rawEnvValue);
if (isNaN(v)) throw new Error(`Invalid number value for env variable ${key} = ${rawEnvValue}`);
(output as any)[key] = v;
} else if (typeof value === 'boolean') {
if (rawEnvValue !== '0' && rawEnvValue !== '1') throw new Error(`Invalid boolean value for env variable ${key}: ${rawEnvValue} (Should be either "0" or "1")`);
(output as any)[key] = rawEnvValue === '1';
} else if (typeof value === 'string') {
(output as any)[key] = `${rawEnvValue}`;
} else {
throw new Error(`Invalid env default value type: ${typeof value}`);
try {
if (typeof value === 'number') {
const v = Number(rawEnvValue);
if (isNaN(v)) throw new Error(`Invalid number value "${rawEnvValue}"`);
(output as any)[key] = v;
} else if (typeof value === 'boolean') {
(output as any)[key] = parseBoolean(rawEnvValue);
} else if (typeof value === 'string') {
(output as any)[key] = `${rawEnvValue}`;
} else {
throw new Error(`Invalid env default value type: ${typeof value}`);
}
} catch (error) {
error.message = `Could not parse key "${key}": ${error.message}`;
throw error;
}
}