Skip to content

Build your personal knowledge base with Trilium Notes

fix(server): replace healthcheck files left by another user

Elian Doran83c9b9787a99
diff --git a/apps/server/scripts/check-docker-non-root.sh b/apps/server/scripts/check-docker-non-root.sh
index bfea4536b0..72025bf58e 100644
--- a/apps/server/scripts/check-docker-non-root.sh
+++ b/apps/server/scripts/check-docker-non-root.sh
@@ -35,6 +35,9 @@ start() {
owner="${3:-}"
containers="$containers $name"
mount="type=volume,src=$name-data,dst=$data_dir"
+ # `docker volume create` reuses an existing volume, such as one an interrupted run left behind.
+ docker rm -f "$name" >/dev/null 2>&1 || true
+ docker volume rm -f "$name-data" >/dev/null
docker volume create "$name-data" >/dev/null
if [ -n "$owner" ]; then
docker run --rm --user 0:0 --entrypoint chown -v "$name-data:/data" "$image" "$owner" /data
diff --git a/apps/server/src/services/data_dir.ts b/apps/server/src/services/data_dir.ts
index e2e8b2fe6a..d4c96811a7 100644
--- a/apps/server/src/services/data_dir.ts
+++ b/apps/server/src/services/data_dir.ts
@@ -10,8 +10,6 @@ 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;
@@ -72,8 +70,6 @@ function checkExistingEntries(dataDirs: ReturnType<typeof getDataDirs>) {
[ 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 ],
diff --git a/apps/server/src/services/healthcheck.spec.ts b/apps/server/src/services/healthcheck.spec.ts
index 26d7fb7070..3c35bde2cf 100644
--- a/apps/server/src/services/healthcheck.spec.ts
+++ b/apps/server/src/services/healthcheck.spec.ts
@@ -73,6 +73,20 @@ describe("publishing the healthcheck target", () => {
expect(readTarget().socket).toBeUndefined();
});
+ it("replaces files it cannot write, as when an earlier run as another user left them", () => {
+ for (const name of [ HEALTHCHECK_URL_FILE, HEALTHCHECK_SOCKET_FILE ]) {
+ fs.writeFileSync(path.join(dataDir, name), "stale");
+ fs.chmodSync(path.join(dataDir, name), 0o444);
+ }
+
+ publishHealthcheckTarget(dataDir, "/run/trilium.sock", false);
+
+ expect(readTarget()).toEqual({
+ url: "http://localhost/api/health-check",
+ socket: "/run/trilium.sock"
+ });
+ });
+
it("writes nothing when the server reports no address", () => {
publishHealthcheckTarget(dataDir, null, false);
diff --git a/apps/server/src/services/healthcheck.ts b/apps/server/src/services/healthcheck.ts
index 09d69fd541..f7a2c1a8eb 100644
--- a/apps/server/src/services/healthcheck.ts
+++ b/apps/server/src/services/healthcheck.ts
@@ -29,16 +29,25 @@ export function publishHealthcheckTarget(
if (typeof address === "string") {
// A unix socket carries no host, so curl needs the path passed separately.
- fs.writeFileSync(urlFile, `${scheme}://localhost${PROBE_PATH}`);
- fs.writeFileSync(socketFile, address);
+ replaceFile(urlFile, `${scheme}://localhost${PROBE_PATH}`);
+ replaceFile(socketFile, address);
return;
}
- fs.writeFileSync(urlFile, `${scheme}://${probeHost(address)}:${address.port}${PROBE_PATH}`);
+ replaceFile(urlFile, `${scheme}://${probeHost(address)}:${address.port}${PROBE_PATH}`);
fs.rmSync(socketFile, { force: true });
}
+/**
+ * Removes `file` before writing it, which needs write access to the directory only. A file left
+ * by an earlier run as another user (root, or a different `--user`) cannot be written in place.
+ */
+function replaceFile(file: string, content: string) {
+ fs.rmSync(file, { force: true });
+ fs.writeFileSync(file, content);
+}
+
/** The address a probe on the same host should dial, given what the server bound. */
function probeHost({ address, family }: AddressInfo) {
if (address === "0.0.0.0") {