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

fix(cognito): quote or mime-encode fromName to comply RFC 5322 #23227

Merged
merged 4 commits into from Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions packages/@aws-cdk/aws-cognito/README.md
Expand Up @@ -421,6 +421,18 @@ new cognito.UserPool(this, 'myuserpool', {
});
```

If `fromName` does not comply RFC 5322 atom or quoted-string, it will be quoted or mime-encoded.

```ts
new cognito.UserPool(this, 'myuserpool', {
email: cognito.UserPoolEmail.withSES({
fromEmail: 'noreply@myawesomeapp.com',
fromName: 'myname@mycompany.com',
}),
});
// => From: "myname@mycompany.com" <noreply@myawesomeapp.com>
```

### Device Tracking

User pools can be configured to track devices that users have logged in to.
Expand Down
17 changes: 14 additions & 3 deletions packages/@aws-cdk/aws-cognito/lib/user-pool-email.ts
Expand Up @@ -164,9 +164,20 @@ class SESEmail extends UserPoolEmail {
throw new Error('Your stack region cannot be determined so "sesRegion" is required in SESOptions');
}

let from = this.options.fromEmail;
let from = encodeAndTest(this.options.fromEmail);
if (this.options.fromName) {
from = `${this.options.fromName} <${this.options.fromEmail}>`;
let fromName = this.options.fromName;
// see RFC 5322 Section 3.4 for details
Tietew marked this conversation as resolved.
Show resolved Hide resolved
if (/[^\u0020-\u007E]/u.test(fromName)) {
// mime encoding for non US-ASCII characters (RFC 2047)
fromName = `=?UTF-8?B?${Buffer.from(fromName, 'utf-8').toString('base64')}?=`;
} else if (/^(?:"(?:[^\\"]|\\.)+"|[\w !#$%&'*+-\/=?^_`{|}~]+)$/.test(fromName)) {
Tietew marked this conversation as resolved.
Show resolved Hide resolved
// atom or quoted-string
} else {
// make quoted-string
fromName = `"${fromName.replace(/["\\]/g, '\\$&')}"`;
Tietew marked this conversation as resolved.
Show resolved Hide resolved
}
from = `${fromName} <${from}>`;
}

if (this.options.sesVerifiedDomain) {
Expand All @@ -177,7 +188,7 @@ class SESEmail extends UserPoolEmail {
}

return {
from: encodeAndTest(from),
from,
replyToEmailAddress: encodeAndTest(this.options.replyTo),
configurationSet: this.options.configurationSetName,
emailSendingAccount: 'DEVELOPER',
Expand Down
151 changes: 151 additions & 0 deletions packages/@aws-cdk/aws-cognito/test/user-pool.test.ts
Expand Up @@ -1708,7 +1708,158 @@ describe('User Pool', () => {
},
},
});
});

test('email withSES with name as quoted-string', () => {
// GIVEN
const stack = new Stack(undefined, undefined, {
env: {
region: 'us-east-1',
account: '11111111111',
},
});

// WHEN
new UserPool(stack, 'Pool', {
email: UserPoolEmail.withSES({
fromEmail: 'mycustomemail@example.com',
fromName: '"My Custom Email"',
}),
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::Cognito::UserPool', {
EmailConfiguration: {
EmailSendingAccount: 'DEVELOPER',
From: '"My Custom Email" <mycustomemail@example.com>',
SourceArn: {
'Fn::Join': [
'',
[
'arn:',
{
Ref: 'AWS::Partition',
},
':ses:us-east-1:11111111111:identity/mycustomemail@example.com',
],
],
},
},
});
});

test('email withSES with name with non atom characters', () => {
// GIVEN
const stack = new Stack(undefined, undefined, {
env: {
region: 'us-east-1',
account: '11111111111',
},
});

// WHEN
new UserPool(stack, 'Pool', {
email: UserPoolEmail.withSES({
fromEmail: 'mycustomemail@example.com',
fromName: 'mycustomname@example.com',
}),
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::Cognito::UserPool', {
EmailConfiguration: {
EmailSendingAccount: 'DEVELOPER',
From: '"mycustomname@example.com" <mycustomemail@example.com>',
SourceArn: {
'Fn::Join': [
'',
[
'arn:',
{
Ref: 'AWS::Partition',
},
':ses:us-east-1:11111111111:identity/mycustomemail@example.com',
],
],
},
},
});
});

test('email withSES with name with quotes', () => {
// GIVEN
const stack = new Stack(undefined, undefined, {
env: {
region: 'us-east-1',
account: '11111111111',
},
});

// WHEN
new UserPool(stack, 'Pool', {
email: UserPoolEmail.withSES({
fromEmail: 'mycustomemail@example.com',
fromName: 'Foo "Bar" \\Baz',
}),
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::Cognito::UserPool', {
EmailConfiguration: {
EmailSendingAccount: 'DEVELOPER',
From: '"Foo \\"Bar\\" \\\\Baz" <mycustomemail@example.com>',
SourceArn: {
'Fn::Join': [
'',
[
'arn:',
{
Ref: 'AWS::Partition',
},
':ses:us-east-1:11111111111:identity/mycustomemail@example.com',
],
],
},
},
});
});

test('email withSES with name with non US-ASCII characters', () => {
// GIVEN
const stack = new Stack(undefined, undefined, {
env: {
region: 'us-east-1',
account: '11111111111',
},
});

// WHEN
new UserPool(stack, 'Pool', {
email: UserPoolEmail.withSES({
fromEmail: 'mycustomemail@example.com',
fromName: 'あいう',
}),
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::Cognito::UserPool', {
EmailConfiguration: {
EmailSendingAccount: 'DEVELOPER',
From: '=?UTF-8?B?44GC44GE44GG?= <mycustomemail@example.com>',
SourceArn: {
'Fn::Join': [
'',
[
'arn:',
{
Ref: 'AWS::Partition',
},
':ses:us-east-1:11111111111:identity/mycustomemail@example.com',
],
],
},
},
});
});

test('email withSES with valid region', () => {
Expand Down