Files
plugins/packages/tabbit/tests/cookie.test.mjs
T
yetone efd8da8064 tabbit 0.1.3: the international app's sign-in reads "Tabbit Safe Storage", and Keychain failures say what to do
Signing in with "Read Tabbit Browser sign-in" failed at once, with no
Keychain prompt, as "Command failed: security find-generic-password -s
Tabbit Browser Safe Storage -w" (shiqi_40904, Discord, macOS 27.2).

Cause: Tabbit ships two macOS builds, and the plugin knew only one
Keychain name. Read out of both 1.16.27 installers (Info.plist, and the
framework code that picks the name from a flag set at start-up):
- Tabbit.app (com.tabbit-ai.Tabbit, international): data folder
  ~/Library/Application Support/Tabbit, service "Tabbit Safe Storage",
  account "Tabbit".
- Tabbit Browser.app (com.tab-browser.Tabbit, China): data folder
  "Tabbit Browser", service "Tabbit Browser Safe Storage", account
  "Tabbit Browser".
The plugin found the international store and asked for the China name;
security answered "item not found" (exit 44) with no prompt. The spaces
were never the problem: the name was already one argv entry, and the
message is only Node's way of printing the command.

Now:
- Each store asks its own build's item first, then the other's.
  Not found moves on at once; security only prompts for an item that
  exists, so a not-found never waits for a prompt.
- With both builds installed, the store that is signed in is read.
- The Keychain call is async with a 2-minute timeout (it was a 15 s
  execFileSync that dropped stderr), so the user has time to answer.
- Errors say what to do: not installed or not signed in -> open Tabbit
  and sign in; not in the Keychain under either name (both named);
  prompt denied (128/51) -> click Allow; no answer in time; keychain
  locked (36); anything else carries security's own stderr. Each
  points at "Paste Tabbit token".
- The method is now "Read the Tabbit app's sign-in (macOS)"; README
  lists both builds.

Run on mote (macOS, Node 22.22.0, Bun):
- node --test tests/*.test.mjs: 83 pass, 0 fail. bun test (temp HOME):
  83 pass. bun scripts/check.mjs: exit 0.
- Broken by hand: safeStorageNames returning only the China name (the
  old behaviour) turned 3 tests red, among them "the international
  build signs in with its Tabbit Safe Storage item (the report)".
- Again after rebasing onto ff9d3a64 (the pushed parent; it changed only
  system-prompt and trae): tabbit node --test 83 pass, whole-repo
  bun test (temp HOME) 7588 pass 0 fail, check exit 0.
- Tests use a fake security and fixture cookie stores; nothing reads
  this machine's Keychain or browser profiles.

Not tried: a real Tabbit account or a real Keychain prompt (none here),
so the prompt's exit codes come from Security.framework's OSStatus
values, not a recorded run. Dev builds' "TabbitDev ..." names are not
tried. magpie pins no min for tabbit (git grep finds none).

Reported-by: shiqi_40904 (Discord)
2026-11-09 17:17:41 +08:00

308 lines
15 KiB
JavaScript

// The cookie store: a synthetic Chromium v10 blob, decrypted with a known
// password, and the store lookup that finds the file.
//
// Every temp directory a test makes is removed before the test returns: these
// build whole cookie stores, and a test run must not leave one behind — the
// same point the sign-in copy itself makes.
import test from "node:test"
import assert from "node:assert/strict"
import crypto from "node:crypto"
import fs from "node:fs"
import os from "node:os"
import path from "node:path"
import { decryptValue, cookieStore, cookieStores, copyStore, profileDirs, readCookies, desktopSignIn, keychainPassword, safeStorageNames, SAFE_STORAGE } from "../src/cookie.mjs"
const HOST = "web.tabbit.com"
const PASSWORD = "test-password"
const made = []
function tmp(prefix) {
const dir = fs.mkdtempSync(path.join(os.tmpdir(), prefix))
made.push(dir)
return dir
}
// withCleanup runs a test body and removes every temp directory it made.
function withCleanup(name, body) {
return test(name, async (ctx) => {
try {
await body(ctx)
} finally {
for (const dir of made.splice(0)) fs.rmSync(dir, { recursive: true, force: true })
}
})
}
// seal makes the blob Chromium's own macOS encryption makes: AES-128-CBC
// under the keychain password, `v10`, a 32-byte SHA-256 of the host in
// front of the plaintext, and PKCS#7 padding.
function seal(plaintext, { password = PASSWORD, host = HOST } = {}) {
const key = crypto.pbkdf2Sync(password, "saltysalt", 1003, 16, "sha1")
const body = Buffer.concat([crypto.createHash("sha256").update(host).digest(), Buffer.from(plaintext, "utf8")])
const pad = 16 - (body.length % 16)
const padded = Buffer.concat([body, Buffer.alloc(pad, pad)])
const cipher = crypto.createCipheriv("aes-128-cbc", key, Buffer.alloc(16, 0x20))
cipher.setAutoPadding(false)
return Buffer.concat([Buffer.from("v10", "latin1"), cipher.update(padded), cipher.final()])
}
test("a v10 blob decrypts to the plaintext", () => {
const token = "eyJhbGciOiJSUzI1NiJ9.payload.sig"
assert.equal(decryptValue(seal(token), { password: PASSWORD }), token)
})
test("the wrong password does not yield a plausible value", () => {
let value = null
try {
value = decryptValue(seal("hello"), { password: "wrong" })
} catch {
/* a wrong key usually throws on the padding */
}
assert.notEqual(value, "hello")
})
test("a blob that isn't v10 is refused", () => {
assert.throws(() => decryptValue(Buffer.from("v20xxxx"), { password: PASSWORD }), /unsupported cookie encryption/)
})
test("profileDirs honours an explicit override first", () => {
const dirs = profileDirs({ TABBIT_PROFILE_DIR: "/tmp/one" }, "/home/u")
assert.equal(dirs[0], "/tmp/one")
})
withCleanup("cookieStore finds Default/Cookies under a profile root", () => {
const root = tmp("tabbit-test-")
fs.mkdirSync(path.join(root, "Default"), { recursive: true })
fs.writeFileSync(path.join(root, "Default", "Cookies"), "")
assert.equal(cookieStore([root]), path.join(root, "Default", "Cookies"))
assert.equal(cookieStore([path.join(root, "missing")]), null)
})
withCleanup("copyStore copies the database and its WAL", () => {
const root = tmp("tabbit-test-")
const file = path.join(root, "Cookies")
fs.writeFileSync(file, "db")
fs.writeFileSync(file + "-wal", "wal")
const copy = copyStore(file, tmp("tabbit-copy-"))
assert.equal(fs.readFileSync(copy, "utf8"), "db")
assert.equal(fs.readFileSync(copy + "-wal", "utf8"), "wal")
})
withCleanup("readCookies reads token and user_id back out of a real sqlite file", () => {
let DatabaseSync
try {
DatabaseSync = globalThis.process.getBuiltinModule("node:sqlite").DatabaseSync
} catch {
return // older Node: the plugin says so at runtime too
}
const root = tmp("tabbit-test-")
const file = path.join(root, "Cookies")
const db = new DatabaseSync(file)
db.exec("create table cookies(host_key text, name text, value text, encrypted_value blob)")
db.prepare("insert into cookies values (?,?,?,?)").run(HOST, "token", "", seal("tok-123"))
db.prepare("insert into cookies values (?,?,?,?)").run(HOST, "user_id", "u-1", Buffer.alloc(0))
db.close()
const rows = readCookies(file)
const token = rows.find((r) => r.name === "token")
assert.equal(decryptValue(token.encrypted, { password: PASSWORD }), "tok-123")
assert.equal(rows.find((r) => r.name === "user_id").value, "u-1")
})
// storeWith builds a real cookie store holding `token` (or none), under a temp
// directory the caller's cleanup removes.
function storeWith(token) {
let DatabaseSync
try {
DatabaseSync = globalThis.process.getBuiltinModule("node:sqlite").DatabaseSync
} catch {
return null
}
const root = tmp("tabbit-store-")
const file = path.join(root, "Cookies")
const db = new DatabaseSync(file)
db.exec("create table cookies(host_key text, name text, value text, encrypted_value blob)")
if (token) db.prepare("insert into cookies values (?,?,?,?)").run(HOST, "token", "", seal(token))
db.prepare("insert into cookies values (?,?,?,?)").run(HOST, "user_id", "u-9", Buffer.alloc(0))
db.close()
return file
}
withCleanup("a successful desktop sign-in leaves no copy of the cookie store", async () => {
const file = storeWith("tok-desktop")
if (!file) return
const parent = tmp("tabbit-parent-")
const sign = await desktopSignIn({ store: file, password: PASSWORD, tmpParent: parent, platform: "darwin" })
assert.match(sign.key, /token=tok-desktop/)
assert.equal(sign.userId, "u-9")
assert.deepEqual(fs.readdirSync(parent), [])
})
withCleanup("a failed desktop sign-in leaves no copy either", async () => {
const file = storeWith(null)
if (!file) return
const parent = tmp("tabbit-parent-")
await assert.rejects(() => desktopSignIn({ store: file, password: PASSWORD, tmpParent: parent, platform: "darwin" }), /not signed in/)
assert.deepEqual(fs.readdirSync(parent), [])
})
// An international account's browser keeps the token under web.tabbit.ai,
// sealed with that host's hash (#833).
withCleanup("a browser signed in to web.tabbit.ai signs in there, and says so", async () => {
let DatabaseSync
try {
DatabaseSync = globalThis.process.getBuiltinModule("node:sqlite").DatabaseSync
} catch {
return
}
const AI = "web.tabbit.ai"
const root = tmp("tabbit-store-ai-")
const file = path.join(root, "Cookies")
const db = new DatabaseSync(file)
db.exec("create table cookies(host_key text, name text, value text, encrypted_value blob)")
db.prepare("insert into cookies values (?,?,?,?)").run(AI, "token", "", seal("tok-intl", { host: AI }))
db.prepare("insert into cookies values (?,?,?,?)").run(AI, "user_id", "", seal("u-intl", { host: AI }))
db.close()
const sign = await desktopSignIn({ store: file, password: PASSWORD, tmpParent: tmp("tabbit-parent-"), platform: "darwin" })
assert.match(sign.key, /^token=tok-intl; user_id=u-intl;/)
assert.equal(sign.userId, "u-intl")
assert.equal(sign.site, "https://web.tabbit.ai")
})
withCleanup("a browser signed in to web.tabbit.com still signs in there", async () => {
const file = storeWith("tok-com")
if (!file) return
const sign = await desktopSignIn({ store: file, password: PASSWORD, tmpParent: tmp("tabbit-parent-"), platform: "darwin" })
assert.match(sign.key, /token=tok-com/)
assert.equal(sign.site, "https://web.tabbit.com")
})
test("the desktop method is macOS-only, and says so elsewhere", async () => {
await assert.rejects(() => desktopSignIn({ store: "/nonexistent/Cookies", platform: "linux" }), /macOS-only/)
await assert.rejects(() => desktopSignIn({ store: "/nonexistent/Cookies", platform: "win32" }), /macOS-only/)
})
// ---- The Keychain (shiqi_40904 on Discord: "Command failed: security
// find-generic-password -s Tabbit Browser Safe Storage -w"). The
// international build, Tabbit.app, keeps its password under "Tabbit Safe
// Storage"; only the China build, Tabbit Browser.app, uses "Tabbit Browser
// Safe Storage". A fake `security` stands in for the Keychain: nothing here
// reads this machine's.
// fakeSecurity answers like macOS's `security`: the password for a service it
// holds, exit 44 (errSecItemNotFound) for one it doesn't, or a fixed answer.
function fakeSecurity(items, { answer } = {}) {
const calls = []
const run = async (args) => {
calls.push(args)
const service = args[args.indexOf("-s") + 1]
if (answer) return { stdout: "", stderr: "", error: "", timedOut: false, ...answer(service) }
if (service in items) return { status: 0, stdout: items[service] + "\n", stderr: "", error: "", timedOut: false }
return { status: 44, stdout: "", stderr: "security: SecKeychainSearchCopyNext: The specified item could not be found in the keychain.", error: "", timedOut: false }
}
return { run, calls }
}
// layoutStore builds a store where a build keeps it: `<root>/<dir>/Default/Cookies`.
function layoutStore(dir, token, { host = HOST, password = PASSWORD } = {}) {
let DatabaseSync
try {
DatabaseSync = globalThis.process.getBuiltinModule("node:sqlite").DatabaseSync
} catch {
return null
}
const root = tmp("tabbit-layout-")
const profile = path.join(root, dir)
fs.mkdirSync(path.join(profile, "Default"), { recursive: true })
const file = path.join(profile, "Default", "Cookies")
const db = new DatabaseSync(file)
db.exec("create table cookies(host_key text, name text, value text, encrypted_value blob)")
if (token) {
db.prepare("insert into cookies values (?,?,?,?)").run(host, "token", "", seal(token, { host, password }))
db.prepare("insert into cookies values (?,?,?,?)").run(host, "user_id", "", seal("u-" + token, { host, password }))
}
db.close()
return { profile, file }
}
test("each build's store asks its own Keychain item first", () => {
assert.deepEqual(safeStorageNames("/Users/u/Library/Application Support/Tabbit/Default/Cookies"), ["Tabbit Safe Storage", "Tabbit Browser Safe Storage"])
assert.deepEqual(safeStorageNames("/Users/u/Library/Application Support/Tabbit Browser/Default/Cookies"), ["Tabbit Browser Safe Storage", "Tabbit Safe Storage"])
assert.deepEqual(SAFE_STORAGE, { intl: "Tabbit Safe Storage", cn: "Tabbit Browser Safe Storage" })
})
test("the service name reaches security as one argument", async () => {
const { run, calls } = fakeSecurity({ "Tabbit Browser Safe Storage": "pw" })
assert.equal(await keychainPassword(["Tabbit Browser Safe Storage"], { run }), "pw")
assert.deepEqual(calls[0], ["find-generic-password", "-s", "Tabbit Browser Safe Storage", "-w"])
})
test("a service the Keychain doesn't hold moves on to the next name", async () => {
const { run, calls } = fakeSecurity({ "Tabbit Safe Storage": "intl-pw" })
assert.equal(await keychainPassword(["Tabbit Browser Safe Storage", "Tabbit Safe Storage"], { run }), "intl-pw")
assert.equal(calls.length, 2)
})
test("no item under any name says to open Tabbit and sign in", async () => {
const { run } = fakeSecurity({})
await assert.rejects(
() => keychainPassword(["Tabbit Safe Storage", "Tabbit Browser Safe Storage"], { run }),
(e) => /isn't in the login keychain/.test(e.message) && /“Tabbit Safe Storage” and “Tabbit Browser Safe Storage”/.test(e.message) && /open Tabbit, sign in there/.test(e.message) && /Paste Tabbit token/.test(e.message) && !/Command failed/.test(e.message),
)
})
for (const [status, name] of [[128, "cancelled"], [51, "denied"]]) {
test(`a Keychain prompt the user ${name} says to click Allow, and asks nothing more`, async () => {
const { run, calls } = fakeSecurity({}, { answer: () => ({ status, stderr: "security: SecKeychainSearchCopyNext: User canceled the operation." }) })
await assert.rejects(() => keychainPassword(["Tabbit Safe Storage", "Tabbit Browser Safe Storage"], { run }), /was denied; try again and click Allow/)
assert.equal(calls.length, 1)
})
}
test("an unanswered prompt, a locked keychain and anything else each say what happened", async () => {
let r = fakeSecurity({}, { answer: () => ({ status: null, timedOut: true }) })
await assert.rejects(() => keychainPassword(["Tabbit Safe Storage"], { run: r.run }), /got no answer in time/)
r = fakeSecurity({}, { answer: () => ({ status: 36 }) })
await assert.rejects(() => keychainPassword(["Tabbit Safe Storage"], { run: r.run }), /keychain is locked/)
r = fakeSecurity({}, { answer: () => ({ status: 1, stderr: "security: something odd" }) })
await assert.rejects(() => keychainPassword(["Tabbit Safe Storage"], { run: r.run }), /reading “Tabbit Safe Storage” from the login keychain failed \(security: something odd\)/)
})
withCleanup("the international build signs in with its Tabbit Safe Storage item (the report)", async () => {
const s = layoutStore("Tabbit", "tok-intl-app", { host: "web.tabbit.ai", password: "intl-pw" })
if (!s) return
const { run, calls } = fakeSecurity({ "Tabbit Safe Storage": "intl-pw" })
const sign = await desktopSignIn({ dirs: [s.profile], run, tmpParent: tmp("tabbit-parent-"), platform: "darwin" })
assert.match(sign.key, /^token=tok-intl-app; user_id=u-tok-intl-app;/)
assert.equal(sign.site, "https://web.tabbit.ai")
assert.equal(calls[0][2], "Tabbit Safe Storage")
})
withCleanup("the China build signs in with its Tabbit Browser Safe Storage item", async () => {
const s = layoutStore("Tabbit Browser", "tok-cn-app", { password: "cn-pw" })
if (!s) return
const { run, calls } = fakeSecurity({ "Tabbit Browser Safe Storage": "cn-pw" })
const sign = await desktopSignIn({ dirs: [s.profile], run, tmpParent: tmp("tabbit-parent-"), platform: "darwin" })
assert.match(sign.key, /^token=tok-cn-app;/)
assert.equal(calls.length, 1)
assert.equal(calls[0][2], "Tabbit Browser Safe Storage")
})
withCleanup("with both builds installed, the one that is signed in is read", async () => {
const cn = layoutStore("Tabbit Browser", null)
const intl = layoutStore("Tabbit", "tok-second", { host: "web.tabbit.ai", password: "intl-pw" })
if (!cn || !intl) return
assert.deepEqual(cookieStores([cn.profile, intl.profile]), [cn.file, intl.file])
const { run } = fakeSecurity({ "Tabbit Safe Storage": "intl-pw" })
const sign = await desktopSignIn({ dirs: [cn.profile, intl.profile], run, tmpParent: tmp("tabbit-parent-"), platform: "darwin" })
assert.match(sign.key, /^token=tok-second;/)
assert.equal(sign.store, intl.file)
})
withCleanup("no store and no sign-in each say to open Tabbit and sign in", async () => {
const missing = path.join(tmp("tabbit-none-"), "Tabbit")
await assert.rejects(() => desktopSignIn({ dirs: [missing], platform: "darwin", run: fakeSecurity({}).run }), /open Tabbit, sign in there, and try again/)
const s = layoutStore("Tabbit", null)
if (!s) return
await assert.rejects(() => desktopSignIn({ dirs: [s.profile], platform: "darwin", run: fakeSecurity({}).run, tmpParent: tmp("tabbit-parent-") }), /not signed in to web\.tabbit\.com or web\.tabbit\.ai; open Tabbit, sign in there/)
})