Skip to content

Commit 6c0ab88

Browse files
authored
Update domain validator (#459)
1 parent a50576d commit 6c0ab88

File tree

2 files changed

+27
-37
lines changed

2 files changed

+27
-37
lines changed

src/modules/networks/resources/ResourceSingleAddressInput.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export const ResourceSingleAddressInput = ({ value, onChange }: Props) => {
3131

3232
// Case 1: If it has characters (potential domain) but is not a CIDR block
3333
if (hasChars && !isCIDRBlock) {
34-
if (!validator.isValidDomainWithWildcard(value)) {
34+
if (!validator.isValidDomain(value)) {
3535
return "Please enter a valid domain, e.g. intra.example.com or *.example.com";
3636
}
3737
return ""; // Valid domain

src/utils/helpers.ts

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -41,47 +41,37 @@ export const sleep = (ms: number) => {
4141
};
4242

4343
export const validator = {
44-
isValidDomain: (domain: string) => {
45-
const unicodeDomain =
46-
/^(?!.*\.\.)(?!.*\.$)(?!.*\s)(?:(?!-)(?!.*--)[a-zA-Z0-9\u00A1-\uFFFF-]{1,63}(?<!-)\.)+(?!-)(?!.*--)[a-zA-Z0-9\u00A1-\uFFFF-]{2,63}$/u;
47-
try {
48-
const minMaxChars = [1, 255];
49-
const isValidDomainLength =
50-
domain.length >= minMaxChars[0] && domain.length <= minMaxChars[1];
51-
const includesDot = domain.includes(".");
52-
const hasNoWhitespace = !domain.includes(" ");
53-
return (
54-
unicodeDomain.test(domain) &&
55-
includesDot &&
56-
hasNoWhitespace &&
57-
isValidDomainLength
58-
);
59-
} catch (e) {
60-
return false;
61-
}
62-
},
63-
isValidDomainWithWildcard: (domain: string) => {
64-
// Basic checks
65-
if (!domain || domain.length > 255 || domain.includes(" ")) {
66-
return false;
67-
}
44+
isValidDomain: (
45+
domain: string,
46+
options?: { allowWildcard?: boolean; allowOnlyTld?: boolean },
47+
) => {
48+
const { allowWildcard = true, allowOnlyTld = true } = options || {
49+
allowWildcard: true,
50+
allowOnlyTld: true,
51+
};
6852

69-
// Handle wildcard
70-
if (domain.includes("*")) {
71-
if (!domain.startsWith("*.") || domain.indexOf("*", 1) !== -1) {
53+
try {
54+
const includesAtLeastOneDot = domain.includes(".");
55+
const hasWhitespace = domain.includes(" ");
56+
const domainRegex =
57+
/^(?!-)[a-z0-9\u00a1-\uffff-*]{0,63}(?<!-)(\.[a-z0-9\u00a1-\uffff-*]{0,63})*$/i;
58+
const isValidUnicodeDomain = domainRegex.test(domain);
59+
if (domain.length < 1 || domain.length > 255) {
7260
return false;
7361
}
74-
domain = "sub" + domain.slice(1); // Replace * with valid subdomain for testing
75-
}
76-
77-
// Split and validate each part
78-
const parts = domain.split(".");
79-
if (parts.length < 2) {
62+
if (!allowWildcard && domain.startsWith("*.")) {
63+
return false;
64+
}
65+
if (allowWildcard && !domain.startsWith("*.") && domain.includes("*")) {
66+
return false;
67+
}
68+
if (!allowOnlyTld && domain.startsWith(".")) {
69+
return false;
70+
}
71+
return includesAtLeastOneDot && isValidUnicodeDomain && !hasWhitespace;
72+
} catch (error) {
8073
return false;
8174
}
82-
83-
const validPart = /^[a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?$/;
84-
return parts.every((part) => validPart.test(part));
8575
},
8676
isValidEmail: (email: string) => {
8777
const regExp = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,63}$/i;

0 commit comments

Comments
 (0)