-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
lib: add NODE_PROXY_TUNNEL env var to disable CONNECT tunneling #62926
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
Closed
+117
−2
Closed
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
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
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
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
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,72 @@ | ||
| // Tests that NODE_PROXY_TUNNEL=false disables CONNECT tunneling | ||
| // and uses direct HTTP forwarding instead. | ||
|
|
||
| import * as common from '../common/index.mjs'; | ||
| import assert from 'node:assert'; | ||
| import http from 'node:http'; | ||
| import { once } from 'events'; | ||
| import { createProxyServer, checkProxiedFetch } from '../common/proxy-server.js'; | ||
|
|
||
| // Start a minimal proxy server. | ||
| const { proxy, logs } = createProxyServer(); | ||
| proxy.listen(0); | ||
| await once(proxy, 'listening'); | ||
|
|
||
| delete process.env.NODE_USE_ENV_PROXY; // Ensure the environment variable is not set. | ||
|
|
||
| // Start a HTTP server to process the final request. | ||
| const server = http.createServer(common.mustCall((req, res) => { | ||
| res.end('Hello world'); | ||
| }, 2)); | ||
| server.on('error', common.mustNotCall((err) => { console.error('Server error', err); })); | ||
| server.listen(0); | ||
| await once(server, 'listening'); | ||
|
|
||
| const serverHost = `localhost:${server.address().port}`; | ||
| const requestUrl = `http://${serverHost}/test`; | ||
|
|
||
| // Test: with NODE_PROXY_TUNNEL=false, fetch should NOT use CONNECT tunneling | ||
| // but instead use direct HTTP forwarding. | ||
| { | ||
| await checkProxiedFetch({ | ||
| FETCH_URL: requestUrl, | ||
| HTTP_PROXY: `http://localhost:${proxy.address().port}`, | ||
| NODE_USE_ENV_PROXY: '1', | ||
| NODE_PROXY_TUNNEL: 'false', | ||
| }, { | ||
| stdout: 'Hello world', | ||
| }); | ||
|
|
||
| // With proxyTunnel: false, undici uses Http1ProxyWrapper which rewrites | ||
| // the request path to include the origin (e.g. "localhost:PORT/test") | ||
| // and sends it as a normal GET request instead of CONNECT. | ||
| assert.strictEqual(logs[0].method, 'GET'); | ||
| assert.strictEqual(logs[0].url, requestUrl); | ||
| assert.strictEqual(logs[0].headers.host, serverHost); | ||
| } | ||
|
|
||
| // Test: without NODE_PROXY_TUNNEL (default), fetch still uses CONNECT tunneling. | ||
| { | ||
| logs.splice(0, logs.length); | ||
| await checkProxiedFetch({ | ||
| FETCH_URL: requestUrl, | ||
| HTTP_PROXY: `http://localhost:${proxy.address().port}`, | ||
| NODE_USE_ENV_PROXY: '1', | ||
| }, { | ||
| stdout: 'Hello world', | ||
| }); | ||
|
|
||
| // Without NODE_PROXY_TUNNEL set, CONNECT tunneling is used by default. | ||
| assert.deepStrictEqual(logs, [{ | ||
| method: 'CONNECT', | ||
| url: serverHost, | ||
| headers: { | ||
| 'connection': 'close', | ||
| 'proxy-connection': 'keep-alive', | ||
| 'host': serverHost, | ||
| }, | ||
| }]); | ||
| } | ||
|
|
||
| server.close(); | ||
| proxy.close(); |
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.
Does this disable tunneling for HTTPS requests too? If so, that sounds like an insecure behavior - HTTPS should always be tunneled while HTTP doesn't need tunneling. That's what curl has and also what http/https.request implements. If it only disables tunneling for HTTP, I think we can just unconditionally set it to false and don't need to add an env var for this unless anyone specifically requests being able to tunnel pure HTTP requests (I doubt anyone would actually need it considering that's not what curl and most other runtimes do)
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.
Good spot. I totally misunderstood this problem. I can autodetect this.