nous 0.1.1: a null or string expires_in beside a valid JWT exp takes the exp, so the sign-in stays usable (#328)

token() counted any present non-number expires_in as a malformed bound
and set expires = 1 even when the access token's JWT exp was valid, so a
renewal recorded unusable_inference_jwt and every request failed. Like
Hermes's _remaining_ttl, a null or non-numeric expires_in is now ignored
when the JWT carries a finite exp. With no valid exp it is still a
malformed bound (expires = 1), and a numeric expires_in <= 0 or
non-finite still is too.

Run: TMPDIR/HOME under the scratch dir, bun test packages/nous, 56 pass;
the new test is red on 0.1.0. bun scripts/check.mjs nous ok. Not tried
against the real Nous Portal.
This commit is contained in:
yetone
2026-10-25 14:26:39 +08:00
parent d43b228463
commit b46bcfcf9f
3 changed files with 23 additions and 3 deletions
+15 -1
View File
@@ -738,7 +738,8 @@ test("known lifetimes take precedence over the fallback and use the earlier boun
test("fallback expiry does not extend an explicitly expired or malformed lifetime", async () => {
const cases = [
...[0, -1, null, "3600", 1e308].map(expires_in => ({ expires_in })),
...[0, -1, 1e308].map(expires_in => ({ expires_in })),
...[null, "3600"].map(expires_in => ({ expires_in, access_token: jwt({ exp: undefined }) })),
...[0, -1, null, "1800003600", 1e308].map(exp => ({ expires_in: undefined, access_token: jwt({ exp }) })),
]
for (const fields of cases) {
@@ -751,6 +752,19 @@ test("fallback expiry does not extend an explicitly expired or malformed lifetim
}
})
// #328: Nous sending expires_in null or as a string must not make a sign-in
// whose JWT exp is valid unusable; the exp decides, as Hermes's does
test("a null or string expires_in beside a valid JWT exp takes the exp", async () => {
for (const expires_in of [null, "3600", "", {}]) {
const c = clock()
const exp = (NOW + 600000) / 1000
const flow = createAuth({}, options(c, async () => json(payload({ expires_in, access_token: jwt({ exp }) }))))
const got = await flow.refresh(expiring())
assert.equal(got.expires, exp * 1000, `expires_in ${JSON.stringify(expires_in)}`)
assert.equal(flow.current({ type: "oauth", ...got }).access, got.access)
}
})
test("fallback expiry cannot make malformed or non-inference JWTs usable", async () => {
for (const fields of [
{ access_token: "not-a-jwt" },
+7 -1
View File
@@ -91,7 +91,13 @@ function token(body, old, started) {
// An absent lifetime needs a stable renewal schedule, not expires=1. An
// explicitly expired or malformed bound must never be replaced by that default.
const expiries = []
if (body?.expires_in !== undefined) expiries.push(typeof body.expires_in === "number" ? started + body.expires_in * 1000 : NaN)
// A null or non-numeric expires_in beside a valid JWT exp is ignored, the
// exp decides, as Hermes's _remaining_ttl does (#328); with no valid exp it
// is still a malformed bound.
const expOK = typeof c.exp === "number" && Number.isFinite(c.exp * 1000)
if (body?.expires_in !== undefined && (typeof body.expires_in === "number" || !expOK)) {
expiries.push(typeof body.expires_in === "number" ? started + body.expires_in * 1000 : NaN)
}
if (c.exp !== undefined) expiries.push(typeof c.exp === "number" ? c.exp * 1000 : NaN)
let expires = started + FALLBACK_LIFETIME
if (expiries.length) expires = expiries.every(Number.isFinite) ? Math.max(1, Math.min(...expiries)) : 1
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/opencode-nous-auth",
"version": "0.1.0",
"version": "0.1.1",
"description": "Nous Portal OAuth with device sign-in and automatic token renewal in OpenCode and magpie",
"type": "module",
"main": "./index.mjs",