From 78896caebb3187bda6fdea06afed8a768f3924ec Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Thu, 28 Mar 2024 16:54:33 -0600 Subject: [PATCH] fix: ensure that the urlPathname is always a pathname --- packages/next/src/server/app-render/app-render.tsx | 3 +-- .../next/src/server/app-render}/validate-url.test.ts | 6 +++--- .../server/app-render/{validate-url.tsx => validate-url.ts} | 4 ++-- 3 files changed, 6 insertions(+), 7 deletions(-) rename {test/unit => packages/next/src/server/app-render}/validate-url.test.ts (61%) rename packages/next/src/server/app-render/{validate-url.tsx => validate-url.ts} (83%) diff --git a/packages/next/src/server/app-render/app-render.tsx b/packages/next/src/server/app-render/app-render.tsx index e6598a5a189a8..0ca1e501a4a18 100644 --- a/packages/next/src/server/app-render/app-render.tsx +++ b/packages/next/src/server/app-render/app-render.tsx @@ -1426,8 +1426,7 @@ export const renderToHTMLOrFlight: AppPageRender = ( query, renderOpts ) => { - // TODO: this includes query string, should it? - const pathname = validateURL(req.url) + const { pathname } = validateURL(req.url) return RequestAsyncStorageWrapper.wrap( renderOpts.ComponentMod.requestAsyncStorage, diff --git a/test/unit/validate-url.test.ts b/packages/next/src/server/app-render/validate-url.test.ts similarity index 61% rename from test/unit/validate-url.test.ts rename to packages/next/src/server/app-render/validate-url.test.ts index a82e9fe985ab1..4415965c8fb74 100644 --- a/test/unit/validate-url.test.ts +++ b/packages/next/src/server/app-render/validate-url.test.ts @@ -1,9 +1,9 @@ -import { validateURL } from 'next/dist/server/app-render/validate-url' +import { validateURL } from './validate-url' describe('validateUrl', () => { it('should return valid pathname', () => { - expect(validateURL('/')).toBe('/') - expect(validateURL('/abc')).toBe('/abc') + expect(validateURL('/').pathname).toBe('/') + expect(validateURL('/abc').pathname).toBe('/abc') }) it('should throw for invalid pathname', () => { diff --git a/packages/next/src/server/app-render/validate-url.tsx b/packages/next/src/server/app-render/validate-url.ts similarity index 83% rename from packages/next/src/server/app-render/validate-url.tsx rename to packages/next/src/server/app-render/validate-url.ts index f43b1061b4398..723442ec4f9fb 100644 --- a/packages/next/src/server/app-render/validate-url.tsx +++ b/packages/next/src/server/app-render/validate-url.ts @@ -1,7 +1,7 @@ const DUMMY_ORIGIN = 'http://n' const INVALID_URL_MESSAGE = 'Invalid request URL' -export function validateURL(url: string | undefined): string { +export function validateURL(url: string | undefined): URL { if (!url) { throw new Error(INVALID_URL_MESSAGE) } @@ -11,7 +11,7 @@ export function validateURL(url: string | undefined): string { if (parsed.origin !== DUMMY_ORIGIN) { throw new Error(INVALID_URL_MESSAGE) } - return url + return parsed } catch { throw new Error(INVALID_URL_MESSAGE) }