Skip to content

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

feat(sessions): revoke a session after its idle window passes

DeryFerdbff8d87c0259
diff --git a/server/sessions.test.ts b/server/sessions.test.ts
index 13b40f56e2..9c794792d5 100644
--- a/server/sessions.test.ts
+++ b/server/sessions.test.ts
@@ -447,6 +447,17 @@ describe("sessions", () => {
expect(readFileSync(blocked, "utf8")).toContain(paired.session.id);
});
+ it("still reduces the in-memory set when persisting a prune fails", () => {
+ pair();
+ clock += SESSION_TTL_MS + 1;
+ const logger = vi.spyOn(console, "error").mockImplementation(() => {});
+ const writes = vi.spyOn(atomic, "writeFileAtomic").mockImplementation(() => { throw new Error("EIO: the session file is unwritable"); });
+ expect(() => registry.list(), "the heartbeat prunes through isLive(), where a throw would exit the process").not.toThrow();
+ expect(registry.list()).toEqual([]);
+ expect(logger.mock.calls.flat().join(" ")).toContain("Could not persist session pruning.");
+ writes.mockRestore();
+ });
+
it("stores only a hash, owner-only, and reloads from disk", () => {
const { token, session } = pair();
const onDisk = readFileSync(file(), "utf8");
@@ -483,6 +494,45 @@ describe("sessions", () => {
expect(statSync(file()).mtimeMs).toBeGreaterThanOrEqual(before);
});
+ it("revokes a session no one has used inside the idle window, before its term is up", () => {
+ const local = new SessionRegistry({ file: file(), now: () => clock, idleMs: 120_000 });
+ const { code } = local.openPairing({ label: "phone" });
+ const paired = local.exchange({ code, label: "phone", source: "a" });
+ if (!paired.ok) throw new Error(paired.error);
+ expect(local.authenticate(paired.token)?.id).toBe(paired.session.id);
+ clock += 121_000; // the 30-day term is still ahead; only the idle window has passed
+ expect(local.authenticate(paired.token)).toBeNull();
+ expect(local.list()).toEqual([]);
+ expect(JSON.parse(readFileSync(file(), "utf8")).sessions).toEqual([]);
+ });
+
+ it("keeps a session used inside the idle window alive, and revokes it once the quiet returns", () => {
+ const local = new SessionRegistry({ file: file(), now: () => clock, idleMs: 120_000 });
+ const { code } = local.openPairing({ label: "phone" });
+ const paired = local.exchange({ code, label: "phone", source: "a" });
+ if (!paired.ok) throw new Error(paired.error);
+ clock += 61_000; // past the once-a-minute last-seen write, so this use counts
+ expect(local.authenticate(paired.token)?.id, "a use inside the window counts").toBe(paired.session.id);
+ clock += 30_000;
+ expect(local.authenticate(paired.token)?.id, "still inside the window after the last use").toBe(paired.session.id);
+ clock += 121_000;
+ expect(local.authenticate(paired.token)).toBeNull();
+ });
+
+ it("closes an idle session's stream tickets and tells revocation listeners", () => {
+ const local = new SessionRegistry({ file: file(), now: () => clock, idleMs: 120_000 });
+ const { code } = local.openPairing({ label: "phone" });
+ const paired = local.exchange({ code, label: "phone", source: "a" });
+ if (!paired.ok) throw new Error(paired.error);
+ const { ticket } = local.issueStreamTicket(paired.session.id);
+ const revoked: string[] = [];
+ local.onSessionRevoked((id) => revoked.push(id));
+ clock += 121_000;
+ expect(local.list()).toEqual([]);
+ expect(revoked).toEqual([paired.session.id]);
+ expect(local.redeemStreamTicket(ticket)).toBeNull();
+ });
+
it("renews a session with half its term or less left, and never revives an expired one", () => {
const { token, session } = pair();
clock += SESSION_TTL_MS / 2 - 60_000;
diff --git a/server/sessions.ts b/server/sessions.ts
index 6d5c288464..b2ab597c9f 100644
--- a/server/sessions.ts
+++ b/server/sessions.ts
@@ -23,6 +23,8 @@ export const SESSION_TTL_MS = daysMs(process.env.OMB_SESSION_TTL_DAYS, 30);
/** OMB_SESSION_MAX_DAYS (default 180): renewal never passes this, so a stolen cookie has a bounded life. */
export const SESSION_MAX_AGE_MS = daysMs(process.env.OMB_SESSION_MAX_DAYS, 180);
export const SESSION_RENEW_WHEN_LEFT_MS = SESSION_TTL_MS / 2;
+/** OMB_SESSION_IDLE_DAYS (default: the session term): revoke a session this many days after its last use. */
+export const SESSION_IDLE_MS = daysMs(process.env.OMB_SESSION_IDLE_DAYS, SESSION_TTL_MS / 86_400_000);
export function cookieMaxAgeSeconds(session: { expiresAt: number }, now = Date.now()): number {
return Math.max(1, Math.floor((session.expiresAt - now) / 1000));
@@ -180,6 +182,10 @@ export interface SessionStoreOptions {
emailScopes?: EmailScopesResolver;
emailScopesSnapshot?: () => EmailScopesResolver;
portalMembership?: boolean;
+ /** Tests only: an idle window shorter than a day is unsound here — `lastSeenAt` is
+ * persisted at most once a minute (LAST_SEEN_WRITE_INTERVAL_MS), so sub-day windows
+ * can revoke a session used moments ago. Production idle windows are whole days. */
+ idleMs?: number;
}
export class SessionRegistry {
@@ -192,6 +198,7 @@ export class SessionRegistry {
private readonly onIssued = new Set<(session: Pick<SessionRecord, "id" | "scopes" | "email" | "teamRole" | "membershipAuthority">) => void>();
private lastSeenWrites = new Map<string, number>();
private readonly now: () => number;
+ private readonly idleMs: number;
private readonly options: SessionStoreOptions;
private readonly openMarker: string;
private closed = false;
@@ -201,6 +208,7 @@ export class SessionRegistry {
constructor(options: SessionStoreOptions) {
this.options = options;
this.now = options.now ?? Date.now;
+ this.idleMs = options.idleMs ?? SESSION_IDLE_MS;
this.openMarker = `${options.file}.open`;
const unclean = existsSync(this.openMarker);
try {
@@ -265,11 +273,16 @@ export class SessionRegistry {
for (const [source, entry] of this.failures) {
if (entry.lockedUntil <= now && now - entry.windowStart > LOCKOUT.windowMs) this.failures.delete(source);
}
- const expired = this.sessions.filter((s) => s.expiresAt <= now);
+ const expired = this.sessions.filter((s) => s.expiresAt <= now || now - s.lastSeenAt > this.idleMs);
if (expired.length) {
- this.sessions = this.sessions.filter((s) => s.expiresAt > now);
+ this.sessions = this.sessions.filter((s) => s.expiresAt > now && now - s.lastSeenAt <= this.idleMs);
for (const s of expired) this.forget(s.id);
- this.persist();
+ try {
+ this.persist();
+ } catch {
+ // Memory already holds the reduced set; the heartbeat reaches prune() through isLive(), where an escaping write failure would exit the process.
+ console.error("Could not persist session pruning.");
+ }
}
}
@@ -547,6 +560,13 @@ export class SessionRegistry {
const now = this.now();
const record = this.sessions.find((s) => sameDigest(s.tokenHash, hash));
if (!record || record.expiresAt <= now || this.personal(record.scopes)) return null;
+ if (now - record.lastSeenAt > this.idleMs) {
+ this.sessions = this.sessions.filter((s) => s.id !== record.id);
+ this.forget(record.id);
+ // The revocation holds in memory either way; a restart re-reads the saved set.
+ try { this.persist(); } catch { console.error("Could not persist idle session revocation."); }
+ return null;
+ }
const lastWrite = this.lastSeenWrites.get(record.id) ?? 0;
if (now - lastWrite >= LAST_SEEN_WRITE_INTERVAL_MS) {
record.lastSeenAt = now;