Skip to content

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

fix(1password): pin item choices before the token, hand items back only last (#4954)

Bugfixer 265120803266f
diff --git a/electron/cloud-send.mjs b/electron/cloud-send.mjs
index f92a13ba17..14a32d818c 100644
--- a/electron/cloud-send.mjs
+++ b/electron/cloud-send.mjs
@@ -900,14 +900,19 @@ export function createCloudSend({ local, cloud, handoff, store, onChange = () =>
const parts = row.parts ?? ["token", "vaults"];
const connecting = row.state === "only-here" || parts.includes("token");
// My Cloud's item choices become this computer's, so one handed back to its vault here is handed back there too.
- // They go ahead of the token, so no item is readable there under its vault's wider choice even for a moment.
+ // This computer's own choices go ahead of the token (`keep`: every other item there stays as it was), so no item is
+ // readable there under its vault's wider choice even for a moment; the ones handed back here are handed back there
+ // only last, once the token and the vaults' new choices have taken.
const items = Object.fromEntries(Object.entries(onePassword.itemGrants ?? {}).filter(([item]) => ITEM_KEY.test(item))
.map(([item, bots]) => [item, botsOnCloud(bots, ctx.cloudBots)]));
- const itemsKept = await ctx.session.call("PUT", "/api/logins/1password/items", { items });
- // A Cloud too old for item choices has none to undo, but an item narrowed here must not open to every bot there.
- if (!itemsKept.ok && !(itemsKept.status === 405 && !Object.keys(items).length)) {
- throw refusedError("refused", itemsKept, "My Cloud could not keep its 1Password logins to the same bots. Update My Cloud and send again.");
- }
+ const putItems = async (keep) => {
+ const kept = await ctx.session.call("PUT", "/api/logins/1password/items", keep ? { items, keep } : { items });
+ // A Cloud too old for item choices has none to undo, but an item narrowed here must not open to every bot there.
+ if (!kept.ok && !(kept.status === 405 && !Object.keys(items).length)) {
+ throw refusedError("refused", kept, "My Cloud could not keep its 1Password logins to the same bots. Update My Cloud and send again.");
+ }
+ };
+ await putItems(true);
if (connecting) {
const connected = await ctx.session.call("POST", "/api/logins/1password", { token: onePassword.token });
if (!connected.ok) throw onePasswordRefusal(connected);
@@ -918,6 +923,7 @@ export function createCloudSend({ local, cloud, handoff, store, onChange = () =>
const granted = await ctx.session.call("PATCH", `/api/logins/1password/vaults/${encodeURIComponent(vault)}`, { bots: botsOnCloud(bots, ctx.cloudBots) });
if (!granted.ok && granted.status !== 404) throw refusedError("refused", granted, "My Cloud connected 1Password but could not keep its vaults to the same bots.");
}
+ await putItems(false);
} catch (error) {
// Fail closed: a token this send connected is taken off again rather than left open wider than here.
if (connecting) {
diff --git a/electron/cloud-send.node-test.mjs b/electron/cloud-send.node-test.mjs
index 86f088c7a1..ef0b3f1236 100644
--- a/electron/cloud-send.node-test.mjs
+++ b/electron/cloud-send.node-test.mjs
@@ -86,11 +86,13 @@ test("a bot's fields reach My Cloud without Custom approval", () => {
function fakeSides({ cloudItems = [], localItems = [], refuse = {} } = {}) {
const calls = [];
const bodies = {};
+ const sent = [];
const reply = (status, body) => ({ status, ok: status < 400, body });
const session = {
call: async (method, route, body) => {
calls.push(`${method} ${route}`);
bodies[`${method} ${route}`] = body;
+ sent.push([`${method} ${route}`, body]);
if (route === "/api/cloud-send" || route === "/api/cloud-send?move=1") return reply(200, summary(cloudItems));
const refused = refuse[`${method} ${route}`] ?? refuse[route];
if (refused) return reply(refused.status, { error: refused.error });
@@ -103,6 +105,7 @@ function fakeSides({ cloudItems = [], localItems = [], refuse = {} } = {}) {
return {
calls,
bodies,
+ sent,
deps: {
local: async (route) => {
if (route === "/api/cloud-send" || route === "/api/cloud-send?move=1") return localJson(summary(localItems));
@@ -421,8 +424,23 @@ test("1Password items with bots of their own keep them on My Cloud; a Cloud too
vaults.deps.handoff = async () => ({ onePassword: { token: "ops_fake", grants: { v1: [] } } });
assert.equal((await createCloudSend(vaults.deps).send()).result.failed.length, 1);
assert.ok(vaults.calls.includes("DELETE /api/logins/1password"), "a vault choice that did not take closes the token too");
- // Item choices are in place there before the token is.
+ // This computer's item choices are in place there before the token is, leaving every other item there as it was; the
+ // whole map (handing back what was handed back here) goes only after the token and the vaults took.
+ const puts = sides.sent.filter(([call]) => call === "PUT /api/logins/1password/items").map(([, body]) => body);
+ assert.deepEqual(puts, [{ items: { "v1:i1": "all" }, keep: true }, { items: { "v1:i1": "all" } }]);
assert.ok(sides.calls.indexOf("PUT /api/logins/1password/items") < sides.calls.indexOf("POST /api/logins/1password"));
+ assert.ok(sides.calls.lastIndexOf("PUT /api/logins/1password/items") > sides.calls.indexOf("PATCH /api/logins/1password/vaults/v1"));
+ // A token My Cloud did not take never hands an item back to a vault whose new choice did not arrive either.
+ const token = fakeSides({ localItems: [onePassword], refuse: { "POST /api/logins/1password": { status: 400, error: "bad token" } } });
+ token.deps.handoff = async () => ({ onePassword: { token: "ops_fake", grants: { v1: [] }, itemGrants: { "v1:i2": [] } } });
+ assert.equal((await createCloudSend(token.deps).send()).result.failed.length, 1);
+ assert.deepEqual(token.sent.filter(([call]) => call === "PUT /api/logins/1password/items").map(([, body]) => body), [{ items: { "v1:i2": [] }, keep: true }]);
+ assert.ok(!token.calls.includes("PATCH /api/logins/1password/vaults/v1"));
+ // Nor does a vault choice that did not take: the token this send connected is taken off again.
+ const late = fakeSides({ localItems: [onePassword], refuse: { "/api/logins/1password/vaults/v1": { status: 500, error: "boom" } } });
+ late.deps.handoff = async () => ({ onePassword: { token: "ops_fake", grants: { v1: [] }, itemGrants: {} } });
+ assert.equal((await createCloudSend(late.deps).send()).result.failed.length, 1);
+ assert.deepEqual(late.sent.filter(([call]) => call === "PUT /api/logins/1password/items").map(([, body]) => body), [{ items: {}, keep: true }]);
const stuck = fakeSides({ localItems: [onePassword], refuse: { "/api/logins/1password/vaults/v1": { status: 500, error: "boom" }, "DELETE /api/logins/1password": { status: 500, error: "down" } } });
stuck.deps.handoff = async () => ({ onePassword: { token: "ops_fake", grants: { v1: [] } } });
const left = (await createCloudSend(stuck.deps).send()).result.failed;
diff --git a/server/bot-logins.ts b/server/bot-logins.ts
index 29fbc2c827..7228b1c5a3 100644
--- a/server/bot-logins.ts
+++ b/server/bot-logins.ts
@@ -184,7 +184,7 @@ export class BotLogins {
return true;
},
// Kept whole, even for an item not loaded here yet: it then opens no wider than the computer that chose it.
- setItemGrants: (grants) => this.store.setOnePasswordItemGrants(grants),
+ setItemGrants: (grants, keep) => this.store.setOnePasswordItemGrants(keep ? { ...this.store.onePasswordItemGrants(), ...grants } : grants),
},
};
}
diff --git a/server/routes/logins.test.ts b/server/routes/logins.test.ts
index 82bd87fc12..74117e6e73 100644
--- a/server/routes/logins.test.ts
+++ b/server/routes/logins.test.ts
@@ -184,6 +184,9 @@ describe("Settings → Logins routes", () => {
expect((await call("PUT", "/api/logins/1password/items", { items: { "v1/i1": [] } })).status).toBe(400);
expect((await call("PUT", "/api/logins/1password/items", { items: [] })).status).toBe(400);
expect(store.onePasswordItemGrants()).toEqual({ "v1:i9": [], "v2:x1": ["quill"] });
+ // keep: only the items named change; every other one stays as it was.
+ expect((await call("PUT", "/api/logins/1password/items", { items: { "v1:i9": ["quill"] }, keep: true })).status).toBe(200);
+ expect(store.onePasswordItemGrants()).toEqual({ "v1:i9": ["quill"], "v2:x1": ["quill"] });
expect((await call("PUT", "/api/logins/1password/items", { items: {} })).status).toBe(200);
expect(store.onePasswordItemGrants()).toEqual({});
});
diff --git a/server/routes/logins.ts b/server/routes/logins.ts
index bcff78fc1f..9070a4f1c1 100644
--- a/server/routes/logins.ts
+++ b/server/routes/logins.ts
@@ -21,8 +21,9 @@ export interface LoginRouteDeps {
/** Bots for one item (`<vault id>:<item id>`) ahead of its vault's; null hands it back. False for an item the token does not
* read, null when 1Password could not be read just now. */
grantItem(item: string, bots: LoginBots | null): Promise<boolean | null>;
- /** Every item's own bots at once (what My Cloud keeps of another computer's choices); an item left out goes back to its vault's. */
- setItemGrants(grants: Record<string, LoginBots>): void;
+ /** Every item's own bots at once (what My Cloud keeps of another computer's choices); an item left out goes back to its vault's,
+ * unless `keep` leaves the items not named as they are. */
+ setItemGrants(grants: Record<string, LoginBots>, keep?: boolean): void;
};
}
@@ -130,7 +131,7 @@ export function createLoginRoutes(deps: LoginRouteDeps): RouteHandler {
if (!ITEM_KEY.test(key) || !bots.ok || bots.value === undefined) return json(res, 400, { error: bots.ok ? LOGIN_ERRORS.bots : bots.error });
grants[key] = bots.value;
}
- deps.onePassword.setItemGrants(grants);
+ deps.onePassword.setItemGrants(grants, body.keep === true);
return json(res, 200, { items: Object.keys(grants).length });
}
if (path === ONE_PASSWORD || path.startsWith(`${ONE_PASSWORD}/`)) return json(res, 405, { error: "method not allowed" });