Skip to content

Build your personal knowledge base with Trilium Notes

core/db: redact custom backup path from backend log messages

Adorian Doran23e7d15d1780
diff --git a/apps/server/src/backup_provider.spec.ts b/apps/server/src/backup_provider.spec.ts
index be799d85ea..350f213f00 100644
--- a/apps/server/src/backup_provider.spec.ts
+++ b/apps/server/src/backup_provider.spec.ts
@@ -1,5 +1,5 @@
import type { OptionNames } from "@triliumnext/commons";
-import { getSql, ws } from "@triliumnext/core";
+import { getLog, getSql, ws } from "@triliumnext/core";
import fs from "fs";
import os from "os";
import path from "path";
@@ -156,6 +156,54 @@ describe("ServerBackupService: fallback when the custom directory cannot be writ
});
});
+describe("ServerBackupService: what reaches the backend log", () => {
+ /** The log is meant to be postable for diagnostics as-is, so no backup path may appear in it. */
+ function loggedBy(method: "info" | "error") {
+ return vi.spyOn(getLog(), method).mockImplementation(() => {});
+ }
+
+ function linesFrom(spy: ReturnType<typeof loggedBy>) {
+ return spy.mock.calls.flat().join("\n");
+ }
+
+ it("names the backup relative to its directory, never the directory itself", async () => {
+ const info = loggedBy("info");
+
+ await desktopService("").backupNow("daily");
+
+ expect(linesFrom(info)).toContain(`Created backup .${path.sep}backup-daily.db`);
+ expect(linesFrom(info)).not.toContain(DEFAULT_DIR);
+ });
+
+ it("says a backup went to the custom location without saying where that is", async () => {
+ const info = loggedBy("info");
+
+ await desktopService(CUSTOM_DIR).backupNow("daily");
+
+ expect(linesFrom(info)).toContain(`Created backup .${path.sep}backup-daily.db in the custom backup location.`);
+ expect(linesFrom(info)).not.toContain(CUSTOM_DIR);
+ });
+
+ it("keeps the reason for a fallback but strips the location the error quotes back", async () => {
+ const error = loggedBy("error");
+ copyDatabase.mockRejectedValueOnce(new Error(`EACCES: permission denied, open '${CUSTOM_DIR}\\backup-daily.db'`));
+
+ await desktopService(CUSTOM_DIR).backupNow("daily");
+
+ expect(linesFrom(error)).toContain("EACCES: permission denied");
+ expect(linesFrom(error)).not.toContain(CUSTOM_DIR);
+ });
+
+ it("strips the location from the failure it propagates when there is nowhere left to fall back to", async () => {
+ copyDatabase.mockRejectedValue(new Error(`ENOSPC: no space left on device, open '${DEFAULT_DIR}\\backup-now.db'`));
+
+ const failure = await desktopService("").backupNow("now").catch((e: Error) => e.message);
+
+ expect(failure).toContain("ENOSPC: no space left on device");
+ expect(failure).not.toContain(DEFAULT_DIR);
+ });
+});
+
describe("ServerBackupService.backupNow: naming", () => {
it("rejects a name with no usable characters", async () => {
await expect(desktopService("").backupNow("../..")).rejects.toThrow("Invalid backup name");
diff --git a/apps/server/src/backup_provider.ts b/apps/server/src/backup_provider.ts
index 6846d70d0e..d97b3ab41b 100644
--- a/apps/server/src/backup_provider.ts
+++ b/apps/server/src/backup_provider.ts
@@ -1,5 +1,5 @@
import type { DatabaseBackup } from "@triliumnext/commons";
-import { BackupOptionsService, BackupService, getLog, sync_mutex as syncMutexService, ws } from "@triliumnext/core";
+import { BackupOptionsService, BackupService, utils as coreUtils, getLog, sync_mutex as syncMutexService, ws } from "@triliumnext/core";
import fs from "fs";
import { t } from "i18next";
import path from "path";
@@ -53,11 +53,12 @@ export default class ServerBackupService extends BackupService {
if (customDir) {
try {
- return await writeBackup(customDir, fileName);
+ return await writeBackup(customDir, fileName, "custom");
} catch (e) {
// A backup that cannot reach the chosen location is still worth having, so the
- // default one takes over instead of the backup being lost altogether.
- getLog().error(`Could not back up to '${customDir}', falling back to the default directory: ${e}`);
+ // default one takes over instead of the backup being lost altogether. The reason
+ // reaches the log; where it was headed reaches only the user, in the toast.
+ getLog().error(`Could not back up to the custom backup location, using the default one instead: ${e}`);
ws.sendMessageToAllClients({
type: "toast",
message: t("backup.custom_directory_unwritable", { location: customDir }),
@@ -66,7 +67,7 @@ export default class ServerBackupService extends BackupService {
}
}
- return await writeBackup(getDefaultBackupDir(), fileName);
+ return await writeBackup(getDefaultBackupDir(), fileName, "default");
});
}
@@ -147,7 +148,7 @@ function listBackupsIn(directory: string): DatabaseBackup[] {
});
}
-async function writeBackup(directory: string, fileName: string): Promise<string> {
+async function writeBackup(directory: string, fileName: string, location: "default" | "custom"): Promise<string> {
const backupFile = path.resolve(directory, fileName);
fs.mkdirSync(directory, { recursive: true, mode: 0o700 });
@@ -159,9 +160,20 @@ async function writeBackup(directory: string, fileName: string): Promise<string>
// Whatever was written before the failure is not a usable database, and would otherwise be
// listed and offered for download as if it were one.
fs.rmSync(backupFile, { force: true });
- throw e;
+ throw new Error(withoutDirectory(e, directory));
}
- getLog().info(`Created backup at ${backupFile}`);
+ getLog().info(`Created backup .${path.sep}${fileName}${location === "custom" ? " in the custom backup location." : ""}`);
return backupFile;
}
+
+/**
+ * Keeps the backup directory out of anything that reaches the log. It carries the user's name on most
+ * platforms, and the backend log is meant to be shareable for diagnostics without having to be censored
+ * first — so the reason for a failure is kept and the location filesystem errors quote back is not.
+ */
+function withoutDirectory(error: unknown, directory: string): string {
+ const message = error instanceof Error ? error.message : String(error);
+
+ return coreUtils.replaceAll(message, directory, "<backup location>");
+}