Skip to content

Commit fb0caa0

Browse files
committed
feat: hide sensitive info in stdout/sdtin
1 parent cdb98f9 commit fb0caa0

File tree

6 files changed

+87
-5
lines changed

6 files changed

+87
-5
lines changed

index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
const marked = require('marked');
22
const TerminalRenderer = require('marked-terminal');
33
const envCi = require('env-ci');
4+
const hookStd = require('hook-std');
5+
const hideSensitive = require('./lib/hide-sensitive');
46
const getConfig = require('./lib/get-config');
57
const getNextVersion = require('./lib/get-next-version');
68
const getCommits = require('./lib/get-commits');
@@ -96,8 +98,10 @@ async function run(opts) {
9698
}
9799

98100
module.exports = async opts => {
101+
const unhook = hookStd({silent: false}, hideSensitive);
99102
try {
100103
const result = await run(opts);
104+
unhook();
101105
return result;
102106
} catch (err) {
103107
const errors = err.name === 'AggregateError' ? Array.from(err).sort(error => !error.semanticRelease) : [err];
@@ -108,6 +112,7 @@ module.exports = async opts => {
108112
logger.error('An error occurred while running semantic-release: %O', error);
109113
}
110114
}
115+
unhook();
111116
throw err;
112117
}
113118
};

lib/hide-sensitive.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const {escapeRegExp} = require('lodash');
2+
3+
const regexp = new RegExp(
4+
Object.keys(process.env)
5+
.filter(envVar => /token|password|credential|secret|private/i.test(envVar))
6+
.map(envVar => escapeRegExp(process.env[envVar]))
7+
.join('|'),
8+
'g'
9+
);
10+
11+
module.exports = output => output.replace(regexp, '[secure]');

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"execa": "^0.9.0",
3434
"get-stream": "^3.0.0",
3535
"git-log-parser": "^1.2.0",
36+
"hook-std": "^0.4.0",
3637
"lodash": "^4.17.4",
3738
"marked": "^0.3.9",
3839
"marked-terminal": "^2.0.0",
@@ -44,6 +45,7 @@
4445
},
4546
"devDependencies": {
4647
"ava": "^0.25.0",
48+
"clear-module": "^2.1.0",
4749
"codecov": "^3.0.0",
4850
"commitizen": "^2.9.6",
4951
"cz-conventional-changelog": "^2.0.0",

test/hide-sensitive.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import test from 'ava';
2+
import clearModule from 'clear-module';
3+
4+
test.beforeEach(() => {
5+
process.env = {};
6+
clearModule('../lib/hide-sensitive');
7+
});
8+
9+
test.serial('Replace multiple sensitive environment variable values', t => {
10+
process.env.SOME_PASSWORD = 'password';
11+
process.env.SOME_TOKEN = 'secret';
12+
t.is(
13+
require('../lib/hide-sensitive')(
14+
`https://user:${process.env.SOME_PASSWORD}@host.com?token=${process.env.SOME_TOKEN}`
15+
),
16+
'https://user:[secure]@host.com?token=[secure]'
17+
);
18+
});
19+
20+
test.serial('Replace multiple occurences of sensitive environment variable values', t => {
21+
process.env.secretKey = 'secret';
22+
t.is(
23+
require('../lib/hide-sensitive')(`https://user:${process.env.secretKey}@host.com?token=${process.env.secretKey}`),
24+
'https://user:[secure]@host.com?token=[secure]'
25+
);
26+
});
27+
28+
test.serial('Escape regexp special characters', t => {
29+
process.env.SOME_CREDENTIALS = 'p$^{.+}\\w[a-z]o.*rd';
30+
t.is(
31+
require('../lib/hide-sensitive')(`https://user:${process.env.SOME_CREDENTIALS}@host.com`),
32+
'https://user:[secure]@host.com'
33+
);
34+
});

test/index.test.js

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import test from 'ava';
22
import proxyquire from 'proxyquire';
33
import {stub} from 'sinon';
44
import tempy from 'tempy';
5+
import clearModule from 'clear-module';
56
import SemanticReleaseError from '@semantic-release/error';
67
import DEFINITIONS from '../lib/plugins/definitions';
78
import {gitHead as getGitHead} from '../lib/git';
@@ -12,21 +13,25 @@ const envBackup = Object.assign({}, process.env);
1213
// Save the current working diretory
1314
const cwd = process.cwd();
1415

15-
stub(process.stdout, 'write');
16-
stub(process.stderr, 'write');
17-
1816
test.beforeEach(t => {
17+
clearModule('../lib/hide-sensitive');
18+
1919
// Stub the logger functions
2020
t.context.log = stub();
2121
t.context.error = stub();
2222
t.context.logger = {log: t.context.log, error: t.context.error};
23+
t.context.stdout = stub(process.stdout, 'write');
24+
t.context.stderr = stub(process.stderr, 'write');
2325
});
2426

25-
test.afterEach.always(() => {
27+
test.afterEach.always(t => {
2628
// Restore process.env
2729
process.env = envBackup;
2830
// Restore the current working directory
2931
process.chdir(cwd);
32+
33+
t.context.stdout.restore();
34+
t.context.stderr.restore();
3035
});
3136

3237
test.serial('Plugins are called with expected values', async t => {
@@ -571,6 +576,31 @@ test.serial('Exclude commits with [skip release] or [release skip] from analysis
571576
t.deepEqual(analyzeCommits.args[0][1].commits[0].message, commits[commits.length - 1].message);
572577
});
573578

579+
test.serial('Hide sensitive environment variable values from the logs', async t => {
580+
process.env.MY_TOKEN = 'secret token';
581+
await gitRepo();
582+
583+
const options = {
584+
branch: 'master',
585+
repositoryUrl: 'git@hostname.com:owner/module.git',
586+
verifyConditions: async (pluginConfig, {logger}) => {
587+
console.log(`Console: The token ${process.env.MY_TOKEN} is invalid`);
588+
logger.log(`Log: The token ${process.env.MY_TOKEN} is invalid`);
589+
logger.error(`Error: The token ${process.env.MY_TOKEN} is invalid`);
590+
throw new Error(`Invalid token ${process.env.MY_TOKEN}`);
591+
},
592+
};
593+
const semanticRelease = proxyquire('..', {
594+
'env-ci': () => ({isCi: true, branch: 'master', isPr: false}),
595+
});
596+
597+
await t.throws(semanticRelease(options));
598+
t.regex(t.context.stdout.args[7][0], /Console: The token \[secure\] is invalid/);
599+
t.regex(t.context.stdout.args[8][0], /Log: The token \[secure\] is invalid/);
600+
t.regex(t.context.stderr.args[0][0], /Error: The token \[secure\] is invalid/);
601+
t.regex(t.context.stderr.args[1][0], /Invalid token \[secure\]/);
602+
});
603+
574604
test.serial('Throw SemanticReleaseError if repositoryUrl is not set and cannot be found from repo config', async t => {
575605
// Create a git repository, set the current working directory at the root of the repo
576606
await gitRepo();

test/integration.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ test.beforeEach(() => {
6161

6262
// Delete all `npm_config` environment variable set by CI as they take precedence over the `.npmrc` because the process that runs the tests is started before the `.npmrc` is created
6363
for (let i = 0, keys = Object.keys(process.env); i < keys.length; i++) {
64-
if (keys[i].startsWith('npm_config')) {
64+
if (keys[i].startsWith('npm_')) {
6565
delete process.env[keys[i]];
6666
}
6767
}

0 commit comments

Comments
 (0)