From 22272f1be8bf0681bf10f663e431d3b95952633d Mon Sep 17 00:00:00 2001 From: yetone Date: Mon, 16 Nov 2026 12:10:02 +0800 Subject: [PATCH] tabbit 0.1.5: a 403 that is an HTML error page is not a lapsed sign-in (#1199) yukinomon measured it (#1199): Tabbit's international site answered the daily check-in with a 403 whose body was a "Service Unavailable" HTML page, with no server or cf-ray header (every JSON answer there carries server: uvicorn). 0.1.4 took every 403 but the 492 paywall as a refused sign-in, so magpie marked the account for a new sign-in; the same token answered every route half an hour later. - refusalOf calls a 403 whose non-empty body isn't JSON a page; call() throws it as a plain TabbitError with the page's , with no signIn and no "refused the sign-in" text. - It settles nothing about the token's site: an unsettled token isn't sent to the other site for it, and such a page from the other site leaves the site unsettled too. - A 401, and a 403 with Tabbit's own JSON, are refused sign-ins as before. Run (mote, HOME=$(mktemp -d)): - bun test packages/tabbit: 91 pass, 0 fail. - bun scripts/check.mjs tabbit: ok. Broken by hand: origin/main's api.mjs with the new tests: 2 fail (the chat path and the check-in path). Not tried: a real Tabbit account behind such a page; the page's bytes are the reporter's. --- packages/tabbit/package.json | 2 +- packages/tabbit/src/api.mjs | 24 ++++++++++++-- packages/tabbit/tests/checkin.test.mjs | 17 ++++++++++ packages/tabbit/tests/plugin.test.mjs | 45 ++++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 3 deletions(-) diff --git a/packages/tabbit/package.json b/packages/tabbit/package.json index 77f4fd6..74d383a 100644 --- a/packages/tabbit/package.json +++ b/packages/tabbit/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-tabbit-auth", - "version": "0.1.4", + "version": "0.1.5", "description": "Tabbit (web.tabbit.com and web.tabbit.ai) as a magpie / OpenCode provider: live model catalog, cookie sign-in, chat completions, cycle usage and the daily check-in", "type": "module", "main": "./opencode.mjs", diff --git a/packages/tabbit/src/api.mjs b/packages/tabbit/src/api.mjs index 590473d..247e993 100644 --- a/packages/tabbit/src/api.mjs +++ b/packages/tabbit/src/api.mjs @@ -197,7 +197,12 @@ async function refusalOf(res) { // still signed in, the model just isn't on its plan. Measured with // Kimi-K3.1-Pro on a free account. const paywalled = res.status === 403 && code === 492 - return { res, body, parsed, detail, code, action, paywalled } + // A 403 whose body isn't JSON is a page from something in front of + // Tabbit's API (an HTML "Service Unavailable", with no server header, + // #1199), not Tabbit refusing the token: the same token answered on every + // route half an hour later. It says nothing about the sign-in. + const page = res.status === 403 && parsed === null && body.trim() !== "" + return { res, body, parsed, detail, code, action, paywalled, page } } const refused = (res) => res.status === 401 || res.status === 403 @@ -235,7 +240,7 @@ async function call(creds, path, init = {}, signal, { totalTimeout = true, settl // way — accepted there, or refused by both (an expired token, which then // stays on BASE and is reported as before). A failure to reach the other // site settles nothing, and the first refusal stands. - if (!known && refusal && !refusal.paywalled) { + if (!known && refusal && !refusal.paywalled && !refusal.page) { let settled = true for (const other of SITES) { if (other === site) continue @@ -253,6 +258,11 @@ async function call(creds, path, init = {}, signal, { totalTimeout = true, settl refusal = null break } + // an error page there says nothing of the token either (#1199) + if (again.status === 403 && (await refusalOf(again)).page) { + settled = false + continue + } try { await again.body?.cancel() } catch { @@ -265,6 +275,16 @@ async function call(creds, path, init = {}, signal, { totalTimeout = true, settl learn(creds.token, site) } + if (refusal?.page) { + const title = /<title[^>]*>([^<]{1,120})<\/title>/i.exec(refusal.body)?.[1]?.trim() + const error = new TabbitError( + `Tabbit's service answered HTTP ${res.status} with an error page${title ? ` (${title})` : `: ${refusal.body.slice(0, 200)}`}; the sign-in was not refused`, + { status: res.status }, + ) + const retryAfter = res.headers.get("retry-after") + if (retryAfter) error.retryAfter = retryAfter + throw error + } if (refusal) { const { body, parsed, detail, code, action, paywalled } = refusal const error = new TabbitError( diff --git a/packages/tabbit/tests/checkin.test.mjs b/packages/tabbit/tests/checkin.test.mjs index 967ec94..be1867f 100644 --- a/packages/tabbit/tests/checkin.test.mjs +++ b/packages/tabbit/tests/checkin.test.mjs @@ -107,6 +107,23 @@ test("a refused sign-in throws with magpie's expired mark", async () => { } }) +test("an HTML error page's 403 fails the check-in without the expired mark (#1199)", async () => { + const original = globalThis.fetch + globalThis.fetch = async () => + new Response('<!DOCTYPE html>\n<html lang="en">\n<head><title>Service Unavailable', { + status: 403, + headers: { "content-type": "text/html" }, + }) + try { + await assert.rejects( + () => checkinOf(creds), + (error) => error.signIn === undefined && /Service Unavailable/.test(error.message) && !/refused the sign-in/.test(error.message), + ) + } finally { + globalThis.fetch = original + } +}) + test("the status call asks for the scene the service takes", async () => { const { calls, restore } = stub({ status: open({ signed_today: true }) }) try { diff --git a/packages/tabbit/tests/plugin.test.mjs b/packages/tabbit/tests/plugin.test.mjs index dc3e958..a44d12b 100644 --- a/packages/tabbit/tests/plugin.test.mjs +++ b/packages/tabbit/tests/plugin.test.mjs @@ -219,6 +219,51 @@ test("a premium-only model's 403 does not mark the sign-in", async () => { } }) +// #1199: the reporter's page, from something in front of Tabbit's API +const EDGE_PAGE = '\n\n\n\nService Unavailable\n\n

Service Unavailable

\n\n' + +test("a 403 that is an HTML error page does not mark the sign-in (#1199)", async () => { + const original = globalThis.fetch + const hosts = new Set() + globalThis.fetch = async (input) => { + hosts.add(new URL(typeof input === "string" ? input : input.url).host) + return new Response(EDGE_PAGE, { status: 403, headers: { "content-type": "text/html" } }) + } + try { + const hooks = await TabbitPlugin() + const loader = await hooks.auth.loader(async () => auth) + const res = await loader.fetch(`${loader.baseURL}/chat/completions`, { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ model: "Default", messages: [{ role: "user", content: "hi" }] }), + }) + assert.equal(res.status, 403) + assert.equal(res.headers.get("x-magpie-sign-in"), null) + const text = await res.text() + assert.match(text, /Service Unavailable/) + assert.doesNotMatch(text, /refused the sign-in/) + } finally { + globalThis.fetch = original + } +}) + +test("a 403 with Tabbit's own JSON still marks the sign-in", async () => { + const original = globalThis.fetch + globalThis.fetch = async () => Response.json({ detail: "invalid bearer token" }, { status: 403 }) + try { + const hooks = await TabbitPlugin() + const loader = await hooks.auth.loader(async () => auth) + const res = await loader.fetch(`${loader.baseURL}/chat/completions`, { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ model: "Default", messages: [{ role: "user", content: "hi" }] }), + }) + assert.equal(res.headers.get("x-magpie-sign-in"), "expired") + } finally { + globalThis.fetch = original + } +}) + test("credsOf accepts a bare JWT and a full cookie string", () => { const bare = credsOf({ key: "aaa.bbb.ccc", metadata: { userId: "u9" } }) assert.match(bare.header, /token=aaa\.bbb\.ccc/)