Skip to content

Commit 20246c0

Browse files
committed
fix: exclude empty env var value from replacement
1 parent 857d418 commit 20246c0

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

lib/hide-sensitive.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
const {escapeRegExp} = require('lodash');
22

3-
const toReplace = Object.keys(process.env).filter(envVar => /token|password|credential|secret|private/i.test(envVar));
3+
const toReplace = Object.keys(process.env).filter(
4+
envVar => /token|password|credential|secret|private/i.test(envVar) && process.env[envVar].trim()
5+
);
6+
47
const regexp = new RegExp(toReplace.map(envVar => escapeRegExp(process.env[envVar])).join('|'), 'g');
58

69
module.exports = output => {

test/hide-sensitive.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,18 @@ test.serial('Accept "undefined" input', t => {
4040
test.serial('Return same string if no environment variable has to be replaced', t => {
4141
t.is(require('../lib/hide-sensitive')('test'), 'test');
4242
});
43+
44+
test.serial('Exclude empty environment variables from the regexp', t => {
45+
process.env.SOME_PASSWORD = 'password';
46+
process.env.SOME_TOKEN = '';
47+
t.is(
48+
require('../lib/hide-sensitive')(`https://user:${process.env.SOME_PASSWORD}@host.com?token=`),
49+
'https://user:[secure]@host.com?token='
50+
);
51+
});
52+
53+
test.serial('Exclude empty environment variables from the regexp if there is only empty ones', t => {
54+
process.env.SOME_PASSWORD = '';
55+
process.env.SOME_TOKEN = ' \n ';
56+
t.is(require('../lib/hide-sensitive')(`https://host.com?token=`), 'https://host.com?token=');
57+
});

0 commit comments

Comments
 (0)