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 a70e55e commit 6e98a57
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 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
13 changes: 13 additions & 0 deletions packages/next/src/server/app-render/validate-url.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { validateURL } from './validate-url'

describe('validateUrl', () => {
it('should return valid pathname', () => {
expect(validateURL('/').pathname).toBe('/')
expect(validateURL('/abc').pathname).toBe('/abc')
})

it('should throw for invalid pathname', () => {
expect(() => validateURL('//**y/\\')).toThrow()
expect(() => validateURL('//google.com')).toThrow()
})
})
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 6e98a57

Please sign in to comment.