71 lines
1.9 KiB
JavaScript
71 lines
1.9 KiB
JavaScript
import { deleteBlobCase } from "../../../actions/azurestorage";
|
|
import {
|
|
hasTrustedSameOrigin,
|
|
readAdminSessionCookie,
|
|
setAdminAuthNoStore,
|
|
validateAdminSession
|
|
} from "../../../lib/admin/adminAuth";
|
|
import { respondError, respondSuccess } from "../middleware/apiResponse";
|
|
|
|
const forbidden = (res) =>
|
|
respondError(res, {
|
|
status: 403,
|
|
code: "FORBIDDEN",
|
|
message: "Request not permitted"
|
|
});
|
|
|
|
const isValidContainer = (value) =>
|
|
typeof value === "string" &&
|
|
value.length >= 3 &&
|
|
value.length <= 63 &&
|
|
/^[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/.test(value);
|
|
|
|
const isValidPathSegment = (value) =>
|
|
typeof value === "string" &&
|
|
value.length > 0 &&
|
|
value.length <= 255 &&
|
|
value === value.trim() &&
|
|
value !== "." &&
|
|
value !== ".." &&
|
|
!/[\\/\u0000-\u001f\u007f]/.test(value);
|
|
|
|
export default async function handler(req, res) {
|
|
setAdminAuthNoStore(res);
|
|
res.setHeader("Allow", "POST");
|
|
|
|
if (req.method !== "POST") {
|
|
return respondError(res, {
|
|
status: 405,
|
|
code: "METHOD_NOT_ALLOWED",
|
|
message: "Method not allowed"
|
|
});
|
|
}
|
|
|
|
const adminSession = readAdminSessionCookie(req);
|
|
if (
|
|
!validateAdminSession(adminSession) ||
|
|
!hasTrustedSameOrigin(req)
|
|
) {
|
|
return forbidden(res);
|
|
}
|
|
|
|
const { container, casefolderID } = req.body ?? {};
|
|
if (!isValidContainer(container) || !isValidPathSegment(casefolderID)) {
|
|
return respondError(res, {
|
|
status: 400,
|
|
code: "INVALID_REQUEST",
|
|
message: "Invalid request"
|
|
});
|
|
}
|
|
|
|
try {
|
|
const data = await deleteBlobCase(container, casefolderID);
|
|
return respondSuccess(res, { data });
|
|
} catch (_error) {
|
|
return respondError(res, {
|
|
status: 400,
|
|
code: "DELETE_BLOB_CASE_FAILED",
|
|
message: "Unable to delete blob case"
|
|
});
|
|
}
|
|
} |