Reject unencoded unix socket URLs instead of silently hitting localhost - #1854
Open
afonsojanu wants to merge 1 commit into
Open
Reject unencoded unix socket URLs instead of silently hitting localhost#1854afonsojanu wants to merge 1 commit into
afonsojanu wants to merge 1 commit into
Conversation
…calhost If the socket path in a http+unix:// or https+unix:// URL still has literal slashes instead of being percent-encoded, the URL parser can't tell the socket path apart from a regular path. It ends up with an empty hostname, and superagent was falling back to a plain HTTP request against localhost with a null path, which just fails with a confusing ECONNREFUSED that has nothing to do with the actual mistake. Now this case is caught right where the socket path gets parsed out, and the request fails with a message that tells you what to do about it. Fixes forwardemail#1767
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.
Closes #1767.
If a
http+unix://(orhttps+unix://) URL's socket path still has literal slashes in it instead of being percent-encoded, the WHATWG URL parser can't separate the socket path from the request path at all. Everything after//just becomes the pathname and the hostname comes out empty, sooptions.socketPathends up as an empty string.Right now that falls all the way through to actually issuing an HTTP request against
localhostwith a null path, which is a pretty confusing thing to debug. You end up chasing anECONNREFUSEDagainst a host you never asked for instead of getting any hint that the URL itself was the problem.I checked exactly what request options were being built for a URL like
http+unix:///path/to/socket.sock/the/actual/url:so the fix is to catch that at the point the socket path gets pulled out of
url.hostname, before any of that gets used to build a request. When the hostname is empty in the+unix:branch, the request now fails immediately with a message pointing at the actual fix (percent-encode the slashes), routed through the samecallback()path used for aborted requests, so it reaches whatever was passed to.end()the normal way.Added a test alongside the existing unix socket tests covering the correctly-encoded case, using a deliberately unencoded path and asserting on the new error message. Ran the full
test/node/unix-sockets.jsfile (still 5/5 including the two pre-existing http/https cases) andeslint -c .eslintrc src testclean (matches what CI runs).One thing I noticed but didn't touch: the local
xo-based pre-commit hook currently fails on this file for reasons unrelated to this change (missingnode:protocol prefixes, a few prettier formatting nits, a couple ofguard-for-inwarnings elsewhere in the file) — looks like pre-existing drift between thexoconfig and the codebase rather than anything introduced here, so I left it alone rather than bundling an unrelated cleanup into this PR.