Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(docker): re-own only wrong-owned paths under /app/data

Yudhi Armyndharis4e53d8918822
diff --git a/Dockerfile b/Dockerfile
index 9847c5a56..48e3585e0 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -262,9 +262,9 @@ COPY --from=builder /app/dashboard/dist ./dashboard/dist
# Create data directories with correct ownership. Only ./data is chowned, NOT all of /app: the app
# tree (node_modules, dist) only needs read access, which root-owned files already grant, and the
-# entrypoint re-chowns /app/data at every container start for the mounted-volume case. A full
-# /app chown walks every production dependency file (issue #1045: ~35 minutes on a small VPS) and
-# duplicates their metadata into a new image layer.
+# entrypoint re-owns any wrong-owned path under /app/data at every start for the mounted-volume
+# case. A full /app chown walks every production dependency file (issue #1045: ~35 minutes on a
+# small VPS) and duplicates their metadata into a new image layer.
RUN mkdir -p ./data/sessions ./data/media ./data/plugins && \
chown -R openwa:openwa ./data
diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh
index e1bf6dac2..8322336f8 100644
--- a/docker-entrypoint.sh
+++ b/docker-entrypoint.sh
@@ -11,13 +11,18 @@ mkdir -p /app/data/sessions /app/data/media /app/data/plugins
# so clearing them lets sessions re-launch after a crash/restart. (#259)
rm -f /app/data/sessions/*/Singleton* 2>/dev/null || true
-# Re-own everything under /app/data except symlinks. Some bind mounts (Docker Desktop's file
-# sharing) refuse to change a symlink's owner, which a link never needs, so a `chown -R` that met
-# any link (a Chromium lock under a custom SESSION_DATA_PATH, a relocated session profile, a backup
-# staging copy) failed under `set -e` and crash-looped the container on every start (#1722).
-# Neither find nor `chown -h` follows a link, so a path swapped for a link between the walk and
-# the chown never re-owns the link's target. A real chown failure still stops the script.
-find /app/data ! -type l -exec chown -h openwa:openwa {} +
+# Re-own every path under /app/data that is not already openwa:openwa, except symlinks. Some bind
+# mounts (Docker Desktop's file sharing) refuse to change a symlink's owner, which a link never
+# needs, so a `chown -R` that met any link (a Chromium lock under a custom SESSION_DATA_PATH, a
+# relocated session profile, a backup staging copy) failed under `set -e` and crash-looped the
+# container on every start (#1722). Neither find nor `chown -h` follows a link, so a path swapped
+# for a link between the walk and the chown never re-owns the link's target. The ownership test
+# keeps a steady-state start to one stat per path: a chown is a metadata write even when the owner
+# is unchanged, which on a large session or media volume (or NFS) held boot for minutes. The walk
+# itself stays, because a file restored or copied in as root must still be re-owned. The
+# parentheses matter: without them `-o` would let the group branch bypass `! -type l`. A real
+# chown failure still stops the script.
+find /app/data ! -type l \( ! -user openwa -o ! -group openwa \) -exec chown -h openwa:openwa {} +
# Chromium resolves its home from the passwd entry (no /home/openwa exists), so it hard-crashes at
# launch unless its config/cache dirs exist and are writable. XDG_CONFIG_HOME/XDG_CACHE_HOME (set in
diff --git a/scripts/smoke-test-non-root.sh b/scripts/smoke-test-non-root.sh
index 2baffbed9..f1e60910d 100755
--- a/scripts/smoke-test-non-root.sh
+++ b/scripts/smoke-test-non-root.sh
@@ -21,8 +21,13 @@ else
echo "==> Using pre-built image: $IMAGE_TAG"
fi
+VOL=""
+
# Only remove an image this script created — never one the caller handed us.
cleanup() {
+ if [ -n "$VOL" ]; then
+ docker volume rm -f "$VOL" > /dev/null 2>&1 || true
+ fi
if [ "$BUILT_HERE" -eq 1 ]; then
docker rmi "$IMAGE_TAG" > /dev/null 2>&1 || true
fi
@@ -61,6 +66,35 @@ else
exit 1
fi
+echo ""
+echo "==> Checking /app/data ownership on start..."
+VOL="openwa-smoke-data-$$"
+docker volume create "$VOL" > /dev/null
+# Seed as root, bypassing the entrypoint: a file restored as root, and a link to a root-owned file.
+docker run --rm --entrypoint sh -v "$VOL":/app/data "$IMAGE_TAG" -c \
+ 'mkdir -p /app/data/sessions/s1 && touch /app/data/sessions/s1/restored && ln -s /etc/passwd /app/data/link'
+OWNERS=$(docker run --rm -v "$VOL":/app/data "$IMAGE_TAG" stat -c '%U:%G' /app/data/sessions/s1/restored /etc/passwd | tr '\n' ' ')
+echo " restored file, link target: $OWNERS"
+if [ "$OWNERS" = "openwa:openwa root:root " ]; then
+ echo "PASS: a root-owned file is re-owned and a symlink target is left alone"
+else
+ echo "FAIL: expected 'openwa:openwa root:root', got '$OWNERS'" >&2
+ cleanup
+ exit 1
+fi
+# A chown is a metadata write even when the owner is unchanged, so it moves ctime. A start that
+# re-owns an already-owned file pays that write per file, which is minutes on a large volume.
+CTIME_BEFORE=$(docker run --rm --entrypoint stat -v "$VOL":/app/data "$IMAGE_TAG" -c %z /app/data/sessions/s1/restored)
+docker run --rm -v "$VOL":/app/data "$IMAGE_TAG" true
+CTIME_AFTER=$(docker run --rm --entrypoint stat -v "$VOL":/app/data "$IMAGE_TAG" -c %z /app/data/sessions/s1/restored)
+if [ "$CTIME_BEFORE" = "$CTIME_AFTER" ]; then
+ echo "PASS: a start leaves already-owned files untouched"
+else
+ echo "FAIL: a start re-owned an already-owned file (ctime $CTIME_BEFORE -> $CTIME_AFTER)" >&2
+ cleanup
+ exit 1
+fi
+
cleanup
echo ""
echo "All smoke tests passed!"
diff --git a/src/common/ci-privilege-gate.spec.ts b/src/common/ci-privilege-gate.spec.ts
index a6b0c09b4..189b7b365 100644
--- a/src/common/ci-privilege-gate.spec.ts
+++ b/src/common/ci-privilege-gate.spec.ts
@@ -95,11 +95,16 @@ describe('the non-root drop is enforced, not merely documented', () => {
// sharing) failed a recursive chown under `set -e` and crash-looped the container (#1722), and a
// lock cleanup can only cover the default path. The ownership fix itself has to skip links.
// `-h` too: find tests the type before the batched chown runs, so without it a path replaced by a
- // link in between would have root re-own the link's target.
- it('re-owns /app/data without touching symlinks', () => {
+ // link in between would have root re-own the link's target. Only wrong-owned paths are chowned: a
+ // chown is a metadata write even when nothing changes, so re-owning the whole volume on every start
+ // held boot for minutes on a large one. The ownership test is parenthesised so `! -type l` still
+ // governs both of its branches.
+ it('re-owns only wrong-owned paths under /app/data, never symlinks', () => {
const entrypoint = fs.readFileSync(path.join(__dirname, '..', '..', 'docker-entrypoint.sh'), 'utf8');
const cleanup = entrypoint.search(/^rm -f \/app\/data\/sessions\/\*\/Singleton\*/m);
- const chown = entrypoint.search(/^find \/app\/data ! -type l -exec chown -h openwa:openwa \{\} \+$/m);
+ const chown = entrypoint.search(
+ /^find \/app\/data ! -type l \\\( ! -user openwa -o ! -group openwa \\\) -exec chown -h openwa:openwa \{\} \+$/m,
+ );
expect(entrypoint).not.toMatch(/^\s*chown\s+-R\b.*\/app\/data/m);
// Swallowing the failure would hide a real refusal (NFS root_squash, SELinux).
expect(entrypoint).not.toMatch(/-exec chown[^\n]*\|\|/);