On one account Trae CN's desktop client's claim went through while the plugin's, on a device id it had made up (digits(19)) with a hard-coded brand and OS, came back 9074 every day (MiFaZhan on yetone/magpie#808; aliceh1 on #1920, where Buddy Switch reads the local client). Now, for a check-in (/trae/api/v2/ug/*) only, the account whose uid matches the userId of this machine's Trae CN client sends the client's own device id, the one Trae's device service registered: the digits in the client's storage.json key iCubeAuthInfo://icube-dc:<id> (seen in real clients' files, #808 and #1705). x-device-type and x-os-version are this machine's (Windows' product name, else its build; elsewhere the kernel release), and no brand is sent, as the client's fb leaves it out with no model. The uid comes from the client's sign-in envelope (iCubeAuthInfo://icube.cloudide), plain JSON or the client's fixed AES-128-CBC codec, read as dingminhua/dsh-connect-trae (MIT) reads it; only userId is looked at, the client's token is never used. The file is only read, never written. Every other account keeps its own device; a missing or unreadable file, one that can't be made out, a failed integrity check, or zero or several icube-dc keys change nothing. No id is made up, changed or rotated (the owner's ruling on #1705 stands). Ran: bun test in packages/trae, 140 pass (11 new in ownclient.test.mjs); bun scripts/check.mjs trae ok. Broken by hand: matching every account instead of the client's uid fails "an account the client isn't signed in as keeps its own device"; not passing the client's device to ugHeaders fails the two matched-account tests. Tests never read this machine's client (fake.mjs points ownClient.paths at nothing). Not tried: a real Trae CN client or account (none here), so whether the claim now goes through for the matched account is unproven; the Linux paths are guesses.
61 lines
2.6 KiB
JavaScript
61 lines
2.6 KiB
JavaScript
// A fake of Trae for the tests: one local server standing in for the
|
|
// realm's authorization page host and its other hosts, which the plugin's
|
|
// HOSTS are pointed at. route(method, path) answers a request; seen lists
|
|
// what came. The CN realm is the default, so a test written before the two
|
|
// realms shared a package is unchanged; pass "trae-global" for the
|
|
// international one, whose hosts and usage page differ.
|
|
import { _internal } from "./index.mjs"
|
|
|
|
const HOST_KEYS = ["web", "auth", "api", "us", "pay", "usPay"]
|
|
|
|
// no test reads this machine's own Trae CN client: one that wants a
|
|
// client points ownClient.paths at a file of its own
|
|
_internal.ownClient.paths = () => []
|
|
|
|
export function fakeTrae(siteId = "trae-cn") {
|
|
const routes = new Map()
|
|
const seen = []
|
|
const server = Bun.serve({
|
|
port: 0,
|
|
hostname: "127.0.0.1",
|
|
async fetch(req) {
|
|
const url = new URL(req.url)
|
|
const text = await req.text()
|
|
const r = { method: req.method, path: url.pathname, query: url.searchParams, headers: req.headers, text }
|
|
if (text) try { r.json = JSON.parse(text) } catch {}
|
|
seen.push(r)
|
|
const h = routes.get(req.method + " " + url.pathname)
|
|
if (!h) return new Response("no route " + req.method + " " + url.pathname, { status: 404 })
|
|
return h(r)
|
|
},
|
|
})
|
|
const origin = `http://127.0.0.1:${server.port}`
|
|
const hosts = _internal.SITES[siteId].hosts
|
|
const was = { ...hosts }
|
|
Object.assign(hosts, { web: origin, auth: origin, api: origin, us: origin, pay: origin, usPay: origin })
|
|
return {
|
|
origin,
|
|
site: _internal.SITES[siteId],
|
|
seen,
|
|
route: (key, h) => routes.set(key, h),
|
|
close() {
|
|
server.stop(true)
|
|
for (const k of HOST_KEYS) if (was[k] === undefined) delete hosts[k]
|
|
Object.assign(hosts, was)
|
|
},
|
|
}
|
|
}
|
|
|
|
export const json = (v, status = 200) => new Response(JSON.stringify(v), { status, headers: { "content-type": "application/json" } })
|
|
|
|
// sse is an answer in Trae's events: [event, data] pairs
|
|
export const sse = (events, status = 200) =>
|
|
new Response(events.map(([e, d]) => `event: ${e}\ndata: ${typeof d === "string" ? d : JSON.stringify(d)}\n\n`).join(""), { status, headers: { "content-type": "text/event-stream" } })
|
|
|
|
export const jwtOf = (claims) => ["e30", Buffer.from(JSON.stringify(claims)).toString("base64url"), "sig"].join(".")
|
|
|
|
export const signedIn = (over = {}) => ({
|
|
type: "oauth", access: "jwt-1", refresh: "r-1", expires: Date.now() + 3600_000, accountId: "Ann", uid: "u-1",
|
|
clientId: "ono9krqynydwx5", deviceId: "1234567890123456789", machineId: "ab".repeat(16), ...over,
|
|
})
|