Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure to encode pathname for custom-route destination #10536

Merged
merged 2 commits into from Feb 14, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/next/next-server/server/router.ts
Expand Up @@ -54,7 +54,8 @@ export const prepareDestination = (destination: string, params: Params) => {
})

try {
newUrl = destinationCompiler(params)
newUrl = encodeURI(destinationCompiler(params))

const [pathname, hash] = newUrl.split('#')
parsedDestination.pathname = pathname
parsedDestination.hash = `${hash ? '#' : ''}${hash || ''}`
Expand Down
5 changes: 5 additions & 0 deletions test/integration/custom-routes/next.config.js
Expand Up @@ -83,6 +83,11 @@ module.exports = {
},
async redirects() {
return [
{
source: '/redirect/me/to-about/:lang',
destination: '/:lang/about',
permanent: false,
},
{
source: '/docs/router-status/:code',
destination: '/docs/v2/network/status-codes#:code',
Expand Down
24 changes: 24 additions & 0 deletions test/integration/custom-routes/test/index.test.js
Expand Up @@ -311,6 +311,22 @@ const runTests = (isDev = false) => {
expect(JSON.parse(data)).toEqual({ query: { name: 'hello' } })
})

it('should handle encoded value in the pathname correctly', async () => {
const res = await fetchViaHTTP(
appPort,
'/redirect/me/to-about/' + encodeURI('\\google.com'),
undefined,
{
redirect: 'manual',
}
)

const { pathname, hostname } = url.parse(res.headers.get('location') || '')
expect(res.status).toBe(307)
expect(pathname).toBe(encodeURI('/\\google.com/about'))
expect(hostname).not.toBe('google.com')
})

if (!isDev) {
it('should output routes-manifest successfully', async () => {
const manifest = await fs.readJSON(
Expand All @@ -331,6 +347,14 @@ const runTests = (isDev = false) => {
pages404: false,
basePath: '',
redirects: [
{
destination: '/:lang/about',
regex: normalizeRegEx(
'^\\/redirect\\/me\\/to-about(?:\\/([^\\/]+?))$'
),
source: '/redirect/me/to-about/:lang',
statusCode: 307,
},
{
source: '/docs/router-status/:code',
destination: '/docs/v2/network/status-codes#:code',
Expand Down