[PR #7729] [MERGED] fix(server): send provider requests through HTTP(S)_PROXY when NODE_USE_ENV_PROXY is on #7901

Closed
opened 2026-10-24 04:08:40 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/7729
Author: @SpicyMarinara
Created: 10/16/2026
Status: ✅ Merged
Merged: 10/16/2026
Merged by: @SpicyMarinara

Base: staging ← Head: fix/7652-llm-proxy-env


📝 Commits (2)

  • d8f2165 fix(server): send provider requests through HTTP(S)_PROXY when NODE_USE_ENV_PROXY is on
  • efd77dc docs(server): note that a proxy resolves the provider name itself

📊 Changes

2 files changed (+125 additions, -6 deletions)

View changed files

📝 packages/server/src/utils/security.ts (+28 -6)
➕ scripts/regressions/llm-env-proxy.regression.ts (+97 -0)

📄 Description

Important

Contributions target staging. Only SpicyMarinara may promote this repository's staging branch or a same-repository hotfix/* branch to main.
Outside and first-time contributors also require an approving review from SpicyMarinara.

Linked issue

Closes #7652

Why this change

  • People who reach their provider through an HTTP proxy (HTTP_PROXY / HTTPS_PROXY with NODE_USE_ENV_PROXY=1, for example v2rayN without TUN mode) could fetch the model list, but every test message and reply failed with fetch failed after about 10 seconds.
  • Cause: with NODE_USE_ENV_PROXY=1, Node puts a proxy-aware agent behind its own fetch. safeFetch passed its own undici Agent as the dispatcher, which replaces that global agent, so those requests went direct and hit the 10-second connect timeout. The model list worked only because, with PROVIDER_LOCAL_URLS_ENABLED=true and no timeouts to set, it kept Node's default agent. Chat calls always set timeouts. With local URLs off, the model list skipped the proxy too, because of the pinned DNS agent.

What changed

  • packages/server/src/utils/security.ts: both places where safeFetch builds its own agent now call one createDispatcher helper. When Node's env proxy switch is on (NODE_USE_ENV_PROXY=1 or --use-env-proxy) and a proxy variable is set, it builds an undici EnvHttpProxyAgent with the same timeout options. Otherwise it builds the same Agent as before. NO_PROXY hosts use that agent's plain inner agent, which keeps the pinned address lookup. Loopback names and addresses (localhost, 127.0.0.1, ::1) always stay direct.
  • This covers every safeFetch caller that sets timeouts or uses the pinned lookup: chat and test messages (llmFetch), image generation, embeddings, package downloads and model lists with local URLs off.
  • scripts/regressions/llm-env-proxy.regression.ts: a local fake CONNECT proxy and target, each with one fixed answer for every route. With local URLs both off and on, it checks that an llmFetch provider call goes through the proxy, that a NO_PROXY host and 127.0.0.1 / localhost stay direct, and that nothing is proxied without NODE_USE_ENV_PROXY. Before the fix it fails with 'direct' !== 'proxy', in both local-URL modes.

Decisions to review

  • The switch matches Node's own: the proxy variables count only when NODE_USE_ENV_PROXY=1 (or --use-env-proxy) is set, so people with HTTP_PROXY set for other tools see no change.
  • Loopback stays direct even when NO_PROXY doesn't list it, as it did before. This keeps local backends such as KoboldCpp or llama.cpp working for anyone who sets a proxy but forgets NO_PROXY. Other LAN or private hosts follow NO_PROXY, as Node's own fetch does.
  • The hostname, protocol and private-address checks still run locally before dispatch. A proxied destination is then resolved again by the configured proxy; that remote resolution cannot be pinned by the local guard (as with Node's global fetch). With local URLs disabled, the destination must still resolve locally: proxy-only DNS is an existing limitation retained by this fix.
  • Proxy transport follows the operator's explicit environment configuration. An authenticated http:// proxy receives its own proxy credentials over HTTP, exactly as Node's built-in fetch does; this change does not add provider credentials to the proxy URL. Rejecting or stripping proxy authentication would break authenticated HTTP proxies, including loopback proxies. HTTPS proxy transport remains available when encryption is required for that connection.

Validation

  • pnpm check passes locally
  • Container (Docker / Podman) built and ran without issue
  • Ran the app, clicked through the changes manually
  • Checked edge cases (light + dark mode, mobile viewport, empty states, error paths)
  • Above manual verification completed (describe below)
  • Read and followed CONTRIBUTING.md

Run by the agent:

  • npx tsc --noEmit for shared, server, client and root: clean.
  • prettier --check on both changed files: clean.
  • Regressions: llm-env-proxy (new), localhost-loopback-fetch, llm-first-chunk-timeout, image-generation-timeout, image-result-url-security, swarmui-video-origin, provider-compat, opencode-session, extension-security, multiplayer-control-security and open-issues all pass.
  • One-off check outside the repo: an HTTPS llmFetch to https://example.com/ through a local CONNECT proxy returned 200 with the real page in both local-URL modes. The proxy saw example.com:443, so TLS works through the tunnel.

Local CodeRabbit review (CLI, on d8f2165ac): three findings, none changed the code.

  • SSRF, major: through the proxy, the proxy resolves the name again, so the pinned address no longer binds the connection (DNS rebinding). The suggestion was to keep the pinned direct agent when local URLs are off. I kept the proxy there on purpose. Proxy users with the default config would otherwise stay broken, and their only workaround would be PROVIDER_LOCAL_URLS_ENABLED=true, which turns off the address check completely. The proxy also has to resolve names itself where local DNS is filtered, which is a common reason to use one. The local address check still runs first, and this only applies after someone opts in with NODE_USE_ENV_PROXY=1, the same as Node's own fetch. efd77dc75 adds a code comment about it. Listed under decisions above.
  • Minor: the regression doesn't restore the env variables it changes. Not needed: scripts/run-regressions.mjs runs each file in its own child process (spawn).
  • Trivial: compute the switch once at startup, and accept other --use-env-proxy forms. Not taken: reading the switch on each request lets a .env reload apply without a restart (runtime-config.ts writes process.env). On Node 24.16, NODE_USE_ENV_PROXY=true does not turn on Node's proxy; only 1 does. I checked this, and the check matches it.

Hosted CodeRabbit completed on efd77dc75: two findings declined with source-based reasons in the review threads. Authenticated HTTP proxy transport is intentionally compatible with Node's explicit environment configuration. Proxy-only DNS remains outside this narrow dispatcher fix because the existing fail-closed address guard is retained; ordinary CONNECT does not expose the remote destination IP for equivalent validation.

Manual verification notes

  • Manually verify that with NODE_USE_ENV_PROXY=1, HTTPS_PROXY=http://127.0.0.1:<port> and a local HTTP proxy (for example v2rayN with TUN off), Test Message and a normal chat reply succeed for an OpenAI and a Custom OpenAI-compatible connection.
  • Manually verify that the model list still loads through the same proxy, with PROVIDER_LOCAL_URLS_ENABLED both true and false.
  • Manually verify that a local backend on localhost / 127.0.0.1 (KoboldCpp, llama.cpp, Ollama) still answers directly with the proxy variables set.
  • Manually verify that a host listed in NO_PROXY is reached directly.
  • Manually verify that without NODE_USE_ENV_PROXY (proxy variables still set), generation behaves exactly as before.

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README) as needed
  • Translated docs on the docs-i18n branch updated to match, or a [docs-i18n] follow-up issue opened (see CONTRIBUTING.md § Translated documentation)
  • Version/release files updated (only if this PR includes a version bump)

No docs are changed. The docs don't mention proxy variables today; a short docs/CONFIGURATION.md note could be a follow-up.

UI evidence (if applicable)

Not applicable (server only).

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Fixed AI provider connections to honor the environment-proxy setting when it is enabled. Requests to hosts excluded by proxy rules and to local addresses continue to connect directly; when the setting is disabled, requests connect directly as well. This improves compatibility in proxy-configured environments while preserving direct connections where appropriate.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/7729 **Author:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Created:** 10/16/2026 **Status:** ✅ Merged **Merged:** 10/16/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `fix/7652-llm-proxy-env` --- ### 📝 Commits (2) - [`d8f2165`](https://github.com/Pasta-Devs/Marinara-Engine/commit/d8f2165ac3dc8eb2c5110f6428478e6780100078) fix(server): send provider requests through HTTP(S)_PROXY when NODE_USE_ENV_PROXY is on - [`efd77dc`](https://github.com/Pasta-Devs/Marinara-Engine/commit/efd77dc75ccb4aa4ad028f2d46c4b65e94d7dcc8) docs(server): note that a proxy resolves the provider name itself ### 📊 Changes **2 files changed** (+125 additions, -6 deletions) <details> <summary>View changed files</summary> 📝 `packages/server/src/utils/security.ts` (+28 -6) ➕ `scripts/regressions/llm-env-proxy.regression.ts` (+97 -0) </details> ### 📄 Description > [!IMPORTANT] > Contributions target `staging`. Only `SpicyMarinara` may promote this repository's `staging` branch or a same-repository `hotfix/*` branch to `main`. > Outside and first-time contributors also require an approving review from `SpicyMarinara`. <!-- See CONTRIBUTING.md § Branches. --> ## Linked issue Closes #7652 ## Why this change - People who reach their provider through an HTTP proxy (`HTTP_PROXY` / `HTTPS_PROXY` with `NODE_USE_ENV_PROXY=1`, for example v2rayN without TUN mode) could fetch the model list, but every test message and reply failed with `fetch failed` after about 10 seconds. - Cause: with `NODE_USE_ENV_PROXY=1`, Node puts a proxy-aware agent behind its own `fetch`. `safeFetch` passed its own undici `Agent` as the `dispatcher`, which replaces that global agent, so those requests went direct and hit the 10-second connect timeout. The model list worked only because, with `PROVIDER_LOCAL_URLS_ENABLED=true` and no timeouts to set, it kept Node's default agent. Chat calls always set timeouts. With local URLs off, the model list skipped the proxy too, because of the pinned DNS agent. ## What changed - `packages/server/src/utils/security.ts`: both places where `safeFetch` builds its own agent now call one `createDispatcher` helper. When Node's env proxy switch is on (`NODE_USE_ENV_PROXY=1` or `--use-env-proxy`) and a proxy variable is set, it builds an undici `EnvHttpProxyAgent` with the same timeout options. Otherwise it builds the same `Agent` as before. `NO_PROXY` hosts use that agent's plain inner agent, which keeps the pinned address lookup. Loopback names and addresses (`localhost`, `127.0.0.1`, `::1`) always stay direct. - This covers every `safeFetch` caller that sets timeouts or uses the pinned lookup: chat and test messages (`llmFetch`), image generation, embeddings, package downloads and model lists with local URLs off. - `scripts/regressions/llm-env-proxy.regression.ts`: a local fake CONNECT proxy and target, each with one fixed answer for every route. With local URLs both off and on, it checks that an `llmFetch` provider call goes through the proxy, that a `NO_PROXY` host and `127.0.0.1` / `localhost` stay direct, and that nothing is proxied without `NODE_USE_ENV_PROXY`. Before the fix it fails with `'direct' !== 'proxy'`, in both local-URL modes. **Decisions to review** - The switch matches Node's own: the proxy variables count only when `NODE_USE_ENV_PROXY=1` (or `--use-env-proxy`) is set, so people with `HTTP_PROXY` set for other tools see no change. - Loopback stays direct even when `NO_PROXY` doesn't list it, as it did before. This keeps local backends such as KoboldCpp or llama.cpp working for anyone who sets a proxy but forgets `NO_PROXY`. Other LAN or private hosts follow `NO_PROXY`, as Node's own `fetch` does. - The hostname, protocol and private-address checks still run locally before dispatch. A proxied destination is then resolved again by the configured proxy; that remote resolution cannot be pinned by the local guard (as with Node's global `fetch`). With local URLs disabled, the destination must still resolve locally: proxy-only DNS is an existing limitation retained by this fix. - Proxy transport follows the operator's explicit environment configuration. An authenticated `http://` proxy receives its own proxy credentials over HTTP, exactly as Node's built-in fetch does; this change does not add provider credentials to the proxy URL. Rejecting or stripping proxy authentication would break authenticated HTTP proxies, including loopback proxies. HTTPS proxy transport remains available when encryption is required for that connection. ## Validation - [ ] `pnpm check` passes locally - [ ] Container (Docker / Podman) built and ran without issue - [ ] Ran the app, clicked through the changes manually - [ ] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [ ] Above manual verification completed (describe below) - [ ] Read and followed `CONTRIBUTING.md` Run by the agent: - `npx tsc --noEmit` for shared, server, client and root: clean. - `prettier --check` on both changed files: clean. - Regressions: `llm-env-proxy` (new), `localhost-loopback-fetch`, `llm-first-chunk-timeout`, `image-generation-timeout`, `image-result-url-security`, `swarmui-video-origin`, `provider-compat`, `opencode-session`, `extension-security`, `multiplayer-control-security` and `open-issues` all pass. - One-off check outside the repo: an HTTPS `llmFetch` to `https://example.com/` through a local CONNECT proxy returned 200 with the real page in both local-URL modes. The proxy saw `example.com:443`, so TLS works through the tunnel. Local CodeRabbit review (CLI, on `d8f2165ac`): three findings, none changed the code. - **SSRF, major:** through the proxy, the proxy resolves the name again, so the pinned address no longer binds the connection (DNS rebinding). The suggestion was to keep the pinned direct agent when local URLs are off. I kept the proxy there on purpose. Proxy users with the default config would otherwise stay broken, and their only workaround would be `PROVIDER_LOCAL_URLS_ENABLED=true`, which turns off the address check completely. The proxy also has to resolve names itself where local DNS is filtered, which is a common reason to use one. The local address check still runs first, and this only applies after someone opts in with `NODE_USE_ENV_PROXY=1`, the same as Node's own `fetch`. `efd77dc75` adds a code comment about it. Listed under decisions above. - **Minor:** the regression doesn't restore the env variables it changes. Not needed: `scripts/run-regressions.mjs` runs each file in its own child process (`spawn`). - **Trivial:** compute the switch once at startup, and accept other `--use-env-proxy` forms. Not taken: reading the switch on each request lets a `.env` reload apply without a restart (`runtime-config.ts` writes `process.env`). On Node 24.16, `NODE_USE_ENV_PROXY=true` does not turn on Node's proxy; only `1` does. I checked this, and the check matches it. Hosted CodeRabbit completed on `efd77dc75`: two findings declined with source-based reasons in the review threads. Authenticated HTTP proxy transport is intentionally compatible with Node's explicit environment configuration. Proxy-only DNS remains outside this narrow dispatcher fix because the existing fail-closed address guard is retained; ordinary CONNECT does not expose the remote destination IP for equivalent validation. ### Manual verification notes - Manually verify that with `NODE_USE_ENV_PROXY=1`, `HTTPS_PROXY=http://127.0.0.1:<port>` and a local HTTP proxy (for example v2rayN with TUN off), **Test Message** and a normal chat reply succeed for an OpenAI and a Custom OpenAI-compatible connection. - Manually verify that the model list still loads through the same proxy, with `PROVIDER_LOCAL_URLS_ENABLED` both `true` and `false`. - Manually verify that a local backend on `localhost` / `127.0.0.1` (KoboldCpp, llama.cpp, Ollama) still answers directly with the proxy variables set. - Manually verify that a host listed in `NO_PROXY` is reached directly. - Manually verify that without `NODE_USE_ENV_PROXY` (proxy variables still set), generation behaves exactly as before. ## Docs and release impact - [ ] No docs changes needed - [ ] Updated docs (README / CONTRIBUTING / android/README) as needed - [ ] Translated docs on the `docs-i18n` branch updated to match, or a `[docs-i18n]` follow-up issue opened (see CONTRIBUTING.md § Translated documentation) - [ ] Version/release files updated (only if this PR includes a version bump) No docs are changed. The docs don't mention proxy variables today; a short `docs/CONFIGURATION.md` note could be a follow-up. ## UI evidence (if applicable) Not applicable (server only). 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Fixed AI provider connections to honor the environment-proxy setting when it is enabled. Requests to hosts excluded by proxy rules and to local addresses continue to connect directly; when the setting is disabled, requests connect directly as well. This improves compatibility in proxy-configured environments while preserving direct connections where appropriate. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-24 04:08:40 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#7901
No description provided.