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,151 @@
|
||||
"use strict";
|
||||
|
||||
// Tests for the file-backed install job store. Contract under test:
|
||||
// - job ids are strictly allow-listed BEFORE any filesystem access
|
||||
// (req.params.jobId flows into a file path — traversal must be impossible);
|
||||
// - records round-trip, are replaced atomically and land with 0600 perms;
|
||||
// - missing/corrupt records fail open to null (progress telemetry, not
|
||||
// bookkeeping state).
|
||||
|
||||
const test = require("node:test");
|
||||
const assert = require("node:assert");
|
||||
const fs = require("node:fs");
|
||||
const os = require("node:os");
|
||||
const path = require("node:path");
|
||||
const crypto = require("node:crypto");
|
||||
|
||||
const {
|
||||
isValidInstallJobId,
|
||||
installJobPath,
|
||||
saveInstallJob,
|
||||
readInstallJob,
|
||||
deleteInstallJob
|
||||
} = require("./install-job-store");
|
||||
|
||||
function tmpDir(t) {
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "install-job-store-"));
|
||||
t.after(() => {
|
||||
try { fs.rmSync(dir, { recursive: true, force: true }); } catch (_) { }
|
||||
});
|
||||
return dir;
|
||||
}
|
||||
|
||||
const MONGODB_JOB = `mongodb-${crypto.randomUUID()}`;
|
||||
const PGADMIN_JOB = `pgadmin-${crypto.randomUUID()}`;
|
||||
|
||||
test("isValidInstallJobId accepts real crypto.randomUUID ids with lowercase type prefixes", () => {
|
||||
assert.strictEqual(isValidInstallJobId(MONGODB_JOB), true);
|
||||
assert.strictEqual(isValidInstallJobId(PGADMIN_JOB), true);
|
||||
});
|
||||
|
||||
test("isValidInstallJobId rejects traversal, junk, malformed and non-string ids", () => {
|
||||
const bad = [
|
||||
"../../etc/passwd",
|
||||
"mongodb-../../x",
|
||||
"mongodb-",
|
||||
"",
|
||||
`${PGADMIN_JOB}/../../escape`,
|
||||
`MongoDB-${crypto.randomUUID()}`, // uppercase prefix
|
||||
`PGADMIN-${crypto.randomUUID()}`, // uppercase prefix
|
||||
`mongodb-${crypto.randomUUID().toUpperCase()}`, // uppercase hex
|
||||
"mongodb-1234", // missing uuid segments
|
||||
"mongodb-deadbeef-dead-dead", // truncated uuid
|
||||
`${MONGODB_JOB}extra`, // trailing junk
|
||||
`${MONGODB_JOB} `, // trailing space
|
||||
` ${MONGODB_JOB}`, // leading space
|
||||
`${MONGODB_JOB}\0`, // embedded NUL
|
||||
`pgadmin-${crypto.randomUUID()}\n`, // trailing newline
|
||||
123,
|
||||
null,
|
||||
undefined,
|
||||
{},
|
||||
[]
|
||||
];
|
||||
for (const id of bad) {
|
||||
assert.strictEqual(isValidInstallJobId(id), false, `expected rejection: ${JSON.stringify(String(id))}`);
|
||||
}
|
||||
});
|
||||
|
||||
test("installJobPath joins the jobs dir and refuses invalid ids", () => {
|
||||
const dir = path.join(os.tmpdir(), "some-jobs");
|
||||
assert.strictEqual(installJobPath(dir, PGADMIN_JOB), path.join(dir, `${PGADMIN_JOB}.json`));
|
||||
assert.throws(() => installJobPath(dir, "../../evil"), /invalid install job id/i);
|
||||
assert.throws(() => installJobPath(dir, ""), /invalid install job id/i);
|
||||
});
|
||||
|
||||
test("save/read round-trip preserves every field", (t) => {
|
||||
const dir = tmpDir(t);
|
||||
const record = {
|
||||
type: "mongodb",
|
||||
status: "running",
|
||||
progress: 42,
|
||||
message: "Downloading packages...",
|
||||
startedAt: 1690000000000,
|
||||
completedAt: null,
|
||||
error: null,
|
||||
result: null,
|
||||
updatedAt: 1690000000123
|
||||
};
|
||||
saveInstallJob(dir, MONGODB_JOB, record);
|
||||
assert.deepStrictEqual(readInstallJob(dir, MONGODB_JOB), record);
|
||||
// Records are persisted 0600.
|
||||
assert.strictEqual(fs.statSync(installJobPath(dir, MONGODB_JOB)).mode & 0o777, 0o600);
|
||||
});
|
||||
|
||||
test("second save replaces the record; only new content is visible and no temp litter remains", (t) => {
|
||||
const dir = tmpDir(t);
|
||||
saveInstallJob(dir, MONGODB_JOB, { status: "running", progress: 10 });
|
||||
saveInstallJob(dir, MONGODB_JOB, { status: "completed", progress: 100, completedAt: 123 });
|
||||
assert.deepStrictEqual(readInstallJob(dir, MONGODB_JOB), { status: "completed", progress: 100, completedAt: 123 });
|
||||
assert.deepStrictEqual(fs.readdirSync(dir), [`${MONGODB_JOB}.json`]);
|
||||
});
|
||||
|
||||
test("delete removes the record; read of a missing record returns null; re-delete is a no-op", (t) => {
|
||||
const dir = tmpDir(t);
|
||||
saveInstallJob(dir, PGADMIN_JOB, { status: "starting" });
|
||||
assert.notStrictEqual(readInstallJob(dir, PGADMIN_JOB), null);
|
||||
deleteInstallJob(dir, PGADMIN_JOB);
|
||||
assert.strictEqual(readInstallJob(dir, PGADMIN_JOB), null);
|
||||
assert.deepStrictEqual(fs.readdirSync(dir), []);
|
||||
assert.doesNotThrow(() => deleteInstallJob(dir, PGADMIN_JOB));
|
||||
assert.strictEqual(readInstallJob(dir, PGADMIN_JOB), null);
|
||||
});
|
||||
|
||||
test("readInstallJob returns null (never throws) for a corrupt record file", (t) => {
|
||||
const dir = tmpDir(t);
|
||||
fs.writeFileSync(path.join(dir, `${MONGODB_JOB}.json`), Buffer.from([0x00, 0xff, 0x7b, 0xde, 0xad]), { mode: 0o600 });
|
||||
assert.strictEqual(readInstallJob(dir, MONGODB_JOB), null);
|
||||
});
|
||||
|
||||
test("readInstallJob treats a payload parsing to a non-object as absent", (t) => {
|
||||
const dir = tmpDir(t);
|
||||
for (const payload of ["null", '"running"', "42", ""]) {
|
||||
fs.writeFileSync(path.join(dir, `${MONGODB_JOB}.json`), payload, { mode: 0o600 });
|
||||
assert.strictEqual(readInstallJob(dir, MONGODB_JOB), null, `payload: ${payload}`);
|
||||
}
|
||||
});
|
||||
|
||||
test("saveInstallJob creates a missing jobs dir (nested, recursive)", (t) => {
|
||||
const dir = tmpDir(t);
|
||||
const jobs = path.join(dir, "data", "install-jobs");
|
||||
saveInstallJob(jobs, MONGODB_JOB, { status: "starting" });
|
||||
assert.deepStrictEqual(readInstallJob(jobs, MONGODB_JOB), { status: "starting" });
|
||||
});
|
||||
|
||||
test("traversal job ids never touch the filesystem", (t) => {
|
||||
const parent = tmpDir(t);
|
||||
const dir = path.join(parent, "install-jobs");
|
||||
fs.mkdirSync(dir);
|
||||
const before = fs.readdirSync(parent).sort();
|
||||
|
||||
assert.throws(() => saveInstallJob(dir, "../../evil", { status: "pwn" }));
|
||||
assert.strictEqual(readInstallJob(dir, "../../evil"), null);
|
||||
assert.strictEqual(readInstallJob(dir, `mongodb-${crypto.randomUUID()}/../evil`), null);
|
||||
assert.doesNotThrow(() => deleteInstallJob(dir, "../../evil"));
|
||||
|
||||
// Nothing escaped the jobs dir: the parent gained no entries and no
|
||||
// evil.json exists anywhere it should not.
|
||||
assert.deepStrictEqual(fs.readdirSync(parent).sort(), before);
|
||||
assert.strictEqual(fs.existsSync(path.join(parent, "evil.json")), false);
|
||||
assert.strictEqual(fs.existsSync(path.join(dir, "evil.json")), false);
|
||||
});
|
||||
Reference in new issue
Block a user