8000 fix: do not hide env variable value if shorter than 5 · semantic-release/semantic-release@b082a2e · GitHub
[go: up one dir, main page]

Skip to content

Commit b082a2e

Browse files
committed
fix: do not hide env variable value if shorter than 5
1 parent 43d0646 commit b082a2e

File tree

3 files changed

+33
-8
lines changed

3 files changed

+33
-8
lines changed

lib/definitions/constants.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,14 @@ const RELEASE_NOTES_SEPARATOR = '\n\n';
1010

1111
const SECRET_REPLACEMENT = '[secure]';
1212

13-
module.exports = {RELEASE_TYPE, FIRST_RELEASE, COMMIT_NAME, COMMIT_EMAIL, RELEASE_NOTES_SEPARATOR, SECRET_REPLACEMENT};
13+
const SECRET_MIN_SIZE = 5;
14+
15+
module.exports = {
16+
RELEASE_TYPE,
17+
FIRST_RELEASE,
18+
COMMIT_NAME,
19+
COMMIT_EMAIL,
20+
RELEASE_NOTES_SEPARATOR,
21+
SECRET_REPLACEMENT,
22+
SECRET_MIN_SIZE,
23+
};

lib/hide-sensitive.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
const {escapeRegExp} = require('lodash');
2-
const {SECRET_REPLACEMENT} = require('./definitions/constants');
1+
const {escapeRegExp, size} = require('lodash');
2+
const {SECRET_REPLACEMENT, SECRET_MIN_SIZE} = require('./definitions/constants');
33

44
module.exports = env => {
55
const toReplace = Object.keys(env).filter(
6-
envVar => /token|password|credential|secret|private/i.test(envVar) && env[envVar].trim()
6+
envVar => /token|password|credential|secret|private/i.test(envVar) && size(env[envVar].trim()) >= SECRET_MIN_SIZE
77
);
88

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

test/hide-sensitive.test.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
11
import test from 'ava';
2+
import {repeat} from 'lodash';
23
import hideSensitive from '../lib/hide-sensitive';
4+
import {SECRET_REPLACEMENT, SECRET_MIN_SIZE} from '../lib/definitions/constants';
35

46
test('Replace multiple sensitive environment variable values', t => {
57
const env = {SOME_PASSWORD: 'password', SOME_TOKEN: 'secret'};
68
t.is(
79
hideSensitive(env)(`https://user:${env.SOME_PASSWORD}@host.com?token=${env.SOME_TOKEN}`),
8-
'https://user:[secure]@host.com?token=[secure]'
10+
`https://user:${SECRET_REPLACEMENT}@host.com?token=${SECRET_REPLACEMENT}`
911
);
1012
});
1113

1214
test('Replace multiple occurences of sensitive environment variable values', t => {
1315
const env = {secretKey: 'secret'};
1416
t.is(
1517
hideSensitive(env)(`https://user:${env.secretKey}@host.com?token=${env.secretKey}`),
16-
'https://user:[secure]@host.com?token=[secure]'
18+
`https://user:${SECRET_REPLACEMENT}@host.com?token=${SECRET_REPLACEMENT}`
1719
);
1820
});
1921

2022
test('Escape regexp special characters', t => {
2123
const env = {SOME_CREDENTIALS: 'p$^{.+}\\w[a-z]o.*rd'};
22-
t.is(hideSensitive(env)(`https://user:${env.SOME_CREDENTIALS}@host.com`), 'https://user:[secure]@host.com');
24+
t.is(
25+
hideSensitive(env)(`https://user:${env.SOME_CREDENTIALS}@host.com`),
26+
`https://user:${SECRET_REPLACEMENT}@host.com`
27+
);
2328
});
2429

2530
test('Accept "undefined" input', t => {
@@ -34,10 +39,20 @@ test('Exclude empty environment variables from the regexp', t => {
3439
const env = {SOME_PASSWORD: 'password', SOME_TOKEN: ''};
3540
t.is(
3641
hideSensitive(env)(`https://user:${env.SOME_PASSWORD}@host.com?token=`),
37-
'https://user:[secure]@host.com?token='
42+
`https://user:${SECRET_REPLACEMENT}@host.com?token=`
3843
);
3944
});
4045

4146
test('Exclude empty environment variables from the regexp if there is only empty ones', t => {
4247
t.is(hideSensitive({SOME_PASSWORD: '', SOME_TOKEN: ' \n '})(`https://host.com?token=`), 'https://host.com?token=');
4348
});
49+
50+
test('Exclude environment variables with value shorter than SECRET_MIN_SIZE from the regexp', t => {
51+
const SHORT_TOKEN = repeat('a', SECRET_MIN_SIZE - 1);
52+
const LONG_TOKEN = repeat('b', SECRET_MIN_SIZE);
53+
const env = {SHORT_TOKEN, LONG_TOKEN};
54+
t.is(
55+
hideSensitive(env)(`https://user:${SHORT_TOKEN}@host.com?token=${LONG_TOKEN}`),
56+
`https://user:${SHORT_TOKEN}@host.com?token=${SECRET_REPLACEMENT}`
57+
);
58+
});

0 commit comments

Comments
 (0)
0