Skip to content

Commit

Permalink
fix: ensure that the urlPathname is always a pathname
Browse files Browse the repository at this point in the history
  • Loading branch information
wyattjoh committed Mar 28, 2024
1 parent ae61e7e commit 78896ca
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 7 deletions.
3 changes: 1 addition & 2 deletions packages/next/src/server/app-render/app-render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
Expand All @@ -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)
}
Expand Down

0 comments on commit 78896ca

Please sign in to comment.