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

Ensure has query encoding is normalized (#25732)

* Ensure has query encoding is normalized

* fix type error
parent b3fad232
...@@ -31,6 +31,17 @@ export function matchHas( ...@@ -31,6 +31,17 @@ export function matchHas(
query: Params query: Params
): false | Params { ): false | Params {
const params: Params = {} const params: Params = {}
let initialQueryValues: string[] = []
if (typeof window === 'undefined') {
initialQueryValues = Object.values((req as any).__NEXT_INIT_QUERY)
}
if (typeof window !== 'undefined') {
initialQueryValues = Array.from(
new URLSearchParams(location.search).values()
)
}
const allMatch = has.every((hasItem) => { const allMatch = has.every((hasItem) => {
let value: undefined | string let value: undefined | string
let key = hasItem.key let key = hasItem.key
...@@ -46,7 +57,12 @@ export function matchHas( ...@@ -46,7 +57,12 @@ export function matchHas(
break break
} }
case 'query': { case 'query': {
// preserve initial encoding of query values
value = query[key!] value = query[key!]
if (initialQueryValues.includes(value || '')) {
value = encodeURIComponent(value!)
}
break break
} }
case 'host': { case 'host': {
......
...@@ -190,6 +190,16 @@ module.exports = { ...@@ -190,6 +190,16 @@ module.exports = {
], ],
destination: '/with-params?idk=:idk', destination: '/with-params?idk=:idk',
}, },
{
source: '/has-rewrite-8',
has: [
{
type: 'query',
key: 'post',
},
],
destination: '/blog/:post',
},
{ {
source: '/blog/about', source: '/blog/about',
destination: '/hello', destination: '/hello',
......
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
const Page = () => ( const Page = () => {
<> const { query } = useRouter()
<p>post: {useRouter().query.post}</p> return (
</> <>
) <p>post: {query.post}</p>
<div id="query">{JSON.stringify(query)}</div>
</>
)
}
Page.getInitialProps = () => ({ hello: 'world' }) Page.getInitialProps = () => ({ hello: 'world' })
......
...@@ -39,6 +39,42 @@ let appPort ...@@ -39,6 +39,42 @@ let appPort
let app let app
const runTests = (isDev = false) => { const runTests = (isDev = false) => {
it('should handle has query encoding correctly', async () => {
for (const expected of [
{
post: 'first',
},
{
post: 'hello%20world',
},
{
post: 'hello/world',
},
{
post: 'hello%2fworld',
},
]) {
const { status = 200, post } = expected
const res = await fetchViaHTTP(
appPort,
'/has-rewrite-8',
`?post=${post}`,
{
redirect: 'manual',
}
)
expect(res.status).toBe(status)
if (status === 200) {
const $ = cheerio.load(await res.text())
expect(JSON.parse($('#query').text())).toEqual({
post: decodeURIComponent(post),
})
}
}
})
it('should support long URLs for rewrites', async () => { it('should support long URLs for rewrites', async () => {
const res = await fetchViaHTTP( const res = await fetchViaHTTP(
appPort, appPort,
...@@ -1718,6 +1754,17 @@ const runTests = (isDev = false) => { ...@@ -1718,6 +1754,17 @@ const runTests = (isDev = false) => {
regex: normalizeRegEx('^\\/has-rewrite-7$'), regex: normalizeRegEx('^\\/has-rewrite-7$'),
source: '/has-rewrite-7', source: '/has-rewrite-7',
}, },
{
destination: '/blog/:post',
has: [
{
key: 'post',
type: 'query',
},
],
regex: normalizeRegEx('^\\/has-rewrite-8$'),
source: '/has-rewrite-8',
},
{ {
destination: '/hello', destination: '/hello',
regex: normalizeRegEx('^\\/blog\\/about$'), regex: normalizeRegEx('^\\/blog\\/about$'),
......
...@@ -83,7 +83,7 @@ export function renderViaHTTP(appPort, pathname, query, opts) { ...@@ -83,7 +83,7 @@ export function renderViaHTTP(appPort, pathname, query, opts) {
export function fetchViaHTTP(appPort, pathname, query, opts) { export function fetchViaHTTP(appPort, pathname, query, opts) {
const url = `http://localhost:${appPort}${pathname}${ const url = `http://localhost:${appPort}${pathname}${
query ? `?${qs.stringify(query)}` : '' typeof query === 'string' ? query : query ? `?${qs.stringify(query)}` : ''
}` }`
return fetch(url, opts) return fetch(url, opts)
} }
......
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