|
| 1 | +// Tests for RFC 7230 §5.3.2 absolute-form request-targets in forward-proxy mode. |
| 2 | +// |
| 3 | +// A client configured with HTTP(S)_PROXY does not always tunnel: axios's |
| 4 | +// built-in proxy support (which the Claude Code CLI's auto-updater and |
| 5 | +// telemetry paths use) sends `GET https://host/path HTTP/1.1` on the plain |
| 6 | +// proxy connection instead of issuing CONNECT. A conforming HTTP proxy must |
| 7 | +// honor the authority in that request-target. The proxy instead treated the |
| 8 | +// absolute URI as an origin-form path and concatenated it onto the configured |
| 9 | +// upstream (`https://api.anthropic.com/https://downloads.claude.ai/...`), |
| 10 | +// misrouting every such request to the upstream host — Cloudflare answers 404 |
| 11 | +// and the CLI renders a permanent "✘ Auto-update failed" banner (its 1P event |
| 12 | +// export and Datadog flush 404 the same way). |
| 13 | +// |
| 14 | +// Contract under test: |
| 15 | +// - forward mode, absolute-form to a FOREIGN host -> relayed to that host |
| 16 | +// - forward mode, absolute-form to the UPSTREAM -> behaves as origin-form |
| 17 | +// - reverse mode, absolute-form -> 404 (contract unchanged) |
| 18 | + |
| 19 | +import { test } from "node:test"; |
| 20 | +import assert from "node:assert/strict"; |
| 21 | +import http from "node:http"; |
| 22 | +import { mkdtempSync, rmSync } from "node:fs"; |
| 23 | +import { tmpdir } from "node:os"; |
| 24 | +import { join } from "node:path"; |
| 25 | + |
| 26 | +import { startProxy } from "../proxy/server.mjs"; |
| 27 | + |
| 28 | +const ENV_KEYS = [ |
| 29 | + "CACHE_FIX_FORWARD_PROXY", "CACHE_FIX_CA_DIR", "CACHE_FIX_PROXY_UPSTREAM", |
| 30 | + "CACHE_FIX_HTTPS_PROXY", "HTTPS_PROXY", "HTTP_PROXY", "https_proxy", "http_proxy", |
| 31 | + "NO_PROXY", "no_proxy", |
| 32 | +]; |
| 33 | + |
| 34 | +function saveEnv() { |
| 35 | + const saved = {}; |
| 36 | + for (const k of ENV_KEYS) saved[k] = process.env[k]; |
| 37 | + return saved; |
| 38 | +} |
| 39 | +function restoreEnv(saved) { |
| 40 | + for (const k of ENV_KEYS) { |
| 41 | + if (saved[k] === undefined) delete process.env[k]; |
| 42 | + else process.env[k] = saved[k]; |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +function listen(server) { |
| 47 | + return new Promise((resolve) => server.listen(0, "127.0.0.1", () => resolve(server.address().port))); |
| 48 | +} |
| 49 | + |
| 50 | +// Send a raw absolute-form request THROUGH the proxy port: the request line's |
| 51 | +// target is the full URI, exactly what axios emits to a plain HTTP proxy. |
| 52 | +function absoluteFormRequest(proxyPort, method, absoluteUrl, body) { |
| 53 | + return new Promise((resolve, reject) => { |
| 54 | + const req = http.request( |
| 55 | + { hostname: "127.0.0.1", port: proxyPort, method, path: absoluteUrl }, |
| 56 | + (res) => { |
| 57 | + const chunks = []; |
| 58 | + res.on("data", (c) => chunks.push(c)); |
| 59 | + res.on("end", () => resolve({ status: res.statusCode, body: Buffer.concat(chunks).toString() })); |
| 60 | + } |
| 61 | + ); |
| 62 | + req.on("error", reject); |
| 63 | + if (body) req.write(body); |
| 64 | + req.end(); |
| 65 | + }); |
| 66 | +} |
| 67 | + |
| 68 | +test("forward mode: absolute-form to a foreign host is relayed to that host, not the upstream", async () => { |
| 69 | + const saved = saveEnv(); |
| 70 | + const caDir = mkdtempSync(join(tmpdir(), "absform-ca-")); |
| 71 | + |
| 72 | + const upstreamHits = []; |
| 73 | + const upstream = http.createServer((req, res) => { |
| 74 | + upstreamHits.push(req.url); |
| 75 | + res.writeHead(200, { "content-type": "application/json" }); |
| 76 | + res.end("{}"); |
| 77 | + }); |
| 78 | + const upstreamPort = await listen(upstream); |
| 79 | + |
| 80 | + const foreignHits = []; |
| 81 | + const foreign = http.createServer((req, res) => { |
| 82 | + foreignHits.push({ url: req.url, host: req.headers.host }); |
| 83 | + res.writeHead(200, { "content-type": "text/plain" }); |
| 84 | + res.end("2.1.999"); |
| 85 | + }); |
| 86 | + const foreignPort = await listen(foreign); |
| 87 | + |
| 88 | + let handle; |
| 89 | + try { |
| 90 | + process.env.CACHE_FIX_FORWARD_PROXY = "on"; |
| 91 | + process.env.CACHE_FIX_CA_DIR = caDir; |
| 92 | + process.env.CACHE_FIX_PROXY_UPSTREAM = `http://127.0.0.1:${upstreamPort}`; |
| 93 | + delete process.env.CACHE_FIX_HTTPS_PROXY; |
| 94 | + delete process.env.HTTPS_PROXY; delete process.env.HTTP_PROXY; |
| 95 | + delete process.env.https_proxy; delete process.env.http_proxy; |
| 96 | + delete process.env.NO_PROXY; delete process.env.no_proxy; |
| 97 | + |
| 98 | + handle = await startProxy({ port: 0, watch: false }); |
| 99 | + |
| 100 | + const r = await absoluteFormRequest( |
| 101 | + handle.port, "GET", `http://127.0.0.1:${foreignPort}/claude-code-releases/latest`); |
| 102 | + |
| 103 | + assert.equal(r.status, 200, "absolute-form request must reach its own target host"); |
| 104 | + assert.equal(r.body, "2.1.999", "response body must stream back from the target"); |
| 105 | + assert.equal(foreignHits.length, 1, "target host must be hit exactly once"); |
| 106 | + assert.equal(foreignHits[0].url, "/claude-code-releases/latest", |
| 107 | + "target must receive the origin-form path, not the absolute URI"); |
| 108 | + assert.deepEqual(upstreamHits, [], |
| 109 | + "the upstream must NOT see a foreign-host absolute-form request"); |
| 110 | + } finally { |
| 111 | + restoreEnv(saved); |
| 112 | + if (handle) await handle.close(); |
| 113 | + upstream.close(); |
| 114 | + foreign.close(); |
| 115 | + try { rmSync(caDir, { recursive: true, force: true }); } catch {} |
| 116 | + } |
| 117 | +}); |
| 118 | + |
| 119 | +test("forward mode: absolute-form to the upstream host behaves as origin-form (path + body intact)", async () => { |
| 120 | + const saved = saveEnv(); |
| 121 | + const caDir = mkdtempSync(join(tmpdir(), "absform-up-ca-")); |
| 122 | + |
| 123 | + const upstreamHits = []; |
| 124 | + const upstream = http.createServer((req, res) => { |
| 125 | + const chunks = []; |
| 126 | + req.on("data", (c) => chunks.push(c)); |
| 127 | + req.on("end", () => { |
| 128 | + upstreamHits.push({ url: req.url, body: Buffer.concat(chunks).toString() }); |
| 129 | + res.writeHead(200, { "content-type": "application/json" }); |
| 130 | + res.end("{}"); |
| 131 | + }); |
| 132 | + }); |
| 133 | + const upstreamPort = await listen(upstream); |
| 134 | + |
| 135 | + let handle; |
| 136 | + try { |
| 137 | + process.env.CACHE_FIX_FORWARD_PROXY = "on"; |
| 138 | + process.env.CACHE_FIX_CA_DIR = caDir; |
| 139 | + process.env.CACHE_FIX_PROXY_UPSTREAM = `http://127.0.0.1:${upstreamPort}`; |
| 140 | + delete process.env.CACHE_FIX_HTTPS_PROXY; |
| 141 | + delete process.env.HTTPS_PROXY; delete process.env.HTTP_PROXY; |
| 142 | + delete process.env.https_proxy; delete process.env.http_proxy; |
| 143 | + delete process.env.NO_PROXY; delete process.env.no_proxy; |
| 144 | + |
| 145 | + handle = await startProxy({ port: 0, watch: false }); |
| 146 | + |
| 147 | + const r = await absoluteFormRequest( |
| 148 | + handle.port, "POST", |
| 149 | + `http://127.0.0.1:${upstreamPort}/api/event_logging/v2/batch`, |
| 150 | + '{"events":[]}'); |
| 151 | + |
| 152 | + assert.equal(r.status, 200); |
| 153 | + assert.equal(upstreamHits.length, 1, "upstream must be hit exactly once"); |
| 154 | + assert.equal(upstreamHits[0].url, "/api/event_logging/v2/batch", |
| 155 | + "upstream must receive the origin-form path, not a concatenated absolute URI"); |
| 156 | + assert.equal(upstreamHits[0].body, '{"events":[]}', "request body must pass through"); |
| 157 | + } finally { |
| 158 | + restoreEnv(saved); |
| 159 | + if (handle) await handle.close(); |
| 160 | + upstream.close(); |
| 161 | + try { rmSync(caDir, { recursive: true, force: true }); } catch {} |
| 162 | + } |
| 163 | +}); |
| 164 | + |
| 165 | +test("reverse mode: absolute-form keeps the 404 contract (no relay)", async () => { |
| 166 | + const saved = saveEnv(); |
| 167 | + |
| 168 | + const foreignHits = []; |
| 169 | + const foreign = http.createServer((req, res) => { |
| 170 | + foreignHits.push(req.url); |
| 171 | + res.writeHead(200); |
| 172 | + res.end("nope"); |
| 173 | + }); |
| 174 | + const foreignPort = await listen(foreign); |
| 175 | + |
| 176 | + let handle; |
| 177 | + try { |
| 178 | + delete process.env.CACHE_FIX_FORWARD_PROXY; |
| 179 | + delete process.env.CACHE_FIX_HTTPS_PROXY; |
| 180 | + delete process.env.HTTPS_PROXY; delete process.env.HTTP_PROXY; |
| 181 | + delete process.env.https_proxy; delete process.env.http_proxy; |
| 182 | + |
| 183 | + handle = await startProxy({ port: 0, watch: false }); |
| 184 | + |
| 185 | + const r = await absoluteFormRequest( |
| 186 | + handle.port, "GET", `http://127.0.0.1:${foreignPort}/anything`); |
| 187 | + |
| 188 | + assert.equal(r.status, 404, "reverse mode must not act as a forward proxy"); |
| 189 | + assert.deepEqual(foreignHits, [], "reverse mode must not relay absolute-form requests"); |
| 190 | + } finally { |
| 191 | + restoreEnv(saved); |
| 192 | + if (handle) await handle.close(); |
| 193 | + foreign.close(); |
| 194 | + } |
| 195 | +}); |
0 commit comments