Skip to content

Commit 5ea9581

Browse files
authored
Merge pull request #38 from LX2000WASD/fix/responses-captcha
fix(responses): /v1/responses never sent captcha tokens — every start-plan request failed with 3007
2 parents 9affffa + da622c9 commit 5ea9581

2 files changed

Lines changed: 189 additions & 9 deletions

File tree

‎src/proxy/responses-handler.test.ts‎

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import { describe, it, expect } from "bun:test";
22
import { handleResponses } from "./responses-handler.js";
33
import { ResponseStore } from "../responses/store.js";
44
import type { ProxyConfig } from "../config/types.js";
5+
import type * as CaptchaExports from "./captcha.js";
6+
7+
type CaptchaModule = typeof CaptchaExports;
58

69
const CONFIG: ProxyConfig = {
710
server: { port: 0, host: "127.0.0.1" },
@@ -156,3 +159,120 @@ describe("handleResponses", () => {
156159
expect(continuationUpstreamBody).toContain("second turn");
157160
});
158161
});
162+
163+
// /v1/responses used to skip captcha entirely (it passed `undefined` where
164+
// handler.ts passes captcha headers), so every start-plan request came back as
165+
// HTTP 400 {"code":3007,"msg":"captcha verify failed"} while the OpenAI and
166+
// Anthropic routes worked.
167+
describe("handleResponses captcha (start-plan)", () => {
168+
const START_PLAN: ProxyConfig = { ...CONFIG, plan: "start-plan", provider: "bigmodel" };
169+
const PARAM_HEADER = "x-aliyun-captcha-verify-param";
170+
const REGION_HEADER = "x-aliyun-captcha-verify-region";
171+
172+
/** Fake captcha module: mints sequential tokens, records how many were asked for. */
173+
function fakeCaptcha(): { module: CaptchaModule; minted: () => number } {
174+
let n = 0;
175+
const module = {
176+
RETRY_HEADERS: { PARAM: PARAM_HEADER, REGION: REGION_HEADER },
177+
getCaptchaToken: async () => {
178+
n += 1;
179+
return { verifyParam: `token-${n}`, region: "cn" };
180+
},
181+
detectCaptchaChallenge: (resp: Response) => resp.headers.get(PARAM_HEADER),
182+
} as unknown as CaptchaModule;
183+
return { module, minted: () => n };
184+
}
185+
186+
it("sends a captcha token upstream on start-plan", async () => {
187+
const seen: (string | null)[] = [];
188+
const fetchImpl = (async (request: Request): Promise<Response> => {
189+
seen.push(request.headers.get(PARAM_HEADER));
190+
return new Response(anthropicMsg("ok"), { status: 200, headers: { "content-type": "application/json" } });
191+
}) as unknown as typeof fetch;
192+
193+
const captcha = fakeCaptcha();
194+
const resp = await handleResponses(makeReq({ model: "glm-5.2", input: "hi" }), {
195+
config: START_PLAN, auth, fetchImpl, captcha: captcha.module,
196+
});
197+
expect(resp.status).toBe(200);
198+
expect(seen).toEqual(["token-1"]);
199+
expect(captcha.minted()).toBe(1);
200+
});
201+
202+
it("retries once with a fresh token when upstream answers in-body 3007", async () => {
203+
const seen: (string | null)[] = [];
204+
const fetchImpl = (async (request: Request): Promise<Response> => {
205+
seen.push(request.headers.get(PARAM_HEADER));
206+
if (seen.length === 1) {
207+
return new Response(JSON.stringify({ code: 3007, msg: "captcha verify failed" }), {
208+
status: 400, headers: { "content-type": "application/json" },
209+
});
210+
}
211+
return new Response(anthropicMsg("recovered"), { status: 200, headers: { "content-type": "application/json" } });
212+
}) as unknown as typeof fetch;
213+
214+
const captcha = fakeCaptcha();
215+
const resp = await handleResponses(makeReq({ model: "glm-5.2", input: "hi" }), {
216+
config: START_PLAN, auth, fetchImpl, captcha: captcha.module,
217+
});
218+
expect(resp.status).toBe(200);
219+
const body = await resp.json();
220+
expect(body.output[0].content[0].text).toBe("recovered");
221+
// The challenged token is already spent, so the retry must carry a NEW one.
222+
expect(seen).toEqual(["token-1", "token-2"]);
223+
});
224+
225+
it("retries when the challenge arrives as a response header", async () => {
226+
const seen: (string | null)[] = [];
227+
const fetchImpl = (async (request: Request): Promise<Response> => {
228+
seen.push(request.headers.get(PARAM_HEADER));
229+
if (seen.length === 1) {
230+
return new Response("denied", { status: 403, headers: { [PARAM_HEADER]: "challenge-abc" } });
231+
}
232+
return new Response(anthropicMsg("ok"), { status: 200, headers: { "content-type": "application/json" } });
233+
}) as unknown as typeof fetch;
234+
235+
const captcha = fakeCaptcha();
236+
const resp = await handleResponses(makeReq({ model: "glm-5.2", input: "hi" }), {
237+
config: START_PLAN, auth, fetchImpl, captcha: captcha.module,
238+
});
239+
expect(resp.status).toBe(200);
240+
expect(seen).toEqual(["token-1", "token-2"]);
241+
});
242+
243+
it("gives up after one retry instead of looping", async () => {
244+
let calls = 0;
245+
const fetchImpl = (async (): Promise<Response> => {
246+
calls += 1;
247+
return new Response(JSON.stringify({ code: 3007, msg: "captcha verify failed" }), {
248+
status: 400, headers: { "content-type": "application/json" },
249+
});
250+
}) as unknown as typeof fetch;
251+
252+
const captcha = fakeCaptcha();
253+
const resp = await handleResponses(makeReq({ model: "glm-5.2", input: "hi" }), {
254+
config: START_PLAN, auth, fetchImpl, captcha: captcha.module,
255+
});
256+
expect(calls).toBe(2);
257+
expect(resp.status).toBe(400);
258+
const body = await resp.json();
259+
expect(body.error.type).toBe("upstream_error");
260+
expect(body.error.message).toContain("3007");
261+
});
262+
263+
it("does not touch captcha on coding-plan", async () => {
264+
const seen: (string | null)[] = [];
265+
const fetchImpl = (async (request: Request): Promise<Response> => {
266+
seen.push(request.headers.get(PARAM_HEADER));
267+
return new Response(anthropicMsg("ok"), { status: 200, headers: { "content-type": "application/json" } });
268+
}) as unknown as typeof fetch;
269+
270+
const captcha = fakeCaptcha();
271+
const resp = await handleResponses(makeReq({ model: "glm-5.2", input: "hi" }), {
272+
config: CONFIG, auth, fetchImpl, captcha: captcha.module,
273+
});
274+
expect(resp.status).toBe(200);
275+
expect(seen).toEqual([null]);
276+
expect(captcha.minted()).toBe(0);
277+
});
278+
});

‎src/proxy/responses-handler.ts‎

Lines changed: 69 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,18 @@ import { transformRequestBody } from "./body-transformer.js";
2121
import { getProvider } from "../provider/providers.js";
2222
import type { ProxyConfig } from "../config/types.js";
2323
import type { AuthManager } from "../auth/manager.js";
24-
import { buildUpstreamRequest, buildUpstreamHeaderPairs } from "./upstream.js";
24+
import { buildUpstreamRequest, buildUpstreamHeaderPairs, type UpstreamHeaderPair } from "./upstream.js";
25+
import type * as CaptchaExports from "./captcha.js";
26+
27+
// Lazy, runtime-gated module load (exception to the static-import rule, same
28+
// as handler.ts): pulling captcha.ts eagerly drags in the happy-dom solver, so
29+
// only start-plan — the one plan whose upstream is captcha-gated — pays for it.
30+
type CaptchaModule = typeof CaptchaExports;
31+
let captchaModule: CaptchaModule | null = null;
32+
async function loadCaptcha(): Promise<CaptchaModule> {
33+
if (!captchaModule) captchaModule = await import("./captcha.js");
34+
return captchaModule;
35+
}
2536
import { getDefaultEndpointRouting, type EndpointRoutingService } from "./endpoint-routing.js";
2637
import { getDefaultClientSigning, sendWithClientSigning, type ClientSigningManager } from "./client-signing.js";
2738
import { credentialString } from "../auth/types.js";
@@ -64,6 +75,8 @@ export interface ResponsesHandlerOptions {
6475
endpointRouting?: EndpointRoutingService | null;
6576
/** Override the process-wide client signing manager (for testing). `null` disables. */
6677
clientSigning?: ClientSigningManager | null;
78+
/** Override the lazily-imported captcha module (for testing). */
79+
captcha?: CaptchaModule;
6780
}
6881

6982
/** Handle POST /v1/responses. */
@@ -166,22 +179,35 @@ export async function handleResponses(
166179
const transformedBody = upstreamRequestBody;
167180

168181
// ── 6. POST upstream ──
169-
const upstreamHeaders = buildUpstreamHeaderPairs(clientReq, upstreamFormat, cred, opts.config.identity, opts.config.plan, undefined, undefined);
170-
const upstreamReq = buildUpstreamRequest(clientReq, upstreamFormat, providerDef, cred, transformedBody, opts.config.identity, opts.config.plan, undefined, undefined);
182+
// start-plan gates every upstream call behind an Aliyun captcha token. The
183+
// Anthropic/OpenAI routes mint one in handler.ts; /v1/responses did not, so
184+
// start-plan users got {"code":3007,"msg":"captcha verify failed"} surfaced
185+
// as HTTP 400 upstream_error on every request.
186+
let captchaHeaders: Record<string, string> | undefined;
187+
if (startPlan) {
188+
try {
189+
const captcha = opts.captcha ?? (await loadCaptcha());
190+
const token = await captcha.getCaptchaToken(opts.config.identity.appVersion);
191+
captchaHeaders = { [captcha.RETRY_HEADERS.PARAM]: token.verifyParam, [captcha.RETRY_HEADERS.REGION]: token.region };
192+
} catch {
193+
// Fall through: the 3007 retry below solves on demand.
194+
}
195+
}
196+
const upstreamHeaders = buildUpstreamHeaderPairs(clientReq, upstreamFormat, cred, opts.config.identity, opts.config.plan, captchaHeaders, undefined);
197+
const upstreamReq = buildUpstreamRequest(clientReq, upstreamFormat, providerDef, cred, transformedBody, opts.config.identity, opts.config.plan, captchaHeaders, undefined);
171198
if (debug) console.log(`[responses] → POST ${upstreamReq.url}`);
172199

173-
let upstreamResp: Response;
174-
try {
175-
const routing = opts.endpointRouting !== undefined ? opts.endpointRouting : getDefaultEndpointRouting(opts.config);
200+
const routing = opts.endpointRouting !== undefined ? opts.endpointRouting : getDefaultEndpointRouting(opts.config);
201+
const signer = opts.clientSigning !== undefined ? opts.clientSigning : getDefaultClientSigning(opts.config);
202+
const dispatch = async (pairs: UpstreamHeaderPair[]): Promise<Response> => {
176203
const routed = routing ? await routing.resolve(upstreamReq.url, credentialString(cred)) : null;
177204
const sendUrl = routed?.routed ? routed.url : upstreamReq.url;
178205
if (debug && routed?.routed) console.log(`[responses] endpoint routing: ${upstreamReq.url} -> ${sendUrl}`);
179-
const signer = opts.clientSigning !== undefined ? opts.clientSigning : getDefaultClientSigning(opts.config);
180206
// signing decisions run against the PRE-routing provider URL (mirrors the
181207
// client, whose signer wraps the routing transport)
182-
upstreamResp = await sendWithClientSigning(signer, {
208+
return sendWithClientSigning(signer, {
183209
url: upstreamReq.url,
184-
headerPairs: upstreamHeaders,
210+
headerPairs: pairs,
185211
credential: credentialString(cred),
186212
appVersion: opts.config.identity.appVersion,
187213
debug: debug ? (message) => console.log(`[responses] ${message}`) : undefined,
@@ -194,10 +220,44 @@ export async function handleResponses(
194220
return fetchImpl(req, { method: "POST", headers: Object.fromEntries(finalPairs), body: transformedBody ?? undefined, signal: clientReq.signal });
195221
},
196222
});
223+
};
224+
225+
let upstreamResp: Response;
226+
try {
227+
upstreamResp = await dispatch(upstreamHeaders);
197228
} catch (err) {
198229
return errorResponse(502, "upstream_unreachable", (err as Error).message);
199230
}
200231

232+
// Captcha challenge retry (mirrors handler.ts): the gateway signals it either
233+
// through the captcha response header or as HTTP 400 with {"code":3007} in
234+
// the body. The challenged token is already spent, so retry once with a fresh
235+
// pooled one.
236+
if (startPlan && !upstreamResp.ok) {
237+
const captcha = opts.captcha ?? (await loadCaptcha());
238+
let challenged = captcha.detectCaptchaChallenge(upstreamResp) !== null;
239+
if (!challenged && !upstreamResp.headers.get("content-type")?.includes("text/event-stream")) {
240+
const peek = await upstreamResp.clone().text().catch(() => "");
241+
challenged = peek.includes('"code":3007') || peek.includes('"code": 3007');
242+
}
243+
if (challenged) {
244+
if (debug) console.log("[responses] captcha challenge — re-solving and retrying once");
245+
try { await upstreamResp.body?.cancel(); } catch { /* already drained */ }
246+
try {
247+
const fresh = await captcha.getCaptchaToken(opts.config.identity.appVersion);
248+
const retryHeaders = {
249+
[captcha.RETRY_HEADERS.PARAM]: fresh.verifyParam,
250+
[captcha.RETRY_HEADERS.REGION]: fresh.region,
251+
};
252+
upstreamResp = await dispatch(
253+
buildUpstreamHeaderPairs(clientReq, upstreamFormat, cred, opts.config.identity, opts.config.plan, retryHeaders, undefined),
254+
);
255+
} catch (err) {
256+
return errorResponse(503, "captcha_solver_failed", (err as Error).message);
257+
}
258+
}
259+
}
260+
201261
if (!upstreamResp.ok) {
202262
const errText = await upstreamResp.text().catch(() => "");
203263
return errorResponse(upstreamResp.status, "upstream_error", errText.slice(0, 500) || `upstream returned ${upstreamResp.status}`);

0 commit comments

Comments
 (0)