From 4762ba33da5d9c7a7ea2aafe21fc12578a09141b Mon Sep 17 00:00:00 2001 From: mbaharip <62494292+mbahArip@users.noreply.github.com> Date: Wed, 8 May 2024 08:54:25 +0700 Subject: [PATCH] Only allows to be accessed from same domain as base path --- src/app/api/thumb/[encryptedId]/route.ts | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/src/app/api/thumb/[encryptedId]/route.ts b/src/app/api/thumb/[encryptedId]/route.ts index 39eeccd..ea13612 100644 --- a/src/app/api/thumb/[encryptedId]/route.ts +++ b/src/app/api/thumb/[encryptedId]/route.ts @@ -2,7 +2,6 @@ import { NextRequest, NextResponse } from "next/server"; import { z } from "zod"; import { decryptData } from "~/utils/encryptionHelper/hash"; -import gdrive from "~/utils/gdriveInstance"; import config from "~/config/gIndex.config"; @@ -20,6 +19,14 @@ export async function GET( const searchParams = new URL(request.nextUrl).searchParams; const size = searchParams.get("size") || "512"; + // Only allow if the request is from the same domain or the referer is the same domain + if ( + process.env.NODE_ENV === "production" && + !request.headers.get("Referer")?.includes(config.basePath) + ) { + throw new Error("Invalid request"); + } + const validSize = z.coerce.number().safeParse(size); if (!validSize.success) { throw new Error("Invalid size"); @@ -33,20 +40,6 @@ export async function GET( ); const decryptedId = await decryptData(encryptedId); - const fileMeta = await gdrive.files.get({ - fileId: decryptedId, - fields: - "id, name, mimeType, fileExtension, webContentLink, thumbnailLink", - supportsAllDrives: config.apiConfig.isTeamDrive, - }); - - if ( - !fileMeta.data.mimeType?.startsWith("image") && - !fileMeta.data.mimeType?.startsWith("video") - ) { - return defaultImage; - } - const url = `https://drive.google.com/thumbnail?id=${decryptedId}&sz=w${size}`; if (!config.apiConfig.proxyThumbnail) {