From 744e1e26f33229b975a77b43e2b08c633cc05af0 Mon Sep 17 00:00:00 2001 From: yetone Date: Thu, 29 Oct 2026 01:46:07 +0800 Subject: [PATCH] autoclaw 0.2.12: the overseas chat sends the official 2.0.3 fingerprint, and X-Tm "linux" on Linux as the official client does (#367) menhouran captured the official AutoClaw 2.0.3 Linux chat request and corrected the report behind 0.2.7: the official client sends X-Tm "linux", not "linux-x64". The overseas gateway refuses "linux" only beside a non-official fingerprint (X-Channel AutoClaw4, X-Version 1.18.1, no X-Auth-*); "linux-x64" got through only because it isn't the one value checked. Read from the official client itself: AutoClaw-2.0.3-x64.AppImage from autoclaw.z.ai's Linux download (sha512 matches latest-linux.yml), resources/app/dist/main.cjs, extracted with 7zz and never run. createZworkManagedRequestHeaders and createZworkManagedModelRequestResolvers build the chat headers: - X-Tm from managedPlatform(): darwin mac, win32 win, else process.platform as is (Linux is "linux" on x64 and arm64); - X-Auth-Sign = md5("100003&&"), the same identity and signature the plugin already sends on its sign-in/wallet paths; - X-Channel official, X-Version app.getVersion() (2.0.3), X-Lang the product language (en in the reporter's capture), X-Product autoclaw, X-Client-Type pc, x_trace_id autoclaw-desktop; - X-Request-Id = X-Trace-Id = a randomUUID per attempt; X-Session-Id the session's UUID; X-Authorization Bearer, no Authorization; - the request goes through the OpenAI SDK 6.26.0 in Electron 42.6.1 (Node v24.18.0), which adds User-Agent "OpenAI/JS 6.26.0" and X-Stainless-* (OS/Arch through the SDK's own normalizers). The overseas chat now sends that set. X-Session-Id is a UUID derived from the conversation's first non-system message, so it stays the same across a conversation's turns as the official one does. The caller's own X-Stainless-* headers are dropped. bizHeaders (sign-in, refresh, wallet, check-in) keep AutoClaw4/1.18.1 and send X-Tm "linux" again, as before 0.2.7, when they worked on Linux. README says what is sent and that linux-x64 was a bypass. Not changed: the overseas chat path keeps /v1/chat/completions, which the reporter verified; the official client posts to .../autoclaw/chat/completions. X-Stainless-Timeout is 600, the SDK default when no per-request timeout is passed; whether zwork passes one wasn't traced to the end. Run: HOME=$(mktemp -d) bun test in packages/autoclaw on mote, 46 pass; bun scripts/check.mjs autoclaw ok; the same suite on Linux (wsl-run, magpie-desktop) 46 pass. The new tests on origin/main's index.mjs: 4 fail (the three #367 tests and the UA check in the 406 test). Not tried: a real overseas account (none here), on Linux or anywhere; the reporter is asked to confirm on #367. --- packages/autoclaw/README.md | 2 + packages/autoclaw/index.mjs | 115 ++++++++++++++++++++++++++----- packages/autoclaw/index.test.mjs | 87 +++++++++++++++++++---- packages/autoclaw/package.json | 2 +- 4 files changed, 174 insertions(+), 32 deletions(-) diff --git a/packages/autoclaw/README.md b/packages/autoclaw/README.md index 0f03d3e..6228a6f 100644 --- a/packages/autoclaw/README.md +++ b/packages/autoclaw/README.md @@ -94,6 +94,8 @@ AutoClaw 对话网关对请求形状有硬性要求,缺任一条都回 `406 No - **不发送 X-Harness-Type**:上游收到 `X-Harness-Type` 即报 406,已删除该残留请求头 - **改写外部 Harness 身份声明**:上游拦截 system 中包含的外部 agent 身份句(如 `You are Codex`、`You are Claude Code` 等),插件的 `unflag` 会自动将其替换为兼容措辞,避免 406 拦截 +- **官方客户端指纹(国际版对话)**:国际版对话请求带官方 AutoClaw 2.0.3 桌面端(`official` 渠道)的整套请求头,取自其 AppImage 的 `dist/main.cjs`:`X-Channel: official`、`X-Version: 2.0.3`、`X-Lang: en`、`X-Client-Type: pc`、`X-Product: autoclaw`、`x_trace_id: autoclaw-desktop`、`X-Auth-Appid/X-Auth-TimeStamp/X-Auth-Sign`(`md5("100003&<秒级时间戳>&")`,与登录/积分接口同一签名)、`X-Request-Id` = `X-Trace-Id`(每次请求一个 UUID)、`X-Session-Id`(同一对话不变的 UUID,插件由对话首条消息导出)、OpenAI SDK 6.26.0 的 `User-Agent` 与 `X-Stainless-*`。`X-Tm` 与官方 `managedPlatform()` 一致:macOS `mac`、Windows `win`、Linux `linux`(x64/arm64 相同)。网关只在**非官方指纹**旁拒收 `X-Tm: linux`(403 `forbidden`,#367);0.2.7–0.2.11 发的 `linux-x64` 只是绕过,不是官方值。 + 国内版 `autoclaw-cn` 走 AutoClaw 2.1.0 managed gateway(`autoglm-acceleration-api.zhipuai.cn`);国际版 `autoclaw` 走海外直连网关(`autoglm-api.autoglm.ai`)。两者均已通过真实调用与推理验证。 ## 已知行为 diff --git a/packages/autoclaw/index.mjs b/packages/autoclaw/index.mjs index 4c558a0..c31408f 100644 --- a/packages/autoclaw/index.mjs +++ b/packages/autoclaw/index.mjs @@ -17,7 +17,7 @@ * callback() 里用 code 调 {vendor}-oauth-login 换 token → {type:"success"} */ -import { createHash, randomBytes } from "node:crypto"; +import { createHash, randomBytes, randomUUID } from "node:crypto"; import { mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs"; import { dirname } from "node:path"; import { Buffer } from "node:buffer"; @@ -31,14 +31,28 @@ const CLIENT_VERSION = "1.18.1"; const CN_CHANNEL = "AutoClaw26"; const CN_CLIENT_VERSION = "2.1.0"; const PLATFORM = process.platform === "darwin" ? "mac" : process.platform === "win32" ? "win" : "linux"; -/** X-Tm: the official Linux client sends "linux-x64"; the overseas gateway - * answers the bare "linux" with 403 {"message":"forbidden"} (#367). */ +/** X-Tm as the official AutoClaw 2.0.3 client builds it (managedPlatform() + * in its main.cjs): darwin is "mac", win32 is "win", anything else is + * process.platform as is, so Linux is "linux" on x64 and arm64 alike. + * The overseas gateway refuses "linux" only beside a non-official + * fingerprint (#367), so the overseas chat sends the official one. */ function platformTm(platform = process.platform) { - return platform === "darwin" ? "mac" : platform === "win32" ? "win" : "linux-x64"; + return platform === "darwin" ? "mac" : platform === "win32" ? "win" : platform; } const PLATFORM_TM = platformTm(); const USER_AGENT = `AutoClaw/${CLIENT_VERSION} (${PLATFORM}-${process.arch})`; +// The overseas chat fingerprint of the official AutoClaw 2.0.3 desktop +// client (channel "official", read from its AppImage's dist/main.cjs: +// createZworkManagedRequestHeaders and createZworkManagedModelRequestResolvers). +// Its model requests go through the OpenAI SDK 6.26.0 inside Electron 42.6.1 +// (Node v24.18.0), which adds its own User-Agent and X-Stainless-* headers. +const OFFICIAL_CHANNEL = "official"; +const OFFICIAL_CLIENT_VERSION = "2.0.3"; +const OFFICIAL_LANG = "en"; +const OPENAI_SDK_VERSION = "6.26.0"; +const OFFICIAL_NODE_VERSION = "v24.18.0"; + const GLOBAL_HOST = "https://autoglm-api.autoglm.ai"; const CN_HOST = "https://autoglm-api.zhipuai.cn"; @@ -346,6 +360,74 @@ function bizHeaders(accessToken) { return h; } +/** The OpenAI SDK's names for an OS and a CPU (its normalizePlatform/normalizeArch). */ +function stainlessOs(platform = process.platform) { + const p = String(platform).toLowerCase(); + if (p.includes("ios")) return "iOS"; + if (p === "android") return "Android"; + if (p === "darwin") return "MacOS"; + if (p === "win32") return "Windows"; + if (p === "freebsd") return "FreeBSD"; + if (p === "openbsd") return "OpenBSD"; + if (p === "linux") return "Linux"; + return p ? `Other:${p}` : "Unknown"; +} +function stainlessArch(arch = process.arch) { + if (arch === "x32") return "x32"; + if (arch === "x86_64" || arch === "x64") return "x64"; + if (arch === "arm") return "arm"; + if (arch === "aarch64" || arch === "arm64") return "arm64"; + return arch ? `other:${arch}` : "unknown"; +} + +/** UUID-shaped session id for one conversation: the official client sends its + * session's UUID as X-Session-Id, the same on every turn of that session. The + * plugin sees no session, so it derives one from the conversation's first + * message (stable across turns); with none, a fresh UUID. */ +function conversationSessionId(payload) { + const messages = Array.isArray(payload?.messages) ? payload.messages : []; + const first = messages.find((m) => m && m.role !== "system"); + if (!first) return randomUUID(); + const h = createHash("sha256").update(JSON.stringify([first.role, first.content ?? null])).digest("hex"); + const variant = ((parseInt(h[16], 16) & 0x3) | 0x8).toString(16); + return `${h.slice(0, 8)}-${h.slice(8, 12)}-4${h.slice(13, 16)}-${variant}${h.slice(17, 20)}-${h.slice(20, 32)}`; +} + +/** The official client's overseas chat headers (see OFFICIAL_CHANNEL), set on `headers`. */ +function setOfficialChatHeaders(headers, { accessToken, model, sessionId, now = Date.now, platform = process.platform, arch = process.arch }) { + const ts = String(Math.floor(now() / 1000)); + const rid = randomUUID(); + headers.set("Accept", "application/json"); + headers.set("User-Agent", `OpenAI/JS ${OPENAI_SDK_VERSION}`); + headers.set("X-Stainless-Retry-Count", "0"); + headers.set("X-Stainless-Timeout", "600"); + headers.set("X-Stainless-Lang", "js"); + headers.set("X-Stainless-Package-Version", OPENAI_SDK_VERSION); + headers.set("X-Stainless-OS", stainlessOs(platform)); + headers.set("X-Stainless-Arch", stainlessArch(arch)); + headers.set("X-Stainless-Runtime", "node"); + headers.set("X-Stainless-Runtime-Version", OFFICIAL_NODE_VERSION); + headers.set("X-Version", OFFICIAL_CLIENT_VERSION); + headers.set("X-Tm", platformTm(platform)); + headers.set("X-Product", PRODUCT); + headers.set("X-Auth-Appid", APP_ID); + headers.set("X-Auth-TimeStamp", ts); + headers.set("X-Auth-Sign", md5Hex(`${APP_ID}&${ts}&${APP_KEY}`)); + headers.set("X-Lang", OFFICIAL_LANG); + headers.set("X-Channel", OFFICIAL_CHANNEL); + headers.set("X-Trace-Id", rid); + headers.set("X-Request-Id", rid); + if (model) headers.set("X-Request-Model", model); + else headers.delete("X-Request-Model"); + headers.set("X-Client-Type", "pc"); + headers.set("x_trace_id", "autoclaw-desktop"); + headers.set("X-Session-Id", sessionId); + headers.delete("Authorization"); + headers.delete("X-Harness-Type"); + headers.set("X-Authorization", `Bearer ${accessToken}`); + return headers; +} + function nonEmptyString(v) { return typeof v === "string" && v.length > 0 ? v : undefined; } @@ -835,32 +917,27 @@ function makeAutoclawPlugin({ providerId, providerName, homeHost, loginMethod }) const headers = new Headers(init?.headers); // magpie's own hint headers (x-magpie-*) are for the plugin, never the vendor for (const k of [...headers.keys()]) if (k.toLowerCase().startsWith("x-magpie-")) headers.delete(k); - headers.set("User-Agent", USER_AGENT); - headers.set("X-Authorization", `Bearer ${accessToken}`); - headers.set("X-Client-Type", "pc"); - headers.set("X-Product", PRODUCT); - headers.delete("X-Harness-Type"); - headers.set("X-Tm", PLATFORM_TM); - headers.set("X-Version", CLIENT_VERSION); - headers.set("X-Lang", "zh-CN"); - headers.set("X-Channel", CHANNEL); - headers.set("x_trace_id", "autoclaw-desktop"); - headers.delete("Authorization"); + // the caller's own SDK headers are not the official client's + for (const k of [...headers.keys()]) if (k.toLowerCase().startsWith("x-stainless-")) headers.delete(k); let routeModel = ""; let nextBody = init?.body; + let sent = null; const rewritten = autoclawRewriteBody(init?.body); if (rewritten) { + sent = rewritten; routeModel = String(rewritten.model ?? ""); nextBody = JSON.stringify(rewritten); headers.delete("content-length"); } else { try { - routeModel = String(JSON.parse(String(init?.body ?? "{}")).model ?? ""); + sent = JSON.parse(String(init?.body ?? "{}")); + routeModel = String(sent?.model ?? ""); } catch {} } - if (routeModel) headers.set("X-Request-Model", routeModel); - headers.set("X-Request-Id", randomHex(16)); + // the official 2.0.3 fingerprint: with it the gateway takes X-Tm + // "linux"; with the old AutoClaw4/1.18.1 one it refuses it (#367) + setOfficialChatHeaders(headers, { accessToken, model: routeModel, sessionId: conversationSessionId(sent) }); const tryFetch = (host) => fetch(`${host}${CHAT_PATH}`, { ...init, headers, body: nextBody, signal: init?.signal }); @@ -1507,7 +1584,7 @@ export const AutoclawCnAuthPlugin = makeAutoclawPlugin({ export const _internal = { quotaRefusal, accountRefusal, bannedSaid, makePluginForTest: makeAutoclawPlugin, - platformTm, md5Hex, jwtClaim, tokenFields, codeValue, bizHeaders, bareToken, newDeviceId, + platformTm, setOfficialChatHeaders, conversationSessionId, stainlessOs, stainlessArch, md5Hex, jwtClaim, tokenFields, codeValue, bizHeaders, bareToken, newDeviceId, dailySignin, signinStatus, maybeSignin, signinAccountKey, signinConfig, checkinOutcome, refreshTokens, balanceObserve, usageStatePath: () => usageStatePath ?? usageStatePathDefault(), setUsageStatePath, beijingDayOf, fetchUserProfile, accountDeviceId, accountDisplayName, startLocalOAuth, diff --git a/packages/autoclaw/index.test.mjs b/packages/autoclaw/index.test.mjs index ea25bf7..3045d79 100644 --- a/packages/autoclaw/index.test.mjs +++ b/packages/autoclaw/index.test.mjs @@ -302,7 +302,7 @@ test("overseas loader().fetch drops X-Harness-Type and explains 406", async () = expect(sent.body.messages[0].content.startsWith(OFFICIAL_SYSTEM_PROMPT)).toBe(true) expect(sent.body.messages[0].content).not.toMatch(/DeepSeek Harness/) expect(sent.headers.get("X-Harness-Type")).toBe(null) - expect(sent.headers.get("User-Agent")).toMatch(/^AutoClaw\//) + expect(sent.headers.get("User-Agent")).toBe("OpenAI/JS 6.26.0") status = 406 const refused = await loader.fetch("x", { method: "POST", body }) @@ -678,30 +678,93 @@ test("usage: the credit balance and its wallets are the balance, never a window } }) -// #367 (menhouran): the overseas gateway answers X-Tm "linux" with -// 403 {"message":"forbidden"} for a valid token; the official Linux client -// sends "linux-x64", which it takes -test("X-Tm is the official client's platform id: mac, win, linux-x64, never the bare linux", async () => { +// #367 (menhouran): the overseas gateway refuses X-Tm "linux" only beside a +// non-official fingerprint. The official AutoClaw 2.0.3 client sends "linux" +// (managedPlatform() in its main.cjs: darwin mac, win32 win, else +// process.platform) with channel "official", version 2.0.3 and a signed +// X-Auth-*; 0.2.7's "linux-x64" was a bypass, not what it sends. +test("X-Tm is the official client's platform id: mac, win, and linux as is", () => { const { platformTm, bizHeaders } = _internal expect(platformTm("darwin")).toBe("mac") expect(platformTm("win32")).toBe("win") - expect(platformTm("linux")).toBe("linux-x64") + expect(platformTm("linux")).toBe("linux") expect(bizHeaders("t")["X-Tm"]).toBe(platformTm(process.platform)) - expect(bizHeaders("t")["User-Agent"]).not.toMatch(/x64-x64|linux-x64-/) +}) + +test("the overseas chat carries the official AutoClaw 2.0.3 fingerprint", async () => { + const { platformTm, md5Hex, stainlessOs, stainlessArch } = _internal const realFetch = globalThis.fetch - let sent - globalThis.fetch = async (input, init) => { sent = new Headers(init?.headers); return new Response("{}") } + const sent = [] + globalThis.fetch = async (input, init) => { sent.push({ url: String(input), headers: new Headers(init?.headers) }); return new Response("{}") } + const body = (text) => JSON.stringify({ model: "zai_glm-5.3-flash", messages: [{ role: "system", content: "sys" }, { role: "user", content: text }] }) try { const hooks = await AutoclawAuthPlugin({ client: { auth: { set: async () => {} } } }) const loader = await hooks.auth.loader(async () => ({ type: "oauth", access: "a1" })) - await loader.fetch("x", { method: "POST", body: JSON.stringify({ model: "zai_glm-5.3-flash", messages: [{ role: "user", content: "hi" }] }) }) - expect(sent.get("X-Tm")).toBe(platformTm(process.platform)) - expect(sent.get("X-Tm")).not.toBe("linux") + const before = Math.floor(Date.now() / 1000) + await loader.fetch("x", { + method: "POST", + headers: { "Authorization": "Bearer caller", "User-Agent": "ai-sdk/openai-compatible", "X-Stainless-Lang": "python", "X-Harness-Type": "x" }, + body: body("hi"), + }) + const after = Math.floor(Date.now() / 1000) + const h = sent[0].headers + expect(sent[0].url).toBe("https://autoglm-api.autoglm.ai/autoclaw-proxy/proxy/autoclaw/v1/chat/completions") + expect(h.get("X-Channel")).toBe("official") + expect(h.get("X-Version")).toBe("2.0.3") + expect(h.get("X-Tm")).toBe(platformTm(process.platform)) + expect(h.get("X-Product")).toBe("autoclaw") + expect(h.get("X-Lang")).toBe("en") + expect(h.get("X-Client-Type")).toBe("pc") + expect(h.get("x_trace_id")).toBe("autoclaw-desktop") + expect(h.get("X-Auth-Appid")).toBe("100003") + const ts = Number(h.get("X-Auth-TimeStamp")) + expect(ts).toBeGreaterThanOrEqual(before) + expect(ts).toBeLessThanOrEqual(after) + expect(h.get("X-Auth-Sign")).toBe(md5Hex(`100003&${h.get("X-Auth-TimeStamp")}&38d2391985e2369a5fb8227d8e6cd5e5`)) + const uuid = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/ + expect(h.get("X-Request-Id")).toMatch(uuid) + expect(h.get("X-Trace-Id")).toBe(h.get("X-Request-Id")) + expect(h.get("X-Session-Id")).toMatch(uuid) + expect(h.get("X-Request-Model")).toBe("zai_glm-5.3-flash") + expect(h.get("X-Authorization")).toBe("Bearer a1") + expect(h.get("Authorization")).toBeNull() + expect(h.get("X-Harness-Type")).toBeNull() + expect(h.get("User-Agent")).toBe("OpenAI/JS 6.26.0") + expect(h.get("X-Stainless-Lang")).toBe("js") + expect(h.get("X-Stainless-Package-Version")).toBe("6.26.0") + expect(h.get("X-Stainless-OS")).toBe(stainlessOs(process.platform)) + expect(h.get("X-Stainless-Arch")).toBe(stainlessArch(process.arch)) + expect(h.get("X-Stainless-Runtime")).toBe("node") + expect(h.get("X-Stainless-Runtime-Version")).toBe("v24.18.0") + + // one conversation keeps its session id across turns, and a new one gets another + await loader.fetch("x", { method: "POST", body: JSON.stringify({ model: "zai_glm-5.3-flash", messages: [{ role: "system", content: "sys" }, { role: "user", content: "hi" }, { role: "assistant", content: "yo" }, { role: "user", content: "more" }] }) }) + await loader.fetch("x", { method: "POST", body: body("another chat") }) + expect(sent[1].headers.get("X-Session-Id")).toBe(h.get("X-Session-Id")) + expect(sent[2].headers.get("X-Session-Id")).not.toBe(h.get("X-Session-Id")) + expect(sent[1].headers.get("X-Request-Id")).not.toBe(h.get("X-Request-Id")) } finally { globalThis.fetch = realFetch } }) +test("on Linux the official fingerprint says linux, x64 and arm64 alike", () => { + const { setOfficialChatHeaders } = _internal + for (const arch of ["x64", "arm64"]) { + const h = setOfficialChatHeaders(new Headers(), { accessToken: "a1", model: "m", sessionId: "s", platform: "linux", arch }) + expect(h.get("X-Tm")).toBe("linux") + expect(h.get("X-Stainless-OS")).toBe("Linux") + expect(h.get("X-Stainless-Arch")).toBe(arch) + expect(h.get("X-Channel")).toBe("official") + } + const mac = setOfficialChatHeaders(new Headers(), { accessToken: "a1", model: "m", sessionId: "s", platform: "darwin", arch: "arm64" }) + expect(mac.get("X-Tm")).toBe("mac") + expect(mac.get("X-Stainless-OS")).toBe("MacOS") + const win = setOfficialChatHeaders(new Headers(), { accessToken: "a1", model: "m", sessionId: "s", platform: "win32", arch: "x64" }) + expect(win.get("X-Tm")).toBe("win") + expect(win.get("X-Stainless-OS")).toBe("Windows") +}) + // plugins#274: the CN managed gateway answers a spent quota with a 403 whose // body says so (code 810000); the same token's next request gets as far as // the model check, so it is the quota, not the sign-in diff --git a/packages/autoclaw/package.json b/packages/autoclaw/package.json index 50ebe07..f1d668f 100644 --- a/packages/autoclaw/package.json +++ b/packages/autoclaw/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-autoclaw-auth", - "version": "0.2.11", + "version": "0.2.12", "description": "OpenCode provider plugin for AutoClaw (Zhipu OpenClaw): oversea web sign-in (Google/z.ai) and CN phone SMS, multi-account pool, credit balance and daily check-in", "type": "module", "main": "./index.mjs",