feat: harden portal security and auditability

This commit is contained in:
Leon Tonhäuser committed 2026-08-10 21:46:54 +02:00
1 parent 7f1ecd9322
commit 21871bb9ff
26 files changed
+2664 -4

No files matched your search

@@ -2,6 +2,7 @@ import { z } from "zod";
import { createRepeatDownload } from "@/modules/downloads/service";
import { requireActiveEditorial, PrincipalAuthorizationError } from "@/modules/auth/principal";
import { assertSameOrigin, CrossOriginRequestError } from "@/modules/security/origin";
import { createNewUsage } from "@/modules/usages/service";
import { UsageAccountingError } from "@/modules/usages/types";
@@ -36,6 +37,7 @@ type IntentHandlerDependencies = {
export function createDownloadIntentHandler(dependencies: IntentHandlerDependencies) {
return async function POST(request: Request): Promise<Response> {
try {
assertSameOrigin(request);
const principal = await dependencies.requireActiveEditorial(request.headers);
const payload = intentPayload.safeParse(await readJson(request));
if (!payload.success) return errorResponse(400, "INVALID_REQUEST");
@@ -83,6 +85,9 @@ async function readJson(request: Request): Promise<unknown> {
}
function mappedError(error: unknown): Response {
if (error instanceof CrossOriginRequestError) {
return errorResponse(403, "CROSS_ORIGIN_REQUEST");
}
if (error instanceof PrincipalAuthorizationError) {
return errorResponse(error.code === "AUTHENTICATION_REQUIRED" ? 401 : 403, "FORBIDDEN");
}