From 12c682b357f09cbba7d66d3dbb6d41dda3b46c7b Mon Sep 17 00:00:00 2001 From: MohamedBassem Date: Sat, 20 Apr 2024 00:03:44 +0100 Subject: [PATCH] fix: Ensure that downloaded asset images are from the allowed content types --- apps/web/app/api/assets/route.ts | 13 +++++-------- packages/shared/assetdb.ts | 10 ++++++++++ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/apps/web/app/api/assets/route.ts b/apps/web/app/api/assets/route.ts index a1ebea0f..f1a17fc9 100644 --- a/apps/web/app/api/assets/route.ts +++ b/apps/web/app/api/assets/route.ts @@ -2,16 +2,13 @@ import { createContextFromRequest } from "@/server/api/client"; import { TRPCError } from "@trpc/server"; import type { ZUploadResponse } from "@hoarder/shared/types/uploads"; -import { newAssetId, saveAsset } from "@hoarder/shared/assetdb"; +import { + newAssetId, + saveAsset, + SUPPORTED_ASSET_TYPES, +} from "@hoarder/shared/assetdb"; import serverConfig from "@hoarder/shared/config"; -const SUPPORTED_ASSET_TYPES = new Set([ - "image/jpeg", - "image/png", - "image/webp", - "application/pdf", -]); - const MAX_UPLOAD_SIZE_BYTES = serverConfig.maxAssetSizeMb * 1024 * 1024; export const dynamic = "force-dynamic"; diff --git a/packages/shared/assetdb.ts b/packages/shared/assetdb.ts index 1033c594..c070ad54 100644 --- a/packages/shared/assetdb.ts +++ b/packages/shared/assetdb.ts @@ -6,6 +6,13 @@ import serverConfig from "./config"; const ROOT_PATH = path.join(serverConfig.dataDir, "assets"); +export const SUPPORTED_ASSET_TYPES = new Set([ + "image/jpeg", + "image/png", + "image/webp", + "application/pdf", +]); + function getAssetDir(userId: string, assetId: string) { return path.join(ROOT_PATH, userId, assetId); } @@ -30,6 +37,9 @@ export async function saveAsset({ asset: Buffer; metadata: z.infer; }) { + if (!SUPPORTED_ASSET_TYPES.has(metadata.contentType)) { + throw new Error("Unsupported asset type"); + } const assetDir = getAssetDir(userId, assetId); await fs.promises.mkdir(assetDir, { recursive: true });