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: handle undici Headers and Maps in redirect-handler #3819

Merged
merged 4 commits into from
Nov 10, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/handler/redirect-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ function cleanRequestHeaders (headers, removeContent, unknownOrigin) {
}
}
} else if (headers && typeof headers === 'object') {
const entries = headers instanceof Headers ? headers.entries() : Object.entries(headers)
const entries = typeof headers[Symbol.iterator] === 'function' ? headers : Object.entries(headers)
for (const [key, value] of entries) {
if (!shouldRemoveHeader(key, removeContent, unknownOrigin)) {
ret.push(key, value)
Expand Down
59 changes: 58 additions & 1 deletion test/redirect-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const {
startRedirectingWithQueryParams
} = require('./utils/redirecting-servers')
const { createReadable, createReadableStream } = require('./utils/stream')
const { Headers: UndiciHeaders } = require('..')
const redirect = undici.interceptors.redirect

for (const factory of [
Expand Down Expand Up @@ -227,7 +228,7 @@ for (const factory of [
await t.completed
})

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

const server = await startRedirectingServer()
Expand Down Expand Up @@ -255,6 +256,62 @@ for (const factory of [
await t.completed
})

test('should remove Host and request body related headers when following HTTP 303 (Undici 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 UndiciHeaders({
'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 remove Host and request body related headers when following HTTP 303 (Maps)', 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 Map([
['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-foo1@1 x-foo2@2 x-foo3@3 x-bar@4`)

await t.completed
})

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

Expand Down
Loading