Skip to content

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

Vault: an unreadable file is read again on the next call, and a use never refuses

milind-soni62da583f141c
diff --git a/server/vault-store.test.ts b/server/vault-store.test.ts
index b5ceedeefe..24f5a09019 100644
--- a/server/vault-store.test.ts
+++ b/server/vault-store.test.ts
@@ -76,12 +76,19 @@ describe("the saved logins store", () => {
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));
+ const rightKey = readFileSync(join(dir, LOGINS_KEY_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);
+ // A use is only bookkeeping, so it never throws while the file can't be opened.
+ expect(() => store.markUsed("anything")).not.toThrow();
+ // Once the right key is back, the same store reads the file again and saves on top of what it holds.
+ writeFileSync(join(dir, LOGINS_KEY_FILE), rightKey);
+ store.create({ ...at("other.com"), username: "b", password: PASSWORD, source: "card", bots: ["b1"] });
+ expect(store.list().map((login) => login.username).sort()).toEqual(["a", "b"]);
warn.mockRestore();
});
diff --git a/server/vault-store.ts b/server/vault-store.ts
index f0ffc95444..afe54c2474 100644
--- a/server/vault-store.ts
+++ b/server/vault-store.ts
@@ -220,6 +220,7 @@ export class VaultStore {
private read(): StoreFile {
if (this.cache) return this.cache;
+ this.unreadable = null;
let data: StoreFile = EMPTY;
if (existsSync(this.file)) {
const key = this.key(false);
@@ -229,7 +230,7 @@ export class VaultStore {
data = { ...disk, logins: disk.logins.map(fromDisk) };
} catch {
// Never log an unreadable file's contents.
- this.unreadable ??= LOGINS_FILE;
+ 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;
@@ -237,6 +238,8 @@ export class VaultStore {
}
const members = this.readMembers();
if (members.length) data = { ...data, secrets: [...data.secrets ?? [], ...members] };
+ // A file that could not be opened is read again next time, so a repaired or moved-aside one is picked up without a restart.
+ if (this.unreadable) return data;
this.cache = data;
this.version++;
return data;
@@ -357,6 +360,8 @@ export class VaultStore {
markUsed(id: string): void {
const data = this.read();
+ // Only bookkeeping: while a file can't be opened, a use is not worth a refused save.
+ if (this.unreadable) return;
const at = this.now();
if (data.logins.some((login) => login.id === id)) {
this.write({ ...data, logins: data.logins.map((login) => login.id === id ? { ...login, lastUsedAt: at } : login) });