mirror of
https://github.com/openpgpjs/openpgpjs.git
synced 2025-11-24 14:35:51 +00:00
Fix email address validity check to still allow unicode values, and further relax constraints
We relaxed constraints in a previous commit, but excluded unicode chars, which are however allowed in v5. We now also drop almost all constraints, by rejecting primarily control chars and whitespace.
This commit is contained in:
parent
c68bd960ce
commit
968efdbaa5
13
src/util.js
13
src/util.js
@ -465,16 +465,19 @@ const util = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test email format based on W3C HTML5 specification.
|
* Test email format to ensure basic compliance:
|
||||||
* This check is not exaustive, and does not match RFC 5322 exactly
|
* - must include a single @
|
||||||
* (see https://html.spec.whatwg.org/multipage/input.html#email-state-(type=email)),
|
* - no control or space unicode chars allowed
|
||||||
* but is commonly used for email address validation.
|
* - no backslash and square brackets (as the latter can mess with the userID parsing)
|
||||||
|
* - cannot end with a punctuation char
|
||||||
|
* These checks are not meant to be exhaustive; applications are strongly encouraged to implement stricter validation,
|
||||||
|
* e.g. based on the W3C HTML spec (https://html.spec.whatwg.org/multipage/input.html#email-state-(type=email)).
|
||||||
*/
|
*/
|
||||||
isEmailAddress: function(data) {
|
isEmailAddress: function(data) {
|
||||||
if (!util.isString(data)) {
|
if (!util.isString(data)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
const re = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
|
const re = /^[^\p{C}\p{Z}@<>\\]+@[^\p{C}\p{Z}@<>\\]+[^\p{C}\p{Z}\p{P}]$/u;
|
||||||
return re.test(data);
|
return re.test(data);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
@ -120,6 +120,10 @@ export default () => describe('Util unit tests', function() {
|
|||||||
const data = 'test@localhost';
|
const data = 'test@localhost';
|
||||||
expect(util.isEmailAddress(data)).to.be.true;
|
expect(util.isEmailAddress(data)).to.be.true;
|
||||||
});
|
});
|
||||||
|
it('should return true for valid email address (unicode chars)', function() {
|
||||||
|
const data = '🙂@localhost';
|
||||||
|
expect(util.isEmailAddress(data)).to.be.true;
|
||||||
|
});
|
||||||
it('should return false for invalid email address (full userID)', function() {
|
it('should return false for invalid email address (full userID)', function() {
|
||||||
const data = 'Test User <test@example.com>';
|
const data = 'Test User <test@example.com>';
|
||||||
expect(util.isEmailAddress(data)).to.be.false;
|
expect(util.isEmailAddress(data)).to.be.false;
|
||||||
@ -128,6 +132,18 @@ export default () => describe('Util unit tests', function() {
|
|||||||
const data = 'testexamplecom';
|
const data = 'testexamplecom';
|
||||||
expect(util.isEmailAddress(data)).to.be.false;
|
expect(util.isEmailAddress(data)).to.be.false;
|
||||||
});
|
});
|
||||||
|
it('should return false for invalid email address (invisible unicode control char)', function() {
|
||||||
|
const data = 'test\u{feff}ctrl@email.it';
|
||||||
|
expect(util.isEmailAddress(data)).to.be.false;
|
||||||
|
});
|
||||||
|
it('should return false for invalid email address (trailing punctuation)', function() {
|
||||||
|
const data = 'test@localhost.';
|
||||||
|
expect(util.isEmailAddress(data)).to.be.false;
|
||||||
|
});
|
||||||
|
it('should return false for invalid email address (including whitespace)', function() {
|
||||||
|
const data = 'test space@email.it';
|
||||||
|
expect(util.isEmailAddress(data)).to.be.false;
|
||||||
|
});
|
||||||
it('should return false for empty string', function() {
|
it('should return false for empty string', function() {
|
||||||
const data = '';
|
const data = '';
|
||||||
expect(util.isEmailAddress(data)).to.be.false;
|
expect(util.isEmailAddress(data)).to.be.false;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user