Skip to content

Commit e9d879b

Browse files
committed
fix(minimax): pin bridge traffic to loopback
1 parent 9eb3a10 commit e9d879b

3 files changed

Lines changed: 89 additions & 2 deletions

File tree

‎src/cli/minimax.ts‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,12 +190,18 @@ export function startMmxTextBridge(
190190
? null
191191
: clearableDeadline(options.headerTimeoutMs, req.signal);
192192
try {
193-
return await fetch(new Request(target, {
193+
const upstreamRequest = new Request(target, {
194194
method: "POST",
195195
headers,
196196
body: req.body,
197197
signal: headerDeadline?.signal ?? req.signal,
198-
}));
198+
});
199+
return await fetch(upstreamRequest, {
200+
// Override HTTP(S)_PROXY with the loopback listener itself. Bun sends
201+
// the HTTP proxy-form request directly to this exact origin, so the
202+
// hop cannot leave the machine even when the parent has proxy vars.
203+
proxy: { url: upstreamOrigin },
204+
});
199205
} catch {
200206
return Response.json({
201207
type: "error",
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { startMmxTextBridge } from "../../src/cli/minimax";
2+
3+
let proxyRequests = 0;
4+
let upstreamBody = "";
5+
const upstream = Bun.serve({
6+
hostname: "127.0.0.1",
7+
port: 0,
8+
async fetch(request) {
9+
upstreamBody = await request.text();
10+
return Response.json({ source: "upstream" });
11+
},
12+
});
13+
const attackerProxy = Bun.serve({
14+
hostname: "127.0.0.1",
15+
port: Number(process.env.TEST_PROXY_PORT),
16+
fetch() {
17+
proxyRequests += 1;
18+
return Response.json({ source: "proxy" });
19+
},
20+
});
21+
const bridge = startMmxTextBridge({ hostname: "127.0.0.1", port: upstream.port });
22+
23+
try {
24+
const response = await fetch(`${bridge.baseUrl}/anthropic/v1/messages`, {
25+
method: "POST",
26+
headers: { "content-type": "application/json" },
27+
body: JSON.stringify({ prompt: "private bridge payload" }),
28+
proxy: { url: bridge.baseUrl },
29+
});
30+
const responseText = await response.text();
31+
console.log(JSON.stringify({
32+
responseStatus: response.status,
33+
responseText,
34+
proxyRequests,
35+
upstreamBody,
36+
}));
37+
} finally {
38+
await bridge.stop();
39+
await upstream.stop(true);
40+
await attackerProxy.stop(true);
41+
}

‎tests/minimax-clients.test.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { describe, expect, test } from "bun:test";
22
import { EventEmitter } from "node:events";
3+
import { createServer } from "node:net";
34
import { join } from "node:path";
45
import {
56
ClientPathError,
@@ -90,6 +91,45 @@ describe("MiniMax Code client config", () => {
9091
});
9192

9293
describe("MiniMax CLI wrapper", () => {
94+
test("keeps the bridge's upstream hop direct when the parent has a proxy", async () => {
95+
const reservation = createServer();
96+
const proxyPort = await new Promise<number>((resolve, reject) => {
97+
reservation.once("error", reject);
98+
reservation.listen(0, "127.0.0.1", () => {
99+
const address = reservation.address();
100+
if (!address || typeof address === "string") reject(new Error("proxy port reservation failed"));
101+
else resolve(address.port);
102+
});
103+
});
104+
await new Promise<void>((resolve, reject) => reservation.close(error => error ? reject(error) : resolve()));
105+
106+
const child = Bun.spawn([process.execPath, join(import.meta.dir, "fixtures/minimax-bridge-direct.ts")], {
107+
env: {
108+
...process.env,
109+
HTTP_PROXY: `http://127.0.0.1:${proxyPort}`,
110+
HTTPS_PROXY: `http://127.0.0.1:${proxyPort}`,
111+
ALL_PROXY: `http://127.0.0.1:${proxyPort}`,
112+
NO_PROXY: "",
113+
no_proxy: "",
114+
TEST_PROXY_PORT: String(proxyPort),
115+
},
116+
stdout: "pipe",
117+
stderr: "pipe",
118+
});
119+
const [stdout, stderr, exitCode] = await Promise.all([
120+
new Response(child.stdout).text(),
121+
new Response(child.stderr).text(),
122+
child.exited,
123+
]);
124+
expect(exitCode, stderr).toBe(0);
125+
expect(JSON.parse(stdout.trim())).toEqual({
126+
responseStatus: 200,
127+
responseText: JSON.stringify({ source: "upstream" }),
128+
proxyRequests: 0,
129+
upstreamBody: JSON.stringify({ prompt: "private bridge payload" }),
130+
});
131+
});
132+
93133
test("passes through only standalone help and officially supported version invocations", () => {
94134
expect(isStandaloneInformationalInvocation(["--help"], "mmx")).toBeTrue();
95135
expect(isStandaloneInformationalInvocation(["--version"], "mmx")).toBeTrue();

0 commit comments

Comments
 (0)