Skip to content

Build your personal knowledge base with Trilium Notes

ci(docker): check that the images run as a non-root user

Elian Doran77627396d196
diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml
index 5a117c0f5f..68ac621810 100644
--- a/.github/workflows/dev.yml
+++ b/.github/workflows/dev.yml
@@ -633,6 +633,9 @@ jobs:
require-status: running
require-healthy: true
+ - name: Check that the image runs as a non-root user
+ run: sh apps/server/scripts/check-docker-non-root.sh ${{ env.TEST_TAG }}
+
# Print the entire log of the container thus far, regardless if the healthcheck failed or succeeded
- name: Print entire log
if: always()
diff --git a/.github/workflows/main-docker.yml b/.github/workflows/main-docker.yml
index b19e1d9579..f4b7d45285 100644
--- a/.github/workflows/main-docker.yml
+++ b/.github/workflows/main-docker.yml
@@ -89,6 +89,9 @@ jobs:
- name: Run Playwright tests
run: TRILIUM_DOCKER=1 TRILIUM_PORT=8082 pnpm --filter=server e2e
+ - name: Check that the image runs as a non-root user
+ run: sh apps/server/scripts/check-docker-non-root.sh ${{ env.TEST_TAG }}
+
- name: Upload Playwright trace
if: failure()
uses: actions/upload-artifact@v7
diff --git a/apps/server/scripts/check-docker-non-root.sh b/apps/server/scripts/check-docker-non-root.sh
new file mode 100644
index 0000000000..e06ab0ac41
--- /dev/null
+++ b/apps/server/scripts/check-docker-non-root.sh
@@ -0,0 +1,77 @@
+#!/bin/sh
+# Starts a Trilium image as a non-root user and fails unless it reports healthy.
+#
+# Usage: check-docker-non-root.sh <image>
+#
+# Every case runs hardened the way Kubernetes `restricted` pods and rootless setups do: a
+# read-only root filesystem, no capabilities and no-new-privileges. The cases differ in who
+# owns the data directory:
+# - UID 1000 (`node`) on a fresh named volume, which Docker creates from the image's
+# mount point, so the image must provide one that `node` can write to.
+# - An arbitrary UID on a volume it owns, as when matching a host user, so the path to
+# the data directory must be traversable by users other than `node`.
+
+set -eu
+
+image="${1:?usage: $0 <image>}"
+data_dir="/home/node/trilium-data"
+timeout=120
+failed=0
+containers=""
+
+cleanup() {
+ for name in $containers; do
+ docker rm -f "$name" >/dev/null 2>&1 || true
+ docker volume rm -f "$name-data" >/dev/null 2>&1 || true
+ done
+}
+trap cleanup EXIT
+
+# start <name> <uid:gid> [owner]: owner, when given, is set on the volume before the start.
+start() {
+ name="$1"
+ user="$2"
+ owner="${3:-}"
+ containers="$containers $name"
+ 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
+ fi
+ docker run -d --name "$name" \
+ --user "$user" \
+ --read-only --tmpfs /tmp \
+ --cap-drop ALL --security-opt no-new-privileges \
+ -v "$name-data:$data_dir" \
+ "$image" >/dev/null
+}
+
+# wait_healthy <name>: waits for Docker's health status and prints the logs on failure.
+wait_healthy() {
+ name="$1"
+ elapsed=0
+ while [ "$elapsed" -lt "$timeout" ]; do
+ state=$(docker inspect -f '{{.State.Status}} {{if .State.Health}}{{.State.Health.Status}}{{end}}' "$name")
+ case "$state" in
+ "running healthy") echo "ok $name"; return 0 ;;
+ "running starting") ;;
+ *) break ;;
+ esac
+ sleep 2
+ elapsed=$((elapsed + 2))
+ done
+
+ echo "FAILED $name: $state after ${elapsed}s"
+ echo "--- container log"
+ docker logs "$name" 2>&1 | tail -n 40
+ echo "--- healthcheck log"
+ docker inspect -f '{{if .State.Health}}{{range .State.Health.Log}}{{.ExitCode}}: {{.Output}}{{end}}{{end}}' "$name"
+ failed=1
+}
+
+start trilium_nonroot_node 1000:1000
+start trilium_nonroot_other 1234:1234 1234:1234
+
+wait_healthy trilium_nonroot_node
+wait_healthy trilium_nonroot_other
+
+exit "$failed"