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

Normalize email addresses to NFC #170

Merged
merged 3 commits into from
Apr 12, 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ CHANGELOG
become `gmail.com`.
* Additional `gmail.com` typos are now normalized when `hashEmail` is used.
For example, `gmali.com` will become `gmail.com`.
* When `hashEmail` is used, the local part of an email address is now
normalized to NFC.

2.0.0 (2023-12-04)
------------------
Expand Down
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
"squizlabs/php_codesniffer": "*",
"phpstan/phpstan": "*"
},
"suggest": {
"ext-intl": "Enables improved email address IDN normalization"
},
"autoload": {
"psr-4": {
"MaxMind\\": "src"
Expand Down
4 changes: 4 additions & 0 deletions src/MinFraud/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,10 @@ public static function maybeHashEmail(array $values): array
$domain = self::cleanDomain(substr($address, $atIdx + 1));
$localPart = substr($address, 0, $atIdx);

if (class_exists('Normalizer')) {
$localPart = \Normalizer::normalize($localPart, \Normalizer::FORM_C);
}

if ($domain !== '' && !isset($values['email']['domain'])) {
$values['email']['domain'] = $domain;
}
Expand Down
40 changes: 40 additions & 0 deletions tests/MaxMind/Test/MinFraud/UtilTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,46 @@ public function testMaybeHashEmail(): void
],
],
],
[
'name' => 'email domain nfc normalization form 1',
'input' => ['email' => ['address' => "example@bu\u{0308}cher.com"]],
'expected' => [
'email' => [
'address' => '2b21bc76dab3c8b1622837c1d698936c',
'domain' => 'xn--bcher-kva.com',
],
],
],
[
'name' => 'email domain nfc normalization form 2',
'input' => ['email' => ['address' => "example@b\u{00FC}cher.com"]],
'expected' => [
'email' => [
'address' => '2b21bc76dab3c8b1622837c1d698936c',
'domain' => 'xn--bcher-kva.com',
],
],
],
[
'name' => 'email local part nfc normalization form 1',
'input' => ['email' => ['address' => "bu\u{0308}[email protected]"]],
'expected' => [
'email' => [
'address' => '53550c712b146287a2d0dd30e5ed6f4b',
'domain' => 'example.com',
],
],
],
[
'name' => 'email local part nfc normalization form 2',
'input' => ['email' => ['address' => "b\u{00FC}[email protected]"]],
'expected' => [
'email' => [
'address' => '53550c712b146287a2d0dd30e5ed6f4b',
'domain' => 'example.com',
],
],
],
);
}

Expand Down