@@ -8,7 +8,9 @@ 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 = / ^ (? ! .* [: ] ) [ - ~ \t \n ] * $ / s;
12+ const secretPathRegex = / ^ (? ! .* [ = ] ) [ - ~ \t \n ] * $ / s;
13+ const vaultNameSecretPathRegex = / ^ ( [ \w \- \. ] + ) (?: : ( [ ^ \0 \\ = ] + ) ) ? $ / ;
1214const secretPathValueRegex = / ^ ( [ a - z A - Z _ ] [ \w ] + ) ? $ / ;
1315const environmentVariableRegex = / ^ ( [ a - z A - Z _ ] + [ a - z A - Z 0 - 9 _ ] * ) ? $ / ;
1416
@@ -80,15 +82,26 @@ function parseSecretPathOptional(
8082 lastEqualIndex === - 1
8183 ? undefined
8284 : secretPath . substring ( lastEqualIndex + 1 ) ;
83- if ( ! secretPathRegex . test ( splitSecretPath ) ) {
85+ if ( ! vaultNameSecretPathRegex . test ( splitSecretPath ) ) {
8486 throw new commander . InvalidArgumentError (
8587 `${ secretPath } is not of the format <vaultName>[:<directoryPath>][=<value>]` ,
8688 ) ;
8789 }
88- const [ , vaultName , directoryPath ] = splitSecretPath . match ( secretPathRegex ) ! ;
90+ const [ , vaultName , directoryPath ] = splitSecretPath . match (
91+ vaultNameSecretPathRegex ,
92+ ) ! ;
8993 return [ vaultName , directoryPath , value ] ;
9094}
9195
96+ function parseVaultName ( vaultName : string ) : string {
97+ if ( ! vaultNameRegex . test ( vaultName ) ) {
98+ throw new commander . InvalidArgumentError (
99+ `${ vaultName } is not a valid vault name` ,
100+ ) ;
101+ }
102+ return vaultName ;
103+ }
104+
92105function parseSecretPath ( secretPath : string ) : [ string , string , string ?] {
93106 // E.g. If 'vault1:a/b/c', ['vault1', 'a/b/c'] is returned
94107 // If 'vault1', an error is thrown
@@ -112,7 +125,39 @@ function parseSecretPathValue(secretPath: string): [string, string, string?] {
112125}
113126
114127function parseSecretPathEnv ( secretPath : string ) : [ string , string , string ?] {
115- const [ vaultName , directoryPath , value ] = parseSecretPath ( secretPath ) ;
128+ // The colon character `:` is prohibited in vaultName, so it's first occurence
129+ // means that this is the delimiter between vaultName and secretPath.
130+ const colonIndex = secretPath . indexOf ( ':' ) ;
131+ // If no colon exists, treat entire string as vault name
132+ if ( colonIndex === - 1 ) {
133+ return [ parseVaultName ( secretPath ) , '' , undefined ] ;
134+ }
135+ // Calculate contents before the `=` separator
136+ const vaultNamePart = secretPath . substring ( 0 , colonIndex ) ;
137+ const secretPathPart = secretPath . substring ( colonIndex + 1 ) ;
138+ // Calculate contents after the `=` separator
139+ const equalIndex = secretPathPart . indexOf ( '=' ) ;
140+ const splitSecretPath =
141+ equalIndex === - 1
142+ ? secretPathPart
143+ : secretPathPart . substring ( 0 , equalIndex ) ;
144+ const valueData =
145+ equalIndex === - 1 ? undefined : secretPathPart . substring ( equalIndex + 1 ) ;
146+ if ( splitSecretPath != null && ! secretPathRegex . test ( splitSecretPath ) ) {
147+ throw new commander . InvalidArgumentError (
148+ `${ secretPath } is not of the format <vaultName>[:<secretPath>][=<value>]` ,
149+ ) ;
150+ }
151+ const parsedVaultName = parseVaultName ( vaultNamePart ) ;
152+ const parsedSecretPath = splitSecretPath . match ( secretPathRegex ) ?. [ 0 ] ?? '/' ;
153+ const [ vaultName , directoryPath , value ] = [
154+ parsedVaultName ,
155+ parsedSecretPath ,
156+ valueData ,
157+ ] ;
158+ console . error ( 'vaultName' , vaultName )
159+ console . error ( 'directoryPath' , directoryPath )
160+ console . error ( 'value' , value )
116161 if ( value != null && ! environmentVariableRegex . test ( value ) ) {
117162 throw new commander . InvalidArgumentError (
118163 `${ value } is not a valid environment variable name` ,
@@ -189,20 +234,24 @@ function parseEnvArgs(
189234 value : string ,
190235 prev : [ Array < [ string , string , string ?] > , Array < string > ] | undefined ,
191236) : [ Array < [ string , string , string ?] > , Array < string > ] {
237+ console . error ( 'original value' , value )
192238 const current : [ Array < [ string , string , string ?] > , Array < string > ] = prev ?? [
193239 [ ] ,
194240 [ ] ,
195241 ] ;
196242 if ( current [ 1 ] . length === 0 ) {
197243 // Parse a secret path
198244 try {
245+ console . error ( `parsed. ${ parseSecretPathEnv ( value ) } ` )
199246 current [ 0 ] . push ( parseSecretPathEnv ( value ) ) ;
200247 } catch ( e ) {
201248 if ( ! ( e instanceof commander . InvalidArgumentError ) ) throw e ;
249+ console . error ( 'errored. value' , value )
202250 // If we get an invalid argument error then we switch over to parsing args verbatim
203251 current [ 1 ] . push ( value ) ;
204252 }
205253 } else {
254+ console . error ( 'added secret path. value' , value )
206255 // Otherwise we just have the cmd args
207256 current [ 1 ] . push ( value ) ;
208257 }
@@ -215,13 +264,15 @@ function parseEnvArgs(
215264}
216265
217266export {
267+ vaultNameRegex ,
218268 secretPathRegex ,
219269 secretPathValueRegex ,
220270 environmentVariableRegex ,
221271 validateParserToArgParser ,
222272 validateParserToArgListParser ,
223273 parseCoreCount ,
224274 parseSecretPathOptional ,
275+ parseVaultName ,
225276 parseSecretPath ,
226277 parseSecretPathValue ,
227278 parseSecretPathEnv ,
0 commit comments