fix(security): enforce ownership on private draft case creation

This commit is contained in:
2026-11-05 16:58:35 +00:00
parent 9b0cb9adc8
commit faf3aa3675
2 changed files with 338 additions and 11 deletions
+80 -11
View File
@@ -9,30 +9,59 @@ import { getTempCaseRef } from "../../../components/utils";
import { v4 as uuidv4 } from "uuid";
import { hashAPIPath } from "../../../actions/core/hash";
import { respondError, respondSuccess } from "../middleware/apiResponse";
import { getServerSession } from "next-auth/next";
import { authOptions } from "../auth/[...nextauth]";
const WEBAPI_URL =
process.env.RELAY_ROOT ||
"https://dev-pedw-ns.servicebus.windows.net/dev-pedw-hc/";
const hasValue = (value) =>
typeof value === "string" && value.trim().length > 0;
const isValidContainer = (value) =>
typeof value === "string" &&
value.length >= 3 &&
value.length <= 63 &&
/^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/.test(value);
const isValidDraftFolder = (value) =>
typeof value === "string" &&
value.length <= 100 &&
!value.includes("..") &&
/^[A-Za-z0-9](?:[A-Za-z0-9/-]*[A-Za-z0-9])?$/.test(value);
const forbidden = (res) =>
respondError(res, {
status: 403,
code: "FORBIDDEN",
message: "Request not permitted"
});
export default async function ApiProxy(req, res) {
var createCaseBody = req.body;
let createCaseBody;
try {
createCaseBody =
typeof req.body === "string" ? JSON.parse(req.body) : req.body;
} catch (_error) {
return respondError(res, {
status: 400,
code: "INVALID_REQUEST",
message: "Invalid request"
});
}
var contactid = req.query.contactid;
var appealTypeId = req.query.appealTypeId;
var containerName = req.query.containername;
var lpaID = req.query.lpaID;
var queryUrl = "incidents";
var token = await getToken();
var tempCaseRef = getTempCaseRef();
if (
typeof contactid === "undefined" ||
contactid.length === 0 ||
typeof appealTypeId === "undefined" ||
appealTypeId.length === 0 ||
typeof containerName === "undefined" ||
containerName.length === 0 ||
typeof lpaID === "undefined" ||
lpaID.length === 0
!hasValue(contactid) ||
!hasValue(appealTypeId) ||
!hasValue(containerName) ||
!hasValue(lpaID)
) {
return respondError(res, {
status: 400,
@@ -42,6 +71,46 @@ export default async function ApiProxy(req, res) {
});
}
if (
!createCaseBody ||
typeof createCaseBody !== "object" ||
Array.isArray(createCaseBody) ||
!isValidContainer(containerName) ||
["container", "containerID", "containername"].some(
(key) => Object.prototype.hasOwnProperty.call(createCaseBody, key)
) ||
["container", "containerID"].some((key) =>
Object.prototype.hasOwnProperty.call(req.query, key)
)
) {
return respondError(res, {
status: 400,
code: "INVALID_REQUEST",
message: "Invalid request"
});
}
const session = await getServerSession(req, res, authOptions(req, res));
if (
typeof session?.user?.id !== "string" ||
session.user.id.length === 0 ||
session.user.id !== containerName
) {
return forbidden(res);
}
var tempCaseRef = getTempCaseRef();
if (!isValidDraftFolder(tempCaseRef)) {
return respondError(res, {
status: 400,
code: "INVALID_REQUEST",
message: "Invalid request"
});
}
var token = await getToken();
var data = {
"title": tempCaseRef,
"ticketnumber": tempCaseRef,