2021-04-09 13:46:53 +02:00
|
|
|
// Email address matcher.
|
|
|
|
// eslint-disable-next-line no-useless-escape
|
2023-04-17 09:11:22 +02:00
|
|
|
const matcher =
|
|
|
|
/[A-Z0-9.!#$%&'*+-/=?^_{|}~]+@[A-Z0-9][A-Z0-9.!#$%&'*+-/=?^_{|}~]*\.[A-Z]{2,}$/i;
|
2021-04-09 13:46:53 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Loosely validate an email address.
|
2023-07-05 11:51:27 +02:00
|
|
|
* Max length of an email address is 320 characters: 64 for the local part + 1 for the @ +
|
|
|
|
* 255 for the domain part.
|
|
|
|
* See https://datatracker.ietf.org/doc/html/rfc5321#section-4.5.3.1.1
|
|
|
|
*
|
|
|
|
* Being a bit extra cautious here and limiting the max length to 500 characters, which prevents
|
|
|
|
* [Regular expression Denial of Service - ReDoS](https://owasp.org/www-community/attacks/Regular_expression_Denial_of_Service_-_ReDoS) attacks
|
|
|
|
* due to polynomial regular expression used on uncontrolled data.
|
2021-04-09 13:46:53 +02:00
|
|
|
*
|
|
|
|
* @param {string} string
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
function isEmail(value: string): boolean {
|
2023-07-05 11:51:27 +02:00
|
|
|
if (value.length > 500) return false;
|
2021-04-09 13:46:53 +02:00
|
|
|
return matcher.test(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Exports.
|
|
|
|
*/
|
|
|
|
|
|
|
|
export default isEmail;
|