Skip to content

Commit

Permalink
Update redirect query encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
ijjk committed Oct 22, 2020
1 parent ac8a0c4 commit 93362bc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
11 changes: 2 additions & 9 deletions packages/next/next-server/server/next-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -730,9 +730,7 @@ export default class Server {
const { query } = parsedDestination
delete (parsedDestination as any).query

parsedDestination.search = stringifyQs(query, undefined, undefined, {
encodeURIComponent: (str: string) => str,
} as any)
parsedDestination.search = stringifyQs(query)

const updatedDestination = formatUrl(parsedDestination)

Expand Down Expand Up @@ -774,12 +772,7 @@ export default class Server {
if (parsedDestination.protocol) {
const { query } = parsedDestination
delete (parsedDestination as any).query
parsedDestination.search = stringifyQs(
query,
undefined,
undefined,
{ encodeURIComponent: (str) => str }
)
parsedDestination.search = stringifyQs(query)

const target = formatUrl(parsedDestination)
const proxy = new Proxy({
Expand Down
25 changes: 23 additions & 2 deletions test/integration/production/test/security.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ module.exports = (context) => {
)
expect(res.status).toBe(307)
expect(pathname).toBe(encodeURI('/\\google.com/about'))
expect(hostname).not.toBe('google.com')
expect(hostname).toBe('localhost')
})

it('should handle encoded value in the pathname correctly %', async () => {
Expand All @@ -211,7 +211,28 @@ module.exports = (context) => {
)
expect(res.status).toBe(307)
expect(pathname).toBe('/%25google.com/about')
expect(hostname).not.toBe('google.com')
expect(hostname).toBe('localhost')
})

it('should handle encoded value in the query correctly', async () => {
const res = await fetchViaHTTP(
context.appPort,
'/trailing-redirect/?url=https%3A%2F%2Fgoogle.com%2Fimage%3Fcrop%3Dfocalpoint%26w%3D24&w=1200&q=100',
undefined,
{
redirect: 'manual',
}
)

const { pathname, hostname, query } = url.parse(
res.headers.get('location') || ''
)
expect(res.status).toBe(308)
expect(pathname).toBe('/trailing-redirect')
expect(hostname).toBe('localhost')
expect(query).toBe(
'url=https%3A%2F%2Fgoogle.com%2Fimage%3Fcrop%3Dfocalpoint%26w%3D24&w=1200&q=100'
)
})
})
}

0 comments on commit 93362bc

Please sign in to comment.