feat: harden portal security and auditability
This commit is contained in:
1 parent
7f1ecd9322
commit
21871bb9ff
26 files changed
+2664
-4
No files matched your search
@@ -7,6 +7,7 @@ import {
|
||||
ReportConflictError,
|
||||
} from "@/modules/billing/reports";
|
||||
import { monthPeriod, quarterPeriod } from "@/modules/billing/period";
|
||||
import { assertSameOrigin, CrossOriginRequestError } from "@/modules/security/origin";
|
||||
|
||||
const finalizePayload = z
|
||||
.object({
|
||||
@@ -29,6 +30,7 @@ type FinalizeHandlerDependencies = {
|
||||
export function createFinalizeHandler(dependencies: FinalizeHandlerDependencies) {
|
||||
return async function POST(request: Request): Promise<Response> {
|
||||
try {
|
||||
assertSameOrigin(request);
|
||||
const principal = await dependencies.requireAdmin(request.headers);
|
||||
const payload = finalizePayload.safeParse(await readJson(request));
|
||||
if (!payload.success) return jsonError(400, "INVALID_REQUEST");
|
||||
@@ -68,6 +70,9 @@ async function readJson(request: Request): Promise<unknown> {
|
||||
}
|
||||
|
||||
function mappedError(error: unknown): Response {
|
||||
if (error instanceof CrossOriginRequestError) {
|
||||
return jsonError(403, "CROSS_ORIGIN_REQUEST");
|
||||
}
|
||||
if (error instanceof PrincipalAuthorizationError) {
|
||||
return jsonError(error.code === "AUTHENTICATION_REQUIRED" ? 401 : 403, "FORBIDDEN");
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user