Skip to content

Commit

Permalink
chore(amplify): domain name validation (#31959)
Browse files Browse the repository at this point in the history
### Issue # (if applicable)

None

### Reason for this change

We can configure amplify domain name but there is no validation for that.

### Description of changes

Add validation for an amplify domain name
- length must be lower than 255 characters
- match with [the regular expression](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-amplify-domain.html#cfn-amplify-domain-domainname)
  - /^(((?!-)[A-Za-z0-9-]{0,62}[A-Za-z0-9])\.)+((?!-)[A-Za-z0-9-]{1,62}[A-Za-z0-9])(\.)?$/

### Description of how you validated changes

Add unit test

### Checklist
- [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md)

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
badmintoncryer authored Nov 1, 2024
1 parent aee1b30 commit 5137e38
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 75 deletions.
9 changes: 8 additions & 1 deletion packages/@aws-cdk/aws-amplify-alpha/lib/domain.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as acm from 'aws-cdk-lib/aws-certificatemanager';
import * as iam from 'aws-cdk-lib/aws-iam';
import { Lazy, Resource, IResolvable } from 'aws-cdk-lib/core';
import { Lazy, Resource, IResolvable, Token } from 'aws-cdk-lib/core';
import { Construct } from 'constructs';
import { CfnDomain } from 'aws-cdk-lib/aws-amplify';
import { IApp } from './app';
Expand Down Expand Up @@ -131,6 +131,13 @@ export class Domain extends Resource {
this.subDomains = props.subDomains || [];

const domainName = props.domainName || id;
if (!Token.isUnresolved(domainName) && domainName.length > 255) {
throw new Error(`Domain name must be 255 characters or less, got: ${domainName.length} characters.`);
}
if (!Token.isUnresolved(domainName) && !/^(((?!-)[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 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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
"EnablePullRequestPreview": true
}
},
"Appexamplecom6AF1A3AD": {
"Appexamplecom93D8EC68": {
"Type": "AWS::Amplify::Domain",
"Properties": {
"AppId": {
Expand All @@ -124,7 +124,7 @@
"Ref": "Certificate4E7ABB08"
}
},
"DomainName": "*.example.com",
"DomainName": "example.com",
"EnableAutoSubDomain": false,
"SubDomainSettings": [
{
Expand Down Expand Up @@ -156,10 +156,10 @@
"Certificate4E7ABB08": {
"Type": "AWS::CertificateManager::Certificate",
"Properties": {
"DomainName": "*.*.example.com",
"DomainName": "*.example.com",
"DomainValidationOptions": [
{
"DomainName": "*.*.example.com",
"DomainName": "*.example.com",
"HostedZoneId": "Z23ABC4XYZL05B"
}
],
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5137e38

Please sign in to comment.