183 lines
8.6 KiB
JavaScript
183 lines
8.6 KiB
JavaScript
"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)
|
|
'"><img src=x onerror=alert(1)>',
|
|
"<script>alert(1)</script>",
|
|
"10.0.0.0/24<script>",
|
|
"javascript:alert(1)",
|
|
"10.0.0.0/24\n# comment",
|
|
"10.0.0.0/24 OR 1=1"
|
|
];
|
|
for (const entry of invalidForms) {
|
|
assert.strictEqual(isValidSubnet(entry), false, `should reject ${JSON.stringify(entry)}`);
|
|
}
|
|
});
|
|
|
|
test("isValidSubnet rejects non-strings and over-length entries", () => {
|
|
assert.strictEqual(isValidSubnet(null), false);
|
|
assert.strictEqual(isValidSubnet(undefined), false);
|
|
assert.strictEqual(isValidSubnet(10), false);
|
|
assert.strictEqual(isValidSubnet({}), false);
|
|
assert.strictEqual(isValidSubnet(["10.0.0.0/24"]), false);
|
|
assert.strictEqual(isValidSubnet("a".repeat(MAX_SUBNET_LENGTH + 1)), false, "over-length entry rejected");
|
|
});
|
|
|
|
test("validateTrustedSubnetsInput is all-or-nothing", () => {
|
|
const bad = validateTrustedSubnetsInput(["10.0.0.0/24", "<script>alert(1)</script>"]);
|
|
assert.strictEqual(bad.ok, false, "one malformed entry rejects the whole update");
|
|
assert.deepStrictEqual(bad.invalid, ["<script>alert(1)</script>"], "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", "<script>alert(1)</script>", "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("<script>"), [], "scalar garbage becomes an empty list");
|
|
assert.deepStrictEqual(sanitizeTrustedSubnets(null), []);
|
|
assert.deepStrictEqual(sanitizeTrustedSubnets(undefined), []);
|
|
});
|
|
|
|
test("ipMatchesTrustedSubnets: IPv4 CIDR match and non-match", () => {
|
|
assert.strictEqual(ipMatchesTrustedSubnets("10.0.0.55", ["10.0.0.0/24"]), true);
|
|
assert.strictEqual(ipMatchesTrustedSubnets("10.0.1.55", ["10.0.0.0/24"]), false);
|
|
});
|
|
|
|
test("ipMatchesTrustedSubnets: bare-IP entry matches exactly", () => {
|
|
assert.strictEqual(ipMatchesTrustedSubnets("10.0.0.5", ["10.0.0.5"]), true);
|
|
assert.strictEqual(ipMatchesTrustedSubnets("10.0.0.6", ["10.0.0.5"]), false);
|
|
});
|
|
|
|
test("ipMatchesTrustedSubnets: IPv6 CIDR match and non-match", () => {
|
|
assert.strictEqual(ipMatchesTrustedSubnets("2001:db8::1", ["2001:db8::/32"]), true);
|
|
assert.strictEqual(ipMatchesTrustedSubnets("2001:db9::1", ["2001:db8::/32"]), false);
|
|
});
|
|
|
|
test("ipMatchesTrustedSubnets: IPv4-mapped request IP matches the IPv4 CIDR", () => {
|
|
// ip-range-check's ipaddr.process() normalizes ::ffff:-prefixed addresses,
|
|
// so a proxied dual-stack socket address still hits the configured IPv4 CIDR.
|
|
assert.strictEqual(ipMatchesTrustedSubnets("::ffff:10.0.0.55", ["10.0.0.0/24"]), true);
|
|
// A bare IPv4-mapped ENTRY also works: both sides normalize to plain IPv4.
|
|
assert.strictEqual(ipMatchesTrustedSubnets("10.0.0.55", ["::ffff:10.0.0.1"]), false);
|
|
assert.strictEqual(ipMatchesTrustedSubnets("10.0.0.1", ["::ffff:10.0.0.1"]), true);
|
|
});
|
|
|
|
test("ipMatchesTrustedSubnets: CIDR boundaries are inclusive on both edges", () => {
|
|
assert.strictEqual(ipMatchesTrustedSubnets("10.0.0.0", ["10.0.0.0/24"]), true, "network address matches");
|
|
assert.strictEqual(ipMatchesTrustedSubnets("10.0.0.255", ["10.0.0.0/24"]), true, "last address matches");
|
|
assert.strictEqual(ipMatchesTrustedSubnets("10.0.1.0", ["10.0.0.0/24"]), false, "first address past the range does not");
|
|
});
|
|
|
|
test("ipMatchesTrustedSubnets fails closed on hostile or broken inputs", () => {
|
|
// unusable IP operand
|
|
assert.strictEqual(ipMatchesTrustedSubnets(null, ["10.0.0.0/24"]), false, "non-string IP");
|
|
assert.strictEqual(ipMatchesTrustedSubnets(33, ["10.0.0.0/24"]), false);
|
|
assert.strictEqual(ipMatchesTrustedSubnets({}, ["10.0.0.0/24"]), false);
|
|
assert.strictEqual(ipMatchesTrustedSubnets("", ["10.0.0.0/24"]), false, "empty IP");
|
|
assert.strictEqual(ipMatchesTrustedSubnets("unknown", ["10.0.0.0/24"]), false, "unresolvable IP");
|
|
assert.strictEqual(ipMatchesTrustedSubnets("10.0.0.55<script>", ["10.0.0.0/24"]), false);
|
|
// unusable subnet list
|
|
assert.strictEqual(ipMatchesTrustedSubnets("10.0.0.55", "10.0.0.0/24"), false, "non-array list");
|
|
assert.strictEqual(ipMatchesTrustedSubnets("10.0.0.55", null), false);
|
|
assert.strictEqual(ipMatchesTrustedSubnets("10.0.0.55", {}), false);
|
|
assert.strictEqual(ipMatchesTrustedSubnets("10.0.0.55", []), false, "empty list");
|
|
});
|
|
|
|
test("ipMatchesTrustedSubnets skips junk entries without throwing", () => {
|
|
// Junk entries are skipped; the valid string entry still decides the result.
|
|
assert.strictEqual(
|
|
ipMatchesTrustedSubnets("10.0.0.55", [null, 33, { subnet: true }, "10.0.0.0/24"]),
|
|
true,
|
|
"junk entry types skipped, valid entry still matches"
|
|
);
|
|
assert.strictEqual(
|
|
ipMatchesTrustedSubnets("10.0.1.55", [null, 33, { subnet: true }, "10.0.0.0/24"]),
|
|
false,
|
|
"junk entry types skipped, no accidental match"
|
|
);
|
|
assert.strictEqual(
|
|
ipMatchesTrustedSubnets("10.0.0.55", ["10.0.0.0/24<script>", "10.0.0.0/24"]),
|
|
true,
|
|
"malformed string entry skipped, valid sibling entry still matches"
|
|
);
|
|
assert.strictEqual(
|
|
ipMatchesTrustedSubnets("10.0.0.55", ["10.0.0.0/24<script>"]),
|
|
false,
|
|
"a list of only malformed entries matches nothing"
|
|
);
|
|
});
|
|
|
|
test("ipMatchesTrustedSubnets: 0.0.0.0/0 covers IPv4 but not IPv6", () => {
|
|
// A global IPv4 bypass must not silently extend to IPv6 loopback.
|
|
assert.strictEqual(ipMatchesTrustedSubnets("8.8.8.8", ["0.0.0.0/0"]), true);
|
|
assert.strictEqual(ipMatchesTrustedSubnets("203.0.113.7", ["0.0.0.0/0"]), true);
|
|
assert.strictEqual(ipMatchesTrustedSubnets("::1", ["0.0.0.0/0"]), false);
|
|
});
|