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,234 @@
|
||||
"use strict";
|
||||
|
||||
// Tests for the local template-override trust boundary (audit finding
|
||||
// "A local directory is silently merged into the store as if vetted").
|
||||
//
|
||||
// <app>/github/<category>/*.json used to be merged into the store cache with
|
||||
// full trust: any local writer got its templates listed to admins as store
|
||||
// items (with fabricated github.com source URLs) and could shadow real
|
||||
// upstream files by filename. The remediation under test:
|
||||
// 1. The merge is off unless ADPANEL_TEMPLATE_STORE_LOCAL_OVERRIDES=1.
|
||||
// 2. When enabled, only files the panel user itself controls are accepted
|
||||
// (no group/world-writable paths, no symlinks, no foreign owners, size
|
||||
// capped like the remote fetch path).
|
||||
// 3. An override colliding with an upstream filename never replaces it.
|
||||
// 4. Serve-time provenance marks non-upstream files untrusted and never
|
||||
// fabricates upstream URLs for them.
|
||||
//
|
||||
// index.js boots servers on require, so the store helpers are sliced out of
|
||||
// the source and evaluated in a vm sandbox (same technique as
|
||||
// runtime-process-command.test.js).
|
||||
|
||||
const test = require("node:test");
|
||||
const assert = require("node:assert");
|
||||
const fs = require("node:fs");
|
||||
const fsp = require("node:fs/promises");
|
||||
const os = require("node:os");
|
||||
const path = require("node:path");
|
||||
const vm = require("node:vm");
|
||||
|
||||
const INDEX_JS = path.join(__dirname, "..", "index.js");
|
||||
|
||||
function endIndexOfFunction(source, startMarker) {
|
||||
const start = source.indexOf(startMarker);
|
||||
assert.ok(start >= 0, `marker ${startMarker} not found in index.js`);
|
||||
const bodyStart = source.indexOf("{", start);
|
||||
let depth = 0;
|
||||
for (let i = bodyStart; i < source.length; i += 1) {
|
||||
if (source[i] === "{") depth += 1;
|
||||
else if (source[i] === "}") {
|
||||
depth -= 1;
|
||||
if (depth === 0) return i + 1;
|
||||
}
|
||||
}
|
||||
assert.fail(`unbalanced braces after marker ${startMarker}`);
|
||||
}
|
||||
|
||||
function buildStoreCode() {
|
||||
const source = fs.readFileSync(INDEX_JS, "utf8");
|
||||
// Store constants + trust helpers + the local-override reader, then the
|
||||
// URL builders, then the serve-time provenance helper.
|
||||
const constStart = source.indexOf("const TEMPLATE_STORE_CACHE_DIR");
|
||||
assert.ok(constStart >= 0, "template store constants not found");
|
||||
const readerEnd = endIndexOfFunction(source, "async function readTemplateStoreLocalOverrides");
|
||||
const urlStart = source.indexOf("function templateStoreGithubBlobUrl");
|
||||
assert.ok(urlStart >= 0, "github blob url builder not found");
|
||||
const urlEnd = endIndexOfFunction(source, "function templateStoreJsdelivrRawUrl");
|
||||
const provenanceStart = source.indexOf("const TEMPLATE_STORE_UPSTREAM_SOURCES");
|
||||
assert.ok(provenanceStart >= 0, "upstream-sources set not found");
|
||||
const provenanceEnd = endIndexOfFunction(source, "function templateStoreCacheFileProvenance");
|
||||
return [
|
||||
source.slice(constStart, readerEnd),
|
||||
source.slice(urlStart, urlEnd),
|
||||
source.slice(provenanceStart, provenanceEnd),
|
||||
].join("\n");
|
||||
}
|
||||
|
||||
const STORE_CODE = buildStoreCode();
|
||||
|
||||
// A fresh sandbox per scenario: its own env (so the opt-in flag and size
|
||||
// limit vary per test) and its own app dir backing the github/ tree.
|
||||
async function buildSandbox(env = {}) {
|
||||
const appDir = await fsp.mkdtemp(path.join(os.tmpdir(), "adpanel-store-test-"));
|
||||
const sandbox = {
|
||||
console: { log() { }, warn() { }, error() { } },
|
||||
process: { env: { ...env }, getuid: () => process.getuid() },
|
||||
path,
|
||||
fs: require("node:fs"),
|
||||
crypto: require("node:crypto"),
|
||||
fsp,
|
||||
__dirname: appDir,
|
||||
normalizeTemplateDefinition: (template) => template,
|
||||
// The sliced store constants now include the trust-anchor block, which
|
||||
// resolves the signing key/mode through utils/template-store-trust.js the
|
||||
// same way index.js does.
|
||||
...require("../utils/template-store-trust"),
|
||||
};
|
||||
vm.createContext(sandbox);
|
||||
vm.runInContext(STORE_CODE, sandbox);
|
||||
return { sandbox, appDir };
|
||||
}
|
||||
|
||||
const VALID_TEMPLATE = JSON.stringify({
|
||||
id: "totally-legit",
|
||||
name: "Totally Legit",
|
||||
docker: { image: "evil/evil", tag: "latest", ports: [8080] },
|
||||
});
|
||||
|
||||
async function writeOverride(appDir, categoryDirName, fileName, content = VALID_TEMPLATE, mode = 0o644) {
|
||||
const dir = path.join(appDir, "github", categoryDirName);
|
||||
await fsp.mkdir(dir, { recursive: true, mode: 0o755 });
|
||||
const filePath = path.join(dir, fileName);
|
||||
await fsp.writeFile(filePath, content, "utf8");
|
||||
await fsp.chmod(filePath, mode);
|
||||
return filePath;
|
||||
}
|
||||
|
||||
const GAMES = "{ id: 'games', label: 'Games' }";
|
||||
|
||||
test("local overrides are disabled unless ADPANEL_TEMPLATE_STORE_LOCAL_OVERRIDES opts in", async () => {
|
||||
const { sandbox, appDir } = await buildSandbox({});
|
||||
await writeOverride(appDir, "games", "innocent.json");
|
||||
const overrides = await vm.runInContext(`readTemplateStoreLocalOverrides(${GAMES})`, sandbox);
|
||||
assert.deepEqual(overrides, []);
|
||||
});
|
||||
|
||||
test("enabled merge accepts a panel-owned override with safe permissions", async () => {
|
||||
const { sandbox, appDir } = await buildSandbox({ ADPANEL_TEMPLATE_STORE_LOCAL_OVERRIDES: "1" });
|
||||
await writeOverride(appDir, "games", "custom.json");
|
||||
const overrides = await vm.runInContext(`readTemplateStoreLocalOverrides(${GAMES})`, sandbox);
|
||||
assert.equal(overrides.length, 1);
|
||||
assert.equal(overrides[0].fileName, "custom.json");
|
||||
assert.ok(JSON.parse(overrides[0].raw).id === "totally-legit");
|
||||
assert.match(overrides[0].hash, /^[0-9a-f]{64}$/);
|
||||
});
|
||||
|
||||
test("enabled merge rejects a group-writable override file", async () => {
|
||||
const { sandbox, appDir } = await buildSandbox({ ADPANEL_TEMPLATE_STORE_LOCAL_OVERRIDES: "1" });
|
||||
await writeOverride(appDir, "games", "groupwritable.json", VALID_TEMPLATE, 0o664);
|
||||
const overrides = await vm.runInContext(`readTemplateStoreLocalOverrides(${GAMES})`, sandbox);
|
||||
assert.deepEqual(overrides, []);
|
||||
});
|
||||
|
||||
test("enabled merge rejects a world-writable override directory", async () => {
|
||||
const { sandbox, appDir } = await buildSandbox({ ADPANEL_TEMPLATE_STORE_LOCAL_OVERRIDES: "1" });
|
||||
await writeOverride(appDir, "games", "fine.json");
|
||||
await fsp.chmod(path.join(appDir, "github", "games"), 0o777);
|
||||
const overrides = await vm.runInContext(`readTemplateStoreLocalOverrides(${GAMES})`, sandbox);
|
||||
assert.deepEqual(overrides, []);
|
||||
});
|
||||
|
||||
test("enabled merge rejects a symlinked override file", async () => {
|
||||
const { sandbox, appDir } = await buildSandbox({ ADPANEL_TEMPLATE_STORE_LOCAL_OVERRIDES: "1" });
|
||||
const target = path.join(appDir, "attacker-controlled.json");
|
||||
await fsp.writeFile(target, VALID_TEMPLATE, "utf8");
|
||||
const dir = path.join(appDir, "github", "games");
|
||||
await fsp.mkdir(dir, { recursive: true });
|
||||
await fsp.symlink(target, path.join(dir, "puppet.json"));
|
||||
const overrides = await vm.runInContext(`readTemplateStoreLocalOverrides(${GAMES})`, sandbox);
|
||||
assert.deepEqual(overrides, []);
|
||||
});
|
||||
|
||||
test("enabled merge rejects an override exceeding the store size limit", async () => {
|
||||
const { sandbox, appDir } = await buildSandbox({
|
||||
ADPANEL_TEMPLATE_STORE_LOCAL_OVERRIDES: "1",
|
||||
// Floor is 32 KiB, so use that and write something larger.
|
||||
ADPANEL_TEMPLATE_STORE_FILE_MAX_BYTES: "32768",
|
||||
});
|
||||
const big = JSON.stringify({ id: "big", name: "Big", pad: "x".repeat(64 * 1024) });
|
||||
await writeOverride(appDir, "games", "toobig.json", big);
|
||||
const overrides = await vm.runInContext(`readTemplateStoreLocalOverrides(${GAMES})`, sandbox);
|
||||
assert.deepEqual(overrides, []);
|
||||
});
|
||||
|
||||
test("enabled merge rejects an override owned by another uid", { skip: process.getuid() !== 0 }, async () => {
|
||||
const { sandbox, appDir } = await buildSandbox({ ADPANEL_TEMPLATE_STORE_LOCAL_OVERRIDES: "1" });
|
||||
const filePath = await writeOverride(appDir, "games", "foreign.json");
|
||||
await fsp.chown(filePath, 4242, 4242);
|
||||
const overrides = await vm.runInContext(`readTemplateStoreLocalOverrides(${GAMES})`, sandbox);
|
||||
assert.deepEqual(overrides, []);
|
||||
});
|
||||
|
||||
test("an override may never shadow an upstream store filename", async () => {
|
||||
const { sandbox } = await buildSandbox({});
|
||||
const shadow = await vm.runInContext(
|
||||
"selectTemplateStoreLocalOverrideDisposition({ fileName: 'minecraft.json' }, new Set(['minecraft.json', 'rust.json']))",
|
||||
sandbox
|
||||
);
|
||||
assert.equal(shadow.apply, false);
|
||||
assert.match(shadow.reason, /minecraft\.json/);
|
||||
const unique = await vm.runInContext(
|
||||
"selectTemplateStoreLocalOverrideDisposition({ fileName: 'my-own.json' }, new Set(['minecraft.json']))",
|
||||
sandbox
|
||||
);
|
||||
assert.equal(unique.apply, true);
|
||||
assert.equal(unique.reason, "");
|
||||
});
|
||||
|
||||
test("serve-time provenance never fabricates upstream URLs for non-upstream files", async () => {
|
||||
const { sandbox } = await buildSandbox({});
|
||||
const cases = [
|
||||
{
|
||||
label: "local-override meta",
|
||||
meta: { source: "local-override", html_url: "", download_url: "" },
|
||||
untrusted: true,
|
||||
expectUrls: false,
|
||||
},
|
||||
{
|
||||
label: "orphan cache file without manifest meta",
|
||||
meta: {},
|
||||
untrusted: true,
|
||||
expectUrls: false,
|
||||
},
|
||||
{
|
||||
label: "upstream github-raw meta with explicit urls",
|
||||
meta: {
|
||||
source: "github-raw",
|
||||
html_url: "https://github.com/r/blob/main/games/a.json",
|
||||
download_url: "https://cdn.jsdelivr.net/gh/r@main/games/a.json",
|
||||
},
|
||||
untrusted: false,
|
||||
expectUrls: true,
|
||||
},
|
||||
{
|
||||
label: "upstream jsdelivr meta with missing urls falls back to repo urls",
|
||||
meta: { source: "jsdelivr" },
|
||||
untrusted: false,
|
||||
expectUrls: true,
|
||||
},
|
||||
];
|
||||
for (const testCase of cases) {
|
||||
const provenance = await vm.runInContext(
|
||||
`templateStoreCacheFileProvenance(${JSON.stringify(testCase.meta)}, "games/a.json")`,
|
||||
sandbox
|
||||
);
|
||||
assert.equal(provenance.untrusted, testCase.untrusted, testCase.label);
|
||||
if (testCase.expectUrls) {
|
||||
assert.ok(provenance.html_url.length > 0, testCase.label);
|
||||
assert.ok(provenance.download_url.length > 0, testCase.label);
|
||||
} else {
|
||||
assert.equal(provenance.html_url, "", testCase.label);
|
||||
assert.equal(provenance.download_url, "", testCase.label);
|
||||
}
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user