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 all 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
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