TASK22019: phase 12 harden download and blob proxy guards

This commit is contained in:
2026-03-13 12:49:54 +00:00
parent 36260bb405
commit 0ca63d0228
5 changed files with 206 additions and 2 deletions
+17 -2
View File
@@ -16,6 +16,19 @@ ApiProxy.get(async (req, res) => {
var blobName = req.query.blobname;
var checkHash = req.query.hash;
if (
typeof containerName === "undefined" ||
containerName.length === 0 ||
typeof casefolderID === "undefined" ||
casefolderID.length === 0 ||
typeof blobName === "undefined" ||
blobName.length === 0 ||
typeof checkHash === "undefined" ||
checkHash.length === 0
) {
return res.status(400).json();
}
var checkquerypath =
"/api/file/downloadblob?container=" +
containerName +
@@ -24,6 +37,10 @@ ApiProxy.get(async (req, res) => {
"&blobname=" +
blobName.trim();
if (hashAPIPath(checkquerypath) != "&hash=" + checkHash) {
return res.status(400).json();
}
if (hashAPIPath(checkquerypath) == "&hash=" + checkHash) {
const bloblocation =
casefolderID + (blobName.indexOf(".json") > 0 ? "/" : "/files/");
@@ -38,8 +55,6 @@ ApiProxy.get(async (req, res) => {
"attachment; filename=" + decodeURI(blobName)
);
return res.status(200).send(downloaded);
} else {
return res.status(400).json();
}
});
@@ -23,6 +23,11 @@ const BASE_URL = process.env.API_ROOT || `http://localhost:${port}`;
export default async function ApiProxy(req, res) {
var containerName = req.query.container;
var checkHash = req.query.hash;
if (typeof containerName === "undefined" || containerName.length === 0) {
return res.status(400).json();
}
var token = await getToken();
var queryUrl =
+10
View File
@@ -24,6 +24,16 @@ export default async function ApiProxy(req, res) {
var containerName = req.query.container;
var casefolderID = req.query.casefolderID;
var checkHash = req.query.hash;
if (
typeof containerName === "undefined" ||
containerName.length === 0 ||
typeof casefolderID === "undefined" ||
casefolderID.length === 0
) {
return res.status(400).json();
}
var token = await getToken();
var queryUrl =
+5
View File
@@ -23,6 +23,11 @@ const BASE_URL = process.env.API_ROOT || `http://localhost:${port}`;
export default async function ApiProxy(req, res) {
var containerName = req.query.container;
var checkHash = req.query.hash;
if (typeof containerName === "undefined" || containerName.length === 0) {
return res.status(400).json();
}
var token = await getToken();
var queryUrl = "/api/file/getrepsblob?container=" + containerName;
+169
View File
@@ -0,0 +1,169 @@
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: () => {}, error: () => {} },
port: 3000,
...injected
};
vm.runInNewContext(source, context, { filename: filePath });
return context.module.exports;
};
const createNextConnectMock = () => {
const router = {
handler: null,
use: () => {},
get(fn) {
this.handler = fn;
}
};
return () => router;
};
const createRes = () => {
const state = {
statusCode: null,
jsonBody: undefined,
sentBody: undefined,
headers: {}
};
return {
state,
status(code) {
state.statusCode = code;
return this;
},
json(payload) {
state.jsonBody = payload;
return payload;
},
send(payload) {
state.sentBody = payload;
return payload;
},
setHeader(name, value) {
state.headers[name] = value;
}
};
};
const tests = [];
const test = (name, fn) => tests.push({ name, fn });
test("downloadblob rejects missing hash with 400", async () => {
const calls = [];
const mod = loadModule("pages/api/file/downloadblob.js", {
hashAPIPath: () => "&hash=expected",
downloadFile: async (...args) => {
calls.push(args);
return Buffer.from("x");
},
nextConnect: createNextConnectMock(),
middleware: () => {}
});
const req = {
query: { container: "c1", casefolderID: "case-1", blobname: "f.pdf" }
};
const res = createRes();
await mod.default.handler(req, res);
assert.strictEqual(res.state.statusCode, 400);
assert.strictEqual(calls.length, 0);
});
test("downloadblob valid hash returns 200", async () => {
const mod = loadModule("pages/api/file/downloadblob.js", {
hashAPIPath: () => "&hash=expected",
downloadFile: async () => Buffer.from("file"),
nextConnect: createNextConnectMock(),
middleware: () => {}
});
const req = {
query: {
container: "c1",
casefolderID: "case-1",
blobname: "f.pdf",
hash: "expected"
}
};
const res = createRes();
await mod.default.handler(req, res);
assert.strictEqual(res.state.statusCode, 200);
});
test("getbloblistproxy rejects missing container with 400", async () => {
const mod = loadModule("pages/api/file/getbloblistproxy.js", {
getToken: async () => ({ access_token: "t" }),
hashAPIPath: () => "&hash=expected",
azureHeaders: () => ({}),
axios: { get: async () => ({ data: { ok: true } }) },
consoleLogger: () => {}
});
const req = { query: { casefolderID: "case-1" } };
const res = createRes();
await mod.default(req, res);
assert.strictEqual(res.state.statusCode, 400);
});
test("getawaitingsubmissionfromblobproxy rejects missing container with 400", async () => {
const mod = loadModule(
"pages/api/file/getawaitingsubmissionfromblobproxy.js",
{
getToken: async () => ({ access_token: "t" }),
hashAPIPath: () => "&hash=expected",
azureHeaders: () => ({}),
axios: { get: async () => ({ data: { ok: true } }) },
consoleLogger: () => {}
}
);
const req = { query: {} };
const res = createRes();
await mod.default(req, res);
assert.strictEqual(res.state.statusCode, 400);
});
const run = async () => {
let passed = 0;
for (const t of tests) {
await t.fn();
passed += 1;
}
console.log(
`Phase 12 behavioural tests passed (${passed}/${tests.length}).`
);
};
run().catch((error) => {
console.error(error);
process.exit(1);
});