fix(start.sh): NOPASSWD precheck on root re-exec — silent fall-through

Per Opus advisor on PR #1969: the original three-guard root re-exec
(EUID==0, hermeswebui exists, sudo on PATH) would exit non-zero with
`sudo: a password is required` on host machines where the developer's
hermeswebui user doesn't have NOPASSWD configured.

Better failure mode: silent fall-through to running as root (back to
pre-PR behavior). Adds a fourth guard `sudo -n -u hermeswebui true 2>/dev/null`
that pre-flights the sudo capability without producing visible output.

Also expands the comment to clarify which guard is load-bearing on the
canonical container path (the production image doesn't ship sudo at all,
so `command -v sudo` is the silent-no-op gate there; the entrypoint
docker_init.bash never invokes start.sh in any case).

No new tests needed — existing behavioral tests already cover the
non-root + non-sudo paths, which is what runs in CI and on host.
This commit is contained in:
nesquena-hermes committed 2026-05-09 19:23:54 +00:00
1 parent 57c71e89f3
commit 1681ce567e
1 file changed
+13 -1
+13 -1
View File
@@ -9,8 +9,20 @@ set -euo pipefail
# Sourced from PR #1686 (@binhpt310) — Cluster 1 (operational hardening),
# extracted to a focused follow-up after the parent PR was deferred over a
# separate sibling-repo build-context concern unrelated to this fix.
#
# Four preconditions to fire (all must hold):
# - EUID == 0
# - hermeswebui user actually exists (id lookup)
# - sudo is on PATH (production image does not ship sudo, so this is the
# load-bearing no-op guard for the canonical container path)
# - sudo -u hermeswebui passes without prompting (NOPASSWD precheck)
# The NOPASSWD precheck via `sudo -n -u hermeswebui true` makes this a silent
# fall-through on host machines where the developer's hermeswebui user
# requires a password — better than exiting non-zero with `sudo: a password
# is required` and surprising the user who didn't ask for sudo behavior.
if [[ ${EUID:-$(id -u)} -eq 0 ]] && id hermeswebui >/dev/null 2>&1 \
&& command -v sudo >/dev/null 2>&1; then
&& command -v sudo >/dev/null 2>&1 \
&& sudo -n -u hermeswebui true 2>/dev/null; then
exec sudo -n -u hermeswebui "$0" "$@"
fi