From 4bb4f1812c099fe4c9941b3a0b9e9d854947371b Mon Sep 17 00:00:00 2001 From: Shivam Mathur Date: Fri, 23 Jul 2021 16:19:00 +0530 Subject: [PATCH] Fix to make utils.readEnv more resilient to user input --- __tests__/utils.test.ts | 4 ++++ dist/index.js | 15 ++++++++------- src/utils.ts | 17 ++++++++++------- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/__tests__/utils.test.ts b/__tests__/utils.test.ts index 75132cb9..47374be0 100644 --- a/__tests__/utils.test.ts +++ b/__tests__/utils.test.ts @@ -19,7 +19,11 @@ async function cleanup(path: string): Promise { describe('Utils tests', () => { it('checking readEnv', async () => { process.env['test'] = 'setup-php'; + process.env['test-hyphen'] = 'setup-php'; expect(await utils.readEnv('test')).toBe('setup-php'); + expect(await utils.readEnv('TEST')).toBe('setup-php'); + expect(await utils.readEnv('test_hyphen')).toBe('setup-php'); + expect(await utils.readEnv('TEST_HYPHEN')).toBe('setup-php'); expect(await utils.readEnv('undefined')).toBe(''); }); diff --git a/dist/index.js b/dist/index.js index 848ba92c..e9586dd6 100644 --- a/dist/index.js +++ b/dist/index.js @@ -877,13 +877,14 @@ const path = __importStar(__nccwpck_require__(622)); const url = __importStar(__nccwpck_require__(835)); const core = __importStar(__nccwpck_require__(186)); async function readEnv(property) { - const value = process.env[property]; - switch (value) { - case undefined: - return ''; - default: - return value; - } + const property_lc = property.toLowerCase(); + const property_uc = property.toUpperCase(); + return (process.env[property] || + process.env[property_lc] || + process.env[property_uc] || + process.env[property_lc.replace('_', '-')] || + process.env[property_uc.replace('_', '-')] || + ''); } exports.readEnv = readEnv; async function getInput(name, mandatory) { diff --git a/src/utils.ts b/src/utils.ts index b69012a7..2f47cb81 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -11,13 +11,16 @@ import * as core from '@actions/core'; * @param property */ export async function readEnv(property: string): Promise { - const value = process.env[property]; - switch (value) { - case undefined: - return ''; - default: - return value; - } + const property_lc: string = property.toLowerCase(); + const property_uc: string = property.toUpperCase(); + return ( + process.env[property] || + process.env[property_lc] || + process.env[property_uc] || + process.env[property_lc.replace('_', '-')] || + process.env[property_uc.replace('_', '-')] || + '' + ); } /**