Skip to content

Build your personal knowledge base with Trilium Notes

client/db backup: fix some issues

Adorian Doran7b6adb5ef5e5
diff --git a/apps/client/src/services/options.spec.ts b/apps/client/src/services/options.spec.ts
index 790f0c7f12..7bd09cfa1f 100644
--- a/apps/client/src/services/options.spec.ts
+++ b/apps/client/src/services/options.spec.ts
@@ -94,6 +94,16 @@ describe("Options service", () => {
expect(put).toHaveBeenCalledWith("options", { str: "saved" });
});
+ it("save() puts the previous value back when the server refuses it", async () => {
+ options.load({ str: "before" });
+ server.put = vi.fn(async () => { throw new Error("HTTP 500"); }) as typeof server.put;
+
+ await expect(options.save(k("str"), "after")).rejects.toThrow("HTTP 500");
+
+ // Reported by whoever asks next, so a failed save cannot pass for a stored one.
+ expect(options.get(k("str"))).toBe("before");
+ });
+
it("saveMany() PUTs the whole record verbatim", async () => {
const put = (server.put = vi.fn(async () => ({})) as typeof server.put);
const payload = { a: "1", b: "2" } as unknown as Record<OptionNames, string>;
diff --git a/apps/client/src/services/options.ts b/apps/client/src/services/options.ts
index b91fab8d8b..8b752d2575 100644
--- a/apps/client/src/services/options.ts
+++ b/apps/client/src/services/options.ts
@@ -72,12 +72,21 @@ class Options {
}
async save(key: OptionNames, value: OptionValue) {
+ const previous = this.arr?.[key];
this.set(key, value);
const payload: Record<string, OptionValue> = {};
payload[key] = value;
- await server.put(`options`, payload);
+ try {
+ await server.put(`options`, payload);
+ } catch (e) {
+ // Set ahead of the request so a read straight after the call sees the new value, but put
+ // back when the server refuses it: a cache that keeps reporting a value which was never
+ // stored makes the failure invisible to whoever asks afterwards.
+ this.set(key, previous as OptionValue);
+ throw e;
+ }
}
/**
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 191b9e5240..d60bb0e209 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -2087,6 +2087,7 @@
"password_stored": "The backup password has been saved.",
"password_not_stored": "The backup password could not be saved.",
"encryption_not_disabled": "The backup password was removed, but encryption could not be switched off. Backups will not be encrypted until you turn encryption on again.",
+ "encryption_not_enabled": "The backup password was saved, but encryption could not be switched on. Backups will not be encrypted until you switch it on.",
"compressed": "Compressed",
"encrypted": "Encrypted"
},
diff --git a/apps/client/src/widgets/type_widgets/options/backup.tsx b/apps/client/src/widgets/type_widgets/options/backup.tsx
index 5d5f9c47cc..4d161c4210 100644
--- a/apps/client/src/widgets/type_widgets/options/backup.tsx
+++ b/apps/client/src/widgets/type_widgets/options/backup.tsx
@@ -182,6 +182,14 @@ export function BackupOptions() {
await refreshPassphrase();
await setEncryptionEnabled(true);
setPasswordModalShown(false);
+
+ // The password is stored either way, but saying it is encrypted when the option never
+ // reached the server would send the next backup out in the clear under that belief.
+ if (!options.is("backupEnableEncryption")) {
+ toast.showError(t("backup.encryption_not_enabled"));
+ return;
+ }
+
toast.showMessage(t("backup.password_stored"));
}