Skip to content

Commit 5137e38

Browse files
chore(amplify): domain name validation (#31959)
### 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*
1 parent aee1b30 commit 5137e38

10 files changed

+130
-75
lines changed

packages/@aws-cdk/aws-amplify-alpha/lib/domain.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as acm from 'aws-cdk-lib/aws-certificatemanager';
22
import * as iam from 'aws-cdk-lib/aws-iam';
3-
import { Lazy, Resource, IResolvable } from 'aws-cdk-lib/core';
3+
import { Lazy, Resource, IResolvable, Token } from 'aws-cdk-lib/core';
44
import { Construct } from 'constructs';
55
import { CfnDomain } from 'aws-cdk-lib/aws-amplify';
66
import { IApp } from './app';
@@ -131,6 +131,13 @@ export class Domain extends Resource {
131131
this.subDomains = props.subDomains || [];
132132

133133
const domainName = props.domainName || id;
134+
if (!Token.isUnresolved(domainName) && domainName.length > 255) {
135+
throw new Error(`Domain name must be 255 characters or less, got: ${domainName.length} characters.`);
136+
}
137+
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)) {
138+
throw new Error(`Domain name must be a valid hostname, got: ${domainName}.`);
139+
}
140+
134141
const domain = new CfnDomain(this, 'Resource', {
135142
appId: props.app.appId,
136143
domainName,

packages/@aws-cdk/aws-amplify-alpha/test/domain.test.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,55 @@ test('map a branch to the domain root', () => {
176176
});
177177
});
178178

179+
test('throw error for invalid domain name length', () => {
180+
const stack = new Stack();
181+
const app = new amplify.App(stack, 'App', {
182+
sourceCodeProvider: new amplify.GitHubSourceCodeProvider({
183+
owner: 'aws',
184+
repository: 'aws-cdk',
185+
oauthToken: SecretValue.unsafePlainText('secret'),
186+
}),
187+
});
188+
const prodBranch = app.addBranch('main');
189+
190+
expect(() => app.addDomain('Domain', {
191+
subDomains: [
192+
{
193+
branch: prodBranch,
194+
prefix: 'prod',
195+
},
196+
],
197+
domainName: 'a'.repeat(256),
198+
})).toThrow('Domain name must be 255 characters or less, got: 256 characters.');
199+
});
200+
201+
test.each([
202+
'-example.com',
203+
'example..com',
204+
'example.com-',
205+
206+
])('throw error for invalid domain name', (domainName) => {
207+
const stack = new Stack();
208+
const app = new amplify.App(stack, 'App', {
209+
sourceCodeProvider: new amplify.GitHubSourceCodeProvider({
210+
owner: 'aws',
211+
repository: 'aws-cdk',
212+
oauthToken: SecretValue.unsafePlainText('secret'),
213+
}),
214+
});
215+
const prodBranch = app.addBranch('main');
216+
217+
expect(() => app.addDomain('Domain', {
218+
subDomains: [
219+
{
220+
branch: prodBranch,
221+
prefix: 'prod',
222+
},
223+
],
224+
domainName,
225+
})).toThrow(`Domain name must be a valid hostname, got: ${domainName}.`);
226+
});
227+
179228
test('throws at synthesis without subdomains', () => {
180229
// GIVEN
181230
const app = new App();

packages/@aws-cdk/aws-amplify-alpha/test/integ.app-custom-domain.js.snapshot/amplifyappcustomdomainintegDefaultTestDeployAssert5F8CD1EB.assets.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/aws-amplify-alpha/test/integ.app-custom-domain.js.snapshot/cdk-amplify-app-custom-domain.assets.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/aws-amplify-alpha/test/integ.app-custom-domain.js.snapshot/cdk-amplify-app-custom-domain.template.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
"EnablePullRequestPreview": true
100100
}
101101
},
102-
"Appexamplecom6AF1A3AD": {
102+
"Appexamplecom93D8EC68": {
103103
"Type": "AWS::Amplify::Domain",
104104
"Properties": {
105105
"AppId": {
@@ -124,7 +124,7 @@
124124
"Ref": "Certificate4E7ABB08"
125125
}
126126
},
127-
"DomainName": "*.example.com",
127+
"DomainName": "example.com",
128128
"EnableAutoSubDomain": false,
129129
"SubDomainSettings": [
130130
{
@@ -156,10 +156,10 @@
156156
"Certificate4E7ABB08": {
157157
"Type": "AWS::CertificateManager::Certificate",
158158
"Properties": {
159-
"DomainName": "*.*.example.com",
159+
"DomainName": "*.example.com",
160160
"DomainValidationOptions": [
161161
{
162-
"DomainName": "*.*.example.com",
162+
"DomainName": "*.example.com",
163163
"HostedZoneId": "Z23ABC4XYZL05B"
164164
}
165165
],

packages/@aws-cdk/aws-amplify-alpha/test/integ.app-custom-domain.js.snapshot/cdk.out

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/aws-amplify-alpha/test/integ.app-custom-domain.js.snapshot/integ.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/@aws-cdk/aws-amplify-alpha/test/integ.app-custom-domain.js.snapshot/manifest.json

Lines changed: 7 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)