Skip to content

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

fix: preserve cancelled sign-in status while rejecting stale polls

Afloat50218fefdbe6
diff --git a/server/provider-auth-sessions.ts b/server/provider-auth-sessions.ts
index c7dd10b0ea..b13b1543d0 100644
--- a/server/provider-auth-sessions.ts
+++ b/server/provider-auth-sessions.ts
@@ -10,6 +10,7 @@ type Flow = {
busy: boolean;
starting: boolean;
revoked: boolean;
+ revision: number;
};
const failure = (message: string, status: number) => Object.assign(new Error(message), { status });
@@ -35,7 +36,7 @@ export class ProviderAuthSessions {
throw failure("A sign-in is already in progress. Finish or cancel it in the browser that started it, or wait for it to expire.", 409);
}
const resuming = existing && existing.owner === owner && existing.expiresAt > Date.now() && !existing.revoked;
- const flow: Flow = resuming ? existing : { instance, owner, flowId: null, expiresAt: Date.now() + 15 * 60_000, busy: false, starting: false, revoked: false };
+ const flow: Flow = resuming ? existing : { instance, owner, flowId: null, expiresAt: Date.now() + 15 * 60_000, busy: false, starting: false, revoked: false, revision: 0 };
flow.busy = true;
flow.starting = true;
this.flows.set(instance.instanceId, flow);
@@ -74,8 +75,9 @@ export class ProviderAuthSessions {
async status(instanceId: string, owner: string, flowId: string) {
const flow = this.owned(instanceId, owner, flowId);
if (!flow.instance.getAuthentication) throw failure("Sign-in status is unavailable for this provider.", 404);
+ const revision = flow.revision;
const status = await flow.instance.getAuthentication(flowId);
- if (flow.revoked || this.flows.get(instanceId) !== flow || flow.flowId !== flowId) throw failure("This sign-in session ended.", 404);
+ if (flow.revoked || this.flows.get(instanceId) !== flow || flow.flowId !== flowId || flow.revision !== revision) throw failure("This sign-in session ended.", 404);
if (status.phase !== "waiting") flow.expiresAt = 0;
if (status.phase === "succeeded") this.signedIn(instanceId);
return status;
@@ -96,7 +98,8 @@ export class ProviderAuthSessions {
async cancel(instanceId: string, owner: string, flowId: string) {
const flow = this.owned(instanceId, owner, flowId);
flow.busy = true;
- flow.revoked = true;
+ // Invalidate polls already in flight, but let a later poll read the provider's cancelled status.
+ flow.revision++;
try { await flow.instance.cancelAuthentication?.(); }
finally { flow.busy = false; flow.expiresAt = 0; }
}
@@ -108,7 +111,7 @@ export class ProviderAuthSessions {
if (existing && (existing.busy || (existing.owner !== owner && existing.expiresAt > Date.now() && !existing.revoked))) {
throw failure("A sign-in is in progress. Finish or cancel it in the browser that started it, or wait for it to expire.", 409);
}
- const flow: Flow = { instance, owner, flowId: null, expiresAt: Date.now() + 60_000, busy: true, starting: false, revoked: false };
+ const flow: Flow = { instance, owner, flowId: null, expiresAt: Date.now() + 60_000, busy: true, starting: false, revoked: false, revision: 0 };
this.flows.set(instance.instanceId, flow);
try {
if (existing) await existing.instance.cancelAuthentication?.();
diff --git a/server/provider-auth-stale.test.ts b/server/provider-auth-stale.test.ts
index faad3e3af7..a4d6919275 100644
--- a/server/provider-auth-stale.test.ts
+++ b/server/provider-auth-stale.test.ts
@@ -1,6 +1,6 @@
import { describe, expect, it, vi } from "vitest";
-import type { ProviderAuthenticationStart } from "./contracts.ts";
+import type { ProviderAuthenticationStart, ProviderAuthenticationStatus } from "./contracts.ts";
import { ProviderAuthSessions } from "./provider-auth-sessions.ts";
function fixture() {
@@ -11,13 +11,26 @@ function fixture() {
const sessions = new ProviderAuthSessions({ signedIn });
const instance = {
instanceId: "engine", startAuthentication: vi.fn(async () => waiting),
- getAuthentication: vi.fn(async () => waiting), completeAuthentication: vi.fn(async () => undefined),
+ getAuthentication: vi.fn(async (): Promise<ProviderAuthenticationStatus> => 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("keeps the cancelled status readable by the owner after cancellation finishes", async () => {
+ const { sessions, instance, waiting, signedIn } = fixture();
+ await sessions.start(instance, "owner");
+ await sessions.cancel("engine", "owner", "original");
+ instance.getAuthentication.mockResolvedValue({ ...waiting, phase: "cancelled", authorizationUrl: null });
+ await expect(sessions.status("engine", "owner", "original")).resolves.toMatchObject({
+ phase: "cancelled", authorizationUrl: null,
+ });
+ expect(signedIn).not.toHaveBeenCalled();
+ expect(sessions.active).toBe(false);
+ await expect(sessions.status("engine", "another-owner", "original")).rejects.toMatchObject({ status: 404 });
+ });
+
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");