Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(#6466): keep a refresh token that never reached the server, or that the server answered without a new one

milind-soni2faa745e3011
diff --git a/server/mcp-oauth-refresh-races.test.ts b/server/mcp-oauth-refresh-races.test.ts
index c521f767b3..15282e886e 100644
--- a/server/mcp-oauth-refresh-races.test.ts
+++ b/server/mcp-oauth-refresh-races.test.ts
@@ -166,6 +166,32 @@ it("does not present a spent refresh token again after a restart", async () => {
}
});
+it("presents the refresh token again when nothing reached the server", async () => {
+ // Offline, or the address does not answer: the request never left, so the token was not spent and the sign-in stays.
+ const closed = createServer();
+ await new Promise<void>((resolve) => closed.listen(0, "127.0.0.1", resolve));
+ const port = (closed.address() as { port: number }).port;
+ await new Promise<void>((resolve) => closed.close(() => resolve()));
+ manager.adopt("docs", resource, { ...record("old"), tokenEndpoint: `http://127.0.0.1:${port}/token` }, () => true);
+ expect(await manager.accessToken("docs", resource)).toBeNull();
+ expect(await manager.accessToken("docs", resource)).toBeNull();
+ expect(events).toEqual(["unreachable", "unreachable"]);
+ expect(manager.authState("docs", resource)).not.toBe("needs-sign-in");
+});
+
+it("presents the refresh token again after the server answered without one", async () => {
+ // A busy or rate-limited server issued nothing, so the next renewal sends the same token.
+ const first = manager.accessToken("docs", resource);
+ const request = await nextRequest();
+ reply(request, 503, { error: "temporarily_unavailable" });
+ expect(await first).toBeNull();
+ const second = manager.accessToken("docs", resource);
+ const again = await nextRequest();
+ expect(again.form.get("refresh_token")).toBe("old-refresh");
+ reply(again, 200, { access_token: "fresh-access", refresh_token: "new-refresh" });
+ expect(await second).toBe("fresh-access");
+});
+
it("presents the refresh token again when the server kept it", async () => {
// A server that does not rotate refresh tokens answers without a new one, and the old token stays valid
// (RFC 6749 section 6). The next renewal has to present it again, so the mark of a spent token is cleared when
diff --git a/server/mcp-oauth.ts b/server/mcp-oauth.ts
index fefed221d0..78fa01fce4 100644
--- a/server/mcp-oauth.ts
+++ b/server/mcp-oauth.ts
@@ -191,6 +191,13 @@ async function postForm(
return { status: response.status, body };
}
+/** Failures before the connection opened: the request never left, so nothing was presented. */
+const NOT_CONNECTED = new Set(["ECONNREFUSED", "ENOTFOUND", "EAI_AGAIN", "ENETUNREACH", "EHOSTUNREACH", "ENETDOWN", "UND_ERR_CONNECT_TIMEOUT"]);
+function neverSent(error: unknown): boolean {
+ const code = (error as { cause?: { code?: unknown } } | null)?.cause?.code;
+ return typeof code === "string" && NOT_CONNECTED.has(code);
+}
+
function tokensFrom(body: Record<string, unknown>, previous?: McpOAuthTokens): McpOAuthTokens | null {
if (typeof body.access_token !== "string" || !body.access_token) return null;
const refresh = typeof body.refresh_token === "string" && body.refresh_token ? body.refresh_token : previous?.refresh;
@@ -477,8 +484,10 @@ export class McpOAuthManager {
refresh_token: tokens.refresh,
resource: record.url,
}, client);
- } catch {
- // The answer was lost: the token stays spent, because the server may have rotated it.
+ } catch (error) {
+ // A connection that never opened sent nothing, so the token is still good. Any other lost answer keeps it spent,
+ // because the server may have rotated it.
+ if (neverSent(error)) this.store.clearSpentRefresh(name, sent);
if (!isDeepStrictEqual(this.store.get(name, record.url), record)) return { token: null, outcome: null };
return { token: stillValid ? tokens.access : null, outcome: "unreachable" };
}
@@ -487,8 +496,12 @@ export class McpOAuthManager {
if (!next) {
const dead = (answer.status === 400 || answer.status === 401)
&& (answer.body.error === "invalid_grant" || answer.body.error === "invalid_client");
- // The answer came, but it was no token: the server may have rotated the token anyway, so it stays spent.
- if (!dead) return { token: stillValid ? tokens.access : null, outcome: "unreachable" };
+ // The server answered without a token (busy, rate-limited, down for a moment): it issued nothing, so the token is
+ // still good and the next attempt may present it.
+ if (!dead) {
+ this.store.clearSpentRefresh(name, sent);
+ return { token: stillValid ? tokens.access : null, outcome: "unreachable" };
+ }
this.markNeedsSignIn(name, record.url);
return { token: null, outcome: "rejected" };
}