Title: fetch() and http.request() disagree when a lower-cased proxy env var is empty · Issue #66202 · nodejs/node · GitHub
Open Graph Title: fetch() and http.request() disagree when a lower-cased proxy env var is empty · Issue #66202 · nodejs/node
X Title: fetch() and http.request() disagree when a lower-cased proxy env var is empty · Issue #66202 · nodejs/node
Description: Version v27.0.0-pre (main, 976a36a) Platform Linux 6.14.0-37-generic x64 Subsystem http What steps will reproduce the bug? Run the script below with a build that has built-in proxy support. It starts a proxy and an origin server, then la...
Open Graph Description: Version v27.0.0-pre (main, 976a36a) Platform Linux 6.14.0-37-generic x64 Subsystem http What steps will reproduce the bug? Run the script below with a build that has built-in proxy support. It star...
X Description: Version v27.0.0-pre (main, 976a36a) Platform Linux 6.14.0-37-generic x64 Subsystem http What steps will reproduce the bug? Run the script below with a build that has built-in proxy support. It star...
Opengraph URL: https://github.com/nodejs/node/issues/66202
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"fetch() and http.request() disagree when a lower-cased proxy env var is empty","articleBody":"### Version\n\nv27.0.0-pre (main, 976a36a5b6c)\n\n### Platform\n\n```text\nLinux 6.14.0-37-generic x64\n```\n\n### Subsystem\n\nhttp\n\n### What steps will reproduce the bug?\n\nRun the script below with a build that has built-in proxy support.\n\nIt starts a proxy and an origin server, then launches a child process that sends one `http.request()` and one `fetch()` using the same proxy environment. The script reports whether each request went through the proxy.\n\n```js\n'use strict';\n\nconst http = require('http');\nconst { spawn } = require('child_process');\nconst { once } = require('events');\n\nconst seen = [];\n\nconst proxy = http.createServer((req, res) => {\n seen.push(req.url);\n\n const url = new URL(req.url);\n http.request({\n hostname: url.hostname,\n port: url.port,\n path: url.pathname,\n })\n .on('response', (proxyRes) => proxyRes.pipe(res))\n .on('error', () => res.end())\n .end();\n});\n\nconst server = http.createServer((req, res) => res.end('Hello world'));\n\nconst child = `\nconst url = process.env.REQUEST_URL;\n\nrequire('http').get(url + 'request', (res) => {\n res.resume();\n\n res.on('end', () => {\n fetch(url + 'fetch')\n .then((r) => r.text())\n .then(\n () => process.exit(0),\n () => process.exit(0),\n );\n });\n});\n`;\n\n(async () => {\n proxy.listen(0);\n server.listen(0);\n\n await Promise.all([\n once(proxy, 'listening'),\n once(server, 'listening'),\n ]);\n\n const PROXY = `http://localhost:${proxy.address().port}`;\n const REQUEST_URL = `http://localhost:${server.address().port}/`;\n\n const cases = [\n ['HTTP_PROXY only ', {\n HTTP_PROXY: PROXY,\n }],\n [\"http_proxy='' HTTP_PROXY=proxy\", {\n http_proxy: '',\n HTTP_PROXY: PROXY,\n }],\n [\"no_proxy='' NO_PROXY='*' \", {\n HTTP_PROXY: PROXY,\n no_proxy: '',\n NO_PROXY: '*',\n }],\n ];\n\n for (const [name, env] of cases) {\n seen.length = 0;\n\n const childEnv = {\n ...process.env,\n NODE_USE_ENV_PROXY: '1',\n REQUEST_URL,\n };\n\n // Avoid inheriting proxy configuration from the machine running\n // the reproduction.\n for (const key of [\n 'http_proxy',\n 'HTTP_PROXY',\n 'https_proxy',\n 'HTTPS_PROXY',\n 'no_proxy',\n 'NO_PROXY',\n ]) {\n delete childEnv[key];\n }\n\n Object.assign(childEnv, env);\n\n const cp = spawn(process.execPath, ['-e', child], {\n env: childEnv,\n stdio: 'ignore',\n });\n\n await once(cp, 'exit');\n\n const via = (tag) =>\n seen.some((url) => url.endsWith('/' + tag)) ? 'proxy ' : 'direct';\n\n console.log(\n `${name} http.request: ${via('request')} fetch: ${via('fetch')}`,\n );\n }\n\n proxy.close();\n server.close();\n})();\n```\n\n### How often does it reproduce? Is there a required condition?\n\nEvery run.\n\nIt requires `NODE_USE_ENV_PROXY=1` (or `--use-env-proxy`) and a lower-cased proxy variable set to the empty string while its upper-cased counterpart is set.\n\nThis does not apply to Windows, where environment variable names are case-insensitive.\n\n### What is the expected behavior? Why is that the expected behavior?\n\n`fetch()` and `http.request()` should interpret the same proxy environment consistently when built-in proxy support is enabled.\n\nWhich interpretation of an empty lower-cased value should be used is a separate question. There are at least two existing behaviors in the ecosystem:\n\n* curl treats an empty lower-cased value as unset and can fall back to the upper-cased variable.\n* Python's `urllib.getproxies_environment()` treats an empty lower-cased value as an override that removes the upper-cased value.\n\nNode.js currently implements both interpretations at once, depending on which client is used.\n\nThe HTTP documentation describes the lower-cased variables as, for example:\n\n> Same as `HTTP_PROXY`. If both are set, `http_proxy` takes precedence.\n\nAn explicitly present empty string makes the intended behavior here ambiguous.\n\n### What do you see instead?\n\n```\nHTTP_PROXY only http.request: proxy fetch: proxy\nhttp_proxy='' HTTP_PROXY=proxy http.request: proxy fetch: direct\nno_proxy='' NO_PROXY='*' http.request: direct fetch: proxy\n```\n\nThe last two cases disagree, in opposite directions.\n\n### Additional information\n\nThe built-in HTTP implementation uses `||` when selecting between lower- and upper-cased variables:\n\n[`lib/internal/http.js#L206`](https://github.com/nodejs/node/blob/976a36a5b6c/lib/internal/http.js#L206):\n\n```js\nenv.http_proxy || env.HTTP_PROXY\n```\n\n[`lib/internal/http.js#L237`](https://github.com/nodejs/node/blob/976a36a5b6c/lib/internal/http.js#L237):\n\n```js\nenv.no_proxy || env.NO_PROXY\n```\n\nTherefore an empty lower-cased value is falsy and the upper-cased value wins.\n\nUndici's `EnvHttpProxyAgent`, used by `fetch()` through `setupHttpProxy()`, uses nullish coalescing instead:\n\n[`deps/undici/src/lib/dispatcher/env-http-proxy-agent.js#L26`](https://github.com/nodejs/node/blob/976a36a5b6c/deps/undici/src/lib/dispatcher/env-http-proxy-agent.js#L26):\n\n```js\nhttpProxy ?? process.env.http_proxy ?? process.env.HTTP_PROXY\n```\n\nand for `NO_PROXY`:\n\n[`deps/undici/src/lib/dispatcher/env-http-proxy-agent.js#L171`](https://github.com/nodejs/node/blob/976a36a5b6c/deps/undici/src/lib/dispatcher/env-http-proxy-agent.js#L171):\n\n```js\nprocess.env.no_proxy ?? process.env.NO_PROXY ?? ''\n```\n\nTherefore an empty lower-cased value wins on the Undici side.\n\nThe built-in behavior has been present since 036b1fd66d8 added proxy support to `http.request()` in v25.0.0. The reproduction above was only run against the stated build of `main`.\n\nThis appears related to #65616, where `NO_PROXY` matching also differs between `fetch()` and `http.request()`.\n\nIt also relates directly to the still-open item in #57872:\n\n> Share code between the fetch and the http(s) builtin implementation (e.g. env var parsing & matching)\n\nA regression test can demonstrate the inconsistency without deciding which interpretation is correct:\n\n```js\nconst viaRequest = await usesProxy('request', env);\nconst viaFetch = await usesProxy('fetch', env);\n\nassert.strictEqual(viaFetch, viaRequest);\n```\n\nOn current behavior, the empty `http_proxy` and empty `no_proxy` cases fail.\n\nI would be happy to send a PR once it is decided which interpretation built-in proxy support should use consistently.\n\nIf the built-in HTTP behavior is preferred, the corresponding Undici behavior would need to be aligned upstream. If the Undici behavior is preferred, the `||` selections in `lib/internal/http.js` can be changed accordingly. The documentation should then specify how empty values are interpreted.\n\nRefs: [https://github.com/nodejs/node/issues/57872](https://github.com/nodejs/node/issues/57872)","author":{"url":"https://github.com/christianaurichzm","@type":"Person","name":"christianaurichzm"},"datePublished":"2026-09-22T10:29:03Z","url":"https://github.com/nodejs/node/issues/66202"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react-reconciler,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@d5750093facf |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:4ae7adfa-1d50-431f-e2e4-f7547c74af75 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A1F8:35BF39:5AAC8A:76B6D3:6AED2AC1 |
| html-safe-nonce | 5f47f316a5f36254553bc648b64e8b33a99bdefb3ec361078d67ca47ed5d9b66 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMUY4OjM1QkYzOTo1QUFDOEE6NzZCNkQzOjZBRUQyQUMxIiwidmlzaXRvcl9pZCI6IjY3NDcyOTI0OTM0NzU0NTc5MyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 9f57e6eed37086c7309ff46c70d23e5bad37b8fa9910d5da5dbd1899c3a17d95 |
| hovercard-subject-tag | issue:5538297472 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/nodejs/node/66202/issue_layout |
| twitter:image | https://opengraph.githubassets.com/14090dbf218948fee7e65667cece4f6895b89ffd838acddc72a194f94743ace8/nodejs/node/issues/66202 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/14090dbf218948fee7e65667cece4f6895b89ffd838acddc72a194f94743ace8/nodejs/node/issues/66202 |
| og:image:alt | Version v27.0.0-pre (main, 976a36a) Platform Linux 6.14.0-37-generic x64 Subsystem http What steps will reproduce the bug? Run the script below with a build that has built-in proxy support. It star... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | christianaurichzm |
| hostname | github.com |
| expected-hostname | github.com |
| None | 9d95365dda0576efe9b71f45807595e396bd0ca9286fba5367a05048e6ddf8f2 |
| turbo-cache-control | no-preview |
| go-import | github.com/nodejs/node git https://github.com/nodejs/node.git |
| octolytics-dimension-user_id | 9950313 |
| octolytics-dimension-user_login | nodejs |
| octolytics-dimension-repository_id | 27193779 |
| octolytics-dimension-repository_nwo | nodejs/node |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 27193779 |
| octolytics-dimension-repository_network_root_nwo | nodejs/node |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 98b41fa4b5635ceb45a4c54320fced73817ff290 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width