From f9d077209ffd4a56264c026b77e1e24c13181366 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gast=C3=B3n=20Fournier?= Date: Fri, 9 Aug 2024 19:18:19 +0200 Subject: [PATCH] fix: after encryption some emails end up being too long (#7828) Encountered this case after encrypting an already long email address. This should mitigate the issue in demo instance. I don't think it's a big issue to ignore the length when validating an email address cause this is already limited at the DB layer by the column length --- src/lib/services/user-service.ts | 12 ++++++++++-- src/lib/types/user.test.ts | 13 +++++++++++-- src/lib/types/user.ts | 2 +- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/lib/services/user-service.ts b/src/lib/services/user-service.ts index d090918792..00752d8149 100644 --- a/src/lib/services/user-service.ts +++ b/src/lib/services/user-service.ts @@ -220,7 +220,11 @@ class UserService { } if (email) { - Joi.assert(email, Joi.string().email(), 'Email'); + Joi.assert( + email, + Joi.string().email({ ignoreLength: true }), + 'Email', + ); } const exists = await this.store.hasUser({ username, email }); @@ -264,7 +268,11 @@ class UserService { const preUser = await this.getUser(id); if (email) { - Joi.assert(email, Joi.string().email(), 'Email'); + Joi.assert( + email, + Joi.string().email({ ignoreLength: true }), + 'Email', + ); } if (rootRole) { diff --git a/src/lib/types/user.test.ts b/src/lib/types/user.test.ts index d6cbfd31cb..b545919b4d 100644 --- a/src/lib/types/user.test.ts +++ b/src/lib/types/user.test.ts @@ -39,8 +39,17 @@ test('Should create user with only email defined', () => { test('Should require valid email', () => { expect(() => { - new User({ id: 11, email: 'some@' }); // eslint-disable-line - }).toThrowError(Error('Email "value" must be a valid email')); + new User({ id: 11, email: 'some@' }); + }).toThrow(Error('Email "value" must be a valid email')); +}); + +test('Should allow long emails on demo', () => { + expect(() => { + new User({ + id: 11, + email: '0a1c1b6a59a582fdbe853739eefc599dxd1eb365eee385e345b5fc41f59172022a8f69c09f61121d8b4a155b792314ee@unleash.run', + }); + }).not.toThrow(); }); test('Should create user with only username defined', () => { diff --git a/src/lib/types/user.ts b/src/lib/types/user.ts index 618d4e815c..5569daf856 100644 --- a/src/lib/types/user.ts +++ b/src/lib/types/user.ts @@ -83,7 +83,7 @@ export default class User implements IUser { if (!id) { throw new ValidationError('Id is required', [], undefined); } - Joi.assert(email, Joi.string().email(), 'Email'); + Joi.assert(email, Joi.string().email({ ignoreLength: true }), 'Email'); Joi.assert(username, Joi.string(), 'Username'); Joi.assert(name, Joi.string(), 'Name');