fix(security): enforce ownership on private draft PDF generation

This commit is contained in:
2026-11-05 14:11:45 +00:00
parent 8b045ab781
commit 93bf7e174f
3 changed files with 627 additions and 44 deletions
+84 -4
View File
@@ -85,27 +85,68 @@ import { getPickLists } from "../../../actions/services/referenceDataService";
import { planningappeals78_pdf } from "../../../components/pdftemplates/planningappeals78_pdf";
import { other_pdf } from "../../../components/pdftemplates/other_pdf";
import { respondError, respondSuccess } from "../middleware/apiResponse";
import { getServerSession } from "next-auth/next";
import { authOptions } from "../auth/[...nextauth]";
//const ApiProxy = nextConnect();
// ApiProxy.use(middleware);
// ApiProxy.post(async (req, res) => {
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 handler(req, res) {
const checkHash = req.query.hash;
const appealType = req.query.appealType;
const isDownload = req.query?.download === "true";
let appealBodyObj =
typeof req.body === "string" ? JSON.parse(req.body) : req.body;
let appealBodyObj;
try {
appealBodyObj =
typeof req.body === "string" ? JSON.parse(req.body) : req.body;
} catch (_error) {
return respondError(res, {
status: 400,
code: "INVALID_REQUEST",
message: "Invalid request"
});
}
if (
!appealBodyObj ||
typeof appealBodyObj !== "object" ||
Array.isArray(appealBodyObj)
) {
return respondError(res, {
status: 400,
code: "INVALID_REQUEST",
message: "Invalid request"
});
}
const containerID = appealBodyObj.containerID;
const casefolderID = appealBodyObj.casefolderID;
const locale = appealBodyObj.locale || "en";
let checkquerypath = "/api/file/generateappealpdf";
if (
typeof checkHash === "undefined" ||
checkHash.length === 0 ||
typeof appealType === "undefined" ||
String(appealType).length === 0 ||
typeof containerID === "undefined" ||
@@ -121,6 +162,45 @@ export default async function handler(req, res) {
});
}
if (
typeof appealType !== "string" ||
!isValidContainer(containerID) ||
!isValidDraftFolder(casefolderID) ||
(typeof req.query.download !== "undefined" &&
typeof req.query.download !== "string") ||
[
"container",
"containerID",
"casefolderID",
"caseRef",
"ticketnumber"
].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 !== containerID
) {
return forbidden(res);
}
if (typeof checkHash !== "string" || checkHash.length === 0) {
return respondError(res, {
status: 400,
code: "MISSING_REQUIRED_QUERY",
message:
"hash, appealType, containerID and casefolderID are required"
});
}
checkquerypath =
checkquerypath +
"?appealType=" +
+92 -3
View File
@@ -84,16 +84,58 @@ import { writtenStatement_pdf } from "../../../components/pdftemplates/writtenSt
import { other_pdf } from "../../../components/pdftemplates/other_pdf";
import { consoleLogger } from "../../../actions/core/logger";
import { respondError, respondSuccess } from "../middleware/apiResponse";
import { getServerSession } from "next-auth/next";
import { authOptions } from "../auth/[...nextauth]";
//const ApiProxy = nextConnect();
// ApiProxy.use(middleware);
// ApiProxy.post(async (req, res) => {
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 handler(req, res) {
const shouldDownload = req.query.download === "true";
const checkHash = req.query.hash;
let reqBodyobj =
typeof req.body === "string" ? JSON.parse(req.body) : req.body;
let reqBodyobj;
try {
reqBodyobj =
typeof req.body === "string" ? JSON.parse(req.body) : req.body;
} catch (_error) {
return respondError(res, {
status: 400,
code: "INVALID_REQUEST",
message: "Invalid request"
});
}
if (
!reqBodyobj ||
typeof reqBodyobj !== "object" ||
Array.isArray(reqBodyobj)
) {
return respondError(res, {
status: 400,
code: "INVALID_REQUEST",
message: "Invalid request"
});
}
const cleanFilesList = (blobList) => {
let newblobList = Array.isArray(blobList?.filesList)
@@ -164,7 +206,54 @@ export default async function handler(req, res) {
checkquerypath += "?download=true";
}
if (typeof checkHash === "undefined" || checkHash.length === 0) {
if (
typeof containerID === "undefined" ||
String(containerID).length === 0 ||
typeof caseRef === "undefined" ||
String(caseRef).length === 0
) {
return respondError(res, {
status: 400,
code: "MISSING_REQUIRED_QUERY",
message: "containerID and case reference are required"
});
}
if (
!isValidContainer(containerID) ||
!isValidDraftFolder(caseRef) ||
["ticketnumber", "caseRef"].some(
(key) =>
Object.prototype.hasOwnProperty.call(reqBodyobj, key) &&
!isValidDraftFolder(reqBodyobj[key])
) ||
(typeof req.query.download !== "undefined" &&
typeof req.query.download !== "string") ||
[
"container",
"containerID",
"casefolderID",
"caseRef",
"ticketnumber"
].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 !== containerID
) {
return forbidden(res);
}
if (typeof checkHash !== "string" || checkHash.length === 0) {
return respondError(res, {
status: 400,
code: "HASH_REQUIRED",