From b46bcfcf9fe481f27c49f4b5a4bc820b621bd24a Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 25 Oct 2026 14:26:35 +0800 Subject: [PATCH] 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. --- packages/nous/index.test.mjs | 16 +++++++++++++++- packages/nous/oauth.mjs | 8 +++++++- packages/nous/package.json | 2 +- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/packages/nous/index.test.mjs b/packages/nous/index.test.mjs index 3d503a1..d87e3e9 100644 --- a/packages/nous/index.test.mjs +++ b/packages/nous/index.test.mjs @@ -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" }, diff --git a/packages/nous/oauth.mjs b/packages/nous/oauth.mjs index e3f5303..428f8a2 100644 --- a/packages/nous/oauth.mjs +++ b/packages/nous/oauth.mjs @@ -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 diff --git a/packages/nous/package.json b/packages/nous/package.json index c8eff93..4785317 100644 --- a/packages/nous/package.json +++ b/packages/nous/package.json @@ -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",