Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

hostClassification.ts

149 lines6.3 KB

1export const normalizeHostname = (host: string): string =>
2 host
3 .toLowerCase()
4 .replace(/^\[|\]$/g, "")
5 .replace(/\.+$/u, "");
6​
7const parseIpv4Address = (host: string): readonly number[] | null => {
8 const parts = normalizeHostname(host).split(".").map(Number);
9 return parts.length === 4 &&
10 parts.every((part) => Number.isInteger(part) && part >= 0 && part <= 255)
11 ? parts
12 : null;
13};
14​
15const parseIpv4MappedIpv6Address = (host: string): readonly number[] | null => {
16 const normalized = normalizeHostname(host);
17 if (!normalized.startsWith("::ffff:")) return null;
18 const suffix = normalized.slice("::ffff:".length);
19 const dotted = parseIpv4Address(suffix);
20 if (dotted) return dotted;
21 const hextets = suffix.split(":");
22 if (hextets.length !== 2 || hextets.some((part) => !/^[\da-f]{1,4}$/u.test(part))) return null;
23 const high = Number.parseInt(hextets[0]!, 16);
24 const low = Number.parseInt(hextets[1]!, 16);
25 return [high >>> 8, high & 0xff, low >>> 8, low & 0xff];
26};
27​
28const parseIpv6Address = (host: string): readonly number[] | null => {
29 const normalized = normalizeHostname(host);
30 if (!normalized.includes(":")) return null;
31 const halves = normalized.split("::");
32 if (halves.length > 2) return null;
33 const head = halves[0] ? halves[0].split(":") : [];
34 const tail = halves[1] ? halves[1].split(":") : [];
35 if ([...head, ...tail].some((part) => !/^[\da-f]{1,4}$/u.test(part))) return null;
36 const missing = 8 - head.length - tail.length;
37 if ((halves.length === 1 && missing !== 0) || (halves.length === 2 && missing < 1)) return null;
38 return [...head, ...Array.from({ length: missing }, () => "0"), ...tail].map((part) =>
39 Number.parseInt(part, 16),
40 );
41};
42​
43const ipv6PrefixMatches = (
44 address: readonly number[],
45 prefix: readonly number[],
46 prefixLength: number,
47): boolean => {
48 const fullHextets = Math.floor(prefixLength / 16);
49 if (address.slice(0, fullHextets).some((part, index) => part !== prefix[index])) return false;
50 const remainingBits = prefixLength % 16;
51 if (remainingBits === 0) return true;
52 const mask = (0xffff << (16 - remainingBits)) & 0xffff;
53 return (address[fullHextets]! & mask) === (prefix[fullHextets]! & mask);
54};
55​
56const isPrivateIpv4Address = (parts: readonly number[]): boolean =>
57 parts[0] === 0 ||
58 parts[0] === 10 ||
59 parts[0] === 127 ||
60 (parts[0] === 100 && parts[1]! >= 64 && parts[1]! <= 127) ||
61 (parts[0] === 172 && parts[1]! >= 16 && parts[1]! <= 31) ||
62 (parts[0] === 192 && parts[1] === 168) ||
63 (parts[0] === 169 && parts[1] === 254) ||
64 (parts[0] === 198 && parts[1]! >= 18 && parts[1]! <= 19);
65​
66const isSpecialPurposeIpv4Address = (parts: readonly number[]): boolean =>
67 isPrivateIpv4Address(parts) ||
68 parts[0]! >= 224 ||
69 // Deliberately suppress the whole protocol-assignment block. IANA marks
70 // .9 and .10 globally reachable, but privacy-safe false negatives are
71 // preferable to disclosing another special-purpose address by mistake.
72 (parts[0] === 192 && parts[1] === 0 && parts[2] === 0) ||
73 (parts[0] === 192 && parts[1] === 0 && parts[2] === 2) ||
74 (parts[0] === 192 && parts[1] === 88 && parts[2] === 99) ||
75 (parts[0] === 198 && parts[1] === 51 && parts[2] === 100) ||
76 (parts[0] === 203 && parts[1] === 0 && parts[2] === 113);
77​
78export const isLocalLoopbackHost = (host: string): boolean => {
79 const normalized = normalizeHostname(host);
80 if (normalized === "localhost" || normalized === "::1") return true;
81 return parseIpv4Address(normalized)?.[0] === 127;
82};
83​
84export const isPrivateNetworkHost = (host: string): boolean => {
85 const normalized = normalizeHostname(host);
86 if (
87 normalized === "::" ||
88 isLocalLoopbackHost(normalized) ||
89 normalized.endsWith(".localhost") ||
90 normalized.endsWith(".local") ||
91 normalized === "home.arpa" ||
92 normalized.endsWith(".home.arpa") ||
93 (!normalized.includes(".") && !normalized.includes(":"))
94 ) {
95 return true;
96 }
97 if (normalized.endsWith(".ts.net")) return true;
98 const parts = parseIpv4Address(normalized) ?? parseIpv4MappedIpv6Address(normalized);
99 if (parts) return isPrivateIpv4Address(parts);
100 const firstIpv6Token = normalized.split(":", 1)[0] ?? "";
101 if (!normalized.includes(":") || !/^[\da-f]{1,4}$/u.test(firstIpv6Token)) return false;
102 const firstIpv6Hextet = Number.parseInt(firstIpv6Token, 16);
103 return (
104 Number.isInteger(firstIpv6Hextet) &&
105 ((firstIpv6Hextet & 0xfe00) === 0xfc00 || (firstIpv6Hextet & 0xffc0) === 0xfe80)
106 );
107};
108​
109/** Whether a hostname is eligible to be disclosed to a public favicon provider. */
110export const isPublicFaviconHost = (host: string): boolean => {
111 // A single trailing dot is a valid absolute DNS name. Repeated trailing
112 // dots are malformed and can conceal legacy numeric forms such as 127.1.
113 if (host.endsWith("..")) return false;
114 const normalized = normalizeHostname(host);
115 if (isPrivateNetworkHost(normalized)) return false;
116 if (
117 [".alt", ".example", ".internal", ".invalid", ".onion", ".test"].some(
118 (suffix) => normalized === suffix.slice(1) || normalized.endsWith(suffix),
119 )
120 ) {
121 return false;
122 }
123 const ipv4 = parseIpv4Address(normalized) ?? parseIpv4MappedIpv6Address(normalized);
124 if (ipv4) return !isSpecialPurposeIpv4Address(ipv4);
125 if (!normalized.includes(":")) return true;
126 const ipv6 = parseIpv6Address(normalized);
127 if (!ipv6) return false;
128 if (ipv6PrefixMatches(ipv6, [0x0064, 0xff9b, 0, 0, 0, 0, 0, 0], 96)) {
129 const embeddedIpv4 = [ipv6[6]! >>> 8, ipv6[6]! & 0xff, ipv6[7]! >>> 8, ipv6[7]! & 0xff];
130 return !isSpecialPurposeIpv4Address(embeddedIpv4);
131 }
132 const first = ipv6[0]!;
133 if ((first & 0xe000) !== 0x2000) return false;
134 if (ipv6PrefixMatches(ipv6, [0x2001, 0, 0, 0, 0, 0, 0, 0], 23)) {
135 const publicProtocolAssignment =
136 (ipv6[1] === 1 &&
137 ipv6.slice(2, 7).every((part) => part === 0) &&
138 [1, 2, 3].includes(ipv6[7]!)) ||
139 ipv6PrefixMatches(ipv6, [0x2001, 3, 0, 0, 0, 0, 0, 0], 32) ||
140 ipv6PrefixMatches(ipv6, [0x2001, 4, 0x0112, 0, 0, 0, 0, 0], 48) ||
141 ipv6PrefixMatches(ipv6, [0x2001, 0x20, 0, 0, 0, 0, 0, 0], 28) ||
142 ipv6PrefixMatches(ipv6, [0x2001, 0x30, 0, 0, 0, 0, 0, 0], 28);
143 return publicProtocolAssignment;
144 }
145 if (ipv6PrefixMatches(ipv6, [0x2001, 0x0db8, 0, 0, 0, 0, 0, 0], 32)) return false;
146 if (ipv6PrefixMatches(ipv6, [0x2002, 0, 0, 0, 0, 0, 0, 0], 16)) return false;
147 if (first === 0x3fff && (ipv6[1]! & 0xf000) === 0) return false;
148 return true;
149};