Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
fix(mcp): never present a refresh token twice
David Neyra467f9fad74b6
diff --git a/server/mcp-oauth-refresh-races.test.ts b/server/mcp-oauth-refresh-races.test.tsindex 1429d21523..af20eff5bf 100644--- a/server/mcp-oauth-refresh-races.test.ts+++ b/server/mcp-oauth-refresh-races.test.ts@@ -129,6 +129,23 @@ it("never shares an old refresh with a replacement entry at another resource URLexpect(await nextPending).toBe("other-access");});+it("never presents a refresh token it already sent, so a rotating server does not revoke the grant", async () => {+ // The server rotates the token but its answer is lost. The client must not present that token again: a server+ // that rotates refresh tokens revokes the whole grant when it sees one of them a second time.+ const first = manager.accessToken("docs", resource);+ const request = await nextRequest();+ expect(request.form.get("refresh_token")).toBe("old-refresh");+ request.response.destroy();+ await first.catch(() => undefined);+ expect(requests).toHaveLength(0);++ // The next attempt must not send the same refresh token again.+ const second = manager.accessToken("docs", resource);+ await second.catch(() => undefined);+ expect(requests).toHaveLength(0);+ expect(manager.authState("docs", resource)).toBe("needs-sign-in");+});+it("signs out immediately and keeps a later sign-in when revocation completes", async () => {const signingOut = manager.signOut("docs", resource);const revocation = await nextRequest();diff --git a/server/mcp-oauth.ts b/server/mcp-oauth.tsindex 2f5d6b4c97..01a55b76a2 100644--- a/server/mcp-oauth.ts+++ b/server/mcp-oauth.ts@@ -221,6 +221,10 @@ export class McpOAuthManager {private readonly starting = new Map<string, StartingFlow>();private readonly refreshing = new Map<string, Promise<{ token: string | null; outcome: McpRefreshOutcome | null }>>();private readonly renewedAfterRefusal = new Map<string, number>();+ /** Refresh tokens already sent to a token endpoint, per server name. A server that rotates refresh tokens revokes+ * the whole grant when it sees one of them again, so a token is never presented twice, not even after a lost+ * answer: the sign-in ends instead of being refreshed a second time with the same token. */+ private readonly spentRefreshTokens = new Map<string, Set<string>>();private readonly clients: (name: string, url: string) => McpOAuthClientConfig | undefined;private readonly onSettled: (name: string, phase: McpSignInPhase, owner: string) => void;private readonly onRefresh: (name: string, outcome: McpRefreshOutcome) => void;@@ -456,6 +460,15 @@ export class McpOAuthManager {this.markNeedsSignIn(name, record.url);return { token: null, outcome: "expired" };}+ // A refresh token the server may already have rotated must never be presented again: such a server revokes the+ // whole grant when it sees one of them a second time. A token this server already sent is therefore spent, whatever+ // the answer was, and the sign-in ends rather than being refreshed a second time with it.+ const spent = this.spentRefreshTokens.get(name);+ if (spent?.has(tokens.refresh)) {+ this.markNeedsSignIn(name, record.url);+ return { token: null, outcome: "expired" };+ }+ this.spentRefreshTokens.set(name, new Set([...(spent ?? []), tokens.refresh]));let answer: Awaited<ReturnType<typeof postForm>>;try {answer = await postForm(record.tokenEndpoint, {