Repository navigation
[fix] Percent-decode the userinfo of the URL - #2352
Merged
Merged
Conversation
The URL parser keeps the username and password percent-encoded, and they were used as they are to build the `Authorization` header, so `ws://user:p%40ss@host` sent the password `p%40ss`. Decode them with `querystring.unescape()`, like `http.request()` does. Unlike `decodeURIComponent()` it leaves malformed sequences as they are, so a password such as `100%` keeps working.
Member
|
This is in the same vein as #2346, but aligning with Node.js behavior is a positive change. |
lpinca
approved these changes
Nov 5, 2026
Member
|
Thank you. |
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.
The URL parser keeps the username and password percent-encoded, and ws used them as they are to build the
Authorizationheader. A@,/or#in a password can only be written percent-encoded in a URL, sonew WebSocket('ws://user:p%40ss@host')sends the passwordp%40ssand authentication fails. Writingws://user:p@ss@hostdoes the same, because the parser encodes the second@itself. The same goes for:, spaces and non-ASCII characters, which the parser also encodes.http.request()decodes the userinfo of a URL (url.urlToHttpOptions()does it too), and so did the oldurl.parse()code path, so this brings ws in line.I used
querystring.unescape()instead ofdecodeURIComponent()because it leaves malformed sequences as they are. A password such as100%is sent as is today, anddecodeURIComponent()would throw on it, which with a redirect would happen inside the'response'handler.I added tests for encoded characters, the unencoded
@, multi-byte characters and malformed sequences. They fail on master.npm testpasses and eslint and prettier are clean.I also compared the header ws builds with the one
http.request()builds for 50,000 random userinfos made of escapes, reserved characters and non-ASCII characters. Of the 26,992 that Node accepts, 25,703 differ on master and none differ now. For the rest Node throws on a malformed sequence and ws does not.