1
1
import test from 'ava' ;
2
+ import { repeat } from 'lodash' ;
2
3
import hideSensitive from '../lib/hide-sensitive' ;
4
+ import { SECRET_REPLACEMENT , SECRET_MIN_SIZE } from '../lib/definitions/constants' ;
3
5
4
6
test ( 'Replace multiple sensitive environment variable values' , t => {
5
7
const env = { SOME_PASSWORD : 'password' , SOME_TOKEN : 'secret' } ;
6
8
t . is (
7
9
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 } `
9
11
) ;
10
12
} ) ;
11
13
12
14
test ( 'Replace multiple occurences of sensitive environment variable values' , t => {
13
15
const env = { secretKey : 'secret' } ;
14
16
t . is (
15
17
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 } `
17
19
) ;
18
20
} ) ;
19
21
20
22
test ( 'Escape regexp special characters' , t => {
21
23
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
+ ) ;
23
28
} ) ;
24
29
25
30
test ( 'Accept "undefined" input' , t => {
@@ -34,10 +39,20 @@ test('Exclude empty environment variables from the regexp', t => {
34
39
const env = { SOME_PASSWORD : 'password' , SOME_TOKEN : '' } ;
35
40
t . is (
36
41
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=`
38
43
) ;
39
44
} ) ;
40
45
41
46
test ( 'Exclude empty environment variables from the regexp if there is only empty ones' , t => {
42
47
t . is ( hideSensitive ( { SOME_PASSWORD : '' , SOME_TOKEN : ' \n ' } ) ( `https://host.com?token=` ) , 'https://host.com?token=' ) ;
43
48
} ) ;
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