From 4004578e8235ffb95e19565054a882f7f1e859d2 Mon Sep 17 00:00:00 2001 From: Eduard Gert Date: Mon, 28 Apr 2025 11:25:05 +0200 Subject: [PATCH] Update domain validator --- .../resources/ResourceSingleAddressInput.tsx | 2 +- src/utils/helpers.ts | 62 ++++++++----------- 2 files changed, 27 insertions(+), 37 deletions(-) diff --git a/src/modules/networks/resources/ResourceSingleAddressInput.tsx b/src/modules/networks/resources/ResourceSingleAddressInput.tsx index 89942c04..a0a78a77 100644 --- a/src/modules/networks/resources/ResourceSingleAddressInput.tsx +++ b/src/modules/networks/resources/ResourceSingleAddressInput.tsx @@ -31,7 +31,7 @@ export const ResourceSingleAddressInput = ({ value, onChange }: Props) => { // Case 1: If it has characters (potential domain) but is not a CIDR block if (hasChars && !isCIDRBlock) { - if (!validator.isValidDomainWithWildcard(value)) { + if (!validator.isValidDomain(value)) { return "Please enter a valid domain, e.g. intra.example.com or *.example.com"; } return ""; // Valid domain diff --git a/src/utils/helpers.ts b/src/utils/helpers.ts index 9a548b5f..e5d48d75 100644 --- a/src/utils/helpers.ts +++ b/src/utils/helpers.ts @@ -41,47 +41,37 @@ export const sleep = (ms: number) => { }; export const validator = { - isValidDomain: (domain: string) => { - const unicodeDomain = - /^(?!.*\.\.)(?!.*\.$)(?!.*\s)(?:(?!-)(?!.*--)[a-zA-Z0-9\u00A1-\uFFFF-]{1,63}(?= minMaxChars[0] && domain.length <= minMaxChars[1]; - const includesDot = domain.includes("."); - const hasNoWhitespace = !domain.includes(" "); - return ( - unicodeDomain.test(domain) && - includesDot && - hasNoWhitespace && - isValidDomainLength - ); - } catch (e) { - return false; - } - }, - isValidDomainWithWildcard: (domain: string) => { - // Basic checks - if (!domain || domain.length > 255 || domain.includes(" ")) { - return false; - } + isValidDomain: ( + domain: string, + options?: { allowWildcard?: boolean; allowOnlyTld?: boolean }, + ) => { + const { allowWildcard = true, allowOnlyTld = true } = options || { + allowWildcard: true, + allowOnlyTld: true, + }; - // Handle wildcard - if (domain.includes("*")) { - if (!domain.startsWith("*.") || domain.indexOf("*", 1) !== -1) { + try { + const includesAtLeastOneDot = domain.includes("."); + const hasWhitespace = domain.includes(" "); + const domainRegex = + /^(?!-)[a-z0-9\u00a1-\uffff-*]{0,63}(? 255) { return false; } - domain = "sub" + domain.slice(1); // Replace * with valid subdomain for testing - } - - // Split and validate each part - const parts = domain.split("."); - if (parts.length < 2) { + if (!allowWildcard && domain.startsWith("*.")) { + return false; + } + if (allowWildcard && !domain.startsWith("*.") && domain.includes("*")) { + return false; + } + if (!allowOnlyTld && domain.startsWith(".")) { + return false; + } + return includesAtLeastOneDot && isValidUnicodeDomain && !hasWhitespace; + } catch (error) { return false; } - - const validPart = /^[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$/; - return parts.every((part) => validPart.test(part)); }, isValidEmail: (email: string) => { const regExp = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,63}$/i;