Skip to content

Commit fc7e4d4

Browse files
authored
fix: validate input stack name for generate commands (#2299)
* fix: validate input stack name for generate commands * lint fix
1 parent 1593ce8 commit fc7e4d4

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

.changeset/shy-ladybugs-buy.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+
validate input stack name for generate commands

packages/cli/src/commands/generate/generate_command.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { GenerateFormsCommand } from './forms/generate_forms_command.js';
44
import { GenerateGraphqlClientCodeCommand } from './graphql-client-code/generate_graphql_client_code_command.js';
55
import { CommandMiddleware } from '../../command_middleware.js';
66
import { GenerateSchemaCommand } from './schema-from-database/generate_schema_command.js';
7+
import { AmplifyUserError } from '@aws-amplify/platform-core';
78

89
/**
910
* An entry point for generate command.
@@ -62,6 +63,20 @@ export class GenerateCommand implements CommandModule {
6263
type: 'string',
6364
array: false,
6465
})
66+
.check(async (argv) => {
67+
const stackNameRegex =
68+
/^[a-zA-Z][-a-zA-Z0-9]{1,127}$|^arn:[-a-zA-Z0-9:/._+]$/;
69+
if (argv['stack'] && typeof argv['stack'] === 'string') {
70+
if (!argv.stack.match(stackNameRegex)) {
71+
throw new AmplifyUserError('InvalidCommandInputError', {
72+
message: `Invalid --stack name provided: ${argv.stack}`,
73+
resolution:
74+
'Check the value of the stack name provided and try again.',
75+
});
76+
}
77+
}
78+
return true;
79+
})
6580
.middleware([this.commandMiddleware.ensureAwsCredentialAndRegion])
6681
);
6782
};

packages/cli/src/commands/generate/generate_command_factory.test.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import yargs from 'yargs';
22
import { describe, it } from 'node:test';
33
import assert from 'node:assert';
4-
import { TestCommandRunner } from '../../test-utils/command_runner.js';
4+
import {
5+
TestCommandError,
6+
TestCommandRunner,
7+
} from '../../test-utils/command_runner.js';
58
import { createGenerateCommand } from './generate_command_factory.js';
69

710
/**
@@ -31,6 +34,20 @@ void describe('top level generate command', () => {
3134
assert.match(output, /Not enough non-option arguments/);
3235
});
3336

37+
void it('fails if stack argument provided is invalid', async () => {
38+
await assert.rejects(
39+
() => commandRunner.runCommand('generate outputs --stack 3-Invalid'),
40+
(error: TestCommandError) => {
41+
assert.strictEqual(error.error.name, 'InvalidCommandInputError');
42+
assert.strictEqual(
43+
error.error.message,
44+
'Invalid --stack name provided: 3-Invalid'
45+
);
46+
return true;
47+
}
48+
);
49+
});
50+
3451
void it('should throw if top level command handler is ever called', () => {
3552
assert.throws(
3653
() => generateCommand.handler({ $0: '', _: [] }),

0 commit comments

Comments
 (0)