@@ -8,7 +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 secretPathRegex = / ^ ( [ \w - ] + ) (?: : ( [ ^ \0 \\ = ] + ) ) ? $ / ;
11+ const vaultNameRegex = / ^ ( [ \w - . ] + ) $ / ;
12+ const secretPathRegex = / ^ ( [ ^ \0 \\ = ] + ) ? $ / ;
1213const secretPathValueRegex = / ^ ( [ a - z A - Z _ ] [ \w ] + ) ? $ / ;
1314const environmentVariableRegex = / ^ ( [ a - z A - Z _ ] + [ a - z A - Z 0 - 9 _ ] * ) ? $ / ;
1415
@@ -65,12 +66,23 @@ function parseCoreCount(v: string): number | undefined {
6566 }
6667}
6768
69+ function parseVaultName ( vaultName : string ) : string {
70+ if ( ! vaultNameRegex . test ( vaultName ) ) {
71+ throw new commander . InvalidArgumentError (
72+ `${ vaultName } is not a valid vault name` ,
73+ ) ;
74+ }
75+ // Make sure we don't accidentally return garbage data
76+ return vaultName . match ( vaultNameRegex ) ! [ 1 ] ;
77+ }
78+
79+ // E.g. If 'vault1:a/b/c', ['vault1', 'a/b/c'] is returned
80+ // If 'vault1', ['vault1, undefined] is returned
81+ // If 'vault1:', an error is thrown
82+ // If 'a/b/c', an error is thrown
83+ // Splits out everything after an `=` separator
6884function parseSecretPath ( secretPath : string ) : [ string , string ?, string ?] {
69- // E.g. If 'vault1:a/b/c', ['vault1', 'a/b/c'] is returned
70- // If 'vault1', ['vault1, undefined] is returned
71- // If 'vault1:', an error is thrown
72- // If 'a/b/c', an error is thrown
73- // Splits out everything after an `=` separator
85+ // Calculate contents after the `=` separator
7486 const lastEqualIndex = secretPath . lastIndexOf ( '=' ) ;
7587 const splitSecretPath =
7688 lastEqualIndex === - 1
@@ -80,13 +92,28 @@ function parseSecretPath(secretPath: string): [string, string?, string?] {
8092 lastEqualIndex === - 1
8193 ? undefined
8294 : secretPath . substring ( lastEqualIndex + 1 ) ;
83- if ( ! secretPathRegex . test ( splitSecretPath ) ) {
95+ // The colon character `:` is prohibited in vaultName, so it's first occurence
96+ // means that this is the delimiter between vaultName and secretPath.
97+ const colonIndex = splitSecretPath . indexOf ( ':' ) ;
98+ // Calculate contents before the `=` separator
99+ const vaultNamePart =
100+ colonIndex === - 1
101+ ? splitSecretPath
102+ : splitSecretPath . substring ( 0 , colonIndex ) ;
103+ const secretPathPart =
104+ colonIndex === - 1 ? undefined : splitSecretPath . substring ( colonIndex + 1 ) ;
105+
106+ if ( secretPathPart && ! secretPathRegex . test ( secretPathPart ) ) {
84107 throw new commander . InvalidArgumentError (
85- `${ secretPath } is not of the format <vaultName>[:<directoryPath >][=<value>]` ,
108+ `${ secretPath } is not of the format <vaultName>[:<secretPath >][=<value>]` ,
86109 ) ;
87110 }
88- const [ , vaultName , directoryPath ] = splitSecretPath . match ( secretPathRegex ) ! ;
89- return [ vaultName , directoryPath , value ] ;
111+ const parsedVaultName = parseVaultName ( vaultNamePart ) ;
112+ const parsedSecretPath =
113+ secretPathPart == null
114+ ? undefined
115+ : secretPathPart . match ( secretPathRegex ) ! [ 1 ] ;
116+ return [ parsedVaultName , parsedSecretPath , value ] ;
90117}
91118
92119function parseSecretPathValue ( secretPath : string ) : [ string , string , string ?] {
@@ -213,12 +240,14 @@ function parseEnvArgs(
213240}
214241
215242export {
243+ vaultNameRegex ,
216244 secretPathRegex ,
217245 secretPathValueRegex ,
218246 environmentVariableRegex ,
219247 validateParserToArgParser ,
220248 validateParserToArgListParser ,
221249 parseCoreCount ,
250+ parseVaultName ,
222251 parseSecretPath ,
223252 parseSecretPathValue ,
224253 parseSecretPathEnv ,
0 commit comments