Skip to content

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

Saved logins: change the site of an existing login

TuftyBruno3e330b24d393
diff --git a/server/routes/logins.test.ts b/server/routes/logins.test.ts
index 74117e6e73..0fd6950708 100644
--- a/server/routes/logins.test.ts
+++ b/server/routes/logins.test.ts
@@ -90,6 +90,20 @@ describe("Settings → Logins routes", () => {
expect((await call("PATCH", `/api/logins/${id}`, { exactHost: "yes" })).body).toEqual({ error: LOGIN_ERRORS.exactHost });
expect(store.get(id)?.exactHost).toBe(true);
+ expect((await call("PATCH", `/api/logins/${id}`, { site: "https://d1abc.cloudfront.net/sign-in" })).body.login)
+ .toMatchObject({ site: "d1abc.cloudfront.net", hosts: ["d1abc.cloudfront.net"], exactHost: true, username: "[email protected]" });
+ expect(store.get(id)?.password).toBe(NEW_PASSWORD);
+ expect(store.usableFor("d1abc.cloudfront.net", "pepper").map((login) => login.id)).toEqual([id]);
+ expect(store.usableFor("accounts.example.com", "pepper")).toEqual([]);
+ expect((await call("PATCH", `/api/logins/${id}`, { site: "javascript:alert(1)" })).body).toEqual({ error: LOGIN_ERRORS.site });
+ expect(store.get(id)?.site).toBe("d1abc.cloudfront.net");
+ expect((await call("PATCH", `/api/logins/${id}`, { site: " " })).body).toEqual({ error: LOGIN_ERRORS.site });
+
+ const other = (await call("POST", "/api/logins", { site: "portal.example.edu", username: "[email protected]", password: PASSWORD })).body.login.id;
+ expect(await call("PATCH", `/api/logins/${id}`, { site: "https://portal.example.edu/login" })).toMatchObject({ status: 409, body: { error: LOGIN_ERRORS.taken } });
+ expect(store.get(id)?.site).toBe("d1abc.cloudfront.net");
+ expect(await call("DELETE", `/api/logins/${other}`)).toMatchObject({ status: 200 });
+
expect(await call("DELETE", `/api/logins/${id}`)).toMatchObject({ status: 200, body: { ok: true } });
expect((await call("GET", "/api/logins")).body.logins).toEqual([]);
expect((await call("DELETE", `/api/logins/${id}`)).status).toBe(404);
diff --git a/server/routes/logins.ts b/server/routes/logins.ts
index 9070a4f1c1..efd3f52168 100644
--- a/server/routes/logins.ts
+++ b/server/routes/logins.ts
@@ -7,7 +7,7 @@ import { PASS, type RouteHandler } from "./table.ts";
export interface LoginRouteDeps {
list(): LoginSummary[];
create(input: NewLogin): LoginSummary;
- update(id: string, patch: LoginPatch): LoginSummary | null;
+ update(id: string, patch: LoginPatch): LoginSummary | null | "taken";
remove(id: string): boolean;
botExists(id: string): boolean;
onePassword: {
@@ -46,6 +46,7 @@ export const LOGIN_ERRORS = {
notServiceAccount: "That is not a 1Password service account token: those start with ops_. Make one in 1Password under Developer → Service accounts. A Connect token, an account password or a Secret Key does not work here.",
exactHost: "exactHost must be true or false.",
readOnly: "This login is in 1Password. Change or remove it there.",
+ taken: "A login with this username is already saved for that site. Change or delete that one instead.",
} as const;
type Parsed<T> = { ok: true; value: T } | { ok: false; error: string };
@@ -169,12 +170,16 @@ export function createLoginRoutes(deps: LoginRouteDeps): RouteHandler {
const bots = parseBots(body.bots, deps.botExists);
if (!bots.ok) return json(res, 400, { error: bots.error });
if (body.exactHost !== undefined && typeof body.exactHost !== "boolean") return json(res, 400, { error: LOGIN_ERRORS.exactHost });
+ const site = body.site === undefined ? undefined : normalizeLoginHost(body.site);
+ if (site === null) return json(res, 400, { error: LOGIN_ERRORS.site });
const login = deps.update(id, {
+ ...(site === undefined ? {} : { site }),
...fields.value,
...(totp.value === undefined ? {} : { totp: totp.value }),
...(bots.value === undefined ? {} : { bots: bots.value }),
...(body.exactHost === undefined ? {} : { exactHost: body.exactHost }),
});
+ if (login === "taken") return json(res, 409, { error: LOGIN_ERRORS.taken });
return login ? json(res, 200, { login }) : json(res, 404, { error: "no such login" });
};
}
diff --git a/server/vault-store.test.ts b/server/vault-store.test.ts
index a16d043759..8cfc60def9 100644
--- a/server/vault-store.test.ts
+++ b/server/vault-store.test.ts
@@ -141,6 +141,8 @@ describe("the saved logins store", () => {
expect(store.remove(login.id)).toBe(true);
expect(store.secretValues()).toEqual([]);
expect(store.update("missing", { password: "x-x-x-x" })).toBeNull();
+ const wide = store.create({ ...loginPlace("example.com", ["example.com", "login.microsoftonline.com"]), username: "wide", password: "fake-wide-pass", source: "settings", bots: "all" });
+ expect(store.update(wide.id, { site: "login.microsoftonline.com", password: "fake-wide-two" })).toMatchObject({ site: "example.com", hosts: ["example.com", "login.microsoftonline.com"] });
});
it("keeps the 1Password token in the same sealed file, and which bots each vault is for across reconnects", () => {
diff --git a/server/vault-store.ts b/server/vault-store.ts
index ec340cadf1..21ea56f0d7 100644
--- a/server/vault-store.ts
+++ b/server/vault-store.ts
@@ -8,7 +8,7 @@ import { join } from "node:path";
import { z } from "zod";
import { HELD_SECRET_MASK } from "../shared/redact.ts";
-import { LOGIN_FIELD_MAX, LOGIN_HOSTS_MAX, loginMatchesHost, loginSite, loginUsableBy, type LoginBots, type LoginPlace, type LoginSummary } from "../shared/logins.ts";
+import { LOGIN_FIELD_MAX, LOGIN_HOSTS_MAX, loginMatchesHost, loginPlace, loginSite, loginUsableBy, type LoginBots, type LoginPlace, type LoginSummary } from "../shared/logins.ts";
import { SECRET_VALUE_MAX_BYTES, VAULT_SECRET_NAME_MAX, vaultNameKey, type VaultSecret } from "../shared/vault.ts";
import { writeFileAtomic } from "./atomic.ts";
import { decryptBrowserState, encryptBrowserState } from "./browser-sites.ts";
@@ -127,6 +127,8 @@ export interface NewLogin extends LoginPlace {
}
export interface LoginPatch {
+ /** A new host for the login: its site becomes that host's, and its sign-in hosts just that host. */
+ site?: string;
username?: string;
password?: string;
totp?: TotpKey | null;
@@ -290,12 +292,15 @@ export class VaultStore {
return summarizeLogin(login);
}
- update(id: string, patch: LoginPatch): LoginSummary | null {
+ /** "taken" when the login would move onto a site where the same account is already saved. */
+ update(id: string, patch: LoginPatch): LoginSummary | null | "taken" {
const data = this.read();
const current = data.logins.find((login) => login.id === id);
if (!current) return null;
+ const moved = patch.site !== undefined && !current.hosts.includes(patch.site);
const next: StoredLogin = {
...current,
+ ...(moved ? loginPlace(patch.site!, [], current.exactHost) : {}),
...(patch.username === undefined ? {} : { username: patch.username }),
...(patch.password === undefined ? {} : { password: patch.password }),
...(patch.bots === undefined ? {} : { bots: patch.bots }),
@@ -304,6 +309,7 @@ export class VaultStore {
};
if (patch.totp === null) delete next.totp;
else if (patch.totp) next.totp = patch.totp;
+ if (moved && data.logins.some((login) => login.id !== id && sameAccount(login, next))) return "taken";
this.write({ ...data, logins: data.logins.map((login) => login.id === id ? next : login) });
return summarizeLogin(next);
}
diff --git a/src/components/VaultSettings.test.ts b/src/components/VaultSettings.test.ts
index 5fb6a3311b..453c667cf5 100644
--- a/src/components/VaultSettings.test.ts
+++ b/src/components/VaultSettings.test.ts
@@ -127,6 +127,25 @@ describe("Settings → Vault: logins", () => {
expect(fixture.api).toHaveBeenNthCalledWith(2, "/api/logins/l1", { method: "PATCH", body: JSON.stringify({ bots: ["pepper"], exactHost: true }) });
});
+ it("moves a login to another site, and sends the site only when it changed", async () => {
+ fixture.api.mockResolvedValueOnce({ logins: [SHARED_LOGIN], onePassword: OFF }).mockResolvedValueOnce({ login: SHARED_LOGIN }).mockResolvedValueOnce({ logins: [SHARED_LOGIN], onePassword: OFF });
+ await render();
+ await click("Change the login for example.org");
+ expect(host.querySelector<HTMLInputElement>('input[name="site"]')!.value).toBe("crm.example.org");
+ await type("site", "d1abc.cloudfront.net");
+ await submit();
+ expect(fixture.api).toHaveBeenNthCalledWith(2, "/api/logins/l2", { method: "PATCH", body: JSON.stringify({ site: "d1abc.cloudfront.net", bots: "all" }) });
+ });
+
+ it("keeps every sign-in host when the site is only written differently", async () => {
+ fixture.api.mockResolvedValueOnce({ logins: [CARD_LOGIN], onePassword: OFF }).mockResolvedValueOnce({ login: CARD_LOGIN }).mockResolvedValueOnce({ logins: [CARD_LOGIN], onePassword: OFF });
+ await render();
+ await click("Change the login for example.com");
+ await type("site", "https://EXAMPLE.com/login");
+ await submit();
+ expect(fixture.api).toHaveBeenNthCalledWith(2, "/api/logins/l1", { method: "PATCH", body: JSON.stringify({ bots: ["pepper"] }) });
+ });
+
it("deletes only on a second click", async () => {
fixture.api.mockResolvedValueOnce({ logins: [CARD_LOGIN], onePassword: OFF }).mockResolvedValueOnce({ ok: true }).mockResolvedValueOnce({ logins: [], onePassword: OFF });
await render();
diff --git a/src/components/VaultSettings.tsx b/src/components/VaultSettings.tsx
index ccb1a88773..65762ba1dd 100644
--- a/src/components/VaultSettings.tsx
+++ b/src/components/VaultSettings.tsx
@@ -1,7 +1,7 @@
import { useEffect, useState, type FormEvent } from "react";
import { ChevronDown, KeyRound, Loader2, LockKeyhole, ShieldCheck, Trash2 } from "lucide-react";
-import { loginHeldBackFor, type LoginBots, type LoginSummary, type OnePasswordItemRef, type OnePasswordStatus, type OnePasswordVault } from "../../shared/logins";
+import { loginHeldBackFor, normalizeLoginHost, type LoginBots, type LoginSummary, type OnePasswordItemRef, type OnePasswordStatus, type OnePasswordVault } from "../../shared/logins";
import type { LinkedSecret, VaultSecret } from "../../shared/vault";
import { t } from "@/lib/i18n";
import { productName } from "@/lib/brand";
@@ -62,6 +62,7 @@ export function VaultSettings() {
const [editing, setEditing] = useState<string | null>(null);
const [editBots, setEditBots] = useState<LoginBots>("all");
const [editExact, setEditExact] = useState(false);
+ const [editSite, setEditSite] = useState("");
const [removeTotp, setRemoveTotp] = useState(false);
const [deleting, setDeleting] = useState<string | null>(null);
const [busy, setBusy] = useState(false);
@@ -93,7 +94,7 @@ export function VaultSettings() {
setAdding(null);
setDeleting(null);
setRemoveTotp(false);
- if (login) { setEditBots(login.bots); setEditExact(login.exactHost); }
+ if (login) { setEditBots(login.bots); setEditExact(login.exactHost); setEditSite(login.hosts[0] ?? login.site); }
setEditing(id);
};
const startAdding = (kind: Adding) => {
@@ -169,10 +170,11 @@ export function VaultSettings() {
{editing === login.id && login.onePassword && itemEditor(login.onePassword, login.bots)}
{editing === login.id && !login.onePassword && (
<div className="ms-6 rounded-lg bg-inset/60 p-3">
- <LoginForm username={false} replacing submitLabel={t("settings.logins.saveChange")} autoFocus
+ <LoginForm site={{ value: editSite, onChange: setEditSite }} username={false} replacing submitLabel={t("settings.logins.saveChange")} autoFocus
onCancel={() => setEditing(null)}
onSubmit={async (fields) => {
- await changeLogin(login.id, { password: fields.password, totp: fields.totp, removeTotp, bots: editBots, ...(editExact === login.exactHost ? {} : { exactHost: editExact }) });
+ const host = normalizeLoginHost(editSite);
+ await changeLogin(login.id, { ...(host && login.hosts.includes(host) ? {} : { site: editSite }), password: fields.password, totp: fields.totp, removeTotp, bots: editBots, ...(editExact === login.exactHost ? {} : { exactHost: editExact }) });
setEditing(null);
await load();
}}>
diff --git a/src/lib/logins.ts b/src/lib/logins.ts
index f10366bff2..e1e49348f4 100644
--- a/src/lib/logins.ts
+++ b/src/lib/logins.ts
@@ -33,10 +33,11 @@ export async function addLogin(site: string, fields: LoginFieldsInput, bots: Log
})).login;
}
-export async function changeLogin(id: string, change: { password?: string; totp?: string; removeTotp?: boolean; bots?: LoginBots; exactHost?: boolean }): Promise<LoginSummary> {
+export async function changeLogin(id: string, change: { site?: string; password?: string; totp?: string; removeTotp?: boolean; bots?: LoginBots; exactHost?: boolean }): Promise<LoginSummary> {
return (await api<{ login: LoginSummary }>(`/api/logins/${encodeURIComponent(id)}`, {
method: "PATCH",
body: JSON.stringify({
+ ...(change.site === undefined ? {} : { site: change.site }),
...(change.password ? { password: change.password } : {}),
...(change.removeTotp ? { totp: null } : change.totp?.trim() ? { totp: change.totp } : {}),
...(change.bots ? { bots: change.bots } : {}),