Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

test(config): reject a commented variable in the secret guard wiring pin

Yudhi Armyndharis9d90204d8de8
diff --git a/src/config/boot-env-validation.spec.ts b/src/config/boot-env-validation.spec.ts
index 01f6cd5cb..ae605f94b 100644
--- a/src/config/boot-env-validation.spec.ts
+++ b/src/config/boot-env-validation.spec.ts
@@ -78,11 +78,64 @@ describe('main.ts hands the production secret guard every value it checks', () =
expect(passed).toContain('nodeEnv: process.env.NODE_ENV');
});
+ // A field is passed when its line is the field's variable, alone or in a `||` chain of variables. A mention in a
+ // comment, or in a condition or a constant, does not count: the guard would receive something else.
+ const withoutComments = (source: string): string =>
+ source.replace(/\/\/.*$|\/\*[\s\S]*?\*\//gm, '').replace(/[ \t]+$/gm, '');
+ const isWired = (call: string, field: string): boolean =>
+ new RegExp(
+ `^ {4}${field}: (?:process\\.env\\.\\w+ \\|\\| )*process\\.env\\.${variableOf(field)}(?: \\|\\| process\\.env\\.\\w+)*,$`,
+ 'm',
+ ).test(withoutComments(call));
+
it('passes every declared field, each read from its own environment variable', () => {
- const unwired = declared.filter(
- field => !new RegExp(`^ {4}${field}: .*process\\.env\\.${variableOf(field)}\\b`, 'm').test(passed),
- );
+ const unwired = declared.filter(field => !isWired(passed, field));
expect(unwired).toEqual([]);
});
+
+ describe('what counts as passing a field', () => {
+ const call = (line: string): string =>
+ `\n nodeEnv: process.env.NODE_ENV,\n${line}\n redisPassword: process.env.REDIS_PASSWORD,\n`;
+
+ it.each([
+ ['from its variable', ' metricsToken: process.env.METRICS_TOKEN,'],
+ ['from its variable with a comment after it', ' metricsToken: process.env.METRICS_TOKEN, // the scrape token'],
+ [
+ 'from its variable after a comment line',
+ ' // the scrape token\n metricsToken: process.env.METRICS_TOKEN,',
+ ],
+ [
+ 'from a legacy variable and then its own',
+ ' metricsToken: process.env.OLD_TOKEN || process.env.METRICS_TOKEN,',
+ ],
+ [
+ 'from its own variable and then a legacy one',
+ ' metricsToken: process.env.METRICS_TOKEN || process.env.OLD_TOKEN,',
+ ],
+ ])('accepts a field passed %s', (_name, line) => {
+ expect(isWired(call(line), 'metricsToken')).toBe(true);
+ });
+
+ it.each([
+ ['a constant with the variable in a line comment', ' metricsToken: undefined, // process.env.METRICS_TOKEN'],
+ [
+ 'a constant with the variable in a block comment',
+ ' metricsToken: undefined /* was process.env.METRICS_TOKEN */,',
+ ],
+ [
+ 'a condition that only mentions the variable',
+ ' metricsToken: ready ? process.env.METRICS_TOKEN : undefined,',
+ ],
+ [
+ 'a condition whose last branch is the variable',
+ ' metricsToken: ready ? undefined : process.env.METRICS_TOKEN,',
+ ],
+ ['another variable whose name starts with it', ' metricsToken: process.env.METRICS_TOKEN_FILE,'],
+ ['a variable of another field', ' metricsToken: process.env.REDIS_PASSWORD,'],
+ ['a call that no longer has the field', ' // metricsToken: process.env.METRICS_TOKEN,'],
+ ])('rejects a field passed as %s', (_name, line) => {
+ expect(isWired(call(line), 'metricsToken')).toBe(false);
+ });
+ });
});