From ad4466a260b6afc776abf5fc204e63bdbb5aeb0d Mon Sep 17 00:00:00 2001 From: Jesse Wierzbinski Date: Mon, 6 May 2024 17:50:50 +0000 Subject: [PATCH] fix(api): :bug: Fix proxy returning incorrect media due to compression --- server/api/media/proxy/:id.ts | 6 ++++- server/api/media/proxy/index.ts | 46 --------------------------------- tests/oauth.test.ts | 2 +- 3 files changed, 6 insertions(+), 48 deletions(-) delete mode 100644 server/api/media/proxy/index.ts diff --git a/server/api/media/proxy/:id.ts b/server/api/media/proxy/:id.ts index 778744a2..ba8054b4 100644 --- a/server/api/media/proxy/:id.ts +++ b/server/api/media/proxy/:id.ts @@ -39,7 +39,11 @@ export default (app: Hono) => 400, ); - return fetch(id).then((res) => { + return fetch(id, { + headers: { + "Accept-Encoding": "identity", + }, + }).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 deleted file mode 100644 index 68f9b107..00000000 --- a/server/api/media/proxy/index.ts +++ /dev/null @@ -1,46 +0,0 @@ -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", - ratelimits: { - max: 100, - duration: 60, - }, - auth: { - required: false, - }, -}); - -export const schemas = { - query: z.object({ - url: z - .string() - .transform((val) => Buffer.from(val, "base64url").toString()), - }), -}; - -export default (app: Hono) => - app.on( - meta.allowedMethods, - meta.route, - zValidator("query", schemas.query, handleZodError), - async (context) => { - const { url } = context.req.valid("query"); - - // Check if URL is valid - if (!URL.canParse(url)) - return errorResponse( - "Invalid URL (it should be encoded as base64url", - 400, - ); - - return fetch(url).then((res) => { - return response(res.body, res.status, res.headers.toJSON()); - }); - }, - ); diff --git a/tests/oauth.test.ts b/tests/oauth.test.ts index 010850fb..7f131d0b 100644 --- a/tests/oauth.test.ts +++ b/tests/oauth.test.ts @@ -112,7 +112,7 @@ describe("POST /api/auth/login/", () => { describe("GET /oauth/authorize/", () => { test("should get a code", async () => { const response = await sendTestRequest( - new Request(new URL(`/oauth/authorize`, base_url), { + new Request(new URL("/oauth/authorize", base_url), { method: "POST", headers: { Cookie: `jwt=${jwt}`,