Files
plugins/packages/tabbit/src/cookie.mjs
T
yetone 50bb3f5ad2 tabbit 0.1.1: international accounts are served by web.tabbit.ai (#833)
Tabbit runs two sites with one API: web.tabbit.com (China) and
web.tabbit.ai (international). Each site refuses the other's tokens.
The plugin only knew .com, so every international account was refused
with 401 "invalid bearer". The reporter saw this as a lost sign-in.

The site is now picked by the account, with nothing to set:
- A desktop sign-in reads the token from whichever host's cookie has
  it, web.tabbit.com or web.tabbit.ai. The host's own hash prefix
  unseals it, and the site is recorded in the account's metadata.site.
- A pasted token, or an account from before this change, starts on
  .com. If .com refuses it, the same request is sent once to .ai.
  - If .ai accepts it, the token is remembered as .ai's for the process.
  - If both sites refuse it, it stays on .com and is reported as an
    expired sign-in, as before.
  - If .ai can't be reached, nothing is settled.
  - A 429 or 5xx settles nothing either.
- The catalog answers any token, so it settles nothing. The model list
  asks usage first to learn the site, so an international account gets
  .ai's catalog.
- Origin and Referer follow the site.

Not fixed here: .ai answers a chat run with 429 code 492 ("Welcome to
Tabbit Browser"), its browser sign-in gate. Sign-in, the model list,
usage and check-in work on .ai. Turns get the 429 passed through as it
is, not marked as a lost sign-in. The README says so.

Run on mote (Bun 1.4.2, Node 22, HOME=$(mktemp -d)):
- packages/tabbit: bun test 70 pass, 0 fail; node --test 70 pass, 0 fail.
- bun scripts/check.mjs tabbit: ok.
- Repo-wide bun test: 6797 pass, 20 skip, 0 fail (6817 tests, 492 files).

The new tests use the reporter's literal bodies from #833 (the 401
"invalid bearer" and the 492 gate JSON) against a stubbed fetch, so
nothing reaches the network.
- Against the old src (the new tests kept), 8 of the 9 site tests
  fail on their assertions, and so do both new cookie tests ("not
  signed in to web.tabbit.com"). The one that passes is the .com
  regression test, as it should.

Broken by hand, each change red in exactly the test meant for it:
- (a) no learning when both sites refuse: "a token both sites refuse".
- (b) an unreachable site settles .com: "web.tabbit.ai out of reach".
- (c) no usage probe before the catalog: "an international token's
  model list".
- (d) learning on any non-refused answer: "a 5xx or the catalog
  settles no site".

Not tried:
- a real web.tabbit.ai or web.tabbit.com account;
- a real Tabbit Browser cookie store holding a web.tabbit.ai row (its
  host_key is assumed to be web.tabbit.ai);
- getting past the 492 gate on .ai.
No magpie-side change is needed: magpie has no tabbit mover or min.

Reported-by: Beyfish
2026-11-07 19:57:13 +08:00

176 lines
8.3 KiB
JavaScript

// Reading the sign-in out of the Tabbit desktop app, without opening it.
//
// Tabbit Browser keeps the same Chromium cookie store every Chromium does:
// macOS ~/Library/Application Support/Tabbit Browser/Default/Cookies
// and protects the value with the platform's own key. The database is copied
// before it is read: a running browser holds a lock on a WAL, and reading the
// live file gives a half-written row. The copy is removed again in a finally:
// it holds every site's cookies, and a copy left behind each sign-in would
// leave that in the temp folder.
//
// macOS: `Tabbit Browser Safe Storage` in the login keychain holds the
// password, and a Chromium cookie is AES-128-CBC under
// PBKDF2-HMAC-SHA1(password, "saltysalt", 1003, 16 bytes) with a sixteen-space
// IV, prefixed `v10`, padded PKCS#7, and (on Chromium 24 and later) a 32-byte
// SHA-256 of the host in front of the plaintext.
//
// Only macOS is served here. Windows needs DPAPI and Linux a different store
// key, neither implemented; both use the paste method instead, and this says
// so rather than failing obscurely.
import fs from "node:fs"
import os from "node:os"
import path from "node:path"
import crypto from "node:crypto"
import { execFileSync } from "node:child_process"
// The two sites a Tabbit account can belong to (see SITES in api.mjs), the
// Chinese one first. A browser signed in to either keeps the token under
// that site's host, so the host it was found under names the account's site.
export const HOSTS = ["web.tabbit.com", "web.tabbit.ai"]
export const HOST = HOSTS[0]
const SAFE_STORAGE = "Tabbit Browser Safe Storage"
// profileDirs are the places Tabbit Browser has kept its profile, newest
// name first. The browser's own data directory is not the same as the
// user-data directory the old Tabbit client used, so both are listed.
export function profileDirs(env = process.env, home = os.homedir()) {
const roots = [env.TABBIT_PROFILE_DIR, env.TABBIT_USER_DATA_DIR].filter(Boolean)
if (process.platform === "darwin") {
roots.push(
path.join(home, "Library", "Application Support", "Tabbit Browser"),
path.join(home, "Library", "Application Support", "Tabbit", "User Data"),
path.join(home, "Library", "Application Support", "Tabbit"),
)
} else if (process.platform === "win32") {
const local = env.LOCALAPPDATA || path.join(home, "AppData", "Local")
roots.push(path.join(local, "Tabbit Browser", "User Data"), path.join(local, "Tabbit", "User Data"))
} else {
const config = env.XDG_CONFIG_HOME || path.join(home, ".config")
roots.push(path.join(config, "Tabbit Browser"), path.join(config, "Tabbit", "User Data"))
}
return [...new Set(roots)]
}
// cookieStore finds the SQLite file, preferring a profile called Default.
export function cookieStore(dirs = profileDirs()) {
for (const dir of dirs) {
for (const rel of [path.join("Default", "Cookies"), path.join("Network", "Cookies"), "Cookies"]) {
const file = path.join(dir, rel)
if (fs.existsSync(file)) return file
}
}
return null
}
// copyStore copies the database (and its WAL, when present) to a temporary
// directory: sqlite reads the pair, and the live one is locked.
export function copyStore(file, tmp = fs.mkdtempSync(path.join(os.tmpdir(), "tabbit-cookies-"))) {
const copy = path.join(tmp, "Cookies")
fs.copyFileSync(file, copy)
for (const suffix of ["-wal", "-shm"]) {
if (fs.existsSync(file + suffix)) fs.copyFileSync(file + suffix, copy + suffix)
}
return copy
}
// keychainPassword asks the login keychain for the browser's storage
// password. A keychain that is locked, or a prompt the user dismisses,
// makes this throw and the caller falls back to a pasted value.
export function keychainPassword(service = SAFE_STORAGE) {
const out = execFileSync("security", ["find-generic-password", "-s", service, "-w"], {
encoding: "utf8",
stdio: ["ignore", "pipe", "ignore"],
timeout: 15_000,
})
const password = String(out).replace(/\r?\n$/, "")
if (!password) throw new Error(`the keychain holds no password under "${service}"`)
return password
}
// decryptValue turns one `encrypted_value` blob into text.
export function decryptValue(blob, { password, host = HOST } = {}) {
const buf = Buffer.isBuffer(blob) ? blob : Buffer.from(blob)
const prefix = buf.subarray(0, 3).toString("latin1")
if (prefix !== "v10" && prefix !== "v11") throw new Error(`unsupported cookie encryption (${prefix})`)
if (typeof password !== "string" || !password) throw new Error("a password is needed to decrypt a Chromium cookie")
const key = crypto.pbkdf2Sync(password, "saltysalt", 1003, 16, "sha1")
const decipher = crypto.createDecipheriv("aes-128-cbc", key, Buffer.alloc(16, 0x20))
decipher.setAutoPadding(false)
let plain = Buffer.concat([decipher.update(buf.subarray(3)), decipher.final()])
const pad = plain[plain.length - 1]
// PKCS#7 only ever strips 1..16 bytes; anything else means the key was wrong
// and stripping would quietly corrupt the value.
if (pad > 0 && pad <= 16 && pad <= plain.length) plain = plain.subarray(0, plain.length - pad)
const hostHash = crypto.createHash("sha256").update(host).digest()
if (plain.length > 32 && plain.subarray(0, 32).equals(hostHash)) plain = plain.subarray(32)
return plain.toString("utf8")
}
// readCookies reads the wanted names for one host out of the copied store.
// node:sqlite is in Node 22 and later, which is what magpie's host and the
// plugin's own engines field ask for.
export function readCookies(store, { host = HOST, names = ["token", "user_id"] } = {}) {
const { DatabaseSync } = require_node_sqlite()
const db = new DatabaseSync(store, { readOnly: true })
try {
const rows = db
.prepare("select name, value, encrypted_value from cookies where host_key = ? and name in (?, ?)")
.all(host, names[0], names[1])
return rows.map((row) => ({ name: row.name, value: row.value, encrypted: row.encrypted_value }))
} finally {
db.close()
}
}
function require_node_sqlite() {
// A static import of a built-in that older Node lacks would fail the whole
// module; resolving it here lets the plugin load and say what is missing.
const sqlite = globalThis.process?.getBuiltinModule?.("node:sqlite")
if (!sqlite?.DatabaseSync) throw new Error("reading Tabbit's cookies needs Node 22 or later (node:sqlite)")
return sqlite
}
// desktopSignIn returns the cookie header and what it belongs to, or throws
// with the reason a caller can show.
//
// `platform` and `tmpParent` are for the tests: the same code path runs with a
// fixture store under a temp parent the test can look into afterwards.
export function desktopSignIn({ dirs = profileDirs(), hosts = HOSTS, store, password, tmpParent, platform = process.platform } = {}) {
if (platform !== "darwin") {
throw new Error(`reading Tabbit Browser's cookies is macOS-only; on ${platform} paste the token instead`)
}
const file = store || cookieStore(dirs)
if (!file) throw new Error(`no Tabbit Browser cookie store found; looked in ${dirs.join(", ")}`)
const dir = fs.mkdtempSync(path.join(tmpParent ?? os.tmpdir(), "tabbit-cookies-"))
try {
const copy = copyStore(file, dir)
let host = hosts[0]
let rows = []
for (const candidate of hosts) {
rows = readCookies(copy, { host: candidate })
host = candidate
if (rows.some((r) => r.name === "token")) break
}
const token = rows.find((r) => r.name === "token")
if (!token) throw new Error(`Tabbit Browser is not signed in to ${hosts.join(" or ")}`)
const secret = password ?? keychainPassword()
const value = token.value?.startsWith("ey") ? token.value : decryptValue(token.encrypted, { password: secret, host })
const uid = rows.find((r) => r.name === "user_id")
const uidValue = uid?.value || (uid?.encrypted ? safeDecrypt(uid.encrypted, secret, host) : "")
const header = [`token=${value}`, uidValue && `user_id=${uidValue}`, uidValue && `SAPISID=${uidValue}`, "managed=tab_browser"].filter(Boolean).join("; ")
return { key: header, userId: uidValue, store: file, site: `https://${host}` }
} finally {
fs.rmSync(dir, { recursive: true, force: true })
}
}
function safeDecrypt(blob, password, host) {
try {
return decryptValue(blob, { password, host })
} catch {
return ""
}
}
export const _internal = { safeDecrypt, SAFE_STORAGE }