"use strict"; // Tests for utils/ip-subnet-validation.js: the validation/sanitization matrix // for security.json's trusted_subnets, plus the fail-closed semantics of // ipMatchesTrustedSubnets — the matcher behind the global rate-limiter bypass // in index.js (both limiter stores). The matcher is the security-critical // surface: a broken or hostile configuration must resolve to "not trusted", // never to a wider bypass or a thrown error. const test = require("node:test"); const assert = require("node:assert"); const { isValidSubnet, validateTrustedSubnetsInput, sanitizeTrustedSubnets, ipMatchesTrustedSubnets, MAX_TRUSTED_SUBNETS, MAX_SUBNET_LENGTH } = require("./ip-subnet-validation"); test("isValidSubnet accepts canonical IPv4/IPv6 bare-IP and CIDR forms", () => { const validForms = [ "10.0.0.5", // bare IPv4 "10.0.0.0/24", // IPv4 CIDR "0.0.0.0/0", // IPv4 default route "255.255.255.255/32", // IPv4 max prefix "2001:db8::1", // bare IPv6 "2001:db8::/32", // IPv6 CIDR "::/0", // IPv6 default route "::1/128", "::ffff:10.0.0.1" // IPv4-mapped bare IPv6 ]; for (const entry of validForms) { assert.strictEqual(isValidSubnet(entry), true, `should accept ${entry}`); } }); test("isValidSubnet rejects malformed, lenient and hostile forms", () => { const invalidForms = [ "10.0.0.0/33", // prefix > 32 "2001:db8::/129", // prefix > 128 "10.0.0.0/", // empty prefix "10.0.0.0/24/8", // second slash "127.1", // IPv4 shorthand ipaddr.js would still parse "3232235520", // integer form "fe80::1%eth0", // zone id "::ffff:10.0.0.0/120",// IPv4-mapped CIDR: prefix applies to the IPv6 form, "::ffff:0:0/96", // but the matcher compares plain IPv4 — never-matchable "10.0.0.256", // bad bare IPv4 "not-a-subnet", "", // empty // injection payloads (the stored-XSS class this validation closes) '">', "", "10.0.0.0/24"]); assert.strictEqual(bad.ok, false, "one malformed entry rejects the whole update"); assert.deepStrictEqual(bad.invalid, [""], "offending entry is named"); const good = validateTrustedSubnetsInput([" 10.0.0.0/24 ", "10.0.0.0/24", "2001:db8::/32"]); assert.strictEqual(good.ok, true); assert.deepStrictEqual(good.entries, ["10.0.0.0/24", "2001:db8::/32"], "entries stored trimmed and deduplicated"); }); test("validateTrustedSubnetsInput rejects non-arrays, oversized arrays and non-string entries", () => { assert.strictEqual(validateTrustedSubnetsInput("10.0.0.0/24").ok, false, "non-array rejected"); assert.strictEqual(validateTrustedSubnetsInput(null).ok, false); assert.strictEqual( validateTrustedSubnetsInput(new Array(MAX_TRUSTED_SUBNETS + 1).fill("10.0.0.0/24")).ok, false, "more entries than the cap rejected" ); const mixedJunk = validateTrustedSubnetsInput([10, null, {}, "10.0.0.0/24"]); assert.strictEqual(mixedJunk.ok, false, "non-string entries fail closed instead of being coerced"); }); test("sanitizeTrustedSubnets drops garbage, caps and dedupes; non-array becomes []", () => { const hostile = sanitizeTrustedSubnets(["10.0.0.0/24", "", "127.1", { x: 1 }, null, 33, "10.0.0.0/24"]); assert.deepStrictEqual(hostile, ["10.0.0.0/24"], "garbage dropped, duplicates collapsed"); const capped = sanitizeTrustedSubnets( Array.from({ length: 200 }, (_, i) => `10.${(i >> 8) & 255}.${i & 255}.0/24`) ); assert.strictEqual(capped.length, MAX_TRUSTED_SUBNETS, "load-path entries capped"); assert.deepStrictEqual(sanitizeTrustedSubnets("