Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(amplify): domain name validation #31959

Merged
merged 9 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -175,5 +175,6 @@
},
"dependencies": {
"string-width": "^4.2.3"
}
},
"packageManager": "[email protected]+sha1.1959a18351b811cdeedbd484a8f86c3cc3bbaf72"
}
7 changes: 7 additions & 0 deletions packages/@aws-cdk/aws-amplify-alpha/lib/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ export class Domain extends Resource {
this.subDomains = props.subDomains || [];

const domainName = props.domainName || id;
if (domainName.length > 255) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we might need to check token.

Token.isUnresolved(domainName)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I keep forgetting to check the token no matter how many times I try... Thank you for the reminder!

throw new Error(`Domain name must be 255 characters or less, got: ${domainName.length}`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw new Error(`Domain name must be 255 characters or less, got: ${domainName.length}`);
throw new Error(`Domain name must be 255 characters or less, got: ${domainName.length} characters.`);

I think it would be more helpful to include "characters" in the error message.

}
if (!/^(((?!-)[A-Za-z0-9-]{0,62}[A-Za-z0-9])\.)+((?!-)[A-Za-z0-9-]{1,62}[A-Za-z0-9])(\.)?$/.test(domainName)) {
throw new Error(`Domain name must be a valid hostname, got: ${domainName}`);
}

const domain = new CfnDomain(this, 'Resource', {
appId: props.app.appId,
domainName,
Expand Down
49 changes: 49 additions & 0 deletions packages/@aws-cdk/aws-amplify-alpha/test/domain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,55 @@ test('map a branch to the domain root', () => {
});
});

test('throw error for invalid domain name length', () => {
const stack = new Stack();
const app = new amplify.App(stack, 'App', {
sourceCodeProvider: new amplify.GitHubSourceCodeProvider({
owner: 'aws',
repository: 'aws-cdk',
oauthToken: SecretValue.unsafePlainText('secret'),
}),
});
const prodBranch = app.addBranch('main');

expect(() => app.addDomain('Domain', {
subDomains: [
{
branch: prodBranch,
prefix: 'prod',
},
],
domainName: 'a'.repeat(256),
})).toThrow('Domain name must be 255 characters or less, got: 256');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
})).toThrow('Domain name must be 255 characters or less, got: 256');
})).toThrow('Domain name must be 255 characters or less, got: 256 characters.');

});

test.each([
'-example.com',
'example..com',
'example.com-',
'[email protected]',
])('throw error for invalid domain name', (domainName) => {
const stack = new Stack();
const app = new amplify.App(stack, 'App', {
sourceCodeProvider: new amplify.GitHubSourceCodeProvider({
owner: 'aws',
repository: 'aws-cdk',
oauthToken: SecretValue.unsafePlainText('secret'),
}),
});
const prodBranch = app.addBranch('main');

expect(() => app.addDomain('Domain', {
subDomains: [
{
branch: prodBranch,
prefix: 'prod',
},
],
domainName,
})).toThrow(`Domain name must be a valid hostname, got: ${domainName}`);
});

test('throws at synthesis without subdomains', () => {
// GIVEN
const app = new App();
Expand Down