-
-
Notifications
You must be signed in to change notification settings - Fork 35.5k
https: fix readable listener leak during proxy CONNECT #62913
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
Open
thisalihassan
wants to merge
1
commit into
nodejs:main
Choose a base branch
from
thisalihassan:fix-https-tunnel-readable-listener-leak
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+98
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
97 changes: 97 additions & 0 deletions
97
test/client-proxy/test-https-proxy-request-slow-connect-response.mjs
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| // Regression test for https://github.com/nodejs/node/issues/62904. | ||
| // This test drives that scenario by writing the CONNECT response one byte at | ||
| // a time with a short delay between writes, and asserts that no warning is | ||
| // ever emitted while the full HTTPS response is received correctly. | ||
|
|
||
| import * as common from '../common/index.mjs'; | ||
| import fixtures from '../common/fixtures.js'; | ||
| import assert from 'node:assert'; | ||
| import http from 'node:http'; | ||
| import net from 'node:net'; | ||
| import { once } from 'events'; | ||
|
|
||
| if (!common.hasCrypto) | ||
| common.skip('missing crypto'); | ||
|
|
||
| // https must be dynamically imported so that builds without crypto support | ||
| // can skip it. | ||
| const { default: https } = await import('node:https'); | ||
|
|
||
| // Target HTTPS server the client ultimately wants to reach. | ||
| const server = https.createServer({ | ||
| cert: fixtures.readKey('agent8-cert.pem'), | ||
| key: fixtures.readKey('agent8-key.pem'), | ||
| }, common.mustCall((req, res) => { | ||
| res.end('Hello world'); | ||
| })); | ||
| server.on('error', common.mustNotCall((err) => { console.error('Server error', err); })); | ||
| server.listen(0); | ||
| await once(server, 'listening'); | ||
|
|
||
| // Proxy server that writes the CONNECT response one byte at a time. Using the | ||
| // built-in HTTP server avoids having to parse the CONNECT request ourselves | ||
| // (which cannot rely on a single TCP chunk containing the whole request line). | ||
| const proxy = http.createServer(); | ||
| proxy.on('connect', common.mustCall(async (req, res, head) => { | ||
| const { hostname, port } = new URL(`https://${req.url}`); | ||
| const normalizedHost = hostname.startsWith('[') && hostname.endsWith(']') ? | ||
| hostname.slice(1, -1) : hostname; | ||
|
|
||
| const upstream = net.connect(Number(port), normalizedHost); | ||
| upstream.on('error', () => res.destroy()); | ||
| res.on('error', () => upstream.destroy()); | ||
| await once(upstream, 'connect'); | ||
|
|
||
| const response = 'HTTP/1.1 200 Connection Established\r\n' + | ||
| 'Proxy-agent: Node.js-Proxy\r\n' + | ||
| '\r\n'; | ||
| // Feed the response byte by byte with a macrotask boundary between writes | ||
| // so the client sees many separate 'readable' events during tunnel setup. | ||
| // This is what triggers the listener leak described in the issue. | ||
| for (let i = 0; i < response.length; i++) { | ||
| if (res.destroyed) { | ||
| upstream.destroy(); | ||
| return; | ||
| } | ||
| res.write(response[i]); | ||
| await new Promise((resolve) => setImmediate(resolve)); | ||
| } | ||
| if (head?.length) upstream.write(head); | ||
| res.pipe(upstream); | ||
| upstream.pipe(res); | ||
| }, 1)); | ||
| proxy.on('error', common.mustNotCall((err) => { console.error('Proxy error', err); })); | ||
| proxy.listen(0); | ||
| await once(proxy, 'listening'); | ||
|
|
||
| // No warning should be emitted during the proxied request. The pre-fix code | ||
| // emits MaxListenersExceededWarning; any other warning here would also be | ||
| // unexpected and should fail the test. | ||
| process.on('warning', | ||
| common.mustNotCall('unexpected warning during proxied request')); | ||
|
|
||
| const agent = new https.Agent({ | ||
| proxyEnv: { | ||
| HTTPS_PROXY: `http://localhost:${proxy.address().port}`, | ||
| }, | ||
| ca: fixtures.readKey('fake-startcom-root-cert.pem'), | ||
| }); | ||
|
|
||
| const req = https.request({ | ||
| hostname: 'localhost', | ||
| port: server.address().port, | ||
| path: '/test', | ||
| agent, | ||
| }, common.mustCall((res) => { | ||
| let data = ''; | ||
| res.setEncoding('utf8'); | ||
| res.on('data', (chunk) => { data += chunk; }); | ||
| res.on('end', common.mustCall(() => { | ||
| assert.strictEqual(data, 'Hello world'); | ||
| assert.strictEqual(res.statusCode, 200); | ||
| proxy.close(); | ||
| server.close(); | ||
| })); | ||
| })); | ||
| req.on('error', common.mustNotCall()); | ||
| req.end(); | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you move the proxy to a separate process? I recall there can be problems otherwise if the environment contains proxy variables.
EDIT: looking at the patterns of other files I think it should be starting the request from a different process instead. Or at least avoid having the requester and the proxy in the same process.