From 89407730d7c593f7aed5ec08f2928a202ec5e353 Mon Sep 17 00:00:00 2001 From: jamsinclair Date: Thu, 8 Jul 2021 13:17:01 +0900 Subject: [PATCH 1/2] fix encoding error with location and refresh headers --- packages/next/server/next-server.ts | 17 ++++++++++++++-- .../custom-routes/test/index.test.js | 20 +++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/packages/next/server/next-server.ts b/packages/next/server/next-server.ts index 938edc9f454e..55ab8e2ae206 100644 --- a/packages/next/server/next-server.ts +++ b/packages/next/server/next-server.ts @@ -860,13 +860,26 @@ export default class Server { // we need to re-encode them here but still allow passing through // values from rewrites/redirects const stringifyQuery = (req: IncomingMessage, query: ParsedUrlQuery) => { - const initialQueryValues = Object.values((req as any).__NEXT_INIT_QUERY) + const initialQuery = (req as any).__NEXT_INIT_QUERY + const initialQueryValues: Array = Object.values( + initialQuery + ) return stringifyQs(query, undefined, undefined, { encodeURIComponent(value) { - if (initialQueryValues.some((val) => val === value)) { + const queryContainsValue = (queryVal: string | string[]) => + Array.isArray(queryVal) + ? queryVal.includes(value) + : queryVal === value + + if ( + value in initialQuery || + initialQueryValues.some(queryContainsValue) + ) { + // Encode keys and values from initial query return encodeURIComponent(value) } + return value }, }) diff --git a/test/integration/custom-routes/test/index.test.js b/test/integration/custom-routes/test/index.test.js index 023a10fb2778..ab2dcafdcd10 100644 --- a/test/integration/custom-routes/test/index.test.js +++ b/test/integration/custom-routes/test/index.test.js @@ -652,6 +652,26 @@ const runTests = (isDev = false) => { expect(res.headers.get('refresh')).toBe(`0;url=/`) }) + it('should have correctly encoded query in location and refresh headers', async () => { + const res = await fetchViaHTTP( + appPort, + // Query unencoded is ?ใƒ†ใ‚นใƒˆ=ใ‚ + '/redirect4?%E3%83%86%E3%82%B9%E3%83%88=%E3%81%82', + undefined, + { + redirect: 'manual', + } + ) + expect(res.status).toBe(308) + + expect(res.headers.get('location').split('?')[1]).toBe( + '%E3%83%86%E3%82%B9%E3%83%88=%E3%81%82' + ) + expect(res.headers.get('refresh')).toBe( + '0;url=/?%E3%83%86%E3%82%B9%E3%83%88=%E3%81%82' + ) + }) + it('should handle basic api rewrite successfully', async () => { const data = await renderViaHTTP(appPort, '/api-hello') expect(JSON.parse(data)).toEqual({ query: {} }) From a9bc79ac1b40efe3bba9eaa4f42e5103a979a2b4 Mon Sep 17 00:00:00 2001 From: jamsinclair Date: Sun, 24 Oct 2021 22:31:12 +0900 Subject: [PATCH 2/2] refactor: prettier fix --- packages/next/server/next-server.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/next/server/next-server.ts b/packages/next/server/next-server.ts index 42c64feb88b5..aed30bc2862c 100644 --- a/packages/next/server/next-server.ts +++ b/packages/next/server/next-server.ts @@ -924,9 +924,8 @@ export default class Server { // values from rewrites/redirects const stringifyQuery = (req: IncomingMessage, query: ParsedUrlQuery) => { const initialQuery = (req as any).__NEXT_INIT_QUERY - const initialQueryValues: Array = Object.values( - initialQuery - ) + const initialQueryValues: Array = + Object.values(initialQuery) return stringifyQs(query, undefined, undefined, { encodeURIComponent(value) {