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
@@ -6,6 +6,7 @@ import {
|
||||
ReportValidationError,
|
||||
ReportConflictError,
|
||||
} from "@/modules/billing/reports";
|
||||
import { assertSameOrigin, CrossOriginRequestError } from "@/modules/security/origin";
|
||||
|
||||
const cancelPayload = z
|
||||
.object({
|
||||
@@ -27,6 +28,7 @@ export function createCancelReportHandler(dependencies: CancelHandlerDependencie
|
||||
context: CancelReportRouteContext,
|
||||
): Promise<Response> {
|
||||
try {
|
||||
assertSameOrigin(request);
|
||||
const principal = await dependencies.requireAdmin(request.headers);
|
||||
const { reportId } = await context.params;
|
||||
const payload = cancelPayload.safeParse(await readJson(request));
|
||||
@@ -57,6 +59,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");
|
||||
}
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ import { z } from "zod";
|
||||
import { requireAdmin, PrincipalAuthorizationError } from "@/modules/auth/principal";
|
||||
import { previewReport, ReportValidationError, ReportConflictError } from "@/modules/billing/reports";
|
||||
import { monthPeriod, quarterPeriod } from "@/modules/billing/period";
|
||||
import { assertSameOrigin, CrossOriginRequestError } from "@/modules/security/origin";
|
||||
|
||||
const previewPayload = z
|
||||
.object({
|
||||
@@ -25,6 +26,7 @@ type PreviewHandlerDependencies = {
|
||||
export function createPreviewHandler(dependencies: PreviewHandlerDependencies) {
|
||||
return async function POST(request: Request): Promise<Response> {
|
||||
try {
|
||||
assertSameOrigin(request);
|
||||
await dependencies.requireAdmin(request.headers);
|
||||
const payload = previewPayload.safeParse(await readJson(request));
|
||||
if (!payload.success) return jsonError(400, "INVALID_REQUEST");
|
||||
@@ -57,6 +59,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");
|
||||
}
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { downloadDeliveryRepository, streamWithAccounting } from "@/modules/downloads/stream";
|
||||
import { requireActiveEditorial, PrincipalAuthorizationError } from "@/modules/auth/principal";
|
||||
import { assertSameOrigin, CrossOriginRequestError } from "@/modules/security/origin";
|
||||
import { immichClient } from "@/modules/immich/client";
|
||||
import type { ImmichBinary } from "@/modules/immich/types";
|
||||
import { normalizeUuid } from "@/modules/usages/types";
|
||||
@@ -18,6 +19,7 @@ export function createDownloadFileHandler(dependencies: DownloadFileHandlerDepen
|
||||
context: DownloadFileRouteContext,
|
||||
): Promise<Response> {
|
||||
try {
|
||||
assertSameOrigin(request);
|
||||
const principal = await dependencies.requireActiveEditorial(request.headers);
|
||||
const downloadId = safeDownloadId((await context.params).downloadId);
|
||||
if (!downloadId) return genericNotFound();
|
||||
@@ -61,6 +63,9 @@ export function createDownloadFileHandler(dependencies: DownloadFileHandlerDepen
|
||||
return jsonError(503, "DOWNLOAD_UNAVAILABLE");
|
||||
}
|
||||
} catch (error) {
|
||||
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