@@ -8,7 +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 secretPathRegex = / ^ ( [ \w - ] + ) (?: : ( [ ^ \0 \\ = ] + ) ) ? $ / ;
11
+ const vaultNameRegex = / ^ ( [ \w - . ] + ) $ / ;
12
+ const secretPathRegex = / ^ ( [ ^ \0 \\ = ] + ) ? $ / ;
12
13
const secretPathValueRegex = / ^ ( [ a - z A - Z _ ] [ \w ] + ) ? $ / ;
13
14
const environmentVariableRegex = / ^ ( [ a - z A - Z _ ] + [ a - z A - Z 0 - 9 _ ] * ) ? $ / ;
14
15
@@ -65,12 +66,23 @@ function parseCoreCount(v: string): number | undefined {
65
66
}
66
67
}
67
68
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
68
84
function 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
74
86
const lastEqualIndex = secretPath . lastIndexOf ( '=' ) ;
75
87
const splitSecretPath =
76
88
lastEqualIndex === - 1
@@ -80,13 +92,28 @@ function parseSecretPath(secretPath: string): [string, string?, string?] {
80
92
lastEqualIndex === - 1
81
93
? undefined
82
94
: 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 ) ) {
84
107
throw new commander . InvalidArgumentError (
85
- `${ secretPath } is not of the format <vaultName>[:<directoryPath >][=<value>]` ,
108
+ `${ secretPath } is not of the format <vaultName>[:<secretPath >][=<value>]` ,
86
109
) ;
87
110
}
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 ] ;
90
117
}
91
118
92
119
function parseSecretPathValue ( secretPath : string ) : [ string , string , string ?] {
@@ -213,12 +240,14 @@ function parseEnvArgs(
213
240
}
214
241
215
242
export {
243
+ vaultNameRegex ,
216
244
secretPathRegex ,
217
245
secretPathValueRegex ,
218
246
environmentVariableRegex ,
219
247
validateParserToArgParser ,
220
248
validateParserToArgListParser ,
221
249
parseCoreCount ,
250
+ parseVaultName ,
222
251
parseSecretPath ,
223
252
parseSecretPathValue ,
224
253
parseSecretPathEnv ,
0 commit comments