mirror of
https://github.com/fosrl/pangolin.git
synced 2026-07-30 01:05:34 +02:00
Fix Punycode domain validation
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user