Files
calendar/packages/features/watchlist/lib/utils/normalization.ts
T
Syed Ali ShahbazandGitHub a2bee76da6 feat: Add spam blocker DI structure (#24040)
* --init

* --

* replace old structure with new DI

* fix type

* --

* moving stuff around

* moving stuff around again

* minor clean up

* --

* resolve conflict

* clea up

* old schema clean up

* further clean up

* removing unwanted merged responsibilities

* --

* improve DI and SOLID

* some type fixes

* --1

* clean up --cont

* fix DI in facade for test

* more fix

* fix

* checking

* o.o

* fix import

* fix failing test --2

* fix failing test --3

* fix failing test --4

* normalization use

* introduce facade container injection

* uniform async telemetry spans

* further improvements

* replace prismock with repo mocks

* ensure we don't pass prisma outside of repo

* fix import

* remove try catch from repo calls

* async await fixes

* using deps pattern

* more clean up and fixes

* more clean up

* address feedback --1

* separation of concern

* clean up

* test clean up

* feedback --2

* remove extra fetch

* remove await

* migrate _post test from prismock

* fix type

* --

* fix tokens path

* rename AuditRepo

* test --1

* update _post to integration test

* fix test

* fix test

* test fix maybe?

* --

* feedback

* feedback

* fixes

* more feedback

* NIT

* use sentry and logger imports as planned

* assertion in test

* add missing test case

* add tests for controllers and services

* NITs

* fix domain normalisation
2025-10-14 10:16:18 +03:00

94 lines
2.5 KiB
TypeScript

import { emailRegex } from "@calcom/lib/emailSchema";
/**
* Centralized normalization utilities for emails and domains
*
*/
/**
* Normalizes an email address for consistent comparison
*
* Rules applied:
* 1. Convert to lowercase
* 2. Trim whitespace
* 3. Validate basic email format
*
* @param email - Raw email address
* @returns Normalized email address
* @throws Error if email format is invalid
*/
export function normalizeEmail(email: string): string {
const normalized = email.trim().toLowerCase();
if (!emailRegex.test(normalized)) {
throw new Error(`Invalid email format: ${email}`);
}
return normalized;
}
/**
* Normalizes a domain for consistent comparison
*
* Rules applied:
* 1. Convert to lowercase
* 2. Trim whitespace
* 3. Ensure proper @ prefix for domain entries
*
* Note: Domains are stored AS-IS with @ prefix (e.g., @mail.google.com, @example.co.uk)
* No subdomain stripping is performed to avoid multi-level TLD issues.
* If you want to block subdomains separately, create separate entries.
*
* @param domain - Raw domain (with or without @ prefix)
* @returns Normalized domain with @ prefix
*/
export function normalizeDomain(domain: string): string {
let normalized = domain.trim().toLowerCase();
if (normalized.startsWith("@")) {
normalized = normalized.slice(1);
}
const domainRegex =
/^[a-zA-Z0-9\u00a1-\uffff]([a-zA-Z0-9\u00a1-\uffff-]*[a-zA-Z0-9\u00a1-\uffff])?(\.[a-zA-Z0-9\u00a1-\uffff]([a-zA-Z0-9\u00a1-\uffff-]*[a-zA-Z0-9\u00a1-\uffff])?)*$/;
if (!domainRegex.test(normalized)) {
throw new Error(`Invalid domain format: ${domain}`);
}
return `@${normalized}`;
}
/**
* Extracts and normalizes domain from an email address
*
* @param email - Email address
* @returns Normalized domain with @ prefix
*/
export function extractDomainFromEmail(email: string): string {
const normalizedEmail = normalizeEmail(email);
const domain = normalizedEmail.split("@")[1];
if (!domain) {
throw new Error(`Could not extract domain from email: ${email}`);
}
return normalizeDomain(domain);
}
/**
* Normalizes a username for consistent comparison
*
* Rules applied:
* 1. Convert to lowercase
* 2. Trim whitespace
*
* @param username - Raw username
* @returns Normalized username
*/
export function normalizeUsername(username: string): string {
if (!username || typeof username !== "string") {
throw new Error("Invalid username: must be a non-empty string");
}
return username.trim().toLowerCase();
}