records.test.js (12493B)
1 import t from "ava"; 2 import fs from "fs-extra"; 3 import path from "path"; 4 5 import disallowedCNAMEs from "../util/disallowed-cnames.json" with { type: "json" }; 6 7 const validRecordTypes = new Set(["A", "AAAA", "CAA", "CNAME", "DS", "MX", "NS", "SRV", "TLSA", "TXT"]); 8 const hostnameRegex = /^(?=.{1,253}$)(?:(?:[_a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)\.)+[a-zA-Z]{2,63}$/; 9 const ipv4Regex = /^(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}$/; 10 const ipv6Regex = 11 /^(?:[0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$|^::(?:[0-9a-fA-F]{1,4}:){0,6}[0-9a-fA-F]{1,4}$|^(?:[0-9a-fA-F]{1,4}:){1,7}:$|^(?:[0-9a-fA-F]{1,4}:){0,6}::(?:[0-9a-fA-F]{1,4}:){0,5}[0-9a-fA-F]{1,4}$/; 12 13 const domainsPath = path.resolve("domains"); 14 const files = fs.readdirSync(domainsPath).filter((file) => file.endsWith(".json")); 15 16 const domainCache = {}; 17 18 function getDomainData(file) { 19 if (domainCache[file]) { 20 return domainCache[file]; 21 } 22 23 try { 24 const data = fs.readJsonSync(path.join(domainsPath, file)); 25 domainCache[file] = data; 26 return data; 27 } catch (error) { 28 throw new Error(`Failed to read JSON for ${file}: ${error.message}`); 29 } 30 } 31 32 function expandIPv6(ip) { 33 let segments = ip.split(":"); 34 const emptyIndex = segments.indexOf(""); 35 36 if (emptyIndex !== -1) { 37 const nonEmptySegments = segments.filter((seg) => seg !== ""); 38 const missingSegments = 8 - nonEmptySegments.length; 39 40 segments = [ 41 ...nonEmptySegments.slice(0, emptyIndex), 42 ...Array(missingSegments).fill("0000"), 43 ...nonEmptySegments.slice(emptyIndex) 44 ]; 45 } 46 47 return segments.map((segment) => segment.padStart(4, "0")).join(":"); 48 } 49 50 function validateIPv4(ip, proxied) { 51 const parts = ip.split(".").map(Number); 52 53 if (parts.length !== 4 || parts.some((part) => isNaN(part) || part < 0 || part > 255)) return false; 54 if (ip === "192.0.2.1" && proxied) return true; 55 56 return !( 57 parts[0] === 10 || 58 (parts[0] === 172 && parts[1] >= 16 && parts[1] <= 31) || 59 (parts[0] === 192 && parts[1] === 168) || 60 (parts[0] === 100 && parts[1] >= 64 && parts[1] <= 127) || 61 (parts[0] === 169 && parts[1] === 254) || 62 (parts[0] === 192 && parts[1] === 0 && parts[2] === 0) || 63 (parts[0] === 192 && parts[1] === 0 && parts[2] === 2) || 64 (parts[0] === 198 && parts[1] === 18) || 65 (parts[0] === 198 && parts[1] === 51 && parts[2] === 100) || 66 (parts[0] === 203 && parts[1] === 0 && parts[2] === 113) || 67 parts[0] >= 224 68 ); 69 } 70 71 function validateIPv6(ip) { 72 return !( 73 ip.toLowerCase().startsWith("fc") || 74 ip.toLowerCase().startsWith("fd") || 75 ip.toLowerCase().startsWith("fe80") || 76 ip.toLowerCase().startsWith("::1") || 77 ip.toLowerCase().startsWith("2001:db8") 78 ); 79 } 80 81 function validateRecordType(recordType) { 82 return validRecordTypes.has(recordType); 83 } 84 85 function isValidHostname(hostname) { 86 return hostnameRegex.test(hostname); 87 } 88 89 function isValidHexadecimal(value) { 90 return /^[0-9a-fA-F]+$/.test(value); 91 } 92 93 function validateRecordValues(t, data, file) { 94 const subdomain = file.replace(/\.json$/, ""); 95 96 Object.entries(data.records).forEach(([key, value]) => { 97 // General validation for arrays 98 if (["A", "AAAA", "MX", "NS"].includes(key)) { 99 t.true(Array.isArray(value), `${file}: Record value for ${key} should be an array`); 100 101 value.forEach((record, idx) => { 102 t.true( 103 typeof record === "string" || typeof record === "object", 104 `${file}: Record value for ${key} should be a string or an object at index ${idx}` 105 ); 106 107 if (key === "A") { 108 t.true(ipv4Regex.test(record), `${file}: Invalid IPv4 address for ${key} at index ${idx}`); 109 t.true( 110 validateIPv4(record, data.proxied), 111 `${file}: Invalid IPv4 address for ${key} at index ${idx}` 112 ); 113 } else if (key === "AAAA") { 114 const expandedIPv6 = expandIPv6(record); 115 t.true(ipv6Regex.test(expandedIPv6), `${file}: Invalid IPv6 address for ${key} at index ${idx}`); 116 t.true(validateIPv6(expandedIPv6), `${file}: Invalid IPv6 address for ${key} at index ${idx}`); 117 } else if (key === "MX") { 118 t.true( 119 typeof record === "object" || typeof record === "string", 120 `${file}: Record value for ${key} should be an object or a string at index ${idx}` 121 ); 122 123 if (typeof record === "string") { 124 t.true(isValidHostname(record), `${file}: Invalid hostname for ${key} at index ${idx}`); 125 } else { 126 t.true(isValidHostname(record.target), `${file}: Invalid target for ${key} at index ${idx}`); 127 t.true( 128 Number.isInteger(record.priority) && record.priority >= 0 && record.priority <= 65535, 129 `${file}: Invalid priority for ${key} at index ${idx}` 130 ); 131 } 132 } else if (key === "NS") { 133 t.true(isValidHostname(record), `${file}: Invalid hostname for ${key} at index ${idx}`); 134 } 135 }); 136 } 137 138 // CNAME validation 139 if (key === "CNAME") { 140 t.true(typeof value === "string", `${file}: Record value for ${key} should be a string`); 141 142 t.true(isValidHostname(value), `${file}: Invalid hostname for ${key}`); 143 t.true(value !== `${subdomain}.is-a.dev`, `${file}: ${key} cannot point to itself`); 144 t.true(value !== "is-a.dev", `${file}: ${key} cannot point to is-a.dev`); 145 146 for (const disallowed of disallowedCNAMEs) { 147 if (disallowed.startsWith(".")) { 148 t.false(value.endsWith(disallowed), `${file}: ${key} cannot end with ${disallowed}`); 149 } else { 150 t.false(value === disallowed, `${file}: ${key} cannot be ${disallowed}`); 151 } 152 } 153 } 154 155 // CAA, DS, SRV, TLSA validations 156 if (["CAA", "DS", "SRV", "TLSA"].includes(key)) { 157 t.true(Array.isArray(value), `${file}: Record value for ${key} should be an array`); 158 159 value.forEach((record, idx) => { 160 t.true( 161 typeof record === "object", 162 `${file}: Record value for ${key} should be an object at index ${idx}` 163 ); 164 165 if (key === "CAA") { 166 t.true( 167 ["issue", "issuewild", "iodef"].includes(record.tag), 168 `${file}: Invalid tag for ${key} at index ${idx}` 169 ); 170 t.true(typeof record.value === "string", `${file}: Invalid value for ${key} at index ${idx}`); 171 t.true( 172 isValidHostname(record.value) || record.value === ";", 173 `${file}: Value must be a hostname or semicolon for ${key} at index ${idx}` 174 ); 175 } else if (key === "DS") { 176 t.true( 177 Number.isInteger(record.key_tag) && record.key_tag >= 0 && record.key_tag <= 65535, 178 `${file}: Invalid key_tag for ${key} at index ${idx}` 179 ); 180 t.true( 181 Number.isInteger(record.algorithm) && record.algorithm >= 0 && record.algorithm <= 255, 182 `${file}: Invalid algorithm for ${key} at index ${idx}` 183 ); 184 t.true( 185 Number.isInteger(record.digest_type) && record.digest_type >= 0 && record.digest_type <= 255, 186 `${file}: Invalid digest_type for ${key} at index ${idx}` 187 ); 188 t.true(isValidHexadecimal(record.digest), `${file}: Invalid digest for ${key} at index ${idx}`); 189 } else if (key === "SRV") { 190 t.true( 191 Number.isInteger(record.priority) && record.priority >= 0 && record.priority <= 65535, 192 `${file}: Invalid priority for ${key} at index ${idx}` 193 ); 194 t.true( 195 Number.isInteger(record.weight) && record.weight >= 0 && record.weight <= 65535, 196 `${file}: Invalid weight for ${key} at index ${idx}` 197 ); 198 t.true( 199 Number.isInteger(record.port) && record.port >= 0 && record.port <= 65535, 200 `${file}: Invalid port for ${key} at index ${idx}` 201 ); 202 t.true(isValidHostname(record.target), `${file}: Invalid target for ${key} at index ${idx}`); 203 } else if (key === "TLSA") { 204 t.true( 205 Number.isInteger(record.usage) && record.usage >= 0 && record.usage <= 255, 206 `${file}: Invalid usage for ${key} at index ${idx}` 207 ); 208 t.true( 209 Number.isInteger(record.selector) && record.selector >= 0 && record.selector <= 255, 210 `${file}: Invalid selector for ${key} at index ${idx}` 211 ); 212 t.true( 213 Number.isInteger(record.matching_type) && 214 record.matching_type >= 0 && 215 record.matching_type <= 255, 216 `${file}: Invalid matching_type for ${key} at index ${idx}` 217 ); 218 t.true( 219 isValidHexadecimal(record.certificate), 220 `${file}: Invalid certificate for ${key} at index ${idx}` 221 ); 222 } 223 }); 224 } 225 226 // TXT validation 227 if (key === "TXT") { 228 const values = Array.isArray(value) ? value : [value]; 229 values.forEach((record, idx) => { 230 t.true(typeof record === "string", `${file}: TXT record value should be a string at index ${idx}`); 231 }); 232 } 233 }); 234 } 235 236 t("All files should have valid records", (t) => { 237 files.forEach((file) => { 238 const data = getDomainData(file); 239 const recordKeys = Object.keys(data.records); 240 241 recordKeys.forEach((key) => { 242 t.true(validateRecordType(key), `${file}: Invalid record type: ${key}`); 243 }); 244 245 // Record type combinations validation 246 if (recordKeys.includes("CNAME")) { 247 if (!data.proxied) { 248 t.is( 249 recordKeys.length, 250 1, 251 `${file}: CNAME records cannot be combined with other records unless proxied` 252 ); 253 } else { 254 t.true( 255 !recordKeys.includes("A") && !recordKeys.includes("AAAA"), 256 `${file}: CNAME records cannot be combined with A or AAAA records` 257 ); 258 } 259 } 260 if (recordKeys.includes("NS")) { 261 t.true( 262 recordKeys.length === 1 || (recordKeys.length === 2 && recordKeys.includes("DS")), 263 `${file}: NS records cannot be combined with other records, except for DS records` 264 ); 265 } 266 if (recordKeys.includes("DS")) { 267 t.true(recordKeys.includes("NS"), `${file}: DS records must be combined with NS records`); 268 } 269 270 validateRecordValues(t, data, file); 271 }); 272 273 t.pass(); 274 }); 275 276 t("Root subdomains should have at least one usable record", (t) => { 277 const usableRecordTypes = ["A", "AAAA", "CNAME", "MX", "NS"]; 278 279 files.forEach((file) => { 280 const subdomain = file.replace(/\.json$/, ""); 281 if (subdomain.includes(".") || subdomain.startsWith("_")) return; 282 283 const data = getDomainData(file); 284 const recordKeys = Object.keys(data.records); 285 286 t.true( 287 usableRecordTypes.some((record) => recordKeys.includes(record)), 288 `${file}: Root subdomains must have at least one A, AAAA, CNAME, MX, or NS record` 289 ); 290 }); 291 });