Skip to content

Commit eed1d3c

Browse files
authoredMay 24, 2020
fix: prevent false positive secret replacement for Golang projects (#1562)
1 parent 5f3a8bb commit eed1d3c

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed
 

‎lib/hide-sensitive.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@ const {escapeRegExp, size, isString} = require('lodash');
22
const {SECRET_REPLACEMENT, SECRET_MIN_SIZE} = require('./definitions/constants');
33

44
module.exports = (env) => {
5-
const toReplace = Object.keys(env).filter(
6-
(envVar) => /token|password|credential|secret|private/i.test(envVar) && size(env[envVar].trim()) >= SECRET_MIN_SIZE
7-
);
5+
const toReplace = Object.keys(env).filter((envVar) => {
6+
// https://github.com/semantic-release/semantic-release/issues/1558
7+
if (envVar === 'GOPRIVATE') {
8+
return false;
9+
}
10+
11+
return /token|password|credential|secret|private/i.test(envVar) && size(env[envVar].trim()) >= SECRET_MIN_SIZE;
12+
});
813

914
const regexp = new RegExp(toReplace.map((envVar) => escapeRegExp(env[envVar])).join('|'), 'g');
1015
return (output) =>

‎test/hide-sensitive.test.js

+10
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ test('Replace multiple occurences of sensitive environment variable values', (t)
1919
);
2020
});
2121

22+
test('Replace sensitive environment variable matching specific regex for "private"', (t) => {
23+
const env = {privateKey: 'secret', GOPRIVATE: 'host.com'};
24+
t.is(hideSensitive(env)(`https://host.com?token=${env.privateKey}`), `https://host.com?token=${SECRET_REPLACEMENT}`);
25+
});
26+
2227
test('Escape regexp special characters', (t) => {
2328
const env = {SOME_CREDENTIALS: 'p$^{.+}\\w[a-z]o.*rd'};
2429
t.is(
@@ -47,6 +52,11 @@ test('Exclude empty environment variables from the regexp if there is only empty
4752
t.is(hideSensitive({SOME_PASSWORD: '', SOME_TOKEN: ' \n '})(`https://host.com?token=`), 'https://host.com?token=');
4853
});
4954

55+
test('Exclude nonsensitive GOPRIVATE environment variable for Golang projects from the regexp', (t) => {
56+
const env = {GOPRIVATE: 'host.com'};
57+
t.is(hideSensitive(env)(`https://host.com?token=`), 'https://host.com?token=');
58+
});
59+
5060
test('Exclude environment variables with value shorter than SECRET_MIN_SIZE from the regexp', (t) => {
5161
const SHORT_TOKEN = repeat('a', SECRET_MIN_SIZE - 1);
5262
const LONG_TOKEN = repeat('b', SECRET_MIN_SIZE);

0 commit comments

Comments
 (0)
Please sign in to comment.