Relax constraints for UserID email address validity

New checks align with the HTML5 W3C spec and should be more lax than the existing ones (meaning, addresses which passed validation before should continue to be valid).
Addresses such as @localhost are now allowed too, since presence of "." is no longer enforced.

These checks should not be considered exhaustive: library users are encouraged to implement separate checks for email validity if needed.
This commit is contained in:
larabr 2023-05-15 13:28:15 +02:00
parent 591b9399a8
commit 7d7a8dc113
3 changed files with 18 additions and 17 deletions

View File

@ -457,11 +457,16 @@ const util = {
return os.cpus().length; return os.cpus().length;
}, },
/**
* Test email format based on W3C HTML5 specification.
* This check is not exaustive, and includes a willful violation of RFC5322
* (see 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 = /^(([^<>()[\]\\.,;:\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,}[0-9]*|xn--[a-zA-Z\-0-9]+)))$/; 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])?)*$/;
return re.test(data); return re.test(data);
}, },

View File

@ -958,7 +958,7 @@ export default () => describe('OpenPGP.js public api tests', function() {
await expect(test).to.eventually.be.rejectedWith(/Invalid user ID format/); await expect(test).to.eventually.be.rejectedWith(/Invalid user ID format/);
}); });
it('should fail for invalid user email address', async function() { it('should fail for invalid user email address (missing @)', async function() {
const opt = { const opt = {
userIDs: [{ name: 'Test User', email: 'textexample.com' }] userIDs: [{ name: 'Test User', email: 'textexample.com' }]
}; };
@ -966,14 +966,6 @@ export default () => describe('OpenPGP.js public api tests', function() {
await expect(test).to.eventually.be.rejectedWith(/Invalid user ID format/); await expect(test).to.eventually.be.rejectedWith(/Invalid user ID format/);
}); });
it('should fail for invalid user email address', async function() {
const opt = {
userIDs: [{ name: 'Test User', email: 'text@examplecom' }]
};
const test = openpgp.generateKey(opt);
await expect(test).to.eventually.be.rejectedWith(/Invalid user ID format/);
});
it('should fail for string user ID', async function() { it('should fail for string user ID', async function() {
const opt = { const opt = {
userIDs: 'Test User <text@example.com>' userIDs: 'Test User <text@example.com>'

View File

@ -108,19 +108,23 @@ export default () => describe('Util unit tests', function() {
const data = 'test@example.com'; const data = 'test@example.com';
expect(util.isEmailAddress(data)).to.be.true; expect(util.isEmailAddress(data)).to.be.true;
}); });
it('should return true for valid email address', function() { it('should return true for valid email address (-- in domain part)', function() {
const data = 'test@xn--wgv.xn--q9jyb4c'; const data = 'test@xn--wgv.xn--q9jyb4c';
expect(util.isEmailAddress(data)).to.be.true; expect(util.isEmailAddress(data)).to.be.true;
}); });
it('should return false for invalid email address', function() { it('should return true for valid email address (trailing numbers in domain)', function() {
const data = 'test1@com.com09';
expect(util.isEmailAddress(data)).to.be.true;
});
it('should return false for invalid email address (no . in domain part)', function() {
const data = 'test@examplecom';
expect(util.isEmailAddress(data)).to.be.true;
});
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;
}); });
it('should return false for invalid email address', function() { it('should return false for invalid email address (missing @)', function() {
const data = 'test@examplecom';
expect(util.isEmailAddress(data)).to.be.false;
});
it('should return false for invalid email address', function() {
const data = 'testexamplecom'; const data = 'testexamplecom';
expect(util.isEmailAddress(data)).to.be.false; expect(util.isEmailAddress(data)).to.be.false;
}); });