From 3425f29f8958fd9a08f026195cb203cc1fe174be Mon Sep 17 00:00:00 2001 From: Shabi Ilyas Date: Sun, 23 Oct 2022 01:44:08 +0200 Subject: [PATCH] fix(redirect): Ignore query config after redirection --- lib/handler/RedirectHandler.js | 1 + test/redirect-request.js | 18 +++++++++++++++++- test/utils/redirecting-servers.js | 20 +++++++++++++++++++- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/lib/handler/RedirectHandler.js b/lib/handler/RedirectHandler.js index 2f726e79f2f..baca27ed147 100644 --- a/lib/handler/RedirectHandler.js +++ b/lib/handler/RedirectHandler.js @@ -109,6 +109,7 @@ class RedirectHandler { this.opts.path = path this.opts.origin = origin this.opts.maxRedirections = 0 + this.opts.query = null // https://tools.ietf.org/html/rfc7231#section-6.4.4 // In case of HTTP 303, always replace method to be either HEAD or GET diff --git a/test/redirect-request.js b/test/redirect-request.js index 930f6512971..2915f1e224a 100644 --- a/test/redirect-request.js +++ b/test/redirect-request.js @@ -8,7 +8,8 @@ const { startRedirectingChainServers, startRedirectingWithoutLocationServer, startRedirectingWithAuthorization, - startRedirectingWithCookie + startRedirectingWithCookie, + startRedirectingWithQueryParams } = require('./utils/redirecting-servers') const { createReadable, createReadableStream } = require('./utils/stream') @@ -300,6 +301,21 @@ for (const factory of [ t.equal(body, 'FINAL') }) + t.test('should ignore query after redirection', async t => { + t.plan(3) + + const server = await startRedirectingWithQueryParams(t) + + const { statusCode, headers, context: { history } } = await request(server, undefined, `http://${server}/`, { + maxRedirections: 10, + query: { param1: 'first' } + }) + + t.equal(statusCode, 200) + t.notOk(headers.location) + t.same(history.map(x => x.toString()), [`http://${server}/`, `http://${server}/?param2=second`]) + }) + t.test('should follow a redirect chain up to the allowed number of times', async t => { t.plan(4) diff --git a/test/utils/redirecting-servers.js b/test/utils/redirecting-servers.js index 4b3f51cd69d..db7a4dd6ded 100644 --- a/test/utils/redirecting-servers.js +++ b/test/utils/redirecting-servers.js @@ -222,6 +222,23 @@ async function startRedirectingWithRelativePath (t) { return server } +async function startRedirectingWithQueryParams (t) { + const server = await startServer(t, (req, res) => { + if (req.url === '/?param1=first') { + res.statusCode = 301 + res.setHeader('Connection', 'close') + res.setHeader('Location', `http://${server}/?param2=second`) + res.end('REDIRECT') + return + } + + res.setHeader('Connection', 'close') + res.end('') + }) + + return server +} + module.exports = { startServer, startRedirectingServer, @@ -230,5 +247,6 @@ module.exports = { startRedirectingChainServers, startRedirectingWithAuthorization, startRedirectingWithCookie, - startRedirectingWithRelativePath + startRedirectingWithRelativePath, + startRedirectingWithQueryParams }