From 8557867ad8e8b2e23feba49c2a50dbf6e2b61f45 Mon Sep 17 00:00:00 2001 From: Jesse Wierzbinski Date: Mon, 6 May 2024 17:31:12 +0000 Subject: [PATCH] fix(api): :bug: Fix incorrect proxy behaviour --- index.ts | 4 ++- server/api/media/{[id] => :id}/index.ts | 2 +- server/api/media/proxy/:id.ts | 46 +++++++++++++++++++++++++ server/api/media/proxy/index.ts | 2 +- utils/response.ts | 2 +- 5 files changed, 52 insertions(+), 4 deletions(-) rename server/api/media/{[id] => :id}/index.ts (98%) create mode 100644 server/api/media/proxy/:id.ts diff --git a/index.ts b/index.ts index f0ae8edd..ff785e75 100644 --- a/index.ts +++ b/index.ts @@ -105,7 +105,9 @@ if (isEntry) { } } -const app = new Hono(); +const app = new Hono({ + strict: false, +}); app.use(ipBans); app.use(agentBans); diff --git a/server/api/media/[id]/index.ts b/server/api/media/:id/index.ts similarity index 98% rename from server/api/media/[id]/index.ts rename to server/api/media/:id/index.ts index 4a5d9cf9..bd64f9d0 100644 --- a/server/api/media/[id]/index.ts +++ b/server/api/media/:id/index.ts @@ -6,7 +6,7 @@ import { z } from "zod"; export const meta = applyConfig({ allowedMethods: ["GET"], - route: "/api/media/:id", + route: "/media/:id", ratelimits: { max: 100, duration: 60, diff --git a/server/api/media/proxy/:id.ts b/server/api/media/proxy/:id.ts new file mode 100644 index 00000000..778744a2 --- /dev/null +++ b/server/api/media/proxy/:id.ts @@ -0,0 +1,46 @@ +import { applyConfig, handleZodError } from "@api"; +import { zValidator } from "@hono/zod-validator"; +import { errorResponse, response } from "@response"; +import type { Hono } from "hono"; +import { z } from "zod"; + +export const meta = applyConfig({ + allowedMethods: ["GET"], + route: "/media/proxy/:id", + ratelimits: { + max: 100, + duration: 60, + }, + auth: { + required: false, + }, +}); + +export const schemas = { + param: z.object({ + id: z + .string() + .transform((val) => Buffer.from(val, "base64url").toString()), + }), +}; + +export default (app: Hono) => + app.on( + meta.allowedMethods, + meta.route, + zValidator("param", schemas.param, handleZodError), + async (context) => { + const { id } = context.req.valid("param"); + + // Check if URL is valid + if (!URL.canParse(id)) + return errorResponse( + "Invalid URL (it should be encoded as base64url", + 400, + ); + + return fetch(id).then((res) => { + return response(res.body, res.status, res.headers.toJSON()); + }); + }, + ); diff --git a/server/api/media/proxy/index.ts b/server/api/media/proxy/index.ts index 583ffeb8..68f9b107 100644 --- a/server/api/media/proxy/index.ts +++ b/server/api/media/proxy/index.ts @@ -6,7 +6,7 @@ import { z } from "zod"; export const meta = applyConfig({ allowedMethods: ["GET"], - route: "/api/media/proxy", + route: "/media/proxy", ratelimits: { max: 100, duration: 60, diff --git a/utils/response.ts b/utils/response.ts index 3bd2ac75..c75ccbbf 100644 --- a/utils/response.ts +++ b/utils/response.ts @@ -76,7 +76,7 @@ export const proxyUrl = (url: string | null) => { const urlAsBase64Url = Buffer.from(url || "").toString("base64url"); return url ? new URL( - `/media/proxy?url=${urlAsBase64Url}`, + `/media/proxy/${urlAsBase64Url}`, config.http.base_url, ).toString() : url;