Skip to content

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

fix(vault): keep an unreadable saved-logins file instead of writing over it

DeryFerd82cfc6526397
diff --git a/server/vault-store.test.ts b/server/vault-store.test.ts
index 5507b3b5f9..b5ceedeefe 100644
--- a/server/vault-store.test.ts
+++ b/server/vault-store.test.ts
@@ -72,6 +72,19 @@ describe("the saved logins store", () => {
warn.mockRestore();
});
+ it("refuses to save over a file it could not open, so one unreadable key cannot erase what it holds", () => {
+ const warn = vi.spyOn(console, "warn").mockImplementation(() => undefined);
+ new VaultStore({ dataDir: dir }).create({ ...at("example.com"), username: "a", password: PASSWORD, source: "card", bots: ["b1"] });
+ const sealed = readFileSync(join(dir, LOGINS_FILE));
+ writeFileSync(join(dir, LOGINS_KEY_FILE), `${"0".repeat(64)}\n`);
+ const store = new VaultStore({ dataDir: dir });
+ expect(store.list()).toEqual([]);
+ expect(() => store.create({ ...at("other.com"), username: "b", password: PASSWORD, source: "card", bots: ["b1"] })).toThrow(/could not be opened/);
+ // The bytes on disk are untouched, so the logins are still there for the key that fits.
+ expect(readFileSync(join(dir, LOGINS_FILE))).toEqual(sealed);
+ warn.mockRestore();
+ });
+
it("gives a login saved for one host its site, so it fills on www and other subdomains, and keeps the file readable by the previous release", () => {
writeFileSync(join(dir, LOGINS_KEY_FILE), `${KEY}\n`, { mode: 0o600 });
writeFileSync(join(dir, LOGINS_FILE), encryptBrowserState(JSON.stringify({ version: 1, logins: [
diff --git a/server/vault-store.ts b/server/vault-store.ts
index bd64b90afd..f0ffc95444 100644
--- a/server/vault-store.ts
+++ b/server/vault-store.ts
@@ -192,6 +192,8 @@ export class VaultStore {
private readonly secretChanged: (id: string) => void;
private cache: StoreFile | null = null;
private unreadableWarned = false;
+ /** The file that exists but could not be opened, so a save refuses rather than writing over it. */
+ private unreadable: string | null = null;
version = 0;
/** `secretChanged`: a saved secret's value or who may use it changed, or it is gone. */
@@ -227,7 +229,8 @@ export class VaultStore {
data = { ...disk, logins: disk.logins.map(fromDisk) };
} catch {
// Never log an unreadable file's contents.
- if (!this.unreadableWarned) console.warn("saved logins: the saved logins file could not be opened; it will be replaced on the next save");
+ this.unreadable ??= LOGINS_FILE;
+ if (!this.unreadableWarned) console.warn("saved logins: the saved logins file could not be opened; nothing is written over it until it is repaired");
this.unreadableWarned = true;
data = EMPTY;
}
@@ -246,13 +249,19 @@ export class VaultStore {
if (!key) throw new Error("missing key");
return memberSecretsSchema.parse(JSON.parse(decryptBrowserState(readFileSync(this.memberFile), key))).secrets;
} catch {
- if (!this.unreadableWarned) console.warn("saved logins: the members' secrets file could not be opened; it will be replaced on the next save");
+ this.unreadable ??= MEMBER_SECRETS_FILE;
+ if (!this.unreadableWarned) console.warn("saved logins: the members' secrets file could not be opened; nothing is written over it until it is repaired");
this.unreadableWarned = true;
return [];
}
}
private write(data: StoreFile): void {
+ // Security: read() answers EMPTY for a file it could not open, so saving over it would replace every login,
+ // secret and 2FA key with whatever this one save holds. Refusing keeps the file for a person to repair.
+ if (this.unreadable) {
+ throw new Error(`The saved logins file (${this.unreadable}) exists but could not be opened, so nothing was written over it. Restore it, or move it aside, then try again.`);
+ }
const members = (data.secrets ?? []).filter((secret) => secret.person);
const team = data.secrets?.filter((secret) => !secret.person);
const parsed = fileSchema.parse({ ...data, logins: data.logins.map(toDisk), ...(team ? { secrets: team } : {}) });