Skip to content

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

fix(shared-computers): a computer follows its session's lender key when that key changes

Bugfixer 2c74d5912a1fc
diff --git a/server/routes/shared-computers.ts b/server/routes/shared-computers.ts
index 7909927a23..ca31a52f6f 100644
--- a/server/routes/shared-computers.ts
+++ b/server/routes/shared-computers.ts
@@ -85,7 +85,8 @@ export function createSharedComputerRoutes(deps: {
if (action === "poll") {
const power = deps.power();
if (power?.sleeping()) return answerAsleep(res, json);
- const job = await deps.computers.poll(id, auth.session.id, secret, power ? AbortSignal.any([responseGone(res), power.sleepSignal()]) : responseGone(res));
+ const job = await deps.computers.poll(id, auth.session.id, secret, power ? AbortSignal.any([responseGone(res), power.sleepSignal()]) : responseGone(res),
+ deps.cloudHomeLender ?? deps.personKey(auth.session));
if (!job && power?.sleeping()) return answerAsleep(res, json);
return json(res, 200, { job });
}
diff --git a/server/shared-computers.test.ts b/server/shared-computers.test.ts
index c6691d8a28..a3270dcb26 100644
--- a/server/shared-computers.test.ts
+++ b/server/shared-computers.test.ts
@@ -21,6 +21,23 @@ const nothingQueued = async (broker: SharedComputers, id: string) => {
return poll;
};
describe("shared computer authority and lifecycle", () => {
+ it("a computer whose person changed is found under its new key after it connects again", () => {
+ const broker = new SharedComputers(id => id === "owner");
+ const computer = registration(); broker.register(computer, alice, secret);
+ broker.register(computer, { session: alice.session, person: "p_lender" }, secret);
+ expect(broker.list(alice.person)).toEqual([]);
+ expect(broker.list("p_lender").map(entry => entry.id)).toEqual([computer.id]);
+ broker.close();
+ });
+ it("a poll from a session that is now someone else drops the computer so it connects again", async () => {
+ const broker = new SharedComputers(id => id === "owner");
+ const computer = registration(); broker.register(computer, alice, secret);
+ await expect(broker.poll(computer.id, alice.session, secret, undefined, "p_lender")).rejects.toMatchObject({ status: 403 });
+ expect(broker.list(alice.person)).toEqual([]);
+ broker.register(computer, { session: alice.session, person: "p_lender" }, secret);
+ expect(broker.list("p_lender").map(entry => entry.id)).toEqual([computer.id]);
+ broker.close();
+ });
it("requires both the owning paired session and the connector secret", async () => {
const broker = new SharedComputers(id => id === "owner");
const computer = registration(); broker.register(computer, alice, secret);
diff --git a/server/shared-computers.ts b/server/shared-computers.ts
index e4aa624b0f..1f94886e69 100644
--- a/server/shared-computers.ts
+++ b/server/shared-computers.ts
@@ -171,7 +171,9 @@ export class SharedComputers {
if (!/^[a-f0-9]{64}$/.test(secret)) throw failure("Invalid computer credential", 400);
const key = SharedComputers.key(owner.session, registration.id);
const old = this.computers.get(key);
- if (old) {
+ // Same session, same person: refresh it. Its person changed (a server that became, or stopped being, one person's own):
+ // it is replaced below, so it is found under the key its session has now.
+ if (old && old.owner.person === owner.person) {
this.authorize(registration.id, owner.session, secret);
// Its link was let in for the internet connection and Chrome as lent then: lent or taken back since, the link starts over.
if ((old.registration.internet === true) !== (registration.internet === true) || (old.registration.chrome === true) !== (registration.chrome === true)) this.links?.drop(key);
@@ -311,8 +313,14 @@ export class SharedComputers {
}));
}
/** The newest poll wins: one whose client dropped never takes the next job, and the reconnecting desktop is not refused. */
- async poll(id: string, session: string, secret: string, gone?: AbortSignal) {
+ /** `person`: who the polling session is now; when it is no longer who registered, the computer is dropped (403) so the
+ * desktop connects again under its current key. */
+ async poll(id: string, session: string, secret: string, gone?: AbortSignal, person?: string) {
const entry = this.authorize(id, session, secret);
+ if (person !== undefined && person !== entry.owner.person) {
+ this.disconnect(id, session, secret);
+ throw failure("This computer's access changed. Connect it again.", 403);
+ }
const mine = ++entry.polls;
entry.wake?.();
entry.seen = Date.now();