Skip to content

Commit

Permalink
fix(internet): filter banned dots from email addresses (#1883)
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Mayer committed Mar 8, 2023
1 parent 4f14533 commit f8926c7
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/modules/internet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
}

Expand Down
24 changes: 24 additions & 0 deletions test/internet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down

0 comments on commit f8926c7

Please sign in to comment.