Skip to content

Commit

Permalink
fix: Ensure that downloaded asset images are from the allowed content…
Browse files Browse the repository at this point in the history
… types
  • Loading branch information
MohamedBassem committed Apr 19, 2024
1 parent e12fe02 commit 12c682b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
13 changes: 5 additions & 8 deletions apps/web/app/api/assets/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
10 changes: 10 additions & 0 deletions packages/shared/assetdb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -30,6 +37,9 @@ export async function saveAsset({
asset: Buffer;
metadata: z.infer<typeof zAssetMetadataSchema>;
}) {
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 });

Expand Down

0 comments on commit 12c682b

Please sign in to comment.