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

fix(redirect): Ignore query config after redirection #1724

Merged
merged 1 commit into from
Oct 23, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions lib/handler/RedirectHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 17 additions & 1 deletion test/redirect-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ const {
startRedirectingChainServers,
startRedirectingWithoutLocationServer,
startRedirectingWithAuthorization,
startRedirectingWithCookie
startRedirectingWithCookie,
startRedirectingWithQueryParams
} = require('./utils/redirecting-servers')
const { createReadable, createReadableStream } = require('./utils/stream')

Expand Down Expand Up @@ -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)

Expand Down
20 changes: 19 additions & 1 deletion test/utils/redirecting-servers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -230,5 +247,6 @@ module.exports = {
startRedirectingChainServers,
startRedirectingWithAuthorization,
startRedirectingWithCookie,
startRedirectingWithRelativePath
startRedirectingWithRelativePath,
startRedirectingWithQueryParams
}