Skip to content

Commit

Permalink
fix: mask secrets when characters get uri encoded
Browse files Browse the repository at this point in the history
  • Loading branch information
travi committed Nov 16, 2020
1 parent 63fa143 commit ca90b34
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/hide-sensitive.js
Expand Up @@ -11,7 +11,12 @@ module.exports = (env) => {
return /token|password|credential|secret|private/i.test(envVar) && size(env[envVar].trim()) >= SECRET_MIN_SIZE;
});

const regexp = new RegExp(toReplace.map((envVar) => escapeRegExp(env[envVar])).join('|'), 'g');
const regexp = new RegExp(
toReplace
.map((envVar) => `${escapeRegExp(env[envVar])}|${encodeURI(escapeRegExp(env[envVar]))}`)
.join('|'),
'g'
);
return (output) =>
output && isString(output) && toReplace.length > 0 ? output.toString().replace(regexp, SECRET_REPLACEMENT) : output;
};
8 changes: 8 additions & 0 deletions test/hide-sensitive.test.js
Expand Up @@ -24,6 +24,14 @@ test('Replace sensitive environment variable matching specific regex for "privat
t.is(hideSensitive(env)(`https://host.com?token=${env.privateKey}`), `https://host.com?token=${SECRET_REPLACEMENT}`);
});

test('Replace url-encoded environment variable', (t) => {
const env = {privateKey: 'secret '};
t.is(
hideSensitive(env)(`https://host.com?token=${encodeURI(env.privateKey)}`),
`https://host.com?token=${SECRET_REPLACEMENT}`
);
});

test('Escape regexp special characters', (t) => {
const env = {SOME_CREDENTIALS: 'p$^{.+}\\w[a-z]o.*rd'};
t.is(
Expand Down

0 comments on commit ca90b34

Please sign in to comment.