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

Replace fewer TLDs when normalizing #94

Merged
merged 1 commit into from
Mar 18, 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
7 changes: 2 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@
* Duplicate `.com`s are now removed from email domain names when
`hash_address` is used. For example, `example.com.com` will become
`example.com`.
* Extraneous characters after `.com` are now removed from email domain
names when `hash_address` is used. For example, `example.comfoo` will
become `example.com`.
* Certain `.com` typos are now normalized to `.com` when `hash_address` is
used. For example, `example.cam` will become `example.com`.
* Certain TLD typos are now normalized when `hash_address` is used. For
example, `example.comcom` will become `example.com`.
* Additional `gmail.com` domain names with leading digits are now
normalized when `hash_address` is used. For example, `100gmail.com` will
become `gmail.com`.
Expand Down
59 changes: 57 additions & 2 deletions lib/minfraud/components/email.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,55 @@ def clean_email_address(address)
}.freeze
private_constant :TYPO_DOMAINS

TYPO_TLDS = {
'comm' => 'com',
'commm' => 'com',
'commmm' => 'com',
'comn' => 'com',

'cbm' => 'com',
'ccm' => 'com',
'cdm' => 'com',
'cem' => 'com',
'cfm' => 'com',
'cgm' => 'com',
'chm' => 'com',
'cim' => 'com',
'cjm' => 'com',
'ckm' => 'com',
'clm' => 'com',
'cmm' => 'com',
'cnm' => 'com',
'cpm' => 'com',
'cqm' => 'com',
'crm' => 'com',
'csm' => 'com',
'ctm' => 'com',
'cum' => 'com',
'cvm' => 'com',
'cwm' => 'com',
'cxm' => 'com',
'cym' => 'com',
'czm' => 'com',

'col' => 'com',
'con' => 'com',

'dom' => 'com',
'don' => 'com',
'som' => 'com',
'son' => 'com',
'vom' => 'com',
'von' => 'com',
'xom' => 'com',
'xon' => 'com',

'clam' => 'com',
'colm' => 'com',
'comcom' => 'com',
}.freeze
private_constant :TYPO_TLDS

EQUIVALENT_DOMAINS = {
'googlemail.com' => 'gmail.com',
'pm.me' => 'protonmail.com',
Expand Down Expand Up @@ -330,10 +379,16 @@ def clean_domain(domain)
domain = SimpleIDN.to_ascii(domain)

domain.sub!(/(?:\.com){2,}$/, '.com')
domain.sub!(/\.com[^.]+$/, '.com')
domain.sub!(/(?:\.(?:com|c[a-z]{1,2}m|co[ln]|[dsvx]o[mn]|))$/, '.com')
domain.sub!(/^\d+(?:gmail?\.com)$/, 'gmail.com')

idx = domain.rindex('.')
if !idx.nil?
tld = domain[idx + 1..]
if TYPO_TLDS.key?(tld)
domain = "#{domain[0, idx]}.#{TYPO_TLDS[tld]}"
end
end

if TYPO_DOMAINS.key?(domain)
domain = TYPO_DOMAINS[domain]
end
Expand Down
7 changes: 4 additions & 3 deletions spec/components/email_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
{ input: ' [email protected]', output: '[email protected]' },
{
input: '[email protected]|abc124472372',
output: '[email protected]',
output: '[email protected]|abc124472372',
},
{ input: '[email protected]', output: '[email protected]' },
{ input: '[email protected]', output: '[email protected]' },
Expand All @@ -76,9 +76,10 @@
{ input: '[email protected]', output: '[email protected]' },
{ input: '[email protected]', output: '[email protected]' },
{ input: '[email protected]', output: '[email protected]' },
{ input: '[email protected]', output: 'foo@example.com' },
{ input: '[email protected]', output: 'foo@example.com' },
{ input: '[email protected]', output: 'foo@example.comfoo' },
{ input: '[email protected]', output: 'foo@example.cam' },
{ input: '[email protected]', output: '[email protected]' },
{ input: '[email protected]', output: '[email protected]' },
]

tests.each do |i|
Expand Down