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] ensure handleUpgrade doesn't break if socket is just a stream #2165

Merged
merged 2 commits into from
Aug 31, 2023

Conversation

pimterry
Copy link
Contributor

If you attempt to handle a websocket over a connection that is a stream but not a net.Socket, right now WS fails to do so. With this very small change (very similar to the equivalent handling in Node itself) it's possible to call handleUpgrade with a socket argument that's actually just a duplex stream, and then successfully handle websocket traffic that way.

This can be useful in some cases: in my specific case this is required to proxy WebSockets over non-sockets, such as HTTP/2 streams (note that this is not the same as real WS-over-HTTP/2 support - I receive a proxy CONNECT sent on an HTTP/2 stream, and then the client does an HTTP/1.1 upgrade on that proxied stream, resulting in running an HTTP/1.1 websocket over an HTTP/2 stream instead of a raw socket).

With this change, that works flawlessly for me, proxying real websocket traffic from Chrome in an HTTP/2 proxy.

This is also a small step towards #1458, since the same change will also eventually be required to add full HTTP/2 support, where the entire underlying connection is HTTP/2 throughout, with no proxying involved.

(Arguably the same could be done for the socket.setTimeout above too, but in my case that doesn't seem to be required, and I wanted to avoid changing anything else unnecessarily).

lib/websocket.js Outdated
@@ -224,7 +224,10 @@ class WebSocket extends EventEmitter {
receiver.on('pong', receiverOnPong);

socket.setTimeout(0);
socket.setNoDelay();
if (socket.setNoDelay) {
// May not be available if 'socket' is actually a stream
Copy link
Member

Choose a reason for hiding this comment

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

Nits:

Suggested change
// May not be available if 'socket' is actually a stream
//
// May not be available if `socket` is actually a stream.
//

@lpinca
Copy link
Member

lpinca commented Aug 30, 2023

Why the same is not needed for socket.setTimeout()? I also wonder if it is possible to add a test.

@pimterry
Copy link
Contributor Author

For setTimeout, my reasoning is in the description:

(Arguably the same could be done for the socket.setTimeout above too, but in my case that doesn't seem to be required, and I wanted to avoid changing anything else unnecessarily).

Happy to change it though, it works for me either way!

I've just pushed a commit to cover setTimeout too, apply your nit suggestion, and add a separate test to cover this (which fails without this change, but now passes & works correctly).

@lpinca lpinca merged commit 31da417 into websockets:master Aug 31, 2023
24 checks passed
@lpinca
Copy link
Member

lpinca commented Aug 31, 2023

Thank you.

Woodpile37 added a commit to Woodpile37/ethers.js that referenced this pull request Oct 5, 2023
<p>This PR was automatically created by Snyk using the credentials of a
real user.</p><br /><h3>Snyk has created this PR to upgrade ws from
8.14.0 to 8.14.1.</h3>

:information_source: Keep your dependencies up-to-date. This makes it
easier to fix existing vulnerabilities and to more quickly identify and
fix newly disclosed vulnerabilities when they affect your project.
<hr/>

- The recommended version is **1 version** ahead of your current
version.
- The recommended version was released **a month ago**, on 2023-09-08.


<details>
<summary><b>Release notes</b></summary>
<br/>
  <details>
    <summary>Package name: <b>ws</b></summary>
    <ul>
      <li>
<b>8.14.1</b> - <a
href="https://snyk.io/redirect/github/websockets/ws/releases/tag/8.14.1">2023-09-08</a></br><h1>Bug
fixes</h1>
<ul>
<li>Improved the reliability of two tests for <a
href="https://snyk.io/redirect/github/nodejs/citgm">CITGM</a> (<a
class="commit-link" data-hovercard-type="commit"
data-hovercard-url="https://github.com/websockets/ws/commit/fd3c64cbd60606f75763350133ba2757b6a64545/hovercard"
href="https://snyk.io/redirect/github/websockets/ws/commit/fd3c64cbd60606f75763350133ba2757b6a64545"><tt>fd3c64c</tt></a>).</li>
</ul>
      </li>
      <li>
<b>8.14.0</b> - <a
href="https://snyk.io/redirect/github/websockets/ws/releases/tag/8.14.0">2023-09-06</a></br><h1>Features</h1>
<ul>
<li>The <code>WebSocket</code> constructor now accepts HTTP(S) URLs (<a
class="issue-link js-issue-link" data-error-text="Failed to load title"
data-id="1858169494" data-permission-text="Title is private"
data-url="websockets/ws#2162"
data-hovercard-type="pull_request"
data-hovercard-url="/websockets/ws/pull/2162/hovercard"
href="https://snyk.io/redirect/github/websockets/ws/pull/2162">#2162</a>).</li>
<li>The <code>socket</code> argument of
<code>server.handleUpgrade()</code> can now be a generic<br>
<code>Duplex</code> stream (<a class="issue-link js-issue-link"
data-error-text="Failed to load title" data-id="1873628991"
data-permission-text="Title is private"
data-url="websockets/ws#2165"
data-hovercard-type="pull_request"
data-hovercard-url="/websockets/ws/pull/2165/hovercard"
href="https://snyk.io/redirect/github/websockets/ws/pull/2165">#2165</a>).</li>
</ul>
<h1>Other notable changes</h1>
<ul>
<li>At most one event per microtask is now emitted (<a class="issue-link
js-issue-link" data-error-text="Failed to load title"
data-id="1857764370" data-permission-text="Title is private"
data-url="websockets/ws#2160"
data-hovercard-type="pull_request"
data-hovercard-url="/websockets/ws/pull/2160/hovercard"
href="https://snyk.io/redirect/github/websockets/ws/pull/2160">#2160</a>).</li>
</ul>
      </li>
    </ul>
from <a href="https://snyk.io/redirect/github/websockets/ws/releases">ws
GitHub release notes</a>
  </details>
</details>


<details>
  <summary><b>Commit messages</b></summary>
  </br>
  <details>
    <summary>Package name: <b>ws</b></summary>
    <ul>
<li><a
href="https://snyk.io/redirect/github/websockets/ws/commit/7460049ff0a61bef8d5eda4b1d5c8170bc7d6b6f">7460049</a>
[dist] 8.14.1</li>
<li><a
href="https://snyk.io/redirect/github/websockets/ws/commit/397b89e3db6782022bbcf328b1191f5a1eb7800f">397b89e</a>
[ci] Update actions/checkout action to v4</li>
<li><a
href="https://snyk.io/redirect/github/websockets/ws/commit/fd3c64cbd60606f75763350133ba2757b6a64545">fd3c64c</a>
[test] Fix flaky tests on Windows</li>
<li><a
href="https://snyk.io/redirect/github/websockets/ws/commit/ae60ce0d1eaa239844bc8d60d220b47e302c3d45">ae60ce0</a>
[ci] Cache downloaded npm dependencies (ethers-io#2166)</li>
<li><a
href="https://snyk.io/redirect/github/websockets/ws/commit/511aefece49ee38c6fcca19d230c115fbfeaefd8">511aefe</a>
[pkg] Silence npm warning</li>
<li><a
href="https://snyk.io/redirect/github/websockets/ws/commit/ddba690ab8c5da2da2fc9af3131d5e5629cbdbd4">ddba690</a>
[doc] Fix the type of the &#x60;socket&#x60; argument</li>
    </ul>

<a
href="https://snyk.io/redirect/github/websockets/ws/compare/d30768405fc295f0365c4bad8b7e14a9ad54c64b...7460049ff0a61bef8d5eda4b1d5c8170bc7d6b6f">Compare</a>
  </details>
</details>
<hr/>

**Note:** *You are seeing this because you or someone else with access
to this repository has authorized Snyk to open upgrade PRs.*

For more information: <img
src="https://api.segment.io/v1/pixel/track?data=eyJ3cml0ZUtleSI6InJyWmxZcEdHY2RyTHZsb0lYd0dUcVg4WkFRTnNCOUEwIiwiYW5vbnltb3VzSWQiOiJjZmMwZDE2Ni0zY2Y0LTQ3NjgtYjBiNi03MjEzMTBjYjBiZWQiLCJldmVudCI6IlBSIHZpZXdlZCIsInByb3BlcnRpZXMiOnsicHJJZCI6ImNmYzBkMTY2LTNjZjQtNDc2OC1iMGI2LTcyMTMxMGNiMGJlZCJ9fQ=="
width="0" height="0"/>

🧐 [View latest project
report](https://app.snyk.io/org/woodpile37/project/0f28f541-dd84-4216-97e4-d1b22c099c64?utm_source&#x3D;github&amp;utm_medium&#x3D;referral&amp;page&#x3D;upgrade-pr)

🛠 [Adjust upgrade PR
settings](https://app.snyk.io/org/woodpile37/project/0f28f541-dd84-4216-97e4-d1b22c099c64/settings/integration?utm_source&#x3D;github&amp;utm_medium&#x3D;referral&amp;page&#x3D;upgrade-pr)

🔕 [Ignore this dependency or unsubscribe from future upgrade
PRs](https://app.snyk.io/org/woodpile37/project/0f28f541-dd84-4216-97e4-d1b22c099c64/settings/integration?pkg&#x3D;ws&amp;utm_source&#x3D;github&amp;utm_medium&#x3D;referral&amp;page&#x3D;upgrade-pr#auto-dep-upgrades)

<!---
(snyk:metadata:{"prId":"cfc0d166-3cf4-4768-b0b6-721310cb0bed","prPublicId":"cfc0d166-3cf4-4768-b0b6-721310cb0bed","dependencies":[{"name":"ws","from":"8.14.0","to":"8.14.1"}],"packageManager":"npm","type":"auto","projectUrl":"https://app.snyk.io/org/woodpile37/project/0f28f541-dd84-4216-97e4-d1b22c099c64?utm_source=github&utm_medium=referral&page=upgrade-pr","projectPublicId":"0f28f541-dd84-4216-97e4-d1b22c099c64","env":"prod","prType":"upgrade","vulns":[],"issuesToFix":[],"upgrade":[],"upgradeInfo":{"versionsDiff":1,"publishedDate":"2023-09-08T16:04:44.943Z"},"templateVariants":[],"hasFixes":false,"isMajorUpgrade":false,"isBreakingChange":false,"priorityScoreList":[]})
--->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants