|
| 1 | +import { rm } from "node:fs/promises"; |
| 2 | +import { Socket } from "node:net"; |
| 3 | +import { afterEach, beforeEach, describe, expect, it } from "vitest"; |
| 4 | +import { assertDebugProxyDirectConnectAllowed, startDebugProxyServer } from "./proxy-server.js"; |
| 5 | + |
| 6 | +const testBlobDir = ".tmp-debug-proxy-test-blobs"; |
| 7 | +const testCertDir = ".tmp-debug-proxy-test-certs"; |
| 8 | + |
| 9 | +async function cleanupTestDirs(): Promise<void> { |
| 10 | + await Promise.all([ |
| 11 | + rm(testBlobDir, { recursive: true, force: true }), |
| 12 | + rm(testCertDir, { recursive: true, force: true }), |
| 13 | + ]); |
| 14 | +} |
| 15 | + |
| 16 | +function makeSettings() { |
| 17 | + return { |
| 18 | + enabled: true, |
| 19 | + required: false, |
| 20 | + dbPath: ":memory:", |
| 21 | + blobDir: testBlobDir, |
| 22 | + certDir: testCertDir, |
| 23 | + sessionId: "debug-proxy-managed-proxy-test", |
| 24 | + sourceProcess: "test", |
| 25 | + }; |
| 26 | +} |
| 27 | + |
| 28 | +async function connectThroughProxy(proxyUrl: string): Promise<string> { |
| 29 | + const target = new URL(proxyUrl); |
| 30 | + const socket = new Socket(); |
| 31 | + let data = ""; |
| 32 | + socket.setEncoding("utf8"); |
| 33 | + socket.on("data", (chunk) => { |
| 34 | + data += chunk; |
| 35 | + }); |
| 36 | + await new Promise<void>((resolve, reject) => { |
| 37 | + socket.once("error", reject); |
| 38 | + socket.connect(Number(target.port), target.hostname, resolve); |
| 39 | + }); |
| 40 | + socket.write("CONNECT example.com:443 HTTP/1.1\r\nHost: example.com:443\r\n\r\n"); |
| 41 | + await new Promise<void>((resolve) => socket.once("end", resolve)); |
| 42 | + socket.destroy(); |
| 43 | + return data; |
| 44 | +} |
| 45 | + |
| 46 | +describe("debug proxy managed-proxy CONNECT policy", () => { |
| 47 | + const originalProxyActive = process.env["OPENCLAW_PROXY_ACTIVE"]; |
| 48 | + const originalAllowDirect = |
| 49 | + process.env["OPENCLAW_DEBUG_PROXY_ALLOW_DIRECT_CONNECT_WITH_MANAGED_PROXY"]; |
| 50 | + |
| 51 | + beforeEach(async () => { |
| 52 | + await cleanupTestDirs(); |
| 53 | + delete process.env["OPENCLAW_PROXY_ACTIVE"]; |
| 54 | + delete process.env["OPENCLAW_DEBUG_PROXY_ALLOW_DIRECT_CONNECT_WITH_MANAGED_PROXY"]; |
| 55 | + }); |
| 56 | + |
| 57 | + afterEach(async () => { |
| 58 | + if (originalProxyActive === undefined) { |
| 59 | + delete process.env["OPENCLAW_PROXY_ACTIVE"]; |
| 60 | + } else { |
| 61 | + process.env["OPENCLAW_PROXY_ACTIVE"] = originalProxyActive; |
| 62 | + } |
| 63 | + if (originalAllowDirect === undefined) { |
| 64 | + delete process.env["OPENCLAW_DEBUG_PROXY_ALLOW_DIRECT_CONNECT_WITH_MANAGED_PROXY"]; |
| 65 | + } else { |
| 66 | + process.env["OPENCLAW_DEBUG_PROXY_ALLOW_DIRECT_CONNECT_WITH_MANAGED_PROXY"] = |
| 67 | + originalAllowDirect; |
| 68 | + } |
| 69 | + await cleanupTestDirs(); |
| 70 | + }); |
| 71 | + |
| 72 | + it("allows direct CONNECT upstreams when managed proxy mode is inactive", () => { |
| 73 | + expect(() => assertDebugProxyDirectConnectAllowed()).not.toThrow(); |
| 74 | + }); |
| 75 | + |
| 76 | + it("rejects direct CONNECT upstreams while managed proxy mode is active", () => { |
| 77 | + process.env["OPENCLAW_PROXY_ACTIVE"] = "1"; |
| 78 | + |
| 79 | + expect(() => assertDebugProxyDirectConnectAllowed()).toThrow( |
| 80 | + /Debug proxy CONNECT upstream forwarding is disabled/, |
| 81 | + ); |
| 82 | + }); |
| 83 | + |
| 84 | + it("allows direct CONNECT upstreams with explicit diagnostic override", () => { |
| 85 | + process.env["OPENCLAW_PROXY_ACTIVE"] = "1"; |
| 86 | + process.env["OPENCLAW_DEBUG_PROXY_ALLOW_DIRECT_CONNECT_WITH_MANAGED_PROXY"] = "1"; |
| 87 | + |
| 88 | + expect(() => assertDebugProxyDirectConnectAllowed()).not.toThrow(); |
| 89 | + }); |
| 90 | + |
| 91 | + it("rejects CONNECT upstreams before opening direct sockets while managed proxy mode is active", async () => { |
| 92 | + process.env["OPENCLAW_PROXY_ACTIVE"] = "1"; |
| 93 | + const server = await startDebugProxyServer({ settings: makeSettings() }); |
| 94 | + try { |
| 95 | + const response = await connectThroughProxy(server.proxyUrl); |
| 96 | + |
| 97 | + expect(response).toContain("502 Bad Gateway"); |
| 98 | + expect(response).toContain("Debug proxy CONNECT upstream forwarding is disabled"); |
| 99 | + } finally { |
| 100 | + await server.stop(); |
| 101 | + } |
| 102 | + }); |
| 103 | +}); |
0 commit comments