Skip to content

Commit f8926c7

Browse files
author
Matt Mayer
authored
fix(internet): filter banned dots from email addresses (#1883)
1 parent 4f14533 commit f8926c7

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

src/modules/internet/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ export class InternetModule {
109109
);
110110
}
111111

112+
// local parts may not contain two or more consecutive . characters
113+
localPart = localPart.replace(/\.{2,}/g, '.');
114+
115+
// local parts may not start with or end with a . character
116+
localPart = localPart.replace(/^\./, '');
117+
localPart = localPart.replace(/\.$/, '');
118+
112119
return `${localPart}@${provider}`;
113120
}
114121

test/internet.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,30 @@ describe('internet', () => {
133133
expect(faker.definitions.internet.free_email).toContain(suffix);
134134
});
135135

136+
it('should not allow an email that starts or ends with a .', () => {
137+
const email = faker.internet.email('...Aiden...', '...Doe...');
138+
139+
expect(email).toBeTruthy();
140+
expect(email).toBeTypeOf('string');
141+
expect(email).toSatisfy(validator.isEmail);
142+
143+
const [prefix] = email.split('@');
144+
expect(prefix).not.toMatch(/^\./);
145+
expect(prefix).not.toMatch(/\.$/);
146+
});
147+
148+
it('should not allow an email with multiple dots', () => {
149+
const email = faker.internet.email('Ai....den');
150+
151+
expect(email).toBeTruthy();
152+
expect(email).toBeTypeOf('string');
153+
expect(email).toSatisfy(validator.isEmail);
154+
155+
const [prefix] = email.split('@');
156+
//expect it not to contain multiple .s
157+
expect(prefix).not.toMatch(/\.{2,}/);
158+
});
159+
136160
it('should return an email with given firstName and lastName', () => {
137161
const email = faker.internet.email('Aiden', 'Harann');
138162

0 commit comments

Comments
 (0)