From 5c2382d2962084faefd8188e5332f4c80106b1c1 Mon Sep 17 00:00:00 2001 From: Alex van Andel Date: Thu, 29 Jan 2026 21:56:55 -0300 Subject: [PATCH] fix: allow wildcard prefix in domain validation (#27413) * fix: allow wildcard prefix in normalizeDomain function The normalizeDomain function now accepts domains with *. prefix (e.g., *.cal.com) for wildcard domain matching in the watchlist. The domain part after *. is validated using the existing regex. Co-Authored-By: alex@cal.com * fix: allow wildcard domains in validation checks Update WatchlistOperationsService and CreateBlocklistEntryModal to accept wildcard domain prefixes (*.) in domain validation. This ensures wildcard domains like *.cal.com can be added through both the API and UI. Co-Authored-By: alex@cal.com --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- .../components/CreateBlocklistEntryModal.tsx | 3 ++- .../lib/service/WatchlistOperationsService.ts | 7 +++++-- .../watchlist/lib/utils/normalization.test.ts | 13 +++++++++++++ .../watchlist/lib/utils/normalization.ts | 16 +++++++++++++--- 4 files changed, 33 insertions(+), 6 deletions(-) diff --git a/packages/features/blocklist/components/CreateBlocklistEntryModal.tsx b/packages/features/blocklist/components/CreateBlocklistEntryModal.tsx index 49f444bec8..327202975a 100644 --- a/packages/features/blocklist/components/CreateBlocklistEntryModal.tsx +++ b/packages/features/blocklist/components/CreateBlocklistEntryModal.tsx @@ -72,7 +72,8 @@ export function CreateBlocklistEntryModal({ return t("invalid_email_address"); } } else if (watchType === WatchlistType.DOMAIN) { - if (!domainRegex.test(value)) { + const domainToValidate = value.startsWith("*.") ? value.slice(2) : value; + if (!domainRegex.test(domainToValidate)) { return t("invalid_domain_format"); } } diff --git a/packages/features/watchlist/lib/service/WatchlistOperationsService.ts b/packages/features/watchlist/lib/service/WatchlistOperationsService.ts index 3e8c18417c..002e9019c9 100644 --- a/packages/features/watchlist/lib/service/WatchlistOperationsService.ts +++ b/packages/features/watchlist/lib/service/WatchlistOperationsService.ts @@ -56,8 +56,11 @@ export abstract class WatchlistOperationsService { throw WatchlistErrors.invalidEmail("Invalid email address format"); } - if (type === WatchlistType.DOMAIN && !domainRegex.test(value)) { - throw WatchlistErrors.invalidDomain("Invalid domain format (e.g., example.com)"); + if (type === WatchlistType.DOMAIN) { + const domainToValidate = value.startsWith("*.") ? value.slice(2) : value; + if (!domainRegex.test(domainToValidate)) { + throw WatchlistErrors.invalidDomain("Invalid domain format (e.g., example.com or *.example.com)"); + } } } diff --git a/packages/features/watchlist/lib/utils/normalization.test.ts b/packages/features/watchlist/lib/utils/normalization.test.ts index 3e5ebf6bcf..7d8343e869 100644 --- a/packages/features/watchlist/lib/utils/normalization.test.ts +++ b/packages/features/watchlist/lib/utils/normalization.test.ts @@ -41,11 +41,24 @@ describe("normalization", () => { expect(normalizeDomain("company.com.au")).toBe("company.com.au"); }); + test("should accept and preserve wildcard prefix", () => { + expect(normalizeDomain("*.cal.com")).toBe("*.cal.com"); + expect(normalizeDomain("*.EXAMPLE.COM")).toBe("*.example.com"); + expect(normalizeDomain(" *.domain.org ")).toBe("*.domain.org"); + expect(normalizeDomain("*.sub.domain.co.uk")).toBe("*.sub.domain.co.uk"); + }); + test("should throw on invalid domains", () => { expect(() => normalizeDomain("invalid..domain")).toThrow("Invalid domain format"); expect(() => normalizeDomain(".domain.com")).toThrow("Invalid domain format"); expect(() => normalizeDomain("domain.")).toThrow("Invalid domain format"); }); + + test("should throw on invalid wildcard domains", () => { + expect(() => normalizeDomain("*.")).toThrow("Invalid domain format"); + expect(() => normalizeDomain("*..domain.com")).toThrow("Invalid domain format"); + expect(() => normalizeDomain("*.invalid..domain")).toThrow("Invalid domain format"); + }); }); describe("extractDomainFromEmail", () => { diff --git a/packages/features/watchlist/lib/utils/normalization.ts b/packages/features/watchlist/lib/utils/normalization.ts index 75e92551cb..4e33bd40dc 100644 --- a/packages/features/watchlist/lib/utils/normalization.ts +++ b/packages/features/watchlist/lib/utils/normalization.ts @@ -33,13 +33,14 @@ export function normalizeEmail(email: string): string { * 1. Convert to lowercase * 2. Trim whitespace * 3. Remove @ prefix if present + * 4. Preserve *. prefix for wildcard domains * * Note: Domains are stored without @ prefix (e.g., mail.google.com, example.co.uk) * Wildcard matching is configurable: * - `*.cal.com` blocks all subdomains (app.cal.com, sub.app.cal.com, etc.) * - `cal.com` only blocks exact matches * - * @param domain - Raw domain (with or without @ prefix) + * @param domain - Raw domain (with or without @ prefix, with or without *. prefix) * @returns Normalized domain without @ prefix */ export function normalizeDomain(domain: string): string { @@ -49,13 +50,22 @@ export function normalizeDomain(domain: string): string { normalized = normalized.slice(1); } + // Check for wildcard prefix and validate the domain part separately + let isWildcard = false; + let domainToValidate = normalized; + + if (normalized.startsWith("*.")) { + isWildcard = true; + domainToValidate = normalized.slice(2); // Remove "*." for validation + } + 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)) { + if (!domainRegex.test(domainToValidate)) { throw new Error(`Invalid domain format: ${domain}`); } - return normalized; + return isWildcard ? `*.${domainToValidate}` : domainToValidate; } /**