Fix Punycode domain validation

This commit is contained in:
Shubham Singh
2026-07-19 13:45:33 +05:30
parent c30fe5b574
commit 0275f44056
2 changed files with 22 additions and 2 deletions
+19
View File
@@ -1,10 +1,29 @@
import {
getResourceRuleValueValidationError,
isValidDomain,
isValidUrlGlobPattern
} from "./validators";
import { assertEquals } from "@test/assert";
function runTests() {
console.log("Running domain validation tests...");
assertEquals(
isValidDomain("example.com"),
true,
"Standard ASCII domain should be valid"
);
assertEquals(
isValidDomain("xn--e1afmkfd.xn--p1ai"),
true,
"Punycode IDN domain should be valid"
);
assertEquals(
isValidDomain("example.invalid-tld"),
false,
"Domain with unknown TLD should be invalid"
);
console.log("Running URL pattern validation tests...");
// Test valid patterns
+3 -2
View File
@@ -171,9 +171,10 @@ export function isValidDomain(domain: string): boolean {
if (!/^[a-zA-Z0-9-]+$/.test(label)) return false;
}
// TLD should be at least 2 characters and contain only letters
// TLD should be at least 2 characters. Punycode TLDs can contain digits
// and hyphens, so validity is ultimately enforced by the TLD allowlist.
const tld = labels[labels.length - 1];
if (tld.length < 2 || !/^[a-zA-Z]+$/.test(tld)) return false;
if (tld.length < 2) return false;
// Check if TLD is in the list of valid TLDs
if (!validTlds.includes(tld.toUpperCase())) return false;