5629 update blocklist for messaging v2 (#5756)

Closes #5629 

- Add subdomain support in blocklist (if @example.com is blocked, every
subdomain will be blocked)
This commit is contained in:
bosiraphael
2024-06-13 07:53:28 +02:00
committed by GitHub
parent 374237a988
commit f825bea071
11 changed files with 165 additions and 113 deletions

View File

@ -2,38 +2,68 @@ import { isEmailBlocklisted } from 'src/modules/calendar-messaging-participant/u
describe('isEmailBlocklisted', () => {
it('should return true if email is blocklisted', () => {
const email = 'hello@example.com';
const blocklist = ['hello@example.com', 'hey@example.com'];
const result = isEmailBlocklisted(email, blocklist);
const channelHandle = 'abc@example.com';
const email = 'hello@twenty.com';
const blocklist = ['hello@twenty.com', 'hey@twenty.com'];
const result = isEmailBlocklisted(channelHandle, email, blocklist);
expect(result).toBe(true);
});
it('should return false if email is not blocklisted', () => {
const channelHandle = 'abc@example.com';
const email = 'hello@twenty.com';
const blocklist = ['hey@example.com'];
const result = isEmailBlocklisted(email, blocklist);
const result = isEmailBlocklisted(channelHandle, email, blocklist);
expect(result).toBe(false);
});
it('should return false if email is null', () => {
const channelHandle = 'abc@twenty.com';
const email = null;
const blocklist = ['@example.com'];
const result = isEmailBlocklisted(email, blocklist);
const result = isEmailBlocklisted(channelHandle, email, blocklist);
expect(result).toBe(false);
});
it('should return true for subdomains', () => {
const channelHandle = 'abc@example.com';
const email = 'hello@twenty.twenty.com';
const blocklist = ['@twenty.com'];
const result = isEmailBlocklisted(channelHandle, email, blocklist);
expect(result).toBe(true);
});
it('should return false for domains which end with blocklisted domain but are not subdomains', () => {
const channelHandle = 'abc@example.com';
const email = 'hello@twentytwenty.com';
const blocklist = ['@twenty.com'];
const result = isEmailBlocklisted(channelHandle, email, blocklist);
expect(result).toBe(false);
});
it('should return false if email is undefined', () => {
const channelHandle = 'abc@example.com';
const email = undefined;
const blocklist = ['@example.com'];
const result = isEmailBlocklisted(email, blocklist);
const blocklist = ['@twenty.com'];
const result = isEmailBlocklisted(channelHandle, email, blocklist);
expect(result).toBe(false);
});
it('should return true if email ends with blocklisted domain', () => {
const email = 'hello@example.com';
const blocklist = ['@example.com'];
const result = isEmailBlocklisted(email, blocklist);
const channelHandle = 'abc@example.com';
const email = 'hello@twenty.com';
const blocklist = ['@twenty.com'];
const result = isEmailBlocklisted(channelHandle, email, blocklist);
expect(result).toBe(true);
});
it('should return false if email is same as channel handle', () => {
const channelHandle = 'hello@twenty.com';
const email = 'hello@twenty.com';
const blocklist = ['@twenty.com'];
const result = isEmailBlocklisted(channelHandle, email, blocklist);
expect(result).toBe(false);
});
});

View File

@ -1,14 +1,17 @@
export const isEmailBlocklisted = (
channelHandle: string,
email: string | null | undefined,
blocklist: string[],
): boolean => {
if (!email) {
if (!email || email === channelHandle) {
return false;
}
return blocklist.some((item) => {
if (item.startsWith('@')) {
return email.endsWith(item);
const domain = email.split('@')[1];
return domain === item.slice(1) || domain.endsWith(`.${item.slice(1)}`);
}
return email === item;