e94473a1ce
* [web] double down on validating email addresses * [web] normalize emails in captcha middleware * [web] add support for regex based allow-list for skipping captcha * [web] skip captcha for trusted users on all actions GitOrigin-RevId: a994ebf6b74e80f462d2dab1fe5113bbffa676a9
43 lines
1.1 KiB
JavaScript
43 lines
1.1 KiB
JavaScript
const { parseOneAddress } = require('email-addresses')
|
|
|
|
// available for frontend in https://github.com/overleaf/internal/blob/19d432c70b173752ee7c6d8978dd6be16b042921/services/web/frontend/js/shared/utils/email.tsx#L4
|
|
const EMAIL_REGEXP =
|
|
// eslint-disable-next-line no-useless-escape
|
|
/^([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
|
|
|
|
function getDomain(email) {
|
|
email = parseEmail(email)
|
|
return email ? email.split('@').pop() : null
|
|
}
|
|
|
|
function parseEmail(email, parseRfcAddress = false) {
|
|
if (typeof email !== 'string' || !email) {
|
|
return null
|
|
}
|
|
|
|
if (parseRfcAddress) {
|
|
const result = parseOneAddress(email)
|
|
if (!result?.address) {
|
|
return null
|
|
}
|
|
email = result.address
|
|
}
|
|
|
|
if (email.length > 254) {
|
|
return null
|
|
}
|
|
email = email.trim().toLowerCase()
|
|
|
|
const matched = email.match(EMAIL_REGEXP)
|
|
if (matched == null || matched[0] == null) {
|
|
return null
|
|
}
|
|
|
|
return matched[0]
|
|
}
|
|
|
|
module.exports = {
|
|
getDomain,
|
|
parseEmail,
|
|
}
|