@@ -8,8 +8,8 @@ import * as gestaltsUtils from 'polykey/dist/gestalts/utils';
8
8
import * as networkUtils from 'polykey/dist/network/utils' ;
9
9
import * as nodesUtils from 'polykey/dist/nodes/utils' ;
10
10
11
- const vaultNameRegex = / ^ ( [ \w \- . ] + ) $ / ;
12
- const secretPathRegex = / ^ ( [ \w \/ ; , . ] + ) ? $ / ;
11
+ const vaultNameRegex = / ^ (? ! . * [: ] ) [ - ~ \t \n ] * $ / s ;
12
+ const secretPathRegex = / ^ (? ! . * [ = ] ) [ - ~ \t \n ] * $ / s ;
13
13
const secretPathValueRegex = / ^ ( [ a - z A - Z _ ] [ \w ] + ) ? $ / ;
14
14
const environmentVariableRegex = / ^ ( [ a - z A - Z _ ] + [ a - z A - Z 0 - 9 _ ] * ) ? $ / ;
15
15
@@ -73,7 +73,7 @@ function parseVaultName(vaultName: string): string {
73
73
) ;
74
74
}
75
75
// Make sure we don't accidentally return garbage data
76
- return vaultName . match ( vaultNameRegex ) ! [ 1 ] ;
76
+ return vaultName . match ( vaultNameRegex ) ! [ 0 ] ;
77
77
}
78
78
79
79
// E.g. If 'vault1:a/b/c', ['vault1', 'a/b/c'] is returned
@@ -82,33 +82,31 @@ function parseVaultName(vaultName: string): string {
82
82
// If 'a/b/c', an error is thrown
83
83
// Splits out everything after an `=` separator
84
84
function parseSecretPath ( secretPath : string ) : [ string , string ?, string ?] {
85
- // Calculate contents after the `=` separator
86
- const lastEqualIndex = secretPath . lastIndexOf ( '=' ) ;
87
- const splitSecretPath =
88
- lastEqualIndex === - 1
89
- ? secretPath
90
- : secretPath . substring ( 0 , lastEqualIndex ) ;
91
- const value =
92
- lastEqualIndex === - 1
93
- ? undefined
94
- : secretPath . substring ( lastEqualIndex + 1 ) ;
95
85
// The colon character `:` is prohibited in vaultName, so it's first occurence
96
86
// means that this is the delimiter between vaultName and secretPath.
97
- const colonIndex = splitSecretPath . indexOf ( ':' ) ;
87
+ const colonIndex = secretPath . indexOf ( ':' ) ;
98
88
// If no colon exists, treat entire string as vault name
99
89
if ( colonIndex === - 1 ) {
100
- return [ parseVaultName ( splitSecretPath ) , undefined , value ] ;
90
+ return [ parseVaultName ( secretPath ) , undefined , undefined ] ;
101
91
}
102
92
// Calculate contents before the `=` separator
103
- const vaultNamePart = splitSecretPath . substring ( 0 , colonIndex ) ;
104
- const secretPathPart = splitSecretPath . substring ( colonIndex + 1 ) ;
105
- if ( secretPathPart != null && ! secretPathRegex . test ( secretPathPart ) ) {
93
+ const vaultNamePart = secretPath . substring ( 0 , colonIndex ) ;
94
+ const secretPathPart = secretPath . substring ( colonIndex + 1 ) ;
95
+ // Calculate contents after the `=` separator
96
+ const equalIndex = secretPathPart . indexOf ( '=' ) ;
97
+ const splitSecretPath =
98
+ equalIndex === - 1
99
+ ? secretPathPart
100
+ : secretPathPart . substring ( 0 , equalIndex ) ;
101
+ const value =
102
+ equalIndex === - 1 ? undefined : secretPathPart . substring ( equalIndex + 1 ) ;
103
+ if ( splitSecretPath != null && ! secretPathRegex . test ( splitSecretPath ) ) {
106
104
throw new commander . InvalidArgumentError (
107
105
`${ secretPath } is not of the format <vaultName>[:<secretPath>][=<value>]` ,
108
106
) ;
109
107
}
110
108
const parsedVaultName = parseVaultName ( vaultNamePart ) ;
111
- const parsedSecretPath = secretPathPart . match ( secretPathRegex ) ?. [ 1 ] ;
109
+ const parsedSecretPath = splitSecretPath . match ( secretPathRegex ) ?. [ 0 ] ;
112
110
return [ parsedVaultName , parsedSecretPath , value ] ;
113
111
}
114
112
0 commit comments