Skip to content

Commit 0ef53e2

Browse files
authored
add validation for sandbox set command secret-name (#2352)
1 parent cf342cc commit 0ef53e2

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

.changeset/old-hornets-type.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@aws-amplify/backend-cli': patch
3+
---
4+
5+
add validation for sandbox set command secret-name

packages/cli/src/commands/sandbox/sandbox-secret/sandbox_secret_set_command.test.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { beforeEach, describe, it, mock } from 'node:test';
22
import { AmplifyPrompter, printer } from '@aws-amplify/cli-core';
33
import yargs, { CommandModule } from 'yargs';
4-
import { TestCommandRunner } from '../../../test-utils/command_runner.js';
4+
import {
5+
TestCommandError,
6+
TestCommandRunner,
7+
} from '../../../test-utils/command_runner.js';
58
import assert from 'node:assert';
69
import { SandboxBackendIdResolver } from '../sandbox_id_resolver.js';
710
import {
@@ -11,6 +14,7 @@ import {
1114
import { SandboxSecretSetCommand } from './sandbox_secret_set_command.js';
1215
import { ReadStream } from 'node:tty';
1316
import { PassThrough } from 'node:stream';
17+
import { AmplifyError } from '@aws-amplify/platform-core';
1418

1519
const testSecretName = 'testSecretName';
1620
const testSecretValue = 'testSecretValue';
@@ -145,6 +149,23 @@ void describe('sandbox secret set command', () => {
145149
]);
146150
});
147151

152+
void it('throws AmplifyUserError if invalid secret name is provided', async () => {
153+
const invalidSecretName = 'invalid@';
154+
await assert.rejects(
155+
() => commandRunner.runCommand(`set ${invalidSecretName}`),
156+
(err: TestCommandError) => {
157+
assert.ok(AmplifyError.isAmplifyError(err.error));
158+
assert.strictEqual(
159+
err.error.message,
160+
'Invalid secret name provided: invalid@'
161+
);
162+
assert.strictEqual(err.error.name, 'InvalidCommandInputError');
163+
return true;
164+
}
165+
);
166+
assert.equal(secretSetMock.mock.callCount(), 0);
167+
});
168+
148169
void it('show --help', async () => {
149170
const output = await commandRunner.runCommand('set --help');
150171
assert.match(output, /Set a sandbox secret/);

packages/cli/src/commands/sandbox/sandbox-secret/sandbox_secret_set_command.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { ArgumentsKebabCase } from '../../../kebab_case.js';
66
import { SandboxCommandGlobalOptions } from '../option_types.js';
77
import { once } from 'events';
88
import { ReadStream } from 'node:tty';
9+
import { AmplifyUserError } from '@aws-amplify/platform-core';
910

1011
/**
1112
* Command to set sandbox secret.
@@ -57,11 +58,24 @@ export class SandboxSecretSetCommand
5758
* @inheritDoc
5859
*/
5960
builder = (yargs: Argv): Argv<SecretSetCommandOptionsKebabCase> => {
60-
return yargs.positional('secret-name', {
61-
describe: 'Name of the secret to set',
62-
type: 'string',
63-
demandOption: true,
64-
});
61+
return yargs
62+
.positional('secret-name', {
63+
describe: 'Name of the secret to set',
64+
type: 'string',
65+
demandOption: true,
66+
})
67+
.check(async (argv) => {
68+
if (argv['secret-name']) {
69+
const secretNameRegex = /^[a-zA-Z0-9_.-]+$/;
70+
if (!argv['secret-name'].match(secretNameRegex)) {
71+
throw new AmplifyUserError('InvalidCommandInputError', {
72+
message: `Invalid secret name provided: ${argv['secret-name']}`,
73+
resolution: 'Use a secret name that matches [a-zA-Z0-9_.-]+',
74+
});
75+
}
76+
}
77+
return true;
78+
});
6579
};
6680

6781
/**

0 commit comments

Comments
 (0)