From 1aef094fc20c2c3a2a5970066d31f06cec8b6b13 Mon Sep 17 00:00:00 2001 From: Jesse Wierzbinski Date: Mon, 22 Apr 2024 09:48:18 -1000 Subject: [PATCH] refactor(frontend): :bug: Refactor frontend/glitch resolution strategy to prevent bugs --- server.ts | 71 +++++++++++++++++++++++++++++------------- server/api/[...404].ts | 21 ------------- 2 files changed, 49 insertions(+), 43 deletions(-) delete mode 100644 server/api/[...404].ts diff --git a/server.ts b/server.ts index af0938ca..24528002 100644 --- a/server.ts +++ b/server.ts @@ -128,22 +128,53 @@ export const createServer = ( ); } - // If route is .well-known, remove dot because the filesystem router can't handle dots for some reason - const matchedRoute = matchRoute( - new Request(req.url.replace(".well-known", "well-known"), { - method: req.method, - }), - ); + const routePaths = [ + "/api", + "/media", + "/nodeinfo", + "/.well-known", + "/users", + "/objects", + "/oauth/token", + "/oauth/providers", + ]; + // Check if URL starts with routePath if ( - matchedRoute?.filePath && - matchedRoute.name !== "/[...404]" && - !( - new URL(req.url).pathname.startsWith("/oauth/authorize") && - req.method === "GET" - ) + routePaths.some((path) => + new URL(req.url).pathname.startsWith(path), + ) || + (new URL(req.url).pathname.startsWith("/oauth/authorize") && + req.method === "POST") ) { - return await processRoute(matchedRoute, req, logger); + // If route is .well-known, remove dot because the filesystem router can't handle dots for some reason + const matchedRoute = matchRoute( + new Request(req.url.replace(".well-known", "well-known"), { + method: req.method, + }), + ); + + if ( + matchedRoute?.filePath && + matchedRoute.name !== "/[...404]" && + !( + new URL(req.url).pathname.startsWith( + "/oauth/authorize", + ) && req.method === "GET" + ) + ) { + return await processRoute(matchedRoute, req, logger); + } + } + + if (config.frontend.glitch.enabled) { + if (!new URL(req.url).pathname.startsWith("/oauth")) { + const glitch = await handleGlitchRequest(req, dualLogger); + + if (glitch) { + return glitch; + } + } } const base_url_with_http = config.http.base_url.replace( @@ -177,16 +208,12 @@ export const createServer = ( proxy?.headers.set("Cache-Control", "max-age=31536000"); if (!proxy || proxy.status === 404) { - if (config.frontend.glitch.enabled) { - return ( - (await handleGlitchRequest(req, dualLogger)) ?? - errorResponse("Route not found", 404) - ); - } - } else { - return proxy; + return errorResponse( + "Route not found on proxy or API route", + 404, + ); } - return errorResponse("Route not found", 404); + return proxy; }, }); diff --git a/server/api/[...404].ts b/server/api/[...404].ts deleted file mode 100644 index 7b9dbd4c..00000000 --- a/server/api/[...404].ts +++ /dev/null @@ -1,21 +0,0 @@ -import { apiRoute, applyConfig } from "@api"; -import { errorResponse } from "@response"; - -export const meta = applyConfig({ - allowedMethods: ["POST", "GET", "PUT", "PATCH", "DELETE"], - auth: { - required: false, - }, - ratelimits: { - duration: 60, - max: 100, - }, - route: "/[...404]", -}); - -/** - * Default catch-all route, returns a 404 error. - */ -export default apiRoute(() => { - return errorResponse("Route not found", 404); -});