Reject Hosts drop prefixed www.

This commit is contained in:
SukkaW
2025-01-24 14:48:09 +08:00
parent 98d37c3749
commit 9790b40a72
5 changed files with 37 additions and 32 deletions

View File

@@ -7,6 +7,32 @@ import { isProbablyIpv4, isProbablyIpv6 } from 'foxts/is-probably-ip';
type TldTsParsed = ReturnType<typeof tldts.parse>;
/**
* Skipped the input non-empty check, the `domain` should not be empty.
*/
export function fastNormalizeDomainWithoutWww(domain: string, parsed: TldTsParsed | null = null) {
// We don't want tldts to call its own "extractHostname" on ip, bail out ip first.
// Now ip has been bailed out, we can safely set normalizeTldtsOpt.detectIp to false.
if (isProbablyIpv4(domain) || isProbablyIpv6(domain)) {
return null;
}
parsed ??= tldts.parse(domain, normalizeTldtsOpt);
// Private invalid domain (things like .tor, .dn42, etc)
if (!parsed.isIcann && !parsed.isPrivate) return null;
if (parsed.subdomain) {
if (parsed.subdomain === 'www') {
return parsed.domain;
}
if (parsed.subdomain.startsWith('www.')) {
return parsed.subdomain.slice(4) + '.' + parsed.domain;
}
}
return parsed.hostname;
}
/**
* Skipped the input non-empty check, the `domain` should not be empty.
*/
@@ -24,24 +50,6 @@ export function fastNormalizeDomain(domain: string, parsed: TldTsParsed | null =
return parsed.hostname;
}
export function fastNormalizeDomainIgnoreWww(domain: string, parsed: TldTsParsed | null = null) {
// We don't want tldts to call its own "extractHostname" on ip, bail out ip first.
// Now ip has been bailed out, we can safely set normalizeTldtsOpt.detectIp to false.
if (isProbablyIpv4(domain) || isProbablyIpv6(domain)) {
return null;
}
parsed ??= tldts.parse(domain, normalizeTldtsOpt);
// Private invalid domain (things like .tor, .dn42, etc)
if (!parsed.isIcann && !parsed.isPrivate) return null;
if (parsed.subdomain === 'www') {
return parsed.domain;
}
return parsed.hostname;
}
export function normalizeDomain(domain: string, parsed: TldTsParsed | null = null) {
if (domain.length === 0) return null;