mirror of
https://github.com/fosrl/pangolin.git
synced 2026-07-31 01:35:34 +02:00
Merge pull request #3460 from shubhamsinnh/fix/idn-punycode-domain-validation
Fix Punycode domain validation
This commit is contained in:
@@ -1,10 +1,29 @@
|
|||||||
import {
|
import {
|
||||||
getResourceRuleValueValidationError,
|
getResourceRuleValueValidationError,
|
||||||
|
isValidDomain,
|
||||||
isValidUrlGlobPattern
|
isValidUrlGlobPattern
|
||||||
} from "./validators";
|
} from "./validators";
|
||||||
import { assertEquals } from "@test/assert";
|
import { assertEquals } from "@test/assert";
|
||||||
|
|
||||||
function runTests() {
|
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...");
|
console.log("Running URL pattern validation tests...");
|
||||||
|
|
||||||
// Test valid patterns
|
// Test valid patterns
|
||||||
|
|||||||
@@ -171,9 +171,10 @@ export function isValidDomain(domain: string): boolean {
|
|||||||
if (!/^[a-zA-Z0-9-]+$/.test(label)) return false;
|
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];
|
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
|
// Check if TLD is in the list of valid TLDs
|
||||||
if (!validTlds.includes(tld.toUpperCase())) return false;
|
if (!validTlds.includes(tld.toUpperCase())) return false;
|
||||||
|
|||||||
Reference in New Issue
Block a user