From 3ceb0def62f3dcfaa90b92dc96f7a61fcb5d7903 Mon Sep 17 00:00:00 2001 From: Maciej Lisiewski Date: Sat, 14 Nov 2026 12:46:58 +0100 Subject: [PATCH] fix(docker): seed auth.json owner-only from the first instant (#155396) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(docker): seed auth.json owner-only from the first instant The HERMES_AUTH_JSON_BOOTSTRAP seed wrote the refresh-token session via a bare root-context redirect, so the file landed 0644 under the ambient umask on host-shared mounts until the chmod caught up — and that chmod was unsilenced, so under `set -eu` a failing one aborted the boot with the credential stranded world-readable (#126950). Mirror the .env seed's pattern: wrap the write in `umask 077` so the file is 0600 at the instant it exists, and downgrade the re-tightening chmod to warn-and-continue. The rebootstrap path is untouched (it edits an existing file as the hermes user, never creates one). * test(docker): exercise the auth.json seed instead of regexing it Replace the salvaged source-pattern assertions with behaviour tests that run the real seed block under set -eu and umask 022: the write alone must yield 0600 (chmod stubbed to a no-op), and a failing chmod must warn and let boot continue. Fixes #126950 * fix(docker): seed the first-boot .env owner-only too The .env copy runs under the ambient 022 umask, so it landed 0644 and then received the generated API_SERVER_KEY; only a best-effort chmod 600 later tightened it. Seed it under umask 077 like auth.json. Refs #126950 * fix(docker): don't abort boot on a failing gateway_state.json chmod The HERMES_GATEWAY_BOOTSTRAP_STATE seed ran an unguarded chmod 644 under set -eu, so a chmod failure (e.g. on a host-shared mount) killed the hook right after the seed write. Make it best-effort with a warning, matching the auth.json seed. Refs #126950 --------- Co-authored-by: liuhao1024 --- docker/stage2-hook.sh | 21 ++- .../test_stage2_hook_seed_one_symlinks.py | 145 +++++++++++++++++- 2 files changed, 160 insertions(+), 6 deletions(-) diff --git a/docker/stage2-hook.sh b/docker/stage2-hook.sh index 7b80140a8d..f164fbf532 100755 --- a/docker/stage2-hook.sh +++ b/docker/stage2-hook.sh @@ -441,7 +441,9 @@ seed_one() { fi fi } -seed_one ".env" ".env.example" +# .env receives a generated API_SERVER_KEY below: seed it owner-only so it is +# never 0644 under the ambient umask even if the later chmod 600 fails. +(umask 077 && seed_one ".env" ".env.example") seed_one "config.yaml" "cli-config.yaml.example" seed_one "SOUL.md" "docker/SOUL.md" @@ -689,9 +691,17 @@ if [ ! -f "$HERMES_HOME/auth.json" ] && [ -n "${HERMES_AUTH_JSON_BOOTSTRAP:-}" ] if refuse_symlinked_path "seed" "$HERMES_HOME/auth.json"; then : else - printf '%s' "$HERMES_AUTH_JSON_BOOTSTRAP" > "$HERMES_HOME/auth.json" + # Seed under a restrictive umask so the credential is 0600 from the + # first instant — a plain root-context redirect lands 0644 under the + # ambient umask and exposes the refresh token on host-shared mounts + # until the chmod catches up (same hazard the .env seed guards + # against above). The re-tighten is best-effort: under `set -eu` an + # aborting chmod would leave the seeded credential behind with the + # boot dead partway. + (umask 077 && printf '%s' "$HERMES_AUTH_JSON_BOOTSTRAP" > "$HERMES_HOME/auth.json") chown hermes:hermes "$HERMES_HOME/auth.json" 2>/dev/null || true - chmod 600 "$HERMES_HOME/auth.json" + chmod 600 "$HERMES_HOME/auth.json" 2>/dev/null \ + || echo "[stage2] Warning: could not tighten auth.json permissions" >&2 fi fi @@ -753,7 +763,10 @@ if [ ! -f "$HERMES_HOME/gateway_state.json" ] && \ else printf '{"gateway_state":"running"}\n' > "$HERMES_HOME/gateway_state.json" chown hermes:hermes "$HERMES_HOME/gateway_state.json" 2>/dev/null || true - chmod 644 "$HERMES_HOME/gateway_state.json" + # Best-effort like the auth.json seed: under `set -eu` a failing chmod + # on this non-secret file would otherwise abort the whole boot. + chmod 644 "$HERMES_HOME/gateway_state.json" 2>/dev/null \ + || echo "[stage2] Warning: could not set gateway_state.json permissions" >&2 fi fi diff --git a/tests/tools/test_stage2_hook_seed_one_symlinks.py b/tests/tools/test_stage2_hook_seed_one_symlinks.py index feedee33ee..ba1a6861f5 100644 --- a/tests/tools/test_stage2_hook_seed_one_symlinks.py +++ b/tests/tools/test_stage2_hook_seed_one_symlinks.py @@ -1,4 +1,10 @@ -"""Regression tests for symlink-safe Docker stage2 first-boot seeds.""" +"""Regression tests for symlink-safe Docker stage2 first-boot seeds. + +Also covers the permission posture of the credential-bearing seeds (#126950): +auth.json and .env are owner-only from the first instant, and a failing +re-tighten chmod does not abort the boot (nor does one on the +gateway_state.json seed). +""" from __future__ import annotations import re @@ -21,7 +27,7 @@ def stage2_text() -> str: def _seed_one_function(text: str) -> str: m = re.search( - r"(seed_one\(\) \{\n(?:.*\n)*?\})\nseed_one", + r"(seed_one\(\) \{\n(?:.*\n)*?\})\n(?:#.*\n)*\(?(?:umask 077 && )?seed_one", text, ) assert m, "stage2-hook.sh must define seed_one before first-boot seeds" @@ -108,3 +114,138 @@ def test_seed_one_is_quiet_for_existing_symlinked_files( assert proc.returncode == 0, proc.stderr assert outside_env.read_text() == "EXISTING=1\n" assert proc.stdout == "" + + +def _auth_seed_block(text: str) -> str: + m = re.search( + r'(?m)^if \[ ! -f "\$HERMES_HOME/auth\.json" \] && ' + r'\[ -n "\$\{HERMES_AUTH_JSON_BOOTSTRAP:-\}" \]; then\n' + r"(?:.*\n)*?^fi\n", + text, + ) + assert m, "stage2-hook.sh must keep the auth.json first-boot seed block" + return m.group(0) + + +def _run_auth_seed(stage2_text: str, tmp_path: Path, chmod_stub: str) -> tuple: + """Run the real auth.json seed block under ``set -eu`` and umask 022. + + ``chmod_stub`` replaces the chmod command so the test can observe the + mode the write itself produced (no-op) or a failing re-tighten. + """ + sh = shutil.which("sh") + if sh is None: + pytest.skip("sh not available") + home = tmp_path / "home" + home.mkdir() + script = ( + "set -eu\n" + "umask 022\n" + f'HERMES_HOME="{home}"\n' + "HERMES_AUTH_JSON_BOOTSTRAP='{\"providers\":{}}'\n" + f"{chmod_stub}\n" + f"{_path_guard_functions(stage2_text)}\n" + f"{_auth_seed_block(stage2_text)}" + "echo SEED_DONE\n" + ) + script_path = tmp_path / "harness.sh" + script_path.write_text(script) + proc = subprocess.run([sh, str(script_path)], capture_output=True, text=True, check=False) + return proc, home / "auth.json" + + +@pytest.mark.platforms("posix") +def test_auth_seed_creates_credential_owner_only(stage2_text: str, tmp_path: Path) -> None: + """The write itself must produce a 0600 file under a 022 ambient umask; + with chmod neutralised, a bare redirect would leave it 0644 (#126950).""" + proc, auth = _run_auth_seed(stage2_text, tmp_path, "chmod() { :; }") + assert proc.returncode == 0, proc.stderr + assert auth.read_text() == '{"providers":{}}' + assert auth.stat().st_mode & 0o777 == 0o600 + + +@pytest.mark.platforms("posix") +def test_auth_seed_survives_failing_chmod(stage2_text: str, tmp_path: Path) -> None: + """A failing re-tighten warns and boot continues under ``set -eu``, + with the credential already owner-only.""" + proc, auth = _run_auth_seed(stage2_text, tmp_path, "chmod() { return 1; }") + assert proc.returncode == 0, proc.stderr + assert "SEED_DONE" in proc.stdout + assert "could not tighten auth.json permissions" in proc.stderr + assert auth.stat().st_mode & 0o777 == 0o600 + + +def _env_seed_line(text: str) -> str: + m = re.search(r'(?m)^.*seed_one "\.env" "\.env\.example".*$', text) + assert m, "stage2-hook.sh must keep the .env first-boot seed" + return m.group(0) + + +@pytest.mark.platforms("posix") +def test_env_seed_is_owner_only_under_ambient_umask(stage2_text: str, tmp_path: Path) -> None: + """The first-boot .env copy (which later receives API_SERVER_KEY) must be + 0600 straight from the seed, not 0644 until a best-effort chmod (#126950).""" + sh = shutil.which("sh") + if sh is None: + pytest.skip("sh not available") + home = tmp_path / "home" + install_dir = tmp_path / "install" + home.mkdir() + install_dir.mkdir() + example = install_dir / ".env.example" + example.write_text("# example\n") + example.chmod(0o644) + script = ( + "set -eu\n" + "umask 022\n" + f'HERMES_HOME="{home}"\n' + f'INSTALL_DIR="{install_dir}"\n' + "as_hermes() { \"$@\"; }\n" + f"{_path_guard_functions(stage2_text)}\n" + f"{_seed_one_function(stage2_text)}\n" + f"{_env_seed_line(stage2_text)}\n" + ) + script_path = tmp_path / "harness.sh" + script_path.write_text(script) + proc = subprocess.run([sh, str(script_path)], capture_output=True, text=True, check=False) + assert proc.returncode == 0, proc.stderr + assert (home / ".env").read_text() == "# example\n" + assert (home / ".env").stat().st_mode & 0o777 == 0o600 + + +def _gateway_state_seed_block(text: str) -> str: + m = re.search( + r'(?m)^if \[ ! -f "\$HERMES_HOME/gateway_state\.json" \] && \\\n' + r"(?:.*\n)*?^fi\n", + text, + ) + assert m, "stage2-hook.sh must keep the gateway_state.json first-boot seed block" + return m.group(0) + + +@pytest.mark.platforms("posix") +def test_gateway_state_seed_survives_failing_chmod(stage2_text: str, tmp_path: Path) -> None: + """A failing chmod on the seeded gateway_state.json warns and boot + continues under ``set -eu`` instead of aborting the hook.""" + sh = shutil.which("sh") + if sh is None: + pytest.skip("sh not available") + home = tmp_path / "home" + home.mkdir() + script = ( + "set -eu\n" + "umask 022\n" + f'HERMES_HOME="{home}"\n' + "HERMES_GATEWAY_BOOTSTRAP_STATE=running\n" + "chmod() { return 1; }\n" + f"{_path_guard_functions(stage2_text)}\n" + f"{_gateway_state_seed_block(stage2_text)}" + "echo SEED_DONE\n" + ) + script_path = tmp_path / "harness.sh" + script_path.write_text(script) + proc = subprocess.run([sh, str(script_path)], capture_output=True, text=True, check=False) + assert proc.returncode == 0, proc.stderr + assert "SEED_DONE" in proc.stdout + assert "could not set gateway_state.json permissions" in proc.stderr + assert (home / "gateway_state.json").read_text() == '{"gateway_state":"running"}\n'