Skip to content

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

fix: discard provider sign-in results after their flow ends

Afloat6141aebed867
diff --git a/server/provider-auth-sessions.ts b/server/provider-auth-sessions.ts
index 2494035ef8..c7dd10b0ea 100644
--- a/server/provider-auth-sessions.ts
+++ b/server/provider-auth-sessions.ts
@@ -75,7 +75,7 @@ export class ProviderAuthSessions {
const flow = this.owned(instanceId, owner, flowId);
if (!flow.instance.getAuthentication) throw failure("Sign-in status is unavailable for this provider.", 404);
const status = await flow.instance.getAuthentication(flowId);
- if (flow.revoked) throw failure("This sign-in session ended.", 404);
+ if (flow.revoked || this.flows.get(instanceId) !== flow || flow.flowId !== flowId) throw failure("This sign-in session ended.", 404);
if (status.phase !== "waiting") flow.expiresAt = 0;
if (status.phase === "succeeded") this.signedIn(instanceId);
return status;
@@ -87,7 +87,8 @@ export class ProviderAuthSessions {
flow.busy = true;
try {
await flow.instance.completeAuthentication(flowId, callbackUrl);
- if (this.flows.get(instanceId) === flow) this.flows.delete(instanceId);
+ if (flow.revoked || this.flows.get(instanceId) !== flow) throw failure("This sign-in session ended.", 404);
+ this.flows.delete(instanceId);
this.signedIn(instanceId);
} finally { flow.busy = false; }
}
@@ -95,6 +96,7 @@ export class ProviderAuthSessions {
async cancel(instanceId: string, owner: string, flowId: string) {
const flow = this.owned(instanceId, owner, flowId);
flow.busy = true;
+ flow.revoked = true;
try { await flow.instance.cancelAuthentication?.(); }
finally { flow.busy = false; flow.expiresAt = 0; }
}
diff --git a/server/provider-auth-stale.test.ts b/server/provider-auth-stale.test.ts
new file mode 100644
index 0000000000..faad3e3af7
--- /dev/null
+++ b/server/provider-auth-stale.test.ts
@@ -0,0 +1,63 @@
+import { describe, expect, it, vi } from "vitest";
+
+import type { ProviderAuthenticationStart } from "./contracts.ts";
+import { ProviderAuthSessions } from "./provider-auth-sessions.ts";
+
+function fixture() {
+ const waiting: ProviderAuthenticationStart = {
+ phase: "waiting", flowId: "original", authorizationUrl: "https://auth.example/device", expiresAt: null,
+ };
+ const signedIn = vi.fn();
+ const sessions = new ProviderAuthSessions({ signedIn });
+ const instance = {
+ instanceId: "engine", startAuthentication: vi.fn(async () => waiting),
+ getAuthentication: vi.fn(async () => waiting), completeAuthentication: vi.fn(async () => undefined),
+ cancelAuthentication: vi.fn(async () => undefined), signOut: vi.fn(async () => undefined),
+ };
+ return { sessions, instance, waiting, signedIn };
+}
+
+describe("obsolete provider sign-in results", () => {
+ it.each(["cancel", "signOut"] as const)("does not announce a successful poll after %s", async (action) => {
+ const { sessions, instance, waiting, signedIn } = fixture();
+ await sessions.start(instance, "owner");
+ let finish!: (value: ProviderAuthenticationStart) => void;
+ instance.getAuthentication.mockImplementation(() => new Promise((resolve) => { finish = resolve; }));
+ const poll = sessions.status("engine", "owner", "original");
+ if (action === "cancel") await sessions.cancel("engine", "owner", "original");
+ else await sessions.signOut(instance, "owner");
+ finish({ ...waiting, phase: "succeeded" });
+ await expect(poll).rejects.toMatchObject({ status: 404 });
+ expect(signedIn).not.toHaveBeenCalled();
+ });
+
+ it("does not let a cancelled poll announce success after a replacement sign-in starts", async () => {
+ const { sessions, instance, waiting, signedIn } = fixture();
+ await sessions.start(instance, "owner");
+ let finish!: (value: ProviderAuthenticationStart) => void;
+ instance.getAuthentication.mockImplementationOnce(() => new Promise((resolve) => { finish = resolve; }));
+ const poll = sessions.status("engine", "owner", "original");
+ await sessions.signOut(instance, "owner");
+ instance.startAuthentication.mockResolvedValue({ ...waiting, flowId: "replacement" });
+ await sessions.start(instance, "new-owner");
+ finish({ ...waiting, phase: "succeeded" });
+ await expect(poll).rejects.toMatchObject({ status: 404 });
+ expect(signedIn).not.toHaveBeenCalled();
+ expect(sessions.active).toBe(true);
+ await expect(sessions.cancel("engine", "new-owner", "replacement")).resolves.toBeUndefined();
+ });
+
+ it.each(["owner", "instance", "fleet"] as const)("does not announce a callback completion after revoking the %s", async (what) => {
+ const { sessions, instance, signedIn } = fixture();
+ await sessions.start(instance, "owner");
+ let finish!: () => void;
+ instance.completeAuthentication.mockImplementation(() => new Promise<undefined>((resolve) => { finish = () => resolve(undefined); }));
+ const completing = sessions.complete("engine", "owner", "original", "https://localhost/callback?code=x");
+ if (what === "owner") sessions.revokeOwner("owner");
+ else if (what === "instance") sessions.clearInstance("engine");
+ else sessions.clear();
+ finish();
+ await expect(completing).rejects.toMatchObject({ status: 404 });
+ expect(signedIn).not.toHaveBeenCalled();
+ });
+});