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: [email protected] <[email protected]> * 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: [email protected] <[email protected]> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
1 parent
f0ca3bbbfd
commit
5c2382d296
4 files changed
+33
-6
No files matched your search
@@ -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");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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", () => {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user