Skip to content

Commit

Permalink
Mail: EAI: Change email address regexp to allow Unicode letters, but …
Browse files Browse the repository at this point in the history
…not special
  • Loading branch information
benbucksch committed Nov 23, 2024
1 parent fe3cd84 commit 74dce04
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
65 changes: 65 additions & 0 deletions app/logic/util/sanitizeDatatypes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { sanitize } from '../../../lib/util/sanitizeDatatypes';
import { expect, test } from 'vitest';

test("Check Latin email addresses", () => {
sanitize.emailAddress("dh@example.com");
sanitize.emailAddress("dh@example.im");
sanitize.emailAddress("dh@example.mobile");
sanitize.emailAddress("dh@example.im");
sanitize.emailAddress("dh@ex-ample.im");
sanitize.emailAddress("dh+g@example.im");
sanitize.emailAddress("dh.g@example.im");
sanitize.emailAddress("dh-g@example.im");
expect(() => {
sanitize.emailAddress("d@com");
}).toThrow();
expect(() => {
sanitize.emailAddress("d@example.c-om");
}).toThrow();
expect(() => {
sanitize.emailAddress("d@example.-com");
}).toThrow();
expect(() => {
sanitize.emailAddress("d@example.com-");
}).toThrow();
expect(() => {
sanitize.emailAddress("d#h@example.com");
}).toThrow();
expect(() => {
sanitize.emailAddress("d h@example.com");
}).toThrow();
expect(() => {
sanitize.emailAddress("d%20h@example.com");
}).toThrow();
expect(() => {
sanitize.emailAddress("d\\h@example.com");
}).toThrow();
expect(() => {
sanitize.emailAddress("d\nh@example.com");
}).toThrow();
expect(() => {
sanitize.emailAddress("d\rh@example.com");
}).toThrow();
expect(() => {
sanitize.emailAddress("d\th@example.com");
}).toThrow();
expect(() => {
sanitize.emailAddress("d`h@example.com");
}).toThrow();
});

test("Check EAI email addresses", () => {
sanitize.emailAddress("ндрис@уайлддк.орг");
sanitize.emailAddress("ндрис@уайлддк.xn--4dn5");
sanitize.emailAddress("нд1²³рис@уай1²³лддк.орг");
sanitize.emailAddress("äüößéáúàèù@äüößéáúàèù.com");
expect(() => {
sanitize.emailAddress("d@d.xn-4dn5");
}).toThrow();
expect(() => {
sanitize.emailAddress("d@d.xn--");
}).toThrow();
expect(() => {
sanitize.emailAddress("d@d.xn-");
}).toThrow();
});
5 changes: 4 additions & 1 deletion lib/util/sanitizeDatatypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ class Sanitize {
return str;
}

// EAI like ндрис@уайлддк.орг need to pass
regexpEMailAddress = /^[\p{Letter}\p{Number}\-+_\.]+@[\p{Letter}\p{Number}\-\.]+\.(?:[\p{Letter}]+|xn--[a-z0-9]+)$/u;

/**
* Email address foo@bar.com
*/
Expand All @@ -120,7 +123,7 @@ class Sanitize {
return haveError("Missing email address", unchecked, fallback);
}
let str = String(unchecked);
if (!/^[a-zA-Z0-9\-%+_\.\*]+@[a-z0-9\-\.]+\.[a-z]+$/i.test(str)) {
if (!this.regexpEMailAddress.test(str)) {
return haveError("Not an email address", unchecked, fallback);
}
return str.toLowerCase();
Expand Down

0 comments on commit 74dce04

Please sign in to comment.