Skip to content

Commit 5825be4

Browse files
committed
Harden environment lookup
1 parent 8d45593 commit 5825be4

3 files changed

Lines changed: 18 additions & 9 deletions

File tree

__tests__/utils.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ describe('Utils tests', () => {
1111
expect(await utils.readEnv('TEST')).toBe('setup-php');
1212
expect(await utils.readEnv('test_hyphen')).toBe('setup-php');
1313
expect(await utils.readEnv('TEST_HYPHEN')).toBe('setup-php');
14+
expect(await utils.readEnv('test invalid')).toBe('');
15+
process.env['conflict_hyphen'] = 'setup-php';
16+
process.env['conflict-hyphen'] = 'wrong';
17+
expect(await utils.readEnv('conflict_hyphen')).toBe('setup-php');
18+
delete process.env['conflict_hyphen'];
19+
delete process.env['conflict-hyphen'];
1420
expect(await utils.readEnv('undefined')).toBe('');
1521
});
1622

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/utils.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,19 @@ import * as fetch from './fetch';
99
* @param property
1010
*/
1111
export async function readEnv(property: string): Promise<string> {
12+
if (!/^[A-Za-z0-9_-]+$/.test(property)) {
13+
return '';
14+
}
1215
const property_lc: string = property.toLowerCase();
1316
const property_uc: string = property.toUpperCase();
14-
return (
15-
process.env[property] ||
16-
process.env[property_lc] ||
17-
process.env[property_uc] ||
18-
process.env[property_lc.replace('_', '-')] ||
19-
process.env[property_uc.replace('_', '-')] ||
20-
''
21-
);
17+
const candidates = [
18+
property,
19+
property_lc,
20+
property_uc,
21+
property_lc.replace('_', '-'),
22+
property_uc.replace('_', '-')
23+
].filter((value, index, array) => array.indexOf(value) === index);
24+
return candidates.map(name => process.env[name] || '').find(Boolean) || '';
2225
}
2326

2427
/**

0 commit comments

Comments
 (0)