Unverified Commit a7337a9b authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

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

* Make sure to encode pathname for custom-route destination

* Add tests to security test suite
parent fbbb1841
...@@ -54,7 +54,8 @@ export const prepareDestination = (destination: string, params: Params) => { ...@@ -54,7 +54,8 @@ export const prepareDestination = (destination: string, params: Params) => {
}) })
try { try {
newUrl = destinationCompiler(params) newUrl = encodeURI(destinationCompiler(params))
const [pathname, hash] = newUrl.split('#') const [pathname, hash] = newUrl.split('#')
parsedDestination.pathname = pathname parsedDestination.pathname = pathname
parsedDestination.hash = `${hash ? '#' : ''}${hash || ''}` parsedDestination.hash = `${hash ? '#' : ''}${hash || ''}`
......
...@@ -83,6 +83,11 @@ module.exports = { ...@@ -83,6 +83,11 @@ module.exports = {
}, },
async redirects() { async redirects() {
return [ return [
{
source: '/redirect/me/to-about/:lang',
destination: '/:lang/about',
permanent: false,
},
{ {
source: '/docs/router-status/:code', source: '/docs/router-status/:code',
destination: '/docs/v2/network/status-codes#:code', destination: '/docs/v2/network/status-codes#:code',
......
...@@ -311,6 +311,22 @@ const runTests = (isDev = false) => { ...@@ -311,6 +311,22 @@ const runTests = (isDev = false) => {
expect(JSON.parse(data)).toEqual({ query: { name: 'hello' } }) 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) { if (!isDev) {
it('should output routes-manifest successfully', async () => { it('should output routes-manifest successfully', async () => {
const manifest = await fs.readJSON( const manifest = await fs.readJSON(
...@@ -331,6 +347,14 @@ const runTests = (isDev = false) => { ...@@ -331,6 +347,14 @@ const runTests = (isDev = false) => {
pages404: false, pages404: false,
basePath: '', basePath: '',
redirects: [ redirects: [
{
destination: '/:lang/about',
regex: normalizeRegEx(
'^\\/redirect\\/me\\/to-about(?:\\/([^\\/]+?))$'
),
source: '/redirect/me/to-about/:lang',
statusCode: 307,
},
{ {
source: '/docs/router-status/:code', source: '/docs/router-status/:code',
destination: '/docs/v2/network/status-codes#:code', destination: '/docs/v2/network/status-codes#:code',
......
...@@ -3,4 +3,15 @@ module.exports = { ...@@ -3,4 +3,15 @@ module.exports = {
// Make sure entries are not getting disposed. // Make sure entries are not getting disposed.
maxInactiveAge: 1000 * 60 * 60, maxInactiveAge: 1000 * 60 * 60,
}, },
experimental: {
redirects() {
return [
{
source: '/redirect/me/to-about/:lang',
destination: '/:lang/about',
permanent: false,
},
]
},
},
} }
/* eslint-env jest */ /* eslint-env jest */
import webdriver from 'next-webdriver' import webdriver from 'next-webdriver'
import { readFileSync } from 'fs' import { readFileSync } from 'fs'
import url from 'url'
import { join, resolve as resolvePath } from 'path' import { join, resolve as resolvePath } from 'path'
import { renderViaHTTP, getBrowserBodyText, waitFor } from 'next-test-utils' import {
renderViaHTTP,
getBrowserBodyText,
waitFor,
fetchViaHTTP,
} from 'next-test-utils'
import { recursiveReadDir } from 'next/dist/lib/recursive-readdir' import { recursiveReadDir } from 'next/dist/lib/recursive-readdir'
import { homedir } from 'os' import { homedir } from 'os'
...@@ -152,5 +158,41 @@ module.exports = context => { ...@@ -152,5 +158,41 @@ module.exports = context => {
await checkInjected(browser) await checkInjected(browser)
await browser.close() await browser.close()
}) })
it('should handle encoded value in the pathname correctly \\', async () => {
const res = await fetchViaHTTP(
context.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')
})
it('should handle encoded value in the pathname correctly %', async () => {
const res = await fetchViaHTTP(
context.appPort,
'/redirect/me/to-about/%25google.com',
undefined,
{
redirect: 'manual',
}
)
const { pathname, hostname } = url.parse(
res.headers.get('location') || ''
)
expect(res.status).toBe(307)
expect(pathname).toBe('/%25google.com/about')
expect(hostname).not.toBe('google.com')
})
}) })
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment