-
Notifications
You must be signed in to change notification settings - Fork 221
fxa(email): Add email normalization check to email libs #19951
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
Open
vbudhram
wants to merge
1
commit into
main
Choose a base branch
from
fxa-12829
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+226
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,16 +3,20 @@ | |
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
|
||
| import { AppError } from '@fxa/accounts/errors'; | ||
| import { EmailNormalization } from 'fxa-shared/email/email-normalization'; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should not reference |
||
|
|
||
| export type BouncesConfig = { | ||
| enabled: boolean; | ||
| aliasCheckEnabled?: boolean; | ||
| emailAliasNormalization?: string; | ||
| hard: Record<number, number>; | ||
| soft: Record<number, number>; | ||
| complaint: Record<number, number>; | ||
| ignoreTemplates: string[]; | ||
| }; | ||
|
|
||
| export type Bounce = { | ||
| email?: string; | ||
| bounceType: number; | ||
| createdAt: number; | ||
| }; | ||
|
|
@@ -34,6 +38,7 @@ export const BOUNCE_TYPE_COMPLAINT = 3; | |
|
|
||
| export class Bounces { | ||
| private readonly bounceRules: Record<number, any>; | ||
| private readonly emailNormalization: EmailNormalization; | ||
|
|
||
| constructor( | ||
| private readonly config: BouncesConfig, | ||
|
|
@@ -44,6 +49,9 @@ export class Bounces { | |
| [BOUNCE_TYPE_SOFT]: Object.freeze(config.soft || {}), | ||
| [BOUNCE_TYPE_COMPLAINT]: Object.freeze(config.complaint || {}), | ||
| }; | ||
| this.emailNormalization = new EmailNormalization( | ||
| config.emailAliasNormalization || '[]' | ||
| ); | ||
| } | ||
|
|
||
| async check(email: string, template: string) { | ||
|
|
@@ -58,10 +66,64 @@ export class Bounces { | |
| return undefined; | ||
| } | ||
|
|
||
| const bounces = await this.db.emailBounces.findByEmail(email); | ||
| let bounces: Array<Bounce>; | ||
|
|
||
| if (this.config.aliasCheckEnabled) { | ||
| bounces = await this.checkBouncesWithAliases(email); | ||
| } else { | ||
| bounces = await this.db.emailBounces.findByEmail(email); | ||
| } | ||
|
|
||
| return this.applyRules(bounces); | ||
| } | ||
|
|
||
| private async checkBouncesWithAliases(email: string): Promise<Array<Bounce>> { | ||
| // Given an email alias like test+123@domain.com: | ||
| // We look for bounces to the 'root' email -> `test@domain.com` | ||
| // And look for bounces to the alias with a wildcard -> `test+%@domain.com` | ||
| // | ||
| // This prevents us from picking up false positives when we replace the alias | ||
| // with a wildcard, and doesn't miss the root email bounces either. We have to | ||
| // use both because just using the wildcard would miss bounces sent to the root | ||
| // and just using the root with a wildcard would pickup false positives. | ||
| // | ||
| // So, test+123@domain.com would match: | ||
| // - test@domain.com Covered by normalized email | ||
| // - test+123@domain.com Covered by wildcard email | ||
| // - test+asdf@domain.com Covered by wildcard email | ||
| // but not | ||
| // - testing@domain.com Not picked up by wildcard since we include the '+' | ||
| const normalizedEmail = this.emailNormalization.normalizeEmailAliases( | ||
| email, | ||
| '' | ||
| ); | ||
| const wildcardEmail = this.emailNormalization.normalizeEmailAliases( | ||
| email, | ||
| '+%' | ||
| ); | ||
|
|
||
| const [normalizedBounces, wildcardBounces] = await Promise.all([ | ||
| this.db.emailBounces.findByEmail(normalizedEmail), | ||
| this.db.emailBounces.findByEmail(wildcardEmail), | ||
| ]); | ||
|
|
||
| // Merge and deduplicate by email+createdAt | ||
| // There shouldn't be any overlap, but just in case | ||
| const seen = new Set<string>(); | ||
| const merged = [...normalizedBounces, ...wildcardBounces].filter( | ||
| (bounce) => { | ||
| const key = `${bounce.email || ''}:${bounce.createdAt}`; | ||
| if (seen.has(key)) { | ||
| return false; | ||
| } | ||
| seen.add(key); | ||
| return true; | ||
| } | ||
| ); | ||
|
|
||
| return merged.sort((a, b) => b.createdAt - a.createdAt); | ||
| } | ||
|
|
||
| private applyRules(bounces: Array<Bounce>) { | ||
| const tallies: Record<number, any> = { | ||
| [BOUNCE_TYPE_HARD]: { | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test’s description and inline comment state that for non-configured domains, both bounce queries should use the original email, but the expectations only assert that
findByEmailis called twice and that it was called with'test+alias@other.com'at least once. As written, an implementation that uses the original email once and a transformed value the second time would still pass. To fully verify the intended behavior, consider asserting each call explicitly (for example, usingtoHaveBeenNthCalledWithfor both calls, or checking the argument list of all invocations) so the test will fail if the wildcard query ever starts transforming the address for non-configured domains.