Strip credential headers when a redirect changes scheme on the same host#447
Merged
tomas merged 1 commit intoJul 1, 2026
Merged
Conversation
Owner
|
Nice PR! Thanks |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When following a redirect, needle removes
authorization,cookieandproxy-authorizationif the new location is on a different host or port (theelsebranch inlib/needle.js). The same-host check,utils.host_and_ports_match, only compares host and port and ignores the scheme. A redirect that keeps the host and port but downgrades the scheme, for examplehttps://example.com:8443tohttp://example.com:8443, therefore keepsauthorizationandproxy-authorizationand sends them over the plaintext connection.The origin is the scheme, host and port together, so a scheme change is still cross-origin. This adds a small
protocols_matchhelper, and when the host and port match but the scheme differs it drops the two auth headers before the request is re-sent. The cookie is left to the existingfollow_set_cookieslogic, which already removes it by default.For reference, node-fetch strips on
!isSameProtocol(...), and undici and @hapi/wreck (hapijs/wreck#313) compare the full origin, so this lines needle up with how the other clients treat the scheme on redirect.The added test in
test/redirect_spec.jsdoes anhttpstohttpredirect on the same host and port and checks the credential headers are gone. It fails on master and passes with this change.