diff --git a/packages/trae/README.md b/packages/trae/README.md index a26af4f..f6ba0c6 100644 --- a/packages/trae/README.md +++ b/packages/trae/README.md @@ -42,9 +42,11 @@ An account of one realm does not sign in to the other: trae.cn's accounts are not trae.ai's. At sign-in the plugin creates the account's device and names it to the -authorization page: `device_id` (19 digits) and `machine_id` (32 hex). Every +authorization page: `device_id` and `machine_id` (32 hex). Every request then sends that same device. The relays saw requests dropped when the -device was new each time. +device was new each time. A Trae CN `device_id` is 16 digits since 0.2.16, the +shape of the ids Trae's device service gives its clients (15 or 16 digits); +Trae Global's stays 19. ### Enterprise (SaaS) accounts @@ -314,11 +316,20 @@ answered it 9074 even with the client's headers. macOS `~/Library/Application Support/Trae CN/…`, Windows `%APPDATA%\Trae CN\…` (or `trae-cn`), Linux `~/.config/trae-cn/…` (or `Trae CN`; not seen on a real Linux install). +- **Every other account's device (0.2.16):** its own device id, in the + client's shape. Trae CN answered a claim on the plugin's 19-digit id + 9074, while the same account and token on a 16-digit id were taken + (magpie-community/plugins#194). An account signed in since 0.2.16 has a + 16-digit id and checks in on it. One signed in before checks in on a + 16-digit id made from its own device id and uid: the same id every day, + never rotated, and no other account's. Its chat requests and renewals + keep the 19-digit device it signed in with. - **What comes back:** Trae's answer as it is. A 401 or code 1001 marks the account for a new sign-in. Code 9095 means this device has checked - in today. No device id is ever made up, changed or rotated to get round - a 9095 or a 9074, and no account is given a device that isn't its - own client's. + in today, and that holds for the day. No device id is ever changed or + rotated to get round a 9095 or a 9074, and no account is given another + account's device or its own client's device unless the client is signed + in as that account. Only `api.trae.cn`'s `/trae/api/` pages are sent this way; any other URL that isn't a chat request is still refused (400). Needs 0.1.4 or later. diff --git a/packages/trae/checkin.test.mjs b/packages/trae/checkin.test.mjs index 4357c27..4bdec94 100644 --- a/packages/trae/checkin.test.mjs +++ b/packages/trae/checkin.test.mjs @@ -3,7 +3,7 @@ // through the account's fetch, which sends them as the account. import "./nonet.mjs" import { afterEach, expect, test } from "bun:test" -import { TraeCNAuthPlugin } from "./index.mjs" +import { TraeCNAuthPlugin, _internal } from "./index.mjs" import { fakeTrae, json, signedIn } from "./fake.mjs" let f @@ -32,7 +32,9 @@ test("a check-in page is sent as the account, its answer back as it is", async ( // device headers and a client's User-Agent, not Bun's expect(r.json).toEqual({ req_source: 1 }) expect(r.headers.get("authorization")).toBe("Cloud-IDE-JWT jwt-1") - expect(r.headers.get("x-device-id")).toBe("1234567890123456789") + // the account's own device in the client's 16-digit shape: this + // account signed in before 0.2.16 with 19 digits (#194) + expect(r.headers.get("x-device-id")).toBe("2866487253502651") expect(r.headers.get("x-device-type")).toBe("Windows") expect(r.headers.get("x-os-version")).toBe("10.0.22631") expect(r.headers.get("x-app-version")).toBe("0.1.69") @@ -89,3 +91,55 @@ test("other hosts' pages and other paths aren't sent as a page", async () => { expect(r.status).toBe(400) // not a chat: refused, nothing sent expect(f.seen).toEqual([]) }) + +// #194: Trae CN answered a claim on the plugin's 19-digit device id 9074, +// and the same account and token on a 16-digit one were taken. The +// check-in goes on the account's own device in that shape; it is never +// rotated, and no two accounts share one. +test("an account signed in before 0.2.16 checks in on one 16-digit id made from its own device, every time", () => { + const a = signedIn() + const id = _internal.ugDevice(a) + expect(id).toMatch(/^[1-9]\d{15}$/) + // the same on every check-in, and not the 19 digits cut short + expect(_internal.ugDevice(signedIn())).toBe(id) + expect(id).not.toBe("1234567890123456") + // another account's, or another device's, is not this one + expect(_internal.ugDevice(signedIn({ uid: "u-2" }))).not.toBe(id) + expect(_internal.ugDevice(signedIn({ deviceId: "9876543210987654321" }))).not.toBe(id) + // an account with no uid still has its own + expect(_internal.ugDevice(signedIn({ uid: "" }))).toMatch(/^[1-9]\d{15}$/) +}) + +test("an id already in the client's shape is the account's own, as it is", () => { + expect(_internal.ugDevice(signedIn({ deviceId: "1234567890123456" }))).toBe("1234567890123456") + expect(_internal.ugDevice(signedIn({ deviceId: "123456789012345" }))).toBe("123456789012345") + expect(_internal.ugDevice(signedIn({ deviceId: "", uid: "" }))).toBe("") +}) + +test("a new Trae CN sign-in's device is 16 digits; Trae Global's stays 19", () => { + expect(_internal.newDevice(_internal.SITES["trae-cn"]).deviceId).toMatch(/^[1-9]\d{15}$/) + expect(_internal.newDevice(_internal.SITES["trae-global"]).deviceId).toMatch(/^[1-9]\d{18}$/) +}) + +test("an account with a 16-digit device checks in on it; an old one's chat pages keep the 19 digits it signed in with", async () => { + f = fakeTrae() + f.route("POST /trae/api/v2/ug/checkin_credits/claim", () => json({ code: 0 })) + f.route("POST /trae/api/v2/pay/ide_user_ent_usage", () => json({ code: 0 })) + const o16 = await fetcher(signedIn({ deviceId: "1234567890123456" })) + await o16.fetch(f.origin + "/trae/api/v2/ug/checkin_credits/claim", { method: "POST", body: "{}" }) + expect(f.seen.at(-1).headers.get("x-device-id")).toBe("1234567890123456") + const o19 = await fetcher() + await o19.fetch(f.origin + "/trae/api/v2/ug/checkin_credits/claim", { method: "POST", body: "{}" }) + expect(f.seen.at(-1).headers.get("x-device-id")).toBe("2866487253502651") + await o19.fetch(f.origin + "/trae/api/v2/pay/ide_user_ent_usage", { method: "POST", body: "{}" }) + expect(f.seen.at(-1).headers.get("x-device-id")).toBe("1234567890123456789") +}) + +test("a 9095 on the account's device is the day's answer: nothing is sent again on another id", async () => { + f = fakeTrae() + f.route("POST /trae/api/v2/ug/checkin_credits/claim", () => json({ code: 9095, message: "当前设备今日已经签到" })) + const o = await fetcher() + const r = await o.fetch(f.origin + "/trae/api/v2/ug/checkin_credits/claim", { method: "POST", body: "{}" }) + expect((await r.json()).code).toBe(9095) + expect(f.seen.length).toBe(1) +}) diff --git a/packages/trae/index.mjs b/packages/trae/index.mjs index 5703c19..f926515 100644 --- a/packages/trae/index.mjs +++ b/packages/trae/index.mjs @@ -296,8 +296,32 @@ const sleep = (ms) => new Promise((r) => setTimeout(r, ms)) // device is the machine an account signs in from, made once at sign-in and // named to trae.cn's authorization page, then sent with every request: a -// fresh one each time is what the relays saw requests dropped for. -const newDevice = () => ({ deviceId: digits(19), machineId: randomBytes(16).toString("hex"), ...newDeviceKey() }) +// fresh one each time is what the relays saw requests dropped for. Trae +// CN's id has the shape its clients' have, 16 digits: the device service +// gives the IDE a 15- or 16-digit one (iCubeAuthInfo://icube-dc:, see +// clientDevice), and Trae CN answers a check-in claim on a 19-digit id +// 9074 where the same account and token on a 16-digit one are taken +// (magpie-community/plugins#194). Trae's global site keeps the 19 digits +// it was signed in with: nothing says its ids are shaped otherwise. +const newDevice = (site) => ({ deviceId: digits(site?.id === "trae-cn" ? 16 : 19), machineId: randomBytes(16).toString("hex"), ...newDeviceKey() }) + +// ugDevice is the device id a Trae CN account's check-in goes on when this +// machine's client isn't signed in as it: its own when that already has a +// client's shape (15 or 16 digits), else, for an account signed in before +// 0.2.16 with 19 digits, a 16-digit id made from its own device id and +// uid. That one is the same on every check-in of the account, never +// rotated and never another account's: it is the account's own device in +// the shape Trae CN takes (#194), not a new device to get past a refusal, +// and a 9095 (this device is in for today) on it stays the day's answer. +// The chat's pages and the renewal keep the device the account signed in +// with. +function ugDevice(a) { + const id = String(a.deviceId ?? "") + if (/^\d{15,16}$/.test(id)) return id + if (!id && !a.uid) return id + const n = BigInt("0x" + createHash("sha256").update(`trae-cn-device\0${id}\0${a.uid ?? ""}`).digest("hex").slice(0, 30)) + return String(1_000_000_000_000_000n + (n % 9_000_000_000_000_000n)) +} // newDeviceKey is the device's key pair, an ECDSA P-256 one as the clients // keep (iCubeAuthInfo://icube-dc:), whose public half goes with a @@ -627,7 +651,7 @@ async function signedIn(q, device, site) { // browserSignIn is the IDE's sign-in: trae.cn's authorization page, back // to a callback on 127.0.0.1 (the page takes no other kind) async function browserSignIn(site) { - const device = newDevice() + const device = newDevice(site) const trace = randomUUID() let over = false let settle @@ -2491,6 +2515,9 @@ const makePlugin = (site) => async ({ client }) => { // other account keeps its own device; none is given the client's. const deviceId = ug && site.id === "trae-cn" ? await clientDevice(a.uid) : undefined const own = deviceId ? { deviceId, ...thisMachine() } : undefined + // every other Trae CN account checks in on its own device, in the + // client's shape (ugDevice, #194) + if (ug && site.id === "trae-cn" && !own) a = { ...a, deviceId: ugDevice(a) } const res = await fetch(url, { method, headers: ug ? ugHeaders(a, site, { Accept: "application/json" }, own) : ideHeaders(a, site, { Accept: "application/json" }), @@ -2861,4 +2888,4 @@ const makePlugin = (site) => async ({ client }) => { export const TraeCNAuthPlugin = makePlugin(SITES["trae-cn"]) export const TraeGlobalAuthPlugin = makePlugin(SITES["trae-global"]) -export const _internal = { ownClient, clientStoragePaths, clientDevice, clientDecode, thisMachine, traeHost, accountOf, HOSTS: SITES["trae-cn"].hosts, MODELS: SITES["trae-cn"].models, TextTools, NativeCalls, looseJSON, glmCall, nameCall, toolNamed, unwrapped, traeMessages, chatBody, credits, dollarUsageOf, whenOf, newDevice, proofOf, SITES, SESSION, conversationID } +export const _internal = { ownClient, clientStoragePaths, clientDevice, clientDecode, thisMachine, traeHost, accountOf, HOSTS: SITES["trae-cn"].hosts, MODELS: SITES["trae-cn"].models, TextTools, NativeCalls, looseJSON, glmCall, nameCall, toolNamed, unwrapped, traeMessages, chatBody, credits, dollarUsageOf, whenOf, newDevice, ugDevice, proofOf, SITES, SESSION, conversationID } diff --git a/packages/trae/ownclient.test.mjs b/packages/trae/ownclient.test.mjs index ea67e97..a1168c4 100644 --- a/packages/trae/ownclient.test.mjs +++ b/packages/trae/ownclient.test.mjs @@ -59,8 +59,10 @@ async function checkIn(auth = signedIn()) { return f.seen.at(-1).headers } -const plugins = (h) => { - expect(h.get("x-device-id")).toBe("1234567890123456789") +// the account's own device, in the client's shape (ugDevice, #194) +const plugins = (h, a = signedIn()) => { + expect(h.get("x-device-id")).toBe(_internal.ugDevice(a)) + expect(h.get("x-device-id")).toMatch(/^\d{16}$/) expect(h.get("x-device-brand")).toBeTruthy() expect(h.get("x-device-type")).toBe("Windows") expect(h.get("x-os-version")).toBe("10.0.22631") @@ -96,7 +98,7 @@ test("an account the client isn't signed in as keeps its own device", async () = test("an account with no uid keeps its own device", async () => { client("") - plugins(await checkIn(signedIn({ uid: "" }))) + plugins(await checkIn(signedIn({ uid: "" })), signedIn({ uid: "" })) }) test("a client with no single device names none", async () => { diff --git a/packages/trae/package.json b/packages/trae/package.json index d490998..506eca0 100644 --- a/packages/trae/package.json +++ b/packages/trae/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-trae-auth", - "version": "0.2.15", + "version": "0.2.16", "description": "OpenCode provider plugins for Trae: Trae CN (trae.cn) and Trae Global (trae.ai), free tier included, with Trae's own browser sign-in", "type": "module", "main": "./index.mjs", diff --git a/packages/trae/signin.test.mjs b/packages/trae/signin.test.mjs index 207c57a..f8cc077 100644 --- a/packages/trae/signin.test.mjs +++ b/packages/trae/signin.test.mjs @@ -20,7 +20,8 @@ async function start() { const q = u.searchParams expect(q.get("client_id")).toBe("ono9krqynydwx5") expect(q.get("login_channel")).toBe("native_ide") - expect(q.get("device_id")).toMatch(/^\d{19}$/) + // a Trae CN device in its clients' 16-digit shape (#194) + expect(q.get("device_id")).toMatch(/^[1-9]\d{15}$/) expect(q.get("x_device_id")).toBe(q.get("device_id")) expect(q.get("machine_id")).toMatch(/^[0-9a-f]{32}$/) const cb = new URL(q.get("auth_callback_url"))