Files
yetone 1a560ba618 qoder 0.2.41: a chat carries the headers Qoder's client puts on a chat, not the machine id three times (#443)
unifilitence (plugins#443, Windows 11 x64, qoder.com) captured a chat
going out with the account's machine UUID as Cosy-ClientIp,
Cosy-MachineId and Cosy-MachineToken alike, Cosy-MachineType 5 and
Cosy-MachineOS x86_64_win32, and asked for each to be what Qoder's own
client sends.

Checked against Qoder.app 0.4.3: its worker runtime
(qoder-agent-sdk/dist/_worker) and the auth wasm it embeds, run here
with its own inputs:

- Cosy-MachineOS is `${x64->x86_64, arm64->aarch64, else arch}_${platform}`,
  so x86_64_win32 is Qoder's own value. Only ia32 differed: the plugin
  sent x86_win32, Qoder sends ia32_win32. Fixed.
- Its non-chat calls (prepareRequest) do send ClientIp, MachineId and
  MachineToken all as the machine id, MachineType 5. The listing, usage
  and queue status keep that set.
- A chat (prepareInferRequest, then its header pass) differs: no
  Cosy-ClientIp; MachineToken and MachineType are the native device
  identity's (umid runtime-info) or absent when there is none; and a
  Cosy-MachineHostname, os.hostname() made safe as its zei does
  (punycode, else "-" runs plus a sha256 mark, 96 chars at most). A chat
  now goes so. The identity is one the check-in already read in this
  process (renewed behind the chat after an hour); a chat never runs the
  helper itself, so with no check-in yet it goes without, as Qoder does
  when it has no identity.
- The signed payload's requestId is a dashed UUID in Qoder's signer; the
  plugin sent it undashed. ideVersion "" is Qoder's own.

Left as it was: Accept-Encoding identity on a chat (Qoder sets none;
its fetch's default differs from Bun's anyway).

Run: packages/qoder `bun test` (276 pass, new headers.test.mjs and one
check-in test); `bun scripts/check.mjs` all green. Real qoder.com
account from bun in a sandbox HOME (token not renewed, no auth.set):
qfmodel answered "pong" with the new chat headers, dfmodel queued
(10605, as before), the model list 200.
Broken by hand: index.mjs at HEAD with the new tests: 6 of 7 in
headers.test.mjs and the check-in chat test red.
Not tried: Windows (the hostname and OS values are unit-tested for
it); a chat carrying a real device identity against the live service.
2026-10-28 00:01:31 +08:00

114 lines
6.2 KiB
JavaScript

// The machine headers a Qoder call carries, against what Qoder's own client
// sends (Qoder.app 0.4.3: its worker runtime and the signer it embeds).
// unifilitence's capture (plugins#443) is the fixture: a Windows x64 chat
// went out with the account's machine UUID as Cosy-ClientIp, -MachineId
// and -MachineToken alike, Cosy-MachineType 5 and Cosy-MachineOS
// x86_64_win32. Qoder's client sends that set on its other calls (its
// prepareRequest), but a chat (prepareInferRequest) carries no ClientIp,
// its device identity's MachineToken and MachineType or none, and the
// machine's hostname. x86_64_win32 is Qoder's own name for the machine.
import { test, expect } from "bun:test"
import { createHash } from "node:crypto"
import { hostname } from "node:os"
import { QoderAuthPlugin, _internal } from "./index.mjs"
const { chatHeaders, cosyHeaders, machineOS, machineHostname } = _internal
const MACHINE = "01a1450c-5eb1-7555-ae94-000000000000" // the capture's, redacted part made up
const cred = { type: "oauth", uid: "u1", machineId: MACHINE, access: "a" }
const URL_CHAT = "https://api3.qoder.sh/algo/api/v2/service/pro/sse/agent_chat_generation?FetchKeys=llm_model_result&AgentId=agent_common&Encode=1"
const model = { key: "auto", source: "system" }
const payloadOf = (h) => JSON.parse(Buffer.from(h.Authorization.split(".")[1], "base64").toString())
test("Cosy-MachineOS is Qoder's own: x86_64_win32 on Windows x64, aarch64_darwin on a Mac", () => {
expect(machineOS("x64", "win32")).toBe("x86_64_win32")
expect(machineOS("arm64", "darwin")).toBe("aarch64_darwin")
expect(machineOS("x64", "linux")).toBe("x86_64_linux")
// Qoder renames only x64 and arm64
expect(machineOS("ia32", "win32")).toBe("ia32_win32")
})
test("a chat carries no Cosy-ClientIp, and no machine token or type without a device identity", () => {
const h = chatHeaders(URL_CHAT, cred, "body", undefined, model)
expect(h["Cosy-ClientIp"]).toBeUndefined()
expect(h["Cosy-MachineToken"]).toBeUndefined()
expect(h["Cosy-MachineType"]).toBeUndefined()
expect(h["Cosy-MachineId"]).toBe(MACHINE)
expect(h["Cosy-MachineOS"]).toBe(machineOS())
expect(h.Accept).toBe("text/event-stream")
expect(h["Cache-Control"]).toBe("no-cache")
expect(h["X-Model-Key"]).toBe("auto")
expect(h["X-Model-Source"]).toBe("system")
expect(h["Cosy-MachineHostname"]).toBe(machineHostname(hostname()))
})
test("a chat carries the device identity's machine token and type when one is known", () => {
const h = chatHeaders(URL_CHAT, cred, "body", undefined, model, { "Cosy-MachineToken": "sdk-token-u1", "Cosy-MachineType": "7" })
expect(h["Cosy-MachineToken"]).toBe("sdk-token-u1")
expect(h["Cosy-MachineType"]).toBe("7")
expect(h["Cosy-ClientIp"]).toBeUndefined()
})
test("every other call keeps Qoder's prepareRequest set: ClientIp, MachineId and MachineToken the machine id", () => {
const h = cosyHeaders("https://api3.qoder.sh/algo/api/v2/model/list?Encode=1", cred, "", undefined)
expect(h["Cosy-ClientIp"]).toBe(MACHINE)
expect(h["Cosy-MachineId"]).toBe(MACHINE)
expect(h["Cosy-MachineToken"]).toBe(MACHINE)
expect(h["Cosy-MachineType"]).toBe("5")
expect(h["Accept-Encoding"]).toBe("identity")
})
test("the signed payload's request id is a dashed UUID and its ideVersion empty, as Qoder's signer writes them", () => {
const p = payloadOf(cosyHeaders(URL_CHAT, cred, "body", undefined))
expect(p.requestId).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/)
expect(p.ideVersion).toBe("")
expect(p.version).toBe("v1")
})
test("Cosy-MachineHostname is the hostname as Qoder's client makes it safe", () => {
const h8 = (v) => createHash("sha256").update(v, "utf8").digest("hex").slice(0, 8)
expect(machineHostname("DESKTOP-ABC123")).toBe("DESKTOP-ABC123")
expect(machineHostname(" my-mac.local ")).toBe("my-mac.local")
expect(machineHostname("")).toBe("")
// a non-ASCII name goes as its punycode
expect(machineHostname("büro")).toBe("xn--bro-hoa")
expect(machineHostname("张三的电脑")).toBe("xn--ehq892buuqjgb581a")
// one that has none is made safe and marked with its hash
expect(machineHostname("PC 张三")).toBe(`PC-${h8("PC 张三")}`)
// at most 96 characters
const long = "a".repeat(120)
expect(machineHostname(long)).toBe(`${"a".repeat(87)}-${h8(long)}`)
})
test("a chat on the wire carries the chat set, not the machine UUID three times (#443)", async () => {
const line = (body) => `data:${JSON.stringify({ headers: {}, body: JSON.stringify(body), statusCodeValue: 200 })}\n\n`
const seen = []
const saved = globalThis.fetch
globalThis.fetch = async (input, init = {}) => {
const url = String(input?.url ?? input)
seen.push({ url, headers: Object.fromEntries(new Headers(init.headers)) })
if (new URL(url).pathname.endsWith("/model/list")) return Response.json({ chat: [{ key: "dfmodel", display_name: "DeepSeek Flash", enable: true }] })
return new Response(line({ choices: [{ delta: { content: "ok" } }] }) + line({ choices: [{ delta: {}, finish_reason: "stop" }] }), { headers: { "Content-Type": "text/event-stream" } })
}
try {
const auth = { type: "oauth", access: "jt", refresh: "rt", expires: Date.now() + 3_600_000, accountId: "one@x", uid: "u1", machineId: MACHINE }
const hooks = await QoderAuthPlugin({ client: { auth: { set: async () => {} } } })
const l = await hooks.auth.loader(async () => auth)
const res = await l.fetch("https://api3.qoder.sh/v1/chat/completions", { method: "POST", body: JSON.stringify({ model: "dfmodel", messages: [{ role: "user", content: "hi" }] }) })
expect(res.status).toBe(200)
await res.text()
const chat = seen.find((r) => r.url.includes("/agent_chat_generation"))
expect(chat).toBeDefined()
expect(chat.headers["cosy-clientip"]).toBeUndefined()
expect(chat.headers["cosy-machinetoken"]).toBeUndefined()
expect(chat.headers["cosy-machinetype"]).toBeUndefined()
expect(chat.headers["cosy-machineid"]).toBe(MACHINE)
expect(chat.headers.accept).toBe("text/event-stream")
// the listing is not a chat: it keeps the prepareRequest set
const list = seen.find((r) => r.url.includes("/model/list"))
expect(list.headers["cosy-clientip"]).toBe(MACHINE)
expect(list.headers["cosy-machinetoken"]).toBe(MACHINE)
} finally {
globalThis.fetch = saved
}
})