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 <title>, 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.
This commit is contained in:
yetone
2026-11-16 12:10:02 +08:00
parent df52820f18
commit 22272f1be8
4 changed files with 85 additions and 3 deletions
+1 -1
View File
@@ -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",
+22 -2
View File
@@ -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(
+17
View File
@@ -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</title></head><body></body></html>', {
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 {
+45
View File
@@ -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 = '<!DOCTYPE html>\n<html lang="en">\n<head>\n<meta charset="utf-8">\n<title>Service Unavailable</title>\n</head>\n<body><h1>Service Unavailable</h1></body>\n</html>\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/)