Skip to content

Commit

Permalink
Bypass email validation for deleted emails
Browse files Browse the repository at this point in the history
  • Loading branch information
Megamind51 committed Jan 8, 2025
1 parent 328d659 commit 5bcdbce
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
29 changes: 25 additions & 4 deletions src/asserts/email-assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@
const { Assert: is, Validator, Violation } = require('validator.js');
let validator;

/**
* Deleted email regex.
*
* The email should contain the following pattern:
* [email protected]
*
* example: [email protected]
*/

const regex = /.+@\/\w+\.\w+\.\d+\.deleted$/;

/**
* Optional peer dependencies.
*/
Expand Down Expand Up @@ -41,16 +52,26 @@ module.exports = function emailAssert() {
throw new Violation(this, value, { value: Validator.errorCode.must_be_a_string });
}

if (!validator.isEmail(value)) {
throw new Violation(this, value);
}

try {
is.ofLength({ max: 254 }).validate(value);
} catch (e) {
throw new Violation(this, value);
}

// We are bypassing the email validation for deleted users.
// This is needed because we have legacy users with invalid emails
// and as part of the deletion flow for expired signups we need to allow
// the deletion of that kind of users. The process of deleting an
// user changes their email from user.email to
// ${user.email}.${timestamp}.deleted so this assert runs on that update.
if (regex.test(value) === true) {
return true;
}

if (!validator.isEmail(value)) {
throw new Violation(this, value);
}

return true;
};

Expand Down
29 changes: 28 additions & 1 deletion test/asserts/email-assert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,35 @@ describe('EmailAssert', () => {
}
});

it('should thrown an error on invalid emails', () => {
[
'@foo.com.123.deleted',
'[email protected]',
'[email protected]',
'føø@båz.',
'føø@båz..123.deleted',
'foo+bar@baz_foo.com',
'foo+bar@baz_foo.com.12345.deleted'
].forEach(choice => {
try {
Assert.prototype.Email().validate(choice);

fail();
} catch (e) {
expect(e.show().assert).toBe('Email');
}
});
});

it('should accept valid emails', () => {
['[email protected]', 'føø@båz.com', '[email protected]'].forEach(choice => {
[
'[email protected]',
'[email protected]',
'føø@båz.com',
'føø@båz.com.123.deleted',
'[email protected]',
'[email protected]'
].forEach(choice => {
Assert.prototype.Email().validate(choice);
});
});
Expand Down

0 comments on commit 5bcdbce

Please sign in to comment.