Skip to content

Commit 13a3821

Browse files
iiAkuflakey5
authored andcommitted
fix: handle Headers in RedirectHandler (nodejs#3777)
1 parent f28e6b3 commit 13a3821

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

lib/handler/redirect-handler.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,10 @@ function cleanRequestHeaders (headers, removeContent, unknownOrigin) {
227227
}
228228
}
229229
} else if (headers && typeof headers === 'object') {
230-
for (const key of Object.keys(headers)) {
230+
const entries = headers instanceof Headers ? headers.entries() : Object.entries(headers)
231+
for (const [key, value] of entries) {
231232
if (!shouldRemoveHeader(key, removeContent, unknownOrigin)) {
232-
ret.push(key, headers[key])
233+
ret.push(key, value)
233234
}
234235
}
235236
} else {

test/redirect-request.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,34 @@ for (const factory of [
227227
await t.completed
228228
})
229229

230+
test('should remove Host and request body related headers when following HTTP 303 (Headers)', async t => {
231+
t = tspl(t, { plan: 3 })
232+
233+
const server = await startRedirectingServer()
234+
235+
const { statusCode, headers, body: bodyStream } = await request(t, server, undefined, `http://${server}/303`, {
236+
method: 'PATCH',
237+
headers: new Headers({
238+
'Content-Encoding': 'gzip',
239+
'X-Foo1': '1',
240+
'X-Foo2': '2',
241+
'Content-Type': 'application/json',
242+
'X-Foo3': '3',
243+
Host: 'localhost',
244+
'X-Bar': '4'
245+
}),
246+
maxRedirections: 10
247+
})
248+
249+
const body = await bodyStream.text()
250+
251+
t.strictEqual(statusCode, 200)
252+
t.ok(!headers.location)
253+
t.strictEqual(body, `GET /5 :: host@${server} connection@keep-alive x-bar@4 x-foo1@1 x-foo2@2 x-foo3@3`)
254+
255+
await t.completed
256+
})
257+
230258
test('should follow redirection after a HTTP 307', async t => {
231259
t = tspl(t, { plan: 3 })
232260

0 commit comments

Comments
 (0)