Skip to content

Commit

Permalink
fix: handle Headers in RedirectHandler (#3777)
Browse files Browse the repository at this point in the history
  • Loading branch information
iiAku authored Nov 7, 2024
1 parent fe1f132 commit dade9de
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/handler/redirect-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,10 @@ function cleanRequestHeaders (headers, removeContent, unknownOrigin) {
}
}
} else if (headers && typeof headers === 'object') {
for (const key of Object.keys(headers)) {
const entries = headers instanceof Headers ? headers.entries() : Object.entries(headers)
for (const [key, value] of entries) {
if (!shouldRemoveHeader(key, removeContent, unknownOrigin)) {
ret.push(key, headers[key])
ret.push(key, value)
}
}
} else {
Expand Down
28 changes: 28 additions & 0 deletions test/redirect-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,34 @@ for (const factory of [
await t.completed
})

test('should remove Host and request body related headers when following HTTP 303 (Headers)', async t => {
t = tspl(t, { plan: 3 })

const server = await startRedirectingServer()

const { statusCode, headers, body: bodyStream } = await request(t, server, undefined, `http://${server}/303`, {
method: 'PATCH',
headers: new Headers({
'Content-Encoding': 'gzip',
'X-Foo1': '1',
'X-Foo2': '2',
'Content-Type': 'application/json',
'X-Foo3': '3',
Host: 'localhost',
'X-Bar': '4'
}),
maxRedirections: 10
})

const body = await bodyStream.text()

t.strictEqual(statusCode, 200)
t.ok(!headers.location)
t.strictEqual(body, `GET /5 :: host@${server} connection@keep-alive x-bar@4 x-foo1@1 x-foo2@2 x-foo3@3`)

await t.completed
})

test('should follow redirection after a HTTP 307', async t => {
t = tspl(t, { plan: 3 })

Expand Down

0 comments on commit dade9de

Please # to comment.