



























@@ -1,12 +1,9 @@
11import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
223-const { fetchWithSsrFGuardMock, hasEnvHttpProxyConfiguredMock, matchesNoProxyMock } = vi.hoisted(
4-() => ({
5-fetchWithSsrFGuardMock: vi.fn(),
6-hasEnvHttpProxyConfiguredMock: vi.fn(() => false),
7-matchesNoProxyMock: vi.fn(() => false),
8-}),
9-);
3+const { fetchWithSsrFGuardMock, shouldUseEnvHttpProxyForUrlMock } = vi.hoisted(() => ({
4+fetchWithSsrFGuardMock: vi.fn(),
5+shouldUseEnvHttpProxyForUrlMock: vi.fn(() => false),
6+}));
107118vi.mock("../infra/net/fetch-guard.js", async () => {
129const actual = await vi.importActual<typeof import("../infra/net/fetch-guard.js")>(
@@ -24,8 +21,7 @@ vi.mock("../infra/net/proxy-env.js", async () => {
2421);
2522return {
2623 ...actual,
27-hasEnvHttpProxyConfigured: hasEnvHttpProxyConfiguredMock,
28-matchesNoProxy: matchesNoProxyMock,
24+shouldUseEnvHttpProxyForUrl: shouldUseEnvHttpProxyForUrlMock,
2925};
3026});
3127@@ -42,8 +38,7 @@ import {
4238} from "./shared.js";
43394440beforeEach(() => {
45-hasEnvHttpProxyConfiguredMock.mockReturnValue(false);
46-matchesNoProxyMock.mockReturnValue(false);
41+shouldUseEnvHttpProxyForUrlMock.mockReturnValue(false);
4742});
48434944afterEach(() => {
@@ -417,7 +412,7 @@ describe("fetchWithTimeoutGuarded", () => {
417412});
418413419414it("does not set a guarded fetch mode when no HTTP proxy env is configured", async () => {
420-hasEnvHttpProxyConfiguredMock.mockReturnValue(false);
415+shouldUseEnvHttpProxyForUrlMock.mockReturnValue(false);
421416fetchWithSsrFGuardMock.mockResolvedValue({
422417response: new Response(null, { status: 200 }),
423418finalUrl: "https://example.com",
@@ -432,7 +427,7 @@ describe("fetchWithTimeoutGuarded", () => {
432427});
433428434429it("auto-selects trusted env proxy mode when HTTP proxy env is configured", async () => {
435-hasEnvHttpProxyConfiguredMock.mockReturnValue(true);
430+shouldUseEnvHttpProxyForUrlMock.mockReturnValue(true);
436431fetchWithSsrFGuardMock.mockResolvedValue({
437432response: new Response(null, { status: 200 }),
438433finalUrl: "https://api.minimax.io",
@@ -454,7 +449,7 @@ describe("fetchWithTimeoutGuarded", () => {
454449});
455450456451it("respects an explicit mode from the caller when HTTP proxy env is configured", async () => {
457-hasEnvHttpProxyConfiguredMock.mockReturnValue(true);
452+shouldUseEnvHttpProxyForUrlMock.mockReturnValue(true);
458453fetchWithSsrFGuardMock.mockResolvedValue({
459454response: new Response(null, { status: 200 }),
460455finalUrl: "https://api.example.com",
@@ -473,7 +468,7 @@ describe("fetchWithTimeoutGuarded", () => {
473468});
474469475470it("auto-upgrades transcription requests to trusted env proxy when proxy env is configured", async () => {
476-hasEnvHttpProxyConfiguredMock.mockReturnValue(true);
471+shouldUseEnvHttpProxyForUrlMock.mockReturnValue(true);
477472fetchWithSsrFGuardMock.mockResolvedValue({
478473response: new Response(null, { status: 200 }),
479474finalUrl: "https://api.openai.com",
@@ -495,7 +490,7 @@ describe("fetchWithTimeoutGuarded", () => {
495490});
496491497492it("forwards an explicit mode override through postJsonRequest even when proxy env is configured", async () => {
498-hasEnvHttpProxyConfiguredMock.mockReturnValue(true);
493+shouldUseEnvHttpProxyForUrlMock.mockReturnValue(true);
499494fetchWithSsrFGuardMock.mockResolvedValue({
500495response: new Response(null, { status: 200 }),
501496finalUrl: "https://api.example.com",
@@ -518,7 +513,7 @@ describe("fetchWithTimeoutGuarded", () => {
518513});
519514520515it("forwards an explicit mode override through postTranscriptionRequest even when proxy env is configured", async () => {
521-hasEnvHttpProxyConfiguredMock.mockReturnValue(true);
516+shouldUseEnvHttpProxyForUrlMock.mockReturnValue(true);
522517fetchWithSsrFGuardMock.mockResolvedValue({
523518response: new Response(null, { status: 200 }),
524519finalUrl: "https://api.example.com",
@@ -541,11 +536,11 @@ describe("fetchWithTimeoutGuarded", () => {
541536});
542537543538it("does not auto-upgrade when only ALL_PROXY is configured (HTTP(S) proxy gate)", async () => {
544-// ALL_PROXY is ignored by EnvHttpProxyAgent; `hasEnvHttpProxyConfigured`
539+// ALL_PROXY is ignored by EnvHttpProxyAgent; the shared proxy URL helper
545540// reflects that by returning false when only ALL_PROXY is set. Auto-upgrade
546541// must NOT fire, otherwise the request would skip pinned-DNS/SSRF checks
547542// and then be dispatched directly.
548-hasEnvHttpProxyConfiguredMock.mockReturnValue(false);
543+shouldUseEnvHttpProxyForUrlMock.mockReturnValue(false);
549544fetchWithSsrFGuardMock.mockResolvedValue({
550545response: new Response(null, { status: 200 }),
551546finalUrl: "https://api.example.com",
@@ -568,7 +563,7 @@ describe("fetchWithTimeoutGuarded", () => {
568563// Callers with custom proxy URL / proxyTls / connect options must keep
569564// control over the dispatcher. Auto-upgrade would build an
570565// EnvHttpProxyAgent that silently drops those overrides.
571-hasEnvHttpProxyConfiguredMock.mockReturnValue(true);
566+shouldUseEnvHttpProxyForUrlMock.mockReturnValue(true);
572567fetchWithSsrFGuardMock.mockResolvedValue({
573568response: new Response(null, { status: 200 }),
574569finalUrl: "https://api.example.com",
@@ -595,8 +590,7 @@ describe("fetchWithTimeoutGuarded", () => {
595590// for NO_PROXY matches, but in TRUSTED_ENV_PROXY mode fetchWithSsrFGuard
596591// skips pinned-DNS checks — so auto-upgrading those targets would bypass
597592// SSRF protection. Keep strict mode for NO_PROXY matches.
598-hasEnvHttpProxyConfiguredMock.mockReturnValue(true);
599-matchesNoProxyMock.mockReturnValue(true);
593+shouldUseEnvHttpProxyForUrlMock.mockReturnValue(false);
600594fetchWithSsrFGuardMock.mockResolvedValue({
601595response: new Response(null, { status: 200 }),
602596finalUrl: "https://internal.corp.example",
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。