// Standalone verification harness for the trusted_subnets validation fix (L-3).
// Proves the security properties of utils/ip-subnet-validation.js and the
// wiring in index.js / utils/db-proxy.js:
// 1. The validator accepts every canonical IPv4/IPv6 bare-IP and CIDR form.
// 2. The validator rejects malformed CIDRs AND injection payloads
// (HTML/JS metacharacters never reach stored state).
// 3. The endpoint contract is all-or-nothing: one bad entry rejects the
// whole update with 400 (no partial save), valid input is stored
// trimmed/deduplicated/capped.
// 4. Stored values always match through the real ip-range-check matcher
// exactly as documented (fail-closed on non-matches, no exceptions).
// 5. sanitizeTrustedSubnets() drops garbage from hand-edited security.json
// on every load path (initial load, fs.watch reload).
// 5b. The global rate-limiter bypass matcher ipMatchesTrustedSubnets()
// resolves validated entries exactly as documented, fail-closed.
// Run: node scripts/verify-trusted-subnet-validation.js
const assert = require("assert");
const crypto = require("crypto");
const express = require("express");
const session = require("express-session");
const http = require("http");
const ipRangeCheck = require("ip-range-check");
const {
isValidSubnet,
validateTrustedSubnetsInput,
sanitizeTrustedSubnets,
ipMatchesTrustedSubnets,
MAX_TRUSTED_SUBNETS,
MAX_SUBNET_LENGTH
} = require("../utils/ip-subnet-validation");
let passed = 0;
function ok(cond, label) {
assert.ok(cond, label);
passed++;
console.log(` ok - ${label}`);
}
// ─── 1. Valid forms are accepted ───────────────────────────────
console.log("[1] valid subnet forms");
const validForms = [
"10.0.0.5", // bare IPv4 (exact match semantics)
"10.0.0.0/24", // canonical IPv4 CIDR
"0.0.0.0/0", // IPv4 default route (boundary prefix 0)
"255.255.255.255/32", // IPv4 max prefix
"192.168.1.37/32",
"2001:db8::/32", // IPv6 CIDR
"2001:db8::1", // bare IPv6
"::/0", // IPv6 default route
"fe80::/10",
"::1/128",
"::ffff:10.0.0.1" // IPv4-mapped bare IPv6 (valid; the matcher normalizes it to plain IPv4)
];
for (const entry of validForms) {
ok(isValidSubnet(entry) === true, `accepts ${entry}`);
}
// Leading/trailing whitespace is trimmed by callers, not part of the value.
ok(isValidSubnet(" 10.0.0.0/24 ") === true, "accepts entry that needs trimming");
// ─── 2. Malformed + injection payloads are rejected ────────────
console.log("[2] malformed and hostile inputs");
const invalidForms = [
// malformed CIDRs
"999.1.2.3/8", // octet out of range
"10.0.0.0/33", // prefix > 32
"2001:db8::/129", // prefix > 128
"10.0.0.0/-1", // negative prefix
"10.0.0.0/1.5", // fractional prefix
"10.0.0.0/+8", // prefix with sign
"10.0.0.0/", // empty prefix
"10.0.0.0/24/8", // second slash
"/24", // empty IP part
"10.0.0.0/08", // non-canonical leading-zero prefix
"10.0.0.0/ 24", // whitespace in prefix
"::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
// shorthand / lenient forms ipaddr.js would still parse — kept out on purpose
"127.1", // IPv4 shorthand
"1", // integer form
"3232235520", // 32-bit integer form
"fe80::1%eth0", // zone id
"10.0.0.256", // bad bare IPv4
"10.0.0", // incomplete IPv4
"10.0.0.0:80", // host:port
"not-a-subnet",
"", // empty
" ", // whitespace only
// injection payloads (the stored-XSS class this fix closes)
'">
',
"",
"10.0.0.0/24","127.1",{"x":1},null,33,"2001:db8::/32"]'));
ok(JSON.stringify(hostile) === JSON.stringify(["10.0.0.0/24", "2001:db8::/32"]),
"hand-edited file entries sanitized to valid CIDRs only");
ok(JSON.stringify(sanitizeTrustedSubnets("