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

Ensure has query encoding is normalized #25732

Merged
merged 5 commits into from
Jun 17, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
16 changes: 16 additions & 0 deletions packages/next/next-server/lib/router/utils/prepare-destination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,17 @@ export function matchHas(
query: Params
): false | 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) => {
let value: undefined | string
let key = hasItem.key
Expand All @@ -46,7 +57,12 @@ export function matchHas(
break
}
case 'query': {
// preserve initial encoding of query values
value = query[key!]

if (initialQueryValues.includes(value || '')) {
value = encodeURIComponent(value!)
}
break
}
case 'host': {
Expand Down
10 changes: 10 additions & 0 deletions test/integration/custom-routes/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,16 @@ module.exports = {
],
destination: '/with-params?idk=:idk',
},
{
source: '/has-rewrite-8',
has: [
{
type: 'query',
key: 'post',
},
],
destination: '/blog/:post',
},
{
source: '/blog/about',
destination: '/hello',
Expand Down
14 changes: 9 additions & 5 deletions test/integration/custom-routes/pages/blog/[post]/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { useRouter } from 'next/router'

const Page = () => (
<>
<p>post: {useRouter().query.post}</p>
</>
)
const Page = () => {
const { query } = useRouter()
return (
<>
<p>post: {query.post}</p>
<div id="query">{JSON.stringify(query)}</div>
</>
)
}

Page.getInitialProps = () => ({ hello: 'world' })

Expand Down
49 changes: 48 additions & 1 deletion test/integration/custom-routes/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,43 @@ let appPort
let app

const runTests = (isDev = false) => {
it('should resolveHref correctly navigating through history', async () => {
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 resolve href correctly navigating through history', async () => {
const browser = await webdriver(appPort, '/')
await browser.eval('window.beforeNav = 1')

Expand Down Expand Up @@ -1707,6 +1743,17 @@ const runTests = (isDev = false) => {
regex: normalizeRegEx('^\\/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',
regex: normalizeRegEx('^\\/blog\\/about$'),
Expand Down
2 changes: 1 addition & 1 deletion test/lib/next-test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export function renderViaHTTP(appPort, pathname, query, opts) {

export function fetchViaHTTP(appPort, pathname, query, opts) {
const url = `http://localhost:${appPort}${pathname}${
query ? `?${qs.stringify(query)}` : ''
typeof query === 'string' ? query : query ? `?${qs.stringify(query)}` : ''
}`
return fetch(url, opts)
}
Expand Down