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 Headers in RedirectHandler #3777

Merged
merged 4 commits into from
Nov 7, 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
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)
metcoder95 marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this won't work for Maps, only Headers

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe better to check for Symbol.Iterable

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iiAku can you create a follow up PR?

const entries = utils.isIterable(headers) ? headers : Object.entries(headers)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this also only works with the global Headers, not Headers imported from undici

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@iiAku can you create a follow up PR?

const entries = utils.isIterable(headers) ? headers : Object.entries(headers)

Yes I can look into this, thanks for reviewing raising that point @KhafraDev

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
Loading