Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(server): stop at startup when data directory files cannot be used
Elian Dorand1b994a972b2
diff --git a/apps/server/src/services/data_dir.spec.ts b/apps/server/src/services/data_dir.spec.tsindex 989823bf3d..3346e9ce9c 100644--- a/apps/server/src/services/data_dir.spec.ts+++ b/apps/server/src/services/data_dir.spec.ts@@ -30,7 +30,7 @@ describe("data_dir.ts unit tests", () => {return {default: {accessSync: mockFn.accessSyncMock,- constants: { X_OK: 1 },+ constants: { R_OK: 4, W_OK: 2, X_OK: 1 },existsSync: mockFn.existsSyncMock,mkdirSync: mockFn.mkdirSyncMock,statSync: mockFn.statSyncMock@@ -365,6 +365,64 @@ describe("data_dir.ts unit tests", () => {expect(changeAttemptResult).toBeInstanceOf(TypeError);}});++ describe("w/ existing entries", () => {+ const W_OK = 2;+ const proc = process as NodeJS.Process & { getuid?: () => number; getgid?: () => number };+ let hadGetuid = false;++ beforeEach(() => {+ resetAllMocks();+ setMockedEnv(null);+ mockFn.pathJoinMock.mockImplementation((...parts: string[]) => parts.join("/"));+ mockFn.existsSyncMock.mockReturnValue(true);+ mockFn.statSyncMock.mockImplementation((path: string) => ({+ uid: 1000, gid: 1000, mode: path === "/data/log" ? 0o40700 : 0o100644+ }));+ vi.spyOn(console, "error").mockImplementation(() => {});+ hadGetuid = "getuid" in proc;+ proc.getuid = () => 1234;+ proc.getgid = () => 1234;+ });++ afterEach(() => {+ if (!hadGetuid) {+ delete proc.getuid;+ delete proc.getgid;+ }+ vi.restoreAllMocks();+ resetAllMocks();+ });++ it("that the user can use – returns the paths", () => {+ expect(getDataDirs("/data").DOCUMENT_PATH).toBe("/data/document.db");+ expect(mockFn.accessSyncMock).toHaveBeenCalledWith("/data/document.db", expect.any(Number));+ expect(console.error).not.toHaveBeenCalled();+ });++ it("that the user cannot write – lists them and exits", () => {+ const blocked = new Set([ "/data/document.db", "/data/log", "/data/config.ini" ]);+ mockFn.accessSyncMock.mockImplementation((path: string, mode: number) => {+ if (blocked.has(path) && (mode & W_OK)) {+ throw new Error("EACCES");+ }+ });+ const exit = vi.spyOn(process, "exit").mockImplementation(() => {+ throw new Error("process.exit");+ });++ expect(() => getDataDirs("/data")).toThrow("process.exit");+ expect(exit).toHaveBeenCalledWith(1);+ const message = String(vi.mocked(console.error).mock.calls[0][0]);+ expect(message).toContain("Trilium cannot start: the user running it cannot use these files in its data directory.");+ expect(message).toContain("Running as UID:GID 1234:1234.");+ expect(message).toContain("/data/document.db is owned by UID:GID 1000:1000 with permissions 644.");+ expect(message).toContain("/data/log is owned by UID:GID 1000:1000 with permissions 700.");+ expect(message).toContain("for example: sudo chown -R 1234:1234 /data");+ // config.ini is only read, so a denied write must not flag it.+ expect(message).not.toContain("config.ini");+ });+ });});describe("createDirIfNotExisting error handling (via getTriliumDataDir case A)", () => {diff --git a/apps/server/src/services/data_dir.ts b/apps/server/src/services/data_dir.tsindex 66f1a968b7..b49555a0fa 100644--- a/apps/server/src/services/data_dir.ts+++ b/apps/server/src/services/data_dir.ts@@ -10,6 +10,8 @@ import fs from "node:fs";import os from "node:os";import { dirname, join as pathJoin } from "node:path";+import { HEALTHCHECK_SOCKET_FILE, HEALTHCHECK_URL_FILE } from "./healthcheck.js";+const DIR_NAME = "trilium-data";const FOLDER_PERMISSIONS = 0o700;@@ -52,11 +54,72 @@ export function getDataDirs(TRILIUM_DATA_DIR: string) {} as const;createDirIfNotExisting(dataDirs.TMP_DIR);+ checkExistingEntries(dataDirs);Object.freeze(dataDirs);return dataDirs;}+/**+ * Stops startup when entries left in the data directory cannot be used, typically because+ * an earlier run as another user (root, `sudo`, a container without `--user`) owns them.+ * Otherwise SQLite opens such a database read-only and only fails at the first write.+ */+function checkExistingEntries(dataDirs: ReturnType<typeof getDataDirs>) {+ const { R_OK, W_OK, X_OK } = fs.constants;+ const inDataDir = (name: string) => pathJoin(dataDirs.TRILIUM_DATA_DIR, name);+ const required: [ path: string, mode: number ][] = [+ [ dataDirs.DOCUMENT_PATH, R_OK | W_OK ],+ [ `${dataDirs.DOCUMENT_PATH}-wal`, R_OK | W_OK ],+ [ `${dataDirs.DOCUMENT_PATH}-shm`, R_OK | W_OK ],+ [ inDataDir(HEALTHCHECK_URL_FILE), R_OK | W_OK ],+ [ inDataDir(HEALTHCHECK_SOCKET_FILE), R_OK | W_OK ],+ [ dataDirs.LOG_DIR, R_OK | W_OK | X_OK ],+ [ dataDirs.TMP_DIR, R_OK | W_OK | X_OK ],+ [ dataDirs.BACKUP_DIR, R_OK | W_OK | X_OK ],+ [ dataDirs.ANONYMIZED_DB_DIR, R_OK | W_OK | X_OK ],+ [ dataDirs.OCR_CACHE_DIR, R_OK | W_OK | X_OK ],+ [ dataDirs.CONFIG_INI_PATH, R_OK ],+ [ inDataDir("session_secret.txt"), R_OK ]+ ];++ const unusable = required.filter(([ path, mode ]) => {+ if (!fs.existsSync(path)) {+ return false;+ }+ try {+ fs.accessSync(path, mode);+ return false;+ } catch {+ return true;+ }+ });+ if (!unusable.length) {+ return;+ }++ const user = getProcessUser();+ const lines = [+ "Trilium cannot start: the user running it cannot use these files in its data directory.",+ ""+ ];+ if (user) {+ lines.push(`Running as UID:GID ${user}.`);+ }+ for (const [ path ] of unusable) {+ lines.push(`${path} ${describeOwnership(path)}.`);+ }+ lines.push("", "To fix this, give the data directory and everything in it to the user running Trilium,");+ if (user) {+ lines.push(`for example: sudo chown -R ${user} ${dataDirs.TRILIUM_DATA_DIR}`);+ }+ lines.push(+ "In Docker, run that on the host directory mounted as the data directory, or start the",+ "container without --user."+ );+ stopStartup(lines.join("\n"));+}+export function getPlatformAppDataDir(platform: ReturnType<typeof os.platform>, ENV_APPDATA_DIR: string | undefined = process.env.APPDATA) {switch (true) {case platform === "win32" && !!ENV_APPDATA_DIR:@@ -75,12 +138,11 @@ export function getPlatformAppDataDir(platform: ReturnType<typeof os.platform>,}/**- * Stops startup with an explanation of why `targetPath` cannot be created. The server prints it- * and exits, since a stack trace adds nothing. Electron shows an uncaught main-process error in a- * dialog, which is the only place a desktop user sees it, so there the explanation is thrown.+ * Stops startup with `message`. The server prints it and exits, since a stack trace adds nothing.+ * Electron shows an uncaught main-process error in a dialog, which is the only place a desktop+ * user sees it, so there the message is thrown.*/-function failWithPermissionError(targetPath: fs.PathLike, cause: unknown): never {- const message = describePermissionError(targetPath.toString());+function stopStartup(message: string, cause?: unknown): never {if (process.versions.electron) {throw new Error(message, { cause });}@@ -88,30 +150,43 @@ function failWithPermissionError(targetPath: fs.PathLike, cause: unknown): neverprocess.exit(1);}+/** Returns the process's `UID:GID`, or `undefined` on Windows, which has neither. */+function getProcessUser() {+ if (process.getuid && process.getgid) {+ return `${process.getuid()}:${process.getgid()}`;+ }+}++/** Completes a sentence about `path`: "is owned by UID:GID 1000:1000 with permissions 700". */+function describeOwnership(path: string) {+ try {+ const stats = fs.statSync(path);+ return `is owned by UID:GID ${stats.uid}:${stats.gid} with permissions ${(stats.mode & 0o777).toString(8)}`;+ } catch {+ return "cannot be inspected";+ }+}++function failWithPermissionError(targetPath: fs.PathLike, cause: unknown): never {+ stopStartup(describePermissionError(targetPath.toString()), cause);+}+function describePermissionError(path: string) {const blockedDir = findUnreachableAncestor(dirname(path));const shownDir = blockedDir ?? dirname(path);+ const user = getProcessUser();const lines = [blockedDir? `Trilium cannot start: ${path} cannot be reached.`: `Trilium cannot start: permission denied while creating ${path}.`,""];-- // process.getuid() and process.getgid() do not exist on Windows.- if (process.getuid && process.getgid) {- lines.push(`Running as UID:GID ${process.getuid()}:${process.getgid()}.`);- }-- try {- const stats = fs.statSync(shownDir);- const ownership = `owned by UID:GID ${stats.uid}:${stats.gid} with permissions ${(stats.mode & 0o777).toString(8)}`;- lines.push(blockedDir- ? `${shownDir} cannot be entered; it is ${ownership}.`- : `${shownDir} is ${ownership}.`);- } catch {- lines.push(`${shownDir} cannot be inspected.`);+ if (user) {+ lines.push(`Running as UID:GID ${user}.`);}+ lines.push(blockedDir+ ? `${shownDir} cannot be entered; it ${describeOwnership(shownDir)}.`+ : `${shownDir} ${describeOwnership(shownDir)}.`);lines.push("",