Initial upload to Gitea
This commit is contained in:
1 parent
78193254ad
commit
8478998cae
1485 files changed
+461025
-42348
No files matched your search
@@ -0,0 +1,200 @@
|
||||
"use strict";
|
||||
|
||||
// trusted_subnets is operator-configured in security.json via
|
||||
// POST /api/settings/security (behind destructive-action step-up) and its one
|
||||
// deliberate effect is a GLOBAL anti-abuse rate-limiter bypass: request IPs
|
||||
// inside the configured subnets skip generic throttling in BOTH limiter
|
||||
// stores (the Redis-backed /api globalLimiter and the app-wide in-memory
|
||||
// rateLimiterMiddleware). The bypass must stay out of every auth-path limiter
|
||||
// (login, recovery, 2FA step-up) — network position must never soften
|
||||
// brute-force protection — and db-proxy must stay free of trusted_subnets
|
||||
// wiring (the IP-authorization fallback there was removed on purpose).
|
||||
//
|
||||
// index.js boots servers on require, so the wiring is locked with source
|
||||
// assertions (same technique as audit-l3-l5-access-stepup.test.js); the
|
||||
// matcher utility itself is required and smoke-tested for real.
|
||||
|
||||
const test = require("node:test");
|
||||
const assert = require("node:assert");
|
||||
const fs = require("node:fs");
|
||||
const path = require("node:path");
|
||||
|
||||
const ROOT = path.join(__dirname, "..");
|
||||
const INDEX_JS = path.join(ROOT, "index.js");
|
||||
const DB_PROXY_JS = path.join(ROOT, "utils", "db-proxy.js");
|
||||
|
||||
const indexSource = fs.readFileSync(INDEX_JS, "utf8");
|
||||
const dbProxySource = fs.readFileSync(DB_PROXY_JS, "utf8");
|
||||
|
||||
function countOccurrences(haystack, needle) {
|
||||
return haystack.split(needle).length - 1;
|
||||
}
|
||||
|
||||
function sliceBetween(source, startMarker, endMarker) {
|
||||
const start = source.indexOf(startMarker);
|
||||
assert.ok(start >= 0, `start marker not found: ${startMarker}`);
|
||||
const end = source.indexOf(endMarker, start + startMarker.length);
|
||||
assert.ok(end > start, `end marker not found after start: ${endMarker}`);
|
||||
return source.slice(start, end);
|
||||
}
|
||||
|
||||
test("the util is genuinely loadable and the matcher works", () => {
|
||||
const { ipMatchesTrustedSubnets } = require("../utils/ip-subnet-validation");
|
||||
assert.strictEqual(typeof ipMatchesTrustedSubnets, "function");
|
||||
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);
|
||||
assert.strictEqual(ipMatchesTrustedSubnets("::ffff:10.0.0.55", ["10.0.0.0/24"]), true);
|
||||
assert.strictEqual(ipMatchesTrustedSubnets("10.0.0.55", []), false, "empty list fails closed");
|
||||
});
|
||||
|
||||
test("index.js imports ipMatchesTrustedSubnets from ./utils/ip-subnet-validation", () => {
|
||||
assert.match(
|
||||
indexSource,
|
||||
/ipMatchesTrustedSubnets,\s*\}\s*=\s*require\("\.\/utils\/ip-subnet-validation"\)/,
|
||||
"matcher must be imported alongside the other trusted-subnet helpers"
|
||||
);
|
||||
});
|
||||
|
||||
test("isRequestFromTrustedSubnet is defined exactly once, right after getRequestIp", () => {
|
||||
assert.strictEqual(countOccurrences(indexSource, "function isRequestFromTrustedSubnet(req)"), 1);
|
||||
const helper = sliceBetween(
|
||||
indexSource,
|
||||
"function isRequestFromTrustedSubnet(req) {",
|
||||
"function securityLog(msg, req) {"
|
||||
);
|
||||
// Fail-closed shape: reads the validated config through getRequestIp, and
|
||||
// treats the sentinel "unknown" (and any matcher error) as untrusted.
|
||||
assert.ok(helper.includes("getRequestIp(req)"), "IP resolution must use getRequestIp");
|
||||
assert.ok(helper.includes("ipMatchesTrustedSubnets(ip, subnets)"), "must delegate to the pure matcher");
|
||||
assert.ok(helper.includes('ip === "unknown"'), 'the "unknown" sentinel must count as untrusted');
|
||||
assert.ok(helper.includes("Array.isArray(subnets)"), "an absent or non-array config must fail closed");
|
||||
assert.ok(helper.includes("catch"), "any matcher error must count as untrusted");
|
||||
});
|
||||
|
||||
test("the Redis-backed globalLimiter skip() consults the trusted-subnet bypass before exempting paths", () => {
|
||||
// adpanel:rate-limit-policy-owner — the Redis-backed skip moved into the
|
||||
// ONE policy owner (services/global-rate-limit-owner.js); the pin tracks
|
||||
// the shipped skip there, preserving the intent exactly: the rate_limiting
|
||||
// gate, then the trusted-subnet bypass, then the path exemptions — and the
|
||||
// exemptions still consult the ORIGINAL request path because the skip runs
|
||||
// inside app.use("/api/"), which strips the mount prefix from req.path.
|
||||
const ownerSource = fs.readFileSync(path.join(ROOT, "services", "global-rate-limit-owner.js"), "utf8");
|
||||
const limiter = sliceBetween(
|
||||
ownerSource,
|
||||
"skip: (req) => {",
|
||||
"handler: (req, res, next, options) => {"
|
||||
);
|
||||
const rateLimitingCheck = limiter.indexOf('if (!security || security.rate_limiting !== true) return true;');
|
||||
const trustedCheck = limiter.indexOf("if (isRequestFromTrustedSubnet(req)) return true;");
|
||||
const firstExemptPath = limiter.indexOf('const path = String(req.originalUrl || req.url || "").split("?")[0];');
|
||||
const exemptConsult = limiter.indexOf("if (isRateLimitExempt(path, req.method)) return true;");
|
||||
assert.ok(rateLimitingCheck !== -1, "rate_limiting gate present");
|
||||
assert.ok(trustedCheck !== -1, "trusted-subnet bypass call present in the Redis skip()");
|
||||
assert.ok(firstExemptPath !== -1, "exempt-path consult derives its path from req.originalUrl");
|
||||
assert.ok(exemptConsult !== -1, "the skip consults the ONE shared isRateLimitExempt helper (no duplicated inline list)");
|
||||
assert.ok(
|
||||
rateLimitingCheck < trustedCheck && trustedCheck < firstExemptPath && firstExemptPath <= exemptConsult,
|
||||
"the bypass must sit after the rate_limiting gate and before the path exemptions"
|
||||
);
|
||||
// The old inline "/api/me" comparison lives only in the shared helper now.
|
||||
assert.ok(
|
||||
indexSource.includes('"/api/me",'),
|
||||
"index.js's shared exempt list keeps the /api/me entry the skip used to compare inline"
|
||||
);
|
||||
});
|
||||
|
||||
test("the in-memory rateLimiterMiddleware consults the bypass before yield, capacity and counting", () => {
|
||||
const middleware = sliceBetween(
|
||||
indexSource,
|
||||
"function rateLimiterMiddleware(req, res, next) {",
|
||||
"setInterval(() => {"
|
||||
);
|
||||
const rateLimitingCheck = middleware.indexOf("if (!security || security.rate_limiting !== true) return next();");
|
||||
const trustedCheck = middleware.indexOf("if (isRequestFromTrustedSubnet(req)) return next();");
|
||||
const redisYield = middleware.indexOf("redisApiLimiterMounted && redisClient && redisClient.isReady");
|
||||
const capacityGuard = middleware.indexOf("rateRequests.size >= RATE_MAX_TRACKED_IPS");
|
||||
const counting = middleware.indexOf("arr.push(now)");
|
||||
assert.ok(rateLimitingCheck !== -1, "rate_limiting gate present");
|
||||
assert.ok(trustedCheck !== -1, "trusted-subnet bypass call present in the in-memory limiter");
|
||||
assert.ok(redisYield !== -1, "redis-yield check present");
|
||||
assert.ok(capacityGuard !== -1, "tracked-IP capacity guard present");
|
||||
assert.ok(counting !== -1, "request counting present");
|
||||
assert.ok(
|
||||
rateLimitingCheck < trustedCheck && trustedCheck < redisYield && redisYield < capacityGuard && capacityGuard < counting,
|
||||
"a trusted IP must be released before it can be counted, 429'd or 503'd"
|
||||
);
|
||||
});
|
||||
|
||||
test("both stores share the identical-policy invariant, documented and enforced", () => {
|
||||
assert.strictEqual(
|
||||
countOccurrences(indexSource, "if (isRequestFromTrustedSubnet(req)) return next();"),
|
||||
1,
|
||||
"exactly one in-memory call site"
|
||||
);
|
||||
// adpanel:rate-limit-policy-owner — the Redis skip() lives in the ONE
|
||||
// policy owner now; there must be exactly one call site across index.js
|
||||
// and the owner module, and no resurrected inline copy in index.js.
|
||||
const ownerSource = fs.readFileSync(path.join(ROOT, "services", "global-rate-limit-owner.js"), "utf8");
|
||||
assert.strictEqual(
|
||||
countOccurrences(indexSource, "if (isRequestFromTrustedSubnet(req)) return true;") +
|
||||
countOccurrences(ownerSource, "if (isRequestFromTrustedSubnet(req)) return true;"),
|
||||
1,
|
||||
"exactly one Redis skip() call site (in the owner module)"
|
||||
);
|
||||
const comment = sliceBetween(
|
||||
indexSource,
|
||||
"const rateRequests = new Map();",
|
||||
"const RATE_LIMIT_EXEMPT_PATHS"
|
||||
);
|
||||
assert.ok(
|
||||
/trusted-subnet bypass is likewise applied identically in both stores/.test(comment),
|
||||
"the sync comment must cover the trusted-subnet bypass too"
|
||||
);
|
||||
assert.ok(
|
||||
comment.includes("ONE exempt-list source"),
|
||||
"the sync comment must name the shared exempt-list helper as the single source"
|
||||
);
|
||||
});
|
||||
|
||||
test("the bypass is locked OUT of every other consumer", () => {
|
||||
// index.js: 1 definition + 3 limiter call sites + 1 owner dep injection.
|
||||
// The three call sites are GENERIC throttlers (the app-wide in-memory
|
||||
// rateLimiterMiddleware, the statusEndpointsRateLimiter covering the status
|
||||
// endpoints the other budget skips by name, and the activityLogRateLimiter
|
||||
// on the activity-log reader — same rationale as the sync comment between
|
||||
// them: read-path abuse brakes, not auth gates). The Redis-backed /api
|
||||
// globalLimiter's call site moved into the ONE policy owner
|
||||
// (services/global-rate-limit-owner.js, pinned at 1 occurrence below),
|
||||
// which index.js injects isRequestFromTrustedSubnet into — same bypass,
|
||||
// same generic budget. No auth-path limiter (login, recovery, 2FA step-up)
|
||||
// and no other route may consult network position: network position must
|
||||
// never soften brute-force protection. The dedicated progress-poll budget
|
||||
// deliberately does NOT join the bypass consumer set (it is not a generic
|
||||
// /api budget — adpanel:progress-poll-budget).
|
||||
const ownerSource = fs.readFileSync(path.join(ROOT, "services", "global-rate-limit-owner.js"), "utf8");
|
||||
assert.strictEqual(
|
||||
countOccurrences(indexSource, "isRequestFromTrustedSubnet"),
|
||||
5,
|
||||
"isRequestFromTrustedSubnet must appear exactly 5 times in index.js (definition + 3 generic-budget call sites + the owner dep injection)"
|
||||
);
|
||||
assert.strictEqual(
|
||||
countOccurrences(ownerSource, "if (isRequestFromTrustedSubnet(req)) return true;"),
|
||||
1,
|
||||
"the owner module carries exactly the one Redis-skip call site"
|
||||
);
|
||||
assert.strictEqual(
|
||||
dbProxySource.includes("trusted_subnets"),
|
||||
false,
|
||||
"db-proxy must not read trusted_subnets"
|
||||
);
|
||||
assert.strictEqual(
|
||||
dbProxySource.includes("ipMatchesTrustedSubnets"),
|
||||
false,
|
||||
"db-proxy must not use the bypass matcher"
|
||||
);
|
||||
assert.strictEqual(
|
||||
dbProxySource.includes("isIpAuthorized"),
|
||||
false,
|
||||
"db-proxy IP-authorization stays removed (audit finding 2.4)"
|
||||
);
|
||||
});
|
||||
Reference in new issue
Block a user