From 26ac64bdb2206f21eb25d4570c5667ba833a1fd4 Mon Sep 17 00:00:00 2001 From: Derek Hsu Date: Thu, 3 Dec 2026 00:41:28 +0800 Subject: [PATCH] fix(hermes-01x): keep cron/ usable by the gateway in the tools image A root build step in Dockerfile.tools ran Hermes on /sandbox/.hermes without HERMES_CONTAINER=1. Hermes' ensure_hermes_home() then chmodded /sandbox/.hermes and cron/ to 0700, so cron/ became 0700 gateway:sandbox and the Hermes gateway, which runs as user sandbox, could not use cron. Set HERMES_CONTAINER=1 on that step, as the base image's cron probes do. Record every directory under /sandbox/.hermes before the first Hermes process and fail the build if any owner, group, or mode differs after the last one, and check the base values for /sandbox/.hermes (3770) and cron/ (2770). check-image.sh now compares those directories between the base and tools images in hermes mode. Fixes #90 Co-Authored-By: Claude Opus 5.5 Signed-off-by: Derek Hsu --- agents/hermes-01x/Dockerfile.tools | 22 ++- ci/source-shape-test-budget.json | 10 ++ .../2026-11-17-hermes-tools-image.md | 10 +- test/hermes-01x-image.test.ts | 134 ++++++++++++++++++ .../fixtures/check-image.sh | 16 +++ 5 files changed, 190 insertions(+), 2 deletions(-) diff --git a/agents/hermes-01x/Dockerfile.tools b/agents/hermes-01x/Dockerfile.tools index 45ddc35053f..53f31d6c900 100644 --- a/agents/hermes-01x/Dockerfile.tools +++ b/agents/hermes-01x/Dockerfile.tools @@ -231,6 +231,22 @@ RUN set -eu; \ 'export CHROMIUM_FLAGS="$CHROMIUM_FLAGS --no-sandbox --no-zygote --disable-gpu"' \ >/etc/chromium.d/nemoclaw-openshell; \ chmod 0644 /etc/chromium.d/nemoclaw-openshell; \ + # Hermes' ensure_hermes_home() chmods HERMES_HOME and cron/ to 0700 unless + # it detects a container, and a build step does not match its markers. + # The root Hermes process that reads /sandbox/.hermes therefore sets + # HERMES_CONTAINER=1, as the base image's cron probes do. Without it, + # cron/ became 0700 gateway:sandbox, and the Hermes gateway, which runs as + # user sandbox, could not use cron (#90). Record every directory under + # /sandbox/.hermes before the first Hermes process and compare after the + # last one, so a changed owner or mode fails the build. + hermes_dirs() { \ + find /sandbox/.hermes -xdev -type d -printf '%p %u:%g %m\n' >"$1.raw"; \ + LC_ALL=C sort "$1.raw" >"$1"; \ + rm "$1.raw"; \ + }; \ + test "$(stat -c '%U:%G %a' /sandbox/.hermes)" = "sandbox:sandbox 3770"; \ + test "$(stat -c '%U:%G %a' /sandbox/.hermes/cron)" = "gateway:sandbox 2770"; \ + hermes_dirs /tmp/nemoclaw-tools/hermes-dirs.before; \ # Keep Hermes on its built-in browser tools instead of the Browser Use CLI # that `uvx browser-use` fetches from PyPI (configure-tools-browser.py # explains why). The managed policy gets the same value, or the dashboard @@ -258,7 +274,7 @@ RUN set -eu; \ # does not compile them on every start. /opt/hermes/.venv/bin/python -I -m py_compile \ /opt/hermes/tools/browser_use_cli.py /opt/hermes/hermes_cli/config_defaults.py; \ - ( cd /opt/hermes && HERMES_HOME=/sandbox/.hermes /opt/hermes/.venv/bin/python -I -c \ + ( cd /opt/hermes && HERMES_CONTAINER=1 HERMES_HOME=/sandbox/.hermes /opt/hermes/.venv/bin/python -I -c \ 'import sys; sys.path.insert(0, "/opt/hermes"); from tools.browser_use_cli import get_browser_backend, is_browser_use_cli_mode; assert get_browser_backend() == "off" and not is_browser_use_cli_mode()' ); \ mcp_digest="$(/opt/hermes/.venv/bin/python -I /usr/local/lib/nemoclaw/build-hermes-mcp-digest.py --guard /usr/local/lib/nemoclaw/hermes-runtime-config-guard.py --config "$config")"; \ test "$mcp_line" = "# nemoclaw-hermes-mcp-state-v1 intended=$mcp_digest applied=$mcp_digest"; \ @@ -295,6 +311,10 @@ RUN set -eu; \ ( cd /opt/hermes && HOME="$profile_root" HERMES_HOME="$profile_home" /opt/hermes/.venv/bin/python -I -c \ 'import sys; sys.path.insert(0, "/opt/hermes"); from tools.browser_use_cli import get_browser_backend, is_browser_use_cli_mode; assert get_browser_backend() == "off" and not is_browser_use_cli_mode()' ); \ rm -rf "$profile_root"; \ + test "$(stat -c '%U:%G %a' /sandbox/.hermes)" = "sandbox:sandbox 3770"; \ + test "$(stat -c '%U:%G %a' /sandbox/.hermes/cron)" = "gateway:sandbox 2770"; \ + hermes_dirs /tmp/nemoclaw-tools/hermes-dirs.after; \ + diff -u /tmp/nemoclaw-tools/hermes-dirs.before /tmp/nemoclaw-tools/hermes-dirs.after; \ rm -rf /tmp/nemoclaw-tools /root/.npm /root/.cache /root/.local/share/pnpm # Same user name as the base image configuration. diff --git a/ci/source-shape-test-budget.json b/ci/source-shape-test-budget.json index d403879941d..abc477a90d3 100644 --- a/ci/source-shape-test-budget.json +++ b/ci/source-shape-test-budget.json @@ -246,6 +246,16 @@ "test": "is the manifest the 0.1.x transaction and image load", "category": "security" }, + { + "file": "test/hermes-01x-image.test.ts", + "test": "sets HERMES_CONTAINER=1 on every line that sets HERMES_HOME=/sandbox/.hermes", + "category": "security" + }, + { + "file": "test/hermes-01x-image.test.ts", + "test": "checks cron/ and every Hermes home directory after the last Hermes process", + "category": "security" + }, { "file": "test/hermes-final-image-layout.test.ts", "test": "keeps security entrypoint hashes synchronized with the copied files", diff --git a/docs/modification/2026-11-17-hermes-tools-image.md b/docs/modification/2026-11-17-hermes-tools-image.md index 9d201f8edd6..7cdcf843bc7 100644 --- a/docs/modification/2026-11-17-hermes-tools-image.md +++ b/docs/modification/2026-11-17-hermes-tools-image.md @@ -91,6 +91,14 @@ Dashboard 使用自己的 profile `dashboard-home`。sandbox 每次啟動時,`st 建置步驟在執行腳本前以 `sha256sum -c` 比對這兩個檔案與 `Dockerfile.tools` 記錄的 hash(`BASE_IMAGE` 中這兩個檔案的 hash),不符時建置失敗;執行後以 `py_compile` 重新產生這兩個 module 的 bytecode。Dashboard policy 檢查之後,建置步驟建立具名 profile `nemoclaw-policy-probe`,確認它沒有 `config.yaml`,接著執行 base 映像的 `profile-policy` 檢查(每個受管路徑,包含 `browser.backend`),並確認 `get_browser_backend()` 為 `off`、不是 Browser Use CLI 模式。 +建置步驟以 root 執行 Hermes 程序時,Hermes 的 `ensure_hermes_home()` 會把 `HERMES_HOME` 與 `cron/` 改成 `0700`,除非它判斷自己在容器內。建置步驟不符合它的判斷條件,所以讀 `/sandbox/.hermes` 的步驟設定 `HERMES_CONTAINER=1`,與 base 映像的 cron 檢查相同。`openshell01x-hermes-0.21.3-2026.11.25-tools.1` 沒有這項設定:`cron/` 變成 `0700 gateway:sandbox`,以 sandbox 使用者執行的 Hermes gateway 無法使用 cron(#90)。建置步驟檢查: + +- 第一個 Hermes 程序之前與最後一個之後,`/sandbox/.hermes` 下每個目錄的 owner、group 與 mode 相同。 +- `/sandbox/.hermes` 是 `sandbox:sandbox 3770`。 +- `cron/` 是 `gateway:sandbox 2770`。 + +任一項不符時建置失敗。 + Hermes Agent v0.21.3 以本機 Chromium 執行時不做私有位址檢查(`tools/browser_tool.py` 的 `_url_policy_error`),所以 OpenShell policy DNS 回傳的 `198.18.0.0/15` 合成 IP 不會被擋;雲端 metadata 位址(例如 `169.254.169.254`)仍一律拒絕。這點與 OpenClaw 不同,不需要設定 SSRF 例外。 要讓瀏覽器開網站,sandbox 的 network policy(由 ClawShell 管理)需要一條規則: @@ -110,7 +118,7 @@ Hermes Agent v0.21.3 以本機 Chromium 執行時不做私有位址檢查(`tools | 步驟 | 內容 | |------|------| -| `check-image.sh ... hermes` | 執行設定、`hermes --version` 與 base 相同;新增的 setuid、setgid 或 file capability 檔案必須列在 `tools/openclaw-tools-image/privileged-files-added.txt` | +| `check-image.sh ... hermes` | 執行設定、`hermes --version` 與 base 相同;新增的 setuid、setgid 或 file capability 檔案必須列在 `tools/openclaw-tools-image/privileged-files-added.txt`;`/sandbox/.hermes` 下每個目錄的 owner、group 與 mode 與 base 相同,tools layer 也不新增目錄 | | `check-tools-start.sh` | base 與 tools 映像以相同限制啟動(無網路、`--cap-drop ALL`、`no-new-privileges`),容器狀態、啟動輸出、`/tmp/nemoclaw-start.log` 與 process 清單必須相同 | | `run-fixtures.sh ... hermes` | 以 sandbox 使用者、唯讀 root、無 capability、無網路執行共用 fixture;OpenClaw 專屬的 `himalaya_skill_override` 與 `browser_config` 記為 `SKIP`,`himalaya_journey` 略過 skill 檢查。`hermes_browser_backend` 確認 `browser.backend` 為 `off`、設定 hash 相符、Hermes 提供內建 browser 工具;`hermes_dashboard_seed` 以映像內的 policy 與 `config.yaml` 執行 Dashboard 設定同步,產生的設定要有模型,`browser.backend` 為 `off`;`agent_browser` 不帶任何 agent-browser 設定開頁,並確認 Chromium 帶著 `/etc/chromium.d` 的參數執行;`hermes_profile_browser` 建立沒有 `config.yaml` 的具名 profile,並把記錄呼叫的 `uvx` 放在 `PATH` 最前面,確認 `browser.backend` 為 `off`、不是 Browser Use CLI 模式、browser 工具組有 `browser_navigate` 與 `browser_snapshot` 而沒有 `browser_exec`,再以 `browser_navigate` 開啟本機檔案頁面並檢查 snapshot;`uvx` 被呼叫時失敗 | | Trivy | tools layer 不得新增可修正的 CRITICAL 或 HIGH 弱點 | diff --git a/test/hermes-01x-image.test.ts b/test/hermes-01x-image.test.ts index 82345e00246..eb7290ccb9b 100644 --- a/test/hermes-01x-image.test.ts +++ b/test/hermes-01x-image.test.ts @@ -1235,3 +1235,137 @@ describe("patch-agent-home-state-symlink.py", () => { expect(changed.result.stderr).toContain("source shape changed"); }); }); + +// ── Hermes home permissions in the tools image (#90) ──────────────────── +// Hermes' ensure_hermes_home() chmods HERMES_HOME and cron/ to 0700 unless it +// detects a container. A root build step that ran Hermes on /sandbox/.hermes +// without HERMES_CONTAINER=1 left cron/ 0700 gateway:sandbox, so the Hermes +// gateway, which runs as user sandbox, could not use cron. +describe("Hermes tools image keeps the base image's Hermes home permissions (#90)", () => { + const TOOLS_DOCKERFILE = path.join(AGENT, "Dockerfile.tools"); + const CHECK_IMAGE = path.resolve( + import.meta.dirname, + "../tools/openclaw-tools-image/fixtures/check-image.sh", + ); + const HERMES_HOME_RUN = "HERMES_HOME=/sandbox/.hermes"; + + // source-shape-contract: security -- Hermes chmods its home and cron/ to 0700 outside a container, which locks the sandbox-run gateway out of cron + it("sets HERMES_CONTAINER=1 on every line that sets HERMES_HOME=/sandbox/.hermes", () => { + const tools = fs.readFileSync(TOOLS_DOCKERFILE, "utf8"); + const lines = tools.split("\n").filter((line) => line.includes(HERMES_HOME_RUN)); + expect(lines.length).toBeGreaterThan(0); + for (const line of lines) expect(line, line.trim()).toContain("HERMES_CONTAINER=1"); + }); + + // source-shape-contract: security -- The build must fail when the tools layer changes the owner or mode the base image gives cron/ and the other Hermes home directories + it("checks cron/ and every Hermes home directory after the last Hermes process", () => { + const tools = fs.readFileSync(TOOLS_DOCKERFILE, "utf8"); + const base = fs.readFileSync(path.join(AGENT, "Dockerfile"), "utf8"); + const baseCron = base.match(/check_metadata \/sandbox\/\.hermes\/cron '([^']+)'/); + expect(baseCron, "base image cron/ metadata check").not.toBeNull(); + const lastHermes = tools.lastIndexOf(HERMES_HOME_RUN); + const cronCheck = tools.lastIndexOf( + `test "$(stat -c '%U:%G %a' /sandbox/.hermes/cron)" = "${baseCron![1]}"`, + ); + const dirsCheck = tools.lastIndexOf( + "diff -u /tmp/nemoclaw-tools/hermes-dirs.before /tmp/nemoclaw-tools/hermes-dirs.after", + ); + expect(cronCheck).toBeGreaterThan(lastHermes); + expect(dirsCheck).toBeGreaterThan(lastHermes); + expect(tools.indexOf("hermes_dirs /tmp/nemoclaw-tools/hermes-dirs.before")).toBeLessThan( + tools.indexOf(HERMES_HOME_RUN), + ); + }); + + describe("check-image.sh", () => { + const BASE = "registry.example/hermes@sha256:" + "a".repeat(64); + const TOOLS = "nemoclaw-hermes-tools:ci"; + const HERMES_DIRS = [ + "/sandbox/.hermes sandbox:sandbox 3770", + "/sandbox/.hermes/cron gateway:sandbox 2770", + "/sandbox/.hermes/sessions sandbox:sandbox 770", + ]; + // Answers only the docker commands check-image.sh runs. + const FAKE_DOCKER = `#!/usr/bin/env bash +set -euo pipefail +image_after_entrypoint() { + while [ "$#" -gt 0 ]; do + if [ "$1" = --entrypoint ]; then echo "$3"; return; fi + shift + done +} +case "$1" in + buildx) echo '{"manifests":[{"platform":{"os":"linux","architecture":"amd64"}},{"platform":{"os":"linux","architecture":"arm64"}}]}' ;; + pull) ;; + image) + if [ "$2" = inspect ] && [ "$3" = -f ]; then echo 100; else echo '[{"Config":{"User":"sandbox"}}]'; fi ;; + run) + image=$(image_after_entrypoint "$@") + case "$*" in + *"--entrypoint hermes"*) echo "Hermes Agent v0.21.3" ;; + *"--entrypoint /usr/bin/find"*) + if [ "$image" = "$FAKE_TOOLS" ] && [ -n "\${FAKE_TOOLS_HERMES_DIRS:-}" ]; then + printf '%s\\n' "$FAKE_TOOLS_HERMES_DIRS" + else + printf '%s\\n' "$FAKE_BASE_HERMES_DIRS" + fi ;; + *"--entrypoint dpkg-query"*) printf 'bash\\t5.2\\n' ;; + *) ;; + esac ;; + *) echo "unexpected docker command: $*" >&2; exit 64 ;; +esac +`; + let dir: string; + beforeEach(() => { + dir = fs.mkdtempSync(path.join(os.tmpdir(), "check-image-")); + fs.mkdirSync(path.join(dir, "bin")); + fs.writeFileSync(path.join(dir, "bin", "docker"), FAKE_DOCKER, { mode: 0o755 }); + }); + afterEach(() => fs.rmSync(dir, { recursive: true, force: true })); + + function checkImage(toolsDirs?: string[]) { + const env: Record = { + ...process.env, + PATH: `${path.join(dir, "bin")}:${process.env.PATH}`, + FAKE_TOOLS: TOOLS, + FAKE_BASE_HERMES_DIRS: [...HERMES_DIRS].reverse().join("\n"), + FAKE_TOOLS_HERMES_DIRS: toolsDirs?.join("\n"), + }; + return spawnSync("bash", [CHECK_IMAGE, BASE, TOOLS, path.join(dir, "out"), "hermes"], { + encoding: "utf8", + env, + }); + } + + it("passes when the tools image keeps every Hermes home directory", () => { + const result = checkImage(HERMES_DIRS); + expect(result.status, result.stderr).toBe(0); + expect(fs.readFileSync(path.join(dir, "out", "tools-hermes-dirs.txt"), "utf8")).toBe( + `${HERMES_DIRS.join("\n")}\n`, + ); + }); + + it("fails when the tools image makes cron/ 0700", () => { + const result = checkImage( + HERMES_DIRS.map((line) => + line.startsWith("/sandbox/.hermes/cron ") + ? "/sandbox/.hermes/cron gateway:sandbox 700" + : line, + ), + ); + expect(result.status).not.toBe(0); + expect(result.stdout).toContain("+/sandbox/.hermes/cron gateway:sandbox 700"); + expect(result.stderr).toContain( + "FAIL: tools layer changes directories under /sandbox/.hermes", + ); + }); + + it("fails when the tools image adds a directory under /sandbox/.hermes", () => { + const result = checkImage([...HERMES_DIRS, "/sandbox/.hermes/backups root:root 755"]); + expect(result.status).not.toBe(0); + expect(result.stderr).toContain( + "FAIL: tools layer changes directories under /sandbox/.hermes", + ); + }); + }); +}); diff --git a/tools/openclaw-tools-image/fixtures/check-image.sh b/tools/openclaw-tools-image/fixtures/check-image.sh index 484d742d49c..112ddafe9f6 100755 --- a/tools/openclaw-tools-image/fixtures/check-image.sh +++ b/tools/openclaw-tools-image/fixtures/check-image.sh @@ -96,6 +96,22 @@ unexpected=$(LC_ALL=C comm -23 "$out/added-privileged.txt" "$out/allowed-privile test -z "$unexpected" || fail "tools layer adds privileged files not in privileged-files-added.txt: $unexpected" +# The Hermes gateway runs as sandbox and reaches cron/ and other state through +# group sandbox. Every directory under /sandbox/.hermes must keep the base +# image's owner, group, and mode, and the tools layer adds none (#90). +if [ "$agent" = hermes ]; then + hermes_dirs() { + docker run --rm --network none --user root --entrypoint /usr/bin/find "$1" \ + /sandbox/.hermes -xdev -type d -printf '%p %u:%g %m\n' >"$2.raw" + LC_ALL=C sort "$2.raw" >"$2" + rm "$2.raw" + } + hermes_dirs "$base" "$out/base-hermes-dirs.txt" + hermes_dirs "$tools" "$out/tools-hermes-dirs.txt" + diff -u "$out/base-hermes-dirs.txt" "$out/tools-hermes-dirs.txt" \ + || fail "tools layer changes directories under /sandbox/.hermes" +fi + base_size=$(docker image inspect -f '{{.Size}}' "$base") tools_size=$(docker image inspect -f '{{.Size}}' "$tools") jq -n --argjson base "$base_size" --argjson tools "$tools_size" \