From f8926c7b1311e5c1e617289f9ee3760b522bea3f Mon Sep 17 00:00:00 2001 From: Matt Mayer Date: Thu, 9 Mar 2023 04:51:28 +0700 Subject: [PATCH] fix(internet): filter banned dots from email addresses (#1883) --- src/modules/internet/index.ts | 7 +++++++ test/internet.spec.ts | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/modules/internet/index.ts b/src/modules/internet/index.ts index 2623f8b3f3f..c0355209b86 100644 --- a/src/modules/internet/index.ts +++ b/src/modules/internet/index.ts @@ -109,6 +109,13 @@ export class InternetModule { ); } + // local parts may not contain two or more consecutive . characters + localPart = localPart.replace(/\.{2,}/g, '.'); + + // local parts may not start with or end with a . character + localPart = localPart.replace(/^\./, ''); + localPart = localPart.replace(/\.$/, ''); + return `${localPart}@${provider}`; } diff --git a/test/internet.spec.ts b/test/internet.spec.ts index c2d8fcb22d8..0730f384b62 100644 --- a/test/internet.spec.ts +++ b/test/internet.spec.ts @@ -133,6 +133,30 @@ describe('internet', () => { expect(faker.definitions.internet.free_email).toContain(suffix); }); + it('should not allow an email that starts or ends with a .', () => { + const email = faker.internet.email('...Aiden...', '...Doe...'); + + expect(email).toBeTruthy(); + expect(email).toBeTypeOf('string'); + expect(email).toSatisfy(validator.isEmail); + + const [prefix] = email.split('@'); + expect(prefix).not.toMatch(/^\./); + expect(prefix).not.toMatch(/\.$/); + }); + + it('should not allow an email with multiple dots', () => { + const email = faker.internet.email('Ai....den'); + + expect(email).toBeTruthy(); + expect(email).toBeTypeOf('string'); + expect(email).toSatisfy(validator.isEmail); + + const [prefix] = email.split('@'); + //expect it not to contain multiple .s + expect(prefix).not.toMatch(/\.{2,}/); + }); + it('should return an email with given firstName and lastName', () => { const email = faker.internet.email('Aiden', 'Harann');