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 2 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
6 changes: 4 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 All @@ -239,3 +240,4 @@ function cleanRequestHeaders (headers, removeContent, unknownOrigin) {
}

module.exports = RedirectHandler
module.exports.cleanRequestHeaders = cleanRequestHeaders
Copy link
Member

Choose a reason for hiding this comment

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

Let's not do that if we are just exporting it to testing it out.

You can attempt to test the same on the redirect-request test suite where you submit a Headers instance instead of the array/object way.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added tests in appropriate file

34 changes: 34 additions & 0 deletions test/clean-request-headers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const { describe, it } = require('node:test')
const { cleanRequestHeaders } = require('../lib/handler/redirect-handler')
const assert = require('node:assert')

describe('clean-request-headers', () => {
const baseHeaderArray = ['content-type', 'application/json', 'accept-ranges', 'bytes']

it('Should clean request header as expected when it is an array', () => {
const headerArray = structuredClone(baseHeaderArray)

const cleanedRequestHeaders = cleanRequestHeaders(headerArray)

assert.ok(cleanedRequestHeaders.every((headerKeyValue) => baseHeaderArray.includes(headerKeyValue)), true)
assert.ok(cleanedRequestHeaders.length === baseHeaderArray.length, true)
})

it('Should clean request header as expected when it is a string record object', () => {
const headersObject = { 'content-type': 'application/json', 'accept-ranges': 'bytes' }

const cleanedRequestHeaders = cleanRequestHeaders(headersObject)

assert.ok(cleanedRequestHeaders.every((headerKeyValue) => baseHeaderArray.includes(headerKeyValue)), true)
assert.ok(cleanedRequestHeaders.length === baseHeaderArray.length, true)
})

it('Should clean request header as expected when it is a Headers native object', () => {
const headers = new Headers({ 'content-type': 'application/json', 'accept-ranges': 'bytes' })

const cleanedRequestHeaders = cleanRequestHeaders(headers)

assert.ok(cleanedRequestHeaders.every((headerKeyValue) => baseHeaderArray.includes(headerKeyValue)), true)
assert.ok(cleanedRequestHeaders.length === baseHeaderArray.length, true)
})
})
Loading