@@ -8,8 +8,8 @@ import * as gestaltsUtils from 'polykey/dist/gestalts/utils';
88import * as networkUtils from 'polykey/dist/network/utils' ;
99import * as nodesUtils from 'polykey/dist/nodes/utils' ;
1010
11- const vaultNameRegex = / ^ ( [ \w \- . ] + ) $ / ;
12- const secretPathRegex = / ^ ( [ \w \/ ; , . ] + ) ? $ / ;
11+ const vaultNameRegex = / ^ (? ! . * [: ] ) [ - ~ \t \n ] * $ / s ;
12+ const secretPathRegex = / ^ (? ! . * [ = ] ) [ - ~ \t \n ] * $ / s ;
1313const secretPathValueRegex = / ^ ( [ a - z A - Z _ ] [ \w ] + ) ? $ / ;
1414const environmentVariableRegex = / ^ ( [ a - z A - Z _ ] + [ a - z A - Z 0 - 9 _ ] * ) ? $ / ;
1515
@@ -73,7 +73,7 @@ function parseVaultName(vaultName: string): string {
7373 ) ;
7474 }
7575 // Make sure we don't accidentally return garbage data
76- return vaultName . match ( vaultNameRegex ) ! [ 1 ] ;
76+ return vaultName . match ( vaultNameRegex ) ! [ 0 ] ;
7777}
7878
7979// E.g. If 'vault1:a/b/c', ['vault1', 'a/b/c'] is returned
@@ -82,33 +82,31 @@ function parseVaultName(vaultName: string): string {
8282// If 'a/b/c', an error is thrown
8383// Splits out everything after an `=` separator
8484function 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 ) ;
9585 // The colon character `:` is prohibited in vaultName, so it's first occurence
9686 // means that this is the delimiter between vaultName and secretPath.
97- const colonIndex = splitSecretPath . indexOf ( ':' ) ;
87+ const colonIndex = secretPath . indexOf ( ':' ) ;
9888 // If no colon exists, treat entire string as vault name
9989 if ( colonIndex === - 1 ) {
100- return [ parseVaultName ( splitSecretPath ) , undefined , value ] ;
90+ return [ parseVaultName ( secretPath ) , undefined , undefined ] ;
10191 }
10292 // 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 ) ) {
106104 throw new commander . InvalidArgumentError (
107105 `${ secretPath } is not of the format <vaultName>[:<secretPath>][=<value>]` ,
108106 ) ;
109107 }
110108 const parsedVaultName = parseVaultName ( vaultNamePart ) ;
111- const parsedSecretPath = secretPathPart . match ( secretPathRegex ) ?. [ 1 ] ;
109+ const parsedSecretPath = splitSecretPath . match ( secretPathRegex ) ?. [ 0 ] ;
112110 return [ parsedVaultName , parsedSecretPath , value ] ;
113111}
114112
0 commit comments