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,160 @@
|
||||
"use strict";
|
||||
|
||||
// Pins the always-on abuse brakes for the store/versions surface and the
|
||||
// streaming (non-buffering) shape of the proxy-mode apply.
|
||||
//
|
||||
// index.js is a ~52k-line application bundle (module side effects, DB and
|
||||
// network at require time), so — matching the convention of
|
||||
// tests/versions-routes-access-order.test.js — these regressions are pinned
|
||||
// structurally on well-anchored source windows.
|
||||
//
|
||||
// Threat being pinned: the shipped security.json has rate_limiting: false, so
|
||||
// the global /api limiter is off in the default deployment. The store-apply
|
||||
// chain (permissions mint → versions/apply) and the papermc/purpurmc manifest
|
||||
// proxies previously had no limiter of their own, so a single authenticated
|
||||
// tenant could loop mint→apply against a large file it hosted (worker OOM via
|
||||
// the old heap-buffered download, now also bounded by the streaming fix and
|
||||
// the download gate) or fire unbounded proxied fetches upstream (panel IP
|
||||
// blocked). These pins fail if any of the four brakes is silently unmounted or
|
||||
// re-coupled to security.rate_limiting, or if the apply path ever goes back to
|
||||
// the buffered httpGetRaw getter.
|
||||
|
||||
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_SRC = fs.readFileSync(path.join(ROOT, "index.js"), "utf8");
|
||||
const APPLY_UTIL_SRC = fs.readFileSync(path.join(ROOT, "utils", "server-asset-apply.js"), "utf8");
|
||||
|
||||
const REG_POSITIONS = (() => {
|
||||
const positions = [];
|
||||
const re = /^(?:app|router)\.(?:get|post|put|delete|patch|all|use)\s*\(/gm;
|
||||
let m;
|
||||
while ((m = re.exec(INDEX_SRC)) !== null) positions.push(m.index);
|
||||
return positions;
|
||||
})();
|
||||
|
||||
function routeWindow(regPrefix, label) {
|
||||
const start = INDEX_SRC.indexOf(regPrefix);
|
||||
assert.notStrictEqual(start, -1, `${label}: route anchor not found: ${regPrefix}`);
|
||||
let end = INDEX_SRC.length;
|
||||
for (const p of REG_POSITIONS) {
|
||||
if (p > start && p < end) end = p;
|
||||
}
|
||||
assert.ok(end > start, `${label}: empty window for ${regPrefix}`);
|
||||
return INDEX_SRC.slice(start, end);
|
||||
}
|
||||
|
||||
function definitionWindow(constName, label) {
|
||||
const policies = require("../services/settings-rate-limiters").SETTINGS_OPERATION_POLICIES;
|
||||
if (policies[constName]) {
|
||||
assert.ok(INDEX_SRC.includes("createSettingsRateLimiters("), "the extracted policy fleet is wired");
|
||||
return JSON.stringify(policies[constName]);
|
||||
}
|
||||
const anchor = `const ${constName} = rateLimit({`;
|
||||
const start = INDEX_SRC.indexOf(anchor);
|
||||
assert.notStrictEqual(start, -1, `${label}: limiter definition not found: ${anchor}`);
|
||||
const end = INDEX_SRC.indexOf("});", start);
|
||||
assert.notStrictEqual(end, -1, `${label}: unterminated limiter definition`);
|
||||
return INDEX_SRC.slice(start, end);
|
||||
}
|
||||
|
||||
test("POST versions/apply mounts its always-on store-apply brake", () => {
|
||||
const registration = 'app.post("/api/servers/:bot/versions/apply", storeApplyRateLimiter, async';
|
||||
assert.ok(
|
||||
INDEX_SRC.includes(registration),
|
||||
`expected ${registration} in index.js — the apply route must keep its dedicated limiter (the global one ships disabled via security.json)`
|
||||
);
|
||||
// The brake is unconditional: it must not consult security.rate_limiting.
|
||||
const w = definitionWindow("storeApplyRateLimiter", "storeApplyRateLimiter");
|
||||
assert.ok(!w.includes("rate_limiting"), "storeApplyRateLimiter must be an always-on abuse brake");
|
||||
});
|
||||
|
||||
test("GET permissions mounts its always-on mint brake behind the cross-site guard", () => {
|
||||
const registration = 'app.get("/api/servers/:name/permissions", rejectCrossSiteRead, serverPermissionsMintRateLimiter, async';
|
||||
assert.ok(
|
||||
INDEX_SRC.includes(registration),
|
||||
`expected ${registration} in index.js — the token mint must keep its dedicated limiter, and it must stay behind rejectCrossSiteRead so rejected cross-site reads never consume budget`
|
||||
);
|
||||
const w = definitionWindow("serverPermissionsMintRateLimiter", "serverPermissionsMintRateLimiter");
|
||||
assert.ok(!w.includes("rate_limiting"), "serverPermissionsMintRateLimiter must be an always-on abuse brake");
|
||||
});
|
||||
|
||||
for (const route of ["/api/papermc/*", "/api/purpurmc/*"]) {
|
||||
test(`GET ${route} mounts both manifest-proxy brakes`, () => {
|
||||
const registration = `app.get("${route}", storeManifestProxySessionRateLimiter, storeManifestProxyIpRateLimiter, async`;
|
||||
assert.ok(
|
||||
INDEX_SRC.includes(registration),
|
||||
`expected ${registration} in index.js — the manifest proxies are an outbound-fetch amplifier without their own always-on brakes`
|
||||
);
|
||||
for (const name of ["storeManifestProxySessionRateLimiter", "storeManifestProxyIpRateLimiter"]) {
|
||||
const w = definitionWindow(name, name);
|
||||
assert.ok(!w.includes("rate_limiting"), `${name} must be an always-on abuse brake`);
|
||||
}
|
||||
// Anonymous requests are skipped on both: unauthenticated probes must not
|
||||
// burn a shared (NAT) IP's budget before the route's own 401 answers them.
|
||||
for (const name of ["storeManifestProxySessionRateLimiter", "storeManifestProxyIpRateLimiter"]) {
|
||||
const w = definitionWindow(name, name);
|
||||
assert.ok(w.includes("skip: (req) => !req.session?.user"), `${name} must skip anonymous requests`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
test("the proxy-mode apply streams to disk and never touches the buffered getter", () => {
|
||||
// The buffered getter is banned on this path: its chunks+Buffer.concat held
|
||||
// up to 2x REMOTE_APPLY_MAX_BYTES of heap per request. (The util's comment
|
||||
// may still NAME httpGetRaw when documenting why — the pin targets the call
|
||||
// shape; tests/server-asset-apply-streaming.test.js additionally enforces
|
||||
// non-usage at runtime with a throwing dep.)
|
||||
assert.ok(
|
||||
!APPLY_UTIL_SRC.includes("deps.httpGetRaw") && !/httpGetRaw\s*\(/.test(APPLY_UTIL_SRC),
|
||||
"server-asset-apply.js must not call httpGetRaw — the apply path streams"
|
||||
);
|
||||
assert.ok(
|
||||
APPLY_UTIL_SRC.includes("deps.httpGetStream"),
|
||||
"the proxy path must consume the injected streaming getter"
|
||||
);
|
||||
assert.ok(
|
||||
APPLY_UTIL_SRC.includes('require("stream/promises")') && APPLY_UTIL_SRC.includes("pipeline("),
|
||||
"the download must be pipelined (backpressure + guaranteed stream teardown)"
|
||||
);
|
||||
assert.ok(
|
||||
APPLY_UTIL_SRC.includes('flags: "wx"'),
|
||||
'the temp artifact file must be created exclusive ("wx") so a pre-planted temp symlink is never followed'
|
||||
);
|
||||
// Defense in depth: the util enforces the byte cap itself, not just the
|
||||
// getter it calls.
|
||||
assert.ok(
|
||||
APPLY_UTIL_SRC.includes("Download too large"),
|
||||
"the util's counting Transform must enforce remoteApplyMaxBytes independently"
|
||||
);
|
||||
// The download is gated: no deps wiring may bypass the concurrency cap.
|
||||
assert.ok(
|
||||
APPLY_UTIL_SRC.includes("remoteApplyDownloadGate || defaultDownloadGate"),
|
||||
"the proxy path must consult the download gate (injectable, module default otherwise)"
|
||||
);
|
||||
const wiringCount = INDEX_SRC.split("httpGetStream: ssrfPipedHttpGet").length - 1;
|
||||
assert.strictEqual(
|
||||
wiringCount,
|
||||
2,
|
||||
"both applyRemoteAssetToServer dep objects (route + serverDeps wrapper) must wire the streaming getter"
|
||||
);
|
||||
});
|
||||
|
||||
test("the download gate is sized by env knobs with sane floors", () => {
|
||||
assert.ok(
|
||||
APPLY_UTIL_SRC.includes("REMOTE_APPLY_MAX_CONCURRENT_DOWNLOADS"),
|
||||
"the concurrency cap must be operator-tunable via env"
|
||||
);
|
||||
assert.ok(
|
||||
APPLY_UTIL_SRC.includes("REMOTE_APPLY_DOWNLOAD_QUEUE_WAIT_MS"),
|
||||
"the queue budget must be operator-tunable via env"
|
||||
);
|
||||
// A zero/negative cap would gate forever; parse must floor it.
|
||||
assert.ok(
|
||||
APPLY_UTIL_SRC.includes("n > 0 ? n : 3"),
|
||||
"the concurrency cap must fall back to its default on a non-positive value"
|
||||
);
|
||||
});
|
||||
Reference in new issue
Block a user