8000 fix: also hide sensitive info when loggin from `cli.js` · semantic-release/semantic-release@43d0646 · GitHub
[go: up one dir, main page]

Skip to content

Commit 43d0646

Browse files
committed
fix: also hide sensitive info when loggin from cli.js
1 parent b2d82c2 commit 43d0646

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

cli.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
const {argv} = require('process');
1+
const {argv, env, stderr} = require('process');
2+
const util = require('util');
3+
const hideSensitive = require('./lib/hide-sensitive');
24

35
const stringList = {
46
type: 'string',
@@ -57,7 +59,7 @@ Usage:
5759
return 0;
5860
} catch (err) {
5961
if (err.name !== 'YError') {
60-
console.error(err);
62+
stderr.write(hideSensitive(env)(util.inspect(err, {colors: true})));
6163
}
6264
return 1;
6365
}

lib/hide-sensitive.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,5 @@ module.exports = env => {
77
);
88

99
const regexp = new RegExp(toReplace.map(envVar => escapeRegExp(env[envVar])).join('|'), 'g');
10-
return output => {
11-
return output && toReplace.length > 0 ? output.toString().replace(regexp, SECRET_REPLACEMENT) : output;
12-
};
10+
return output => (output && toReplace.length > 0 ? output.toString().replace(regexp, SECRET_REPLACEMENT) : output);
1311
};

test/cli.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import test from 'ava';
2+
import {escapeRegExp} from 'lodash';
23
import proxyquire from 'proxyquire';
34
import {stub} from 'sinon';
5+
import {SECRET_REPLACEMENT} from '../lib/definitions/constants';
46

57
const requireNoCache = proxyquire.noPreserveCache();
68

@@ -208,3 +210,15 @@ test.serial('Return error code if semantic-release throw error', async t => {
208210
t.regex(t.context.errors, /semantic-release error/);
209211
t.is(exitCode, 1);
210212
});
213+
214+
test.serial('Hide sensitive environment variable values from the logs', async t => {
215+
const env = {MY_TOKEN: 'secret token'};
216+
const run = stub().rejects(new Error(`Throw error: Exposing token ${env.MY_TOKEN}`));
217+
const argv = ['', ''];
218+
const cli = requireNoCache('../cli', {'.': run, process: {...process, argv, env: {...process.env, ...env}}});
219+
220+
const exitCode = await cli();
221+
222+
t.regex(t.context.errors, new RegExp(`Throw error: Exposing token ${escapeRegExp(SECRET_REPLACEMENT)}`));
223+
t.is(exitCode, 1);
224+
});

0 commit comments

Comments
 (0)
0