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,203 @@
|
||||
"use strict";
|
||||
|
||||
// Regression tests for the store-apply destPath constraint (audit finding 4).
|
||||
//
|
||||
// POST /api/servers/:bot/versions/apply is gated by store_access — a
|
||||
// browse-and-install permission, unlike the sibling write paths which demand
|
||||
// files_upload/files_create. Every store caller (the dashboard's Modrinth
|
||||
// flow, the assistant install_plugin tool) only ever targets the version core
|
||||
// (server.jar) or a plugin/mod build one level under plugins/ or mods/, so
|
||||
// the shared validateStoreApplyDestPath pins destPath to exactly that
|
||||
// surface. Before the constraint, a store-only role could write ANY filename
|
||||
// under ANY subdirectory of its own server root — e.g. the bot's own entry
|
||||
// script — with content fetched from any public URL. Traversal was already
|
||||
// refused, so the fix narrows the accepted surface to the intended one
|
||||
// instead of demanding a new permission (which would break the store-only
|
||||
// role the panel explicitly supports).
|
||||
|
||||
const test = require("node:test");
|
||||
const assert = require("node:assert");
|
||||
const crypto = require("crypto");
|
||||
|
||||
const {
|
||||
applyRemoteAssetToServer,
|
||||
validateStoreApplyDestPath,
|
||||
} = require("./server-asset-apply");
|
||||
|
||||
const DERIVED_SECRET = "ab".repeat(32);
|
||||
const NODE = {
|
||||
id: "node-1",
|
||||
uuid: "node-uuid-1",
|
||||
address: "203.0.113.10",
|
||||
api_port: 8080,
|
||||
ssl_enabled: false,
|
||||
};
|
||||
const ENTRY = { name: "Test Server", bot: "test-server", nodeId: "node-1" };
|
||||
const JAR_URL = "https://example.com/server.jar";
|
||||
|
||||
test("validateStoreApplyDestPath accepts the store's real destination surface", () => {
|
||||
const accepted = [
|
||||
// Absent/blank destinations default to the version core.
|
||||
["", "server.jar"],
|
||||
[" ", "server.jar"],
|
||||
[undefined, "server.jar"],
|
||||
[null, "server.jar"],
|
||||
// Explicit version core (leading slashes were always stripped).
|
||||
["server.jar", "server.jar"],
|
||||
["/server.jar", "server.jar"],
|
||||
// Modrinth plugin/mod builds: one file, one level deep.
|
||||
["plugins/Lithium.jar", "plugins/Lithium.jar"],
|
||||
["plugins/essentialsx-2.20.1.jar", "plugins/essentialsx-2.20.1.jar"],
|
||||
["mods/create-6.0.0.jar", "mods/create-6.0.0.jar"],
|
||||
["mods/old-mod.litemod", "mods/old-mod.litemod"],
|
||||
["plugins/resource-pack.zip", "plugins/resource-pack.zip"],
|
||||
];
|
||||
for (const [input, expected] of accepted) {
|
||||
const result = validateStoreApplyDestPath(input);
|
||||
assert.deepStrictEqual(result, { ok: true, destPath: expected }, `input: ${String(input)}`);
|
||||
}
|
||||
});
|
||||
|
||||
test("validateStoreApplyDestPath refuses write targets outside the store surface", () => {
|
||||
const rejected = [
|
||||
// The audit's example: overwriting the bot's own entry file.
|
||||
"discordbot/index.js",
|
||||
// Other arbitrary subdirectories and root files.
|
||||
"bot.js",
|
||||
"start.sh",
|
||||
"world/level.dat",
|
||||
"config/settings.yml",
|
||||
"plugins/nested/Lithium.jar",
|
||||
// No filename / bare directory.
|
||||
"plugins",
|
||||
"plugins/",
|
||||
"mods/.",
|
||||
// Non-store extensions and extensionless files.
|
||||
"plugins/payload.php",
|
||||
"plugins/payload.sh",
|
||||
"plugins/payload.js",
|
||||
"plugins/payload",
|
||||
"plugins/.jar",
|
||||
// Traversal and Windows-style separators (already refused before; must stay refused).
|
||||
"plugins/../index.js",
|
||||
"..",
|
||||
"plugins/..",
|
||||
"plugins/x..jar",
|
||||
"plugins\\Lithium.jar",
|
||||
"..\\plugins\\Lithium.jar",
|
||||
// Dot-segment and duplicate-slash shapes: the single-segment regex cannot
|
||||
// span a second "/", so these fail closed (found during verification).
|
||||
"plugins/./x.jar",
|
||||
"plugins//x.jar",
|
||||
"./plugins/x.jar",
|
||||
"plugins/x.jar/",
|
||||
// Case is exact: roots and the version core are lowercase tokens.
|
||||
"PLUGINS/Lithium.jar",
|
||||
"Plugins/Lithium.jar",
|
||||
"Server.jar",
|
||||
"server.JAR",
|
||||
];
|
||||
for (const input of rejected) {
|
||||
const result = validateStoreApplyDestPath(input);
|
||||
assert.deepStrictEqual(
|
||||
result,
|
||||
{ ok: false, error: "invalid-destPath" },
|
||||
`input: ${String(input)}`
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test("validateStoreApplyDestPath refuses NUL bytes in the destination", () => {
|
||||
assert.deepStrictEqual(validateStoreApplyDestPath("plugins/x\0.jar"), {
|
||||
ok: false,
|
||||
error: "invalid-destPath",
|
||||
});
|
||||
});
|
||||
|
||||
function makeDeps(overrides = {}) {
|
||||
const calls = { findServer: 0, nodeFsPost: [], httpRequestJson: [] };
|
||||
const deps = {
|
||||
findServer: async () => {
|
||||
calls.findServer += 1;
|
||||
return ENTRY;
|
||||
},
|
||||
isRemoteEntry: () => true,
|
||||
findNodeByIdOrName: async () => NODE,
|
||||
buildNodeBaseUrl: () => "http://203.0.113.10:8080",
|
||||
nodeUnderCloudflare: () => false,
|
||||
nodeAuthHeadersFor: () => ({
|
||||
"Content-Type": "application/json",
|
||||
Authorization: "Bearer node-token",
|
||||
}),
|
||||
nodeAdminSigningSecrets: () => [DERIVED_SECRET],
|
||||
httpRequestJson: async (url, method, headers, body) => {
|
||||
calls.httpRequestJson.push({ url, method, headers, body });
|
||||
return { status: 200, json: { ok: true } };
|
||||
},
|
||||
assertSafeRemoteUrl: async (u) => new URL(u),
|
||||
remoteApplyProxyDownload: false,
|
||||
remoteApplyMaxBytes: 1024 * 1024 * 1024,
|
||||
remoteApplyTimeoutMs: 300000,
|
||||
remoteFetchMaxRedirects: 4,
|
||||
...overrides,
|
||||
};
|
||||
return { deps, calls };
|
||||
}
|
||||
|
||||
test("applyRemoteAssetToServer refuses a hostile destPath before any lookup or node contact", async () => {
|
||||
for (const destPath of ["discordbot/index.js", "plugins/../start.sh", "start.sh", "plugins/nested/x.jar"]) {
|
||||
const { deps, calls } = makeDeps({
|
||||
findServer: async () => {
|
||||
throw new Error("findServer must not be reached for an invalid destPath");
|
||||
},
|
||||
httpRequestJson: async () => {
|
||||
throw new Error("no node contact may happen for an invalid destPath");
|
||||
},
|
||||
});
|
||||
const result = await applyRemoteAssetToServer(deps, {
|
||||
serverName: "test-server",
|
||||
entry: ENTRY,
|
||||
url: JAR_URL,
|
||||
destPath,
|
||||
providerId: "modrinth-plugin",
|
||||
versionId: "1.0.0",
|
||||
});
|
||||
assert.deepStrictEqual(result, { ok: false, statusCode: 400, error: "invalid-destPath" }, `destPath: ${destPath}`);
|
||||
assert.strictEqual(calls.findServer, 0);
|
||||
assert.strictEqual(calls.httpRequestJson.length, 0);
|
||||
}
|
||||
});
|
||||
|
||||
test("applyRemoteAssetToServer still forwards store-legal destinations to the node", async () => {
|
||||
for (const destPath of [undefined, "server.jar", "plugins/Lithium.jar", "mods/create-6.0.0.jar"]) {
|
||||
const { deps, calls } = makeDeps();
|
||||
const result = await applyRemoteAssetToServer(deps, {
|
||||
serverName: "test-server",
|
||||
entry: ENTRY,
|
||||
url: JAR_URL,
|
||||
destPath,
|
||||
});
|
||||
assert.strictEqual(result.ok, true, `destPath: ${String(destPath)}`);
|
||||
assert.strictEqual(result.destPath, destPath ? destPath : "server.jar");
|
||||
assert.strictEqual(calls.httpRequestJson.length, 1);
|
||||
assert.strictEqual(calls.httpRequestJson[0].body.destPath, destPath ? destPath : "server.jar");
|
||||
}
|
||||
});
|
||||
|
||||
test("applyRemoteAssetToServer signs the forwarded request with the validated destination", async () => {
|
||||
const { deps, calls } = makeDeps();
|
||||
const result = await applyRemoteAssetToServer(deps, {
|
||||
serverName: "test-server",
|
||||
entry: ENTRY,
|
||||
url: JAR_URL,
|
||||
destPath: "plugins/Lithium.jar",
|
||||
});
|
||||
assert.strictEqual(result.ok, true);
|
||||
const call = calls.httpRequestJson[0];
|
||||
assert.strictEqual(call.url, "http://203.0.113.10:8080/v1/servers/test-server/apply-version");
|
||||
const ts = call.headers["x-panel-ts"];
|
||||
const nonce = call.headers["x-panel-nonce"] || "";
|
||||
const base = `test-server|||${JAR_URL}|${ts}${nonce ? `|${nonce}` : ""}`;
|
||||
const expected = crypto.createHmac("sha256", DERIVED_SECRET).update(base).digest("hex");
|
||||
assert.strictEqual(call.headers["x-panel-sign"], expected);
|
||||
});
|
||||
Reference in new issue
Block a user