TASK22019: phase 9 harden file handlers hash guards and negative paths
This commit is contained in:
@@ -0,0 +1,250 @@
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const vm = require("vm");
|
||||
const assert = require("assert");
|
||||
|
||||
const rootDir = path.resolve(__dirname, "..", "..");
|
||||
|
||||
const loadModule = (relativePath, injected = {}) => {
|
||||
const filePath = path.join(rootDir, relativePath);
|
||||
let source = fs.readFileSync(filePath, "utf8");
|
||||
|
||||
source = source.replace(/import[\s\S]*?from\s+"[^"]+";\n?/g, "");
|
||||
source = source.replace(
|
||||
/export default async function\s+(\w+)\s*\(/,
|
||||
"async function $1("
|
||||
);
|
||||
source = source.replace(/export const\s+/g, "const ");
|
||||
source = source.replace(
|
||||
/export default\s+(\w+);/g,
|
||||
"module.exports.default = $1;"
|
||||
);
|
||||
|
||||
source +=
|
||||
'\nif (typeof ApiProxy !== "undefined" && !module.exports.default) module.exports.default = ApiProxy;\n';
|
||||
|
||||
const context = {
|
||||
module: { exports: {} },
|
||||
exports: {},
|
||||
require,
|
||||
process,
|
||||
console: {
|
||||
log: () => {},
|
||||
info: () => {},
|
||||
warn: () => {},
|
||||
error: () => {}
|
||||
},
|
||||
...injected
|
||||
};
|
||||
|
||||
vm.runInNewContext(source, context, { filename: filePath });
|
||||
return context.module.exports;
|
||||
};
|
||||
|
||||
const createNextConnectMock = () => {
|
||||
const router = {
|
||||
handler: null,
|
||||
use: () => {},
|
||||
get(fn) {
|
||||
this.handler = fn;
|
||||
},
|
||||
post(fn) {
|
||||
this.handler = fn;
|
||||
}
|
||||
};
|
||||
|
||||
return () => router;
|
||||
};
|
||||
|
||||
const createRes = () => {
|
||||
const state = {
|
||||
statusCode: null,
|
||||
jsonBody: undefined
|
||||
};
|
||||
|
||||
return {
|
||||
state,
|
||||
status(code) {
|
||||
state.statusCode = code;
|
||||
return this;
|
||||
},
|
||||
json(payload) {
|
||||
state.jsonBody = payload;
|
||||
return payload;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
const tests = [];
|
||||
const test = (name, fn) => tests.push({ name, fn });
|
||||
|
||||
test("createrepcompletemessage_api rejects missing hash with 400", async () => {
|
||||
const calls = [];
|
||||
const nextConnect = createNextConnectMock();
|
||||
|
||||
const mod = loadModule("pages/api/file/createrepcompletemessage_api.js", {
|
||||
hashAPIPath: () => "&hash=expected",
|
||||
consoleLogger: () => {},
|
||||
createRepCompleteMessage: async (...args) => {
|
||||
calls.push(args);
|
||||
return { ok: true };
|
||||
},
|
||||
nextConnect,
|
||||
middleware: () => {}
|
||||
});
|
||||
|
||||
const req = {
|
||||
query: {
|
||||
container: "c1",
|
||||
tempcaseref: "temp-1",
|
||||
repid: "rep-1"
|
||||
}
|
||||
};
|
||||
const res = createRes();
|
||||
|
||||
await mod.default.handler(req, res);
|
||||
|
||||
assert.strictEqual(res.state.statusCode, 400);
|
||||
assert.strictEqual(calls.length, 0);
|
||||
});
|
||||
|
||||
test("upload rejects missing hash with 400", async () => {
|
||||
const blobCalls = [];
|
||||
const nextConnect = createNextConnectMock();
|
||||
|
||||
const mod = loadModule("pages/api/file/upload.js", {
|
||||
hashAPIPath: () => "?hash=expected",
|
||||
createBlob: async (...args) => {
|
||||
blobCalls.push(args);
|
||||
return { ok: true };
|
||||
},
|
||||
createRepBlob: async (...args) => {
|
||||
blobCalls.push(args);
|
||||
return { ok: true };
|
||||
},
|
||||
uploadFile: async () => {},
|
||||
nextConnect,
|
||||
middleware: () => {}
|
||||
});
|
||||
|
||||
const req = {
|
||||
query: {},
|
||||
body: {
|
||||
appealData: {},
|
||||
containerID: ["c1"],
|
||||
casefolderID: ["case-1"]
|
||||
},
|
||||
files: {}
|
||||
};
|
||||
const res = createRes();
|
||||
|
||||
await mod.default.handler(req, res);
|
||||
|
||||
assert.strictEqual(res.state.statusCode, 400);
|
||||
assert.strictEqual(blobCalls.length, 0);
|
||||
});
|
||||
|
||||
test("uploadsinglefile rejects invalid hash with 400", async () => {
|
||||
const uploadCalls = [];
|
||||
const nextConnect = createNextConnectMock();
|
||||
|
||||
const mod = loadModule("pages/api/file/uploadsinglefile.js", {
|
||||
hashAPIPath: () => "?hash=expected",
|
||||
uploadSingleFile: async (...args) => {
|
||||
uploadCalls.push(args);
|
||||
return { ok: true };
|
||||
},
|
||||
consoleLogger: () => {},
|
||||
fileTypeFromBuffer: async () => ({ mime: "application/pdf" }),
|
||||
fs: { readFileSync: () => Buffer.from("file") },
|
||||
path: { basename: (value) => value },
|
||||
nextConnect,
|
||||
middleware: () => {}
|
||||
});
|
||||
|
||||
const req = {
|
||||
query: { hash: "wrong" },
|
||||
body: { containerID: ["c1"], casefolderID: ["case-1"] },
|
||||
files: {}
|
||||
};
|
||||
const res = createRes();
|
||||
|
||||
await mod.default.handler(req, res);
|
||||
|
||||
assert.strictEqual(res.state.statusCode, 400);
|
||||
assert.strictEqual(uploadCalls.length, 0);
|
||||
});
|
||||
|
||||
test("setupcontainer rejects missing ident with 400", async () => {
|
||||
const createCalls = [];
|
||||
const nextConnect = createNextConnectMock();
|
||||
|
||||
const mod = loadModule("pages/api/file/setupcontainer.js", {
|
||||
hashAPIPath: () => "&hash=expected",
|
||||
createContainer: async (...args) => {
|
||||
createCalls.push(args);
|
||||
return { ok: true };
|
||||
},
|
||||
createContainerSas: async () => {},
|
||||
getContainers: async () => {},
|
||||
getBlobs: async () => {},
|
||||
uploadFile: async () => {},
|
||||
consoleLogger: () => {},
|
||||
nextConnect,
|
||||
middleware: () => {}
|
||||
});
|
||||
|
||||
const req = { query: { hash: "expected" } };
|
||||
const res = createRes();
|
||||
|
||||
await mod.default.handler(req, res);
|
||||
|
||||
assert.strictEqual(res.state.statusCode, 400);
|
||||
assert.strictEqual(createCalls.length, 0);
|
||||
});
|
||||
|
||||
test("setupcontainer rejects invalid hash with 400", async () => {
|
||||
const createCalls = [];
|
||||
const nextConnect = createNextConnectMock();
|
||||
|
||||
const mod = loadModule("pages/api/file/setupcontainer.js", {
|
||||
hashAPIPath: () => "&hash=expected",
|
||||
createContainer: async (...args) => {
|
||||
createCalls.push(args);
|
||||
return { ok: true };
|
||||
},
|
||||
createContainerSas: async () => {},
|
||||
getContainers: async () => {},
|
||||
getBlobs: async () => {},
|
||||
uploadFile: async () => {},
|
||||
consoleLogger: () => {},
|
||||
nextConnect,
|
||||
middleware: () => {}
|
||||
});
|
||||
|
||||
const req = { query: { ident: "container-1", hash: "wrong" } };
|
||||
const res = createRes();
|
||||
|
||||
await mod.default.handler(req, res);
|
||||
|
||||
assert.strictEqual(res.state.statusCode, 400);
|
||||
assert.strictEqual(createCalls.length, 0);
|
||||
});
|
||||
|
||||
const run = async () => {
|
||||
let passed = 0;
|
||||
|
||||
for (const currentTest of tests) {
|
||||
await currentTest.fn();
|
||||
passed += 1;
|
||||
}
|
||||
|
||||
console.log(
|
||||
`Phase 9 behavioural tests passed (${passed}/${tests.length}).`
|
||||
);
|
||||
};
|
||||
|
||||
run().catch((error) => {
|
||||
console.error(error);
|
||||
process.exit(1);
|
||||
});
|
||||
Reference in New Issue
Block a user