Skip to content

Commit 616df30

Browse files
committed
feat: updated parsers to be more robust
1 parent efb3579 commit 616df30

File tree

1 file changed

+17
-19
lines changed

1 file changed

+17
-19
lines changed

src/utils/parsers.ts

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import * as gestaltsUtils from 'polykey/dist/gestalts/utils';
88
import * as networkUtils from 'polykey/dist/network/utils';
99
import * 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;
1313
const secretPathValueRegex = /^([a-zA-Z_][\w]+)?$/;
1414
const environmentVariableRegex = /^([a-zA-Z_]+[a-zA-Z0-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
8484
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);
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

Comments
 (0)