File size: 9,780 Bytes
fc93158
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import http from "node:http";
import https from "node:https";
import { afterEach, beforeEach, describe, expect, it } from "vitest";
import {
  getDirectAgentForCdp,
  hasProxyEnv,
  withNoProxyForCdpUrl,
  withNoProxyForLocalhost,
} from "./cdp-proxy-bypass.js";

const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));

async function withIsolatedNoProxyEnv(fn: () => Promise<void>) {
  const origNoProxy = process.env.NO_PROXY;
  const origNoProxyLower = process.env.no_proxy;
  const origHttpProxy = process.env.HTTP_PROXY;
  delete process.env.NO_PROXY;
  delete process.env.no_proxy;
  process.env.HTTP_PROXY = "http://proxy:8080";

  try {
    await fn();
  } finally {
    if (origHttpProxy !== undefined) {
      process.env.HTTP_PROXY = origHttpProxy;
    } else {
      delete process.env.HTTP_PROXY;
    }
    if (origNoProxy !== undefined) {
      process.env.NO_PROXY = origNoProxy;
    } else {
      delete process.env.NO_PROXY;
    }
    if (origNoProxyLower !== undefined) {
      process.env.no_proxy = origNoProxyLower;
    } else {
      delete process.env.no_proxy;
    }
  }
}

describe("cdp-proxy-bypass", () => {
  describe("getDirectAgentForCdp", () => {
    it("returns http.Agent for http://localhost URLs", () => {
      const agent = getDirectAgentForCdp("http://localhost:9222");
      expect(agent).toBeInstanceOf(http.Agent);
    });

    it("returns http.Agent for http://127.0.0.1 URLs", () => {
      const agent = getDirectAgentForCdp("http://127.0.0.1:9222/json/version");
      expect(agent).toBeInstanceOf(http.Agent);
    });

    it("returns https.Agent for wss://localhost URLs", () => {
      const agent = getDirectAgentForCdp("wss://localhost:9222");
      expect(agent).toBeInstanceOf(https.Agent);
    });

    it("returns https.Agent for https://127.0.0.1 URLs", () => {
      const agent = getDirectAgentForCdp("https://127.0.0.1:9222/json/version");
      expect(agent).toBeInstanceOf(https.Agent);
    });

    it("returns http.Agent for ws://[::1] URLs", () => {
      const agent = getDirectAgentForCdp("ws://[::1]:9222");
      expect(agent).toBeInstanceOf(http.Agent);
    });

    it("returns undefined for non-loopback URLs", () => {
      expect(getDirectAgentForCdp("http://remote-host:9222")).toBeUndefined();
      expect(getDirectAgentForCdp("https://example.com:9222")).toBeUndefined();
    });

    it("returns undefined for invalid URLs", () => {
      expect(getDirectAgentForCdp("not-a-url")).toBeUndefined();
    });
  });

  describe("hasProxyEnv", () => {
    const proxyVars = [
      "HTTP_PROXY",
      "http_proxy",
      "HTTPS_PROXY",
      "https_proxy",
      "ALL_PROXY",
      "all_proxy",
    ];
    const saved: Record<string, string | undefined> = {};

    beforeEach(() => {
      for (const v of proxyVars) {
        saved[v] = process.env[v];
      }
      for (const v of proxyVars) {
        delete process.env[v];
      }
    });

    afterEach(() => {
      for (const v of proxyVars) {
        if (saved[v] !== undefined) {
          process.env[v] = saved[v];
        } else {
          delete process.env[v];
        }
      }
    });

    it("returns false when no proxy vars set", () => {
      expect(hasProxyEnv()).toBe(false);
    });

    it("returns true when HTTP_PROXY is set", () => {
      process.env.HTTP_PROXY = "http://proxy:8080";
      expect(hasProxyEnv()).toBe(true);
    });

    it("returns true when ALL_PROXY is set", () => {
      process.env.ALL_PROXY = "socks5://proxy:1080";
      expect(hasProxyEnv()).toBe(true);
    });
  });

  describe("withNoProxyForLocalhost", () => {
    const saved: Record<string, string | undefined> = {};
    const vars = ["HTTP_PROXY", "NO_PROXY", "no_proxy"];

    beforeEach(() => {
      for (const v of vars) {
        saved[v] = process.env[v];
      }
    });

    afterEach(() => {
      for (const v of vars) {
        if (saved[v] !== undefined) {
          process.env[v] = saved[v];
        } else {
          delete process.env[v];
        }
      }
    });

    it("sets NO_PROXY when proxy is configured", async () => {
      process.env.HTTP_PROXY = "http://proxy:8080";
      delete process.env.NO_PROXY;
      delete process.env.no_proxy;

      let capturedNoProxy: string | undefined;
      await withNoProxyForLocalhost(async () => {
        capturedNoProxy = process.env.NO_PROXY;
      });

      expect(capturedNoProxy).toContain("localhost");
      expect(capturedNoProxy).toContain("127.0.0.1");
      expect(capturedNoProxy).toContain("[::1]");
      // Restored after
      expect(process.env.NO_PROXY).toBeUndefined();
    });

    it("extends existing NO_PROXY", async () => {
      process.env.HTTP_PROXY = "http://proxy:8080";
      process.env.NO_PROXY = "internal.corp";

      let capturedNoProxy: string | undefined;
      await withNoProxyForLocalhost(async () => {
        capturedNoProxy = process.env.NO_PROXY;
      });

      expect(capturedNoProxy).toContain("internal.corp");
      expect(capturedNoProxy).toContain("localhost");
      // Restored
      expect(process.env.NO_PROXY).toBe("internal.corp");
    });

    it("skips when no proxy env is set", async () => {
      delete process.env.HTTP_PROXY;
      delete process.env.HTTPS_PROXY;
      delete process.env.ALL_PROXY;
      delete process.env.NO_PROXY;

      await withNoProxyForLocalhost(async () => {
        expect(process.env.NO_PROXY).toBeUndefined();
      });
    });

    it("restores env even on error", async () => {
      process.env.HTTP_PROXY = "http://proxy:8080";
      delete process.env.NO_PROXY;

      await expect(
        withNoProxyForLocalhost(async () => {
          throw new Error("boom");
        }),
      ).rejects.toThrow("boom");

      expect(process.env.NO_PROXY).toBeUndefined();
    });
  });
});

describe("withNoProxyForLocalhost concurrency", () => {
  it("does not leak NO_PROXY when called concurrently", async () => {
    await withIsolatedNoProxyEnv(async () => {
      const { withNoProxyForLocalhost } = await import("./cdp-proxy-bypass.js");

      // Simulate concurrent calls
      const callA = withNoProxyForLocalhost(async () => {
        // While A is running, NO_PROXY should be set
        expect(process.env.NO_PROXY).toContain("localhost");
        expect(process.env.NO_PROXY).toContain("[::1]");
        await delay(50);
        return "a";
      });
      const callB = withNoProxyForLocalhost(async () => {
        await delay(20);
        return "b";
      });

      await Promise.all([callA, callB]);

      // After both complete, NO_PROXY should be restored (deleted)
      expect(process.env.NO_PROXY).toBeUndefined();
      expect(process.env.no_proxy).toBeUndefined();
    });
  });
});

describe("withNoProxyForLocalhost reverse exit order", () => {
  it("restores NO_PROXY when first caller exits before second", async () => {
    await withIsolatedNoProxyEnv(async () => {
      const { withNoProxyForLocalhost } = await import("./cdp-proxy-bypass.js");

      // Call A enters first, exits first (short task)
      // Call B enters second, exits last (long task)
      const callA = withNoProxyForLocalhost(async () => {
        await delay(10);
        return "a";
      });
      const callB = withNoProxyForLocalhost(async () => {
        await delay(60);
        return "b";
      });

      await Promise.all([callA, callB]);

      // After both complete, NO_PROXY must be cleaned up
      expect(process.env.NO_PROXY).toBeUndefined();
      expect(process.env.no_proxy).toBeUndefined();
    });
  });
});

describe("withNoProxyForLocalhost preserves user-configured NO_PROXY", () => {
  it("does not delete NO_PROXY when loopback entries already present", async () => {
    const userNoProxy = "localhost,127.0.0.1,[::1],myhost.internal";
    process.env.NO_PROXY = userNoProxy;
    process.env.no_proxy = userNoProxy;
    process.env.HTTP_PROXY = "http://proxy:8080";

    try {
      const { withNoProxyForLocalhost } = await import("./cdp-proxy-bypass.js");

      await withNoProxyForLocalhost(async () => {
        // Should not modify since loopback is already covered
        expect(process.env.NO_PROXY).toBe(userNoProxy);
        return "ok";
      });

      // After call completes, user's NO_PROXY must still be intact
      expect(process.env.NO_PROXY).toBe(userNoProxy);
      expect(process.env.no_proxy).toBe(userNoProxy);
    } finally {
      delete process.env.HTTP_PROXY;
      delete process.env.NO_PROXY;
      delete process.env.no_proxy;
    }
  });
});

describe("withNoProxyForCdpUrl", () => {
  it("does not mutate NO_PROXY for non-loopback CDP URLs", async () => {
    process.env.HTTP_PROXY = "http://proxy:8080";
    delete process.env.NO_PROXY;
    delete process.env.no_proxy;
    try {
      await withNoProxyForCdpUrl("https://browserless.example/chrome?token=abc", async () => {
        expect(process.env.NO_PROXY).toBeUndefined();
        expect(process.env.no_proxy).toBeUndefined();
      });
    } finally {
      delete process.env.HTTP_PROXY;
      delete process.env.NO_PROXY;
      delete process.env.no_proxy;
    }
  });

  it("does not overwrite external NO_PROXY changes made during execution", async () => {
    process.env.HTTP_PROXY = "http://proxy:8080";
    delete process.env.NO_PROXY;
    delete process.env.no_proxy;
    try {
      await withNoProxyForCdpUrl("http://127.0.0.1:9222", async () => {
        process.env.NO_PROXY = "externally-set";
        process.env.no_proxy = "externally-set";
      });
      expect(process.env.NO_PROXY).toBe("externally-set");
      expect(process.env.no_proxy).toBe("externally-set");
    } finally {
      delete process.env.HTTP_PROXY;
      delete process.env.NO_PROXY;
      delete process.env.no_proxy;
    }
  });
});