Переглянути джерело

【修复,入口】数据卷属主修正与 trustedHosts 注入优化

chown -R 仅当数据卷根目录属主非 1000:1000 时全量执行(避免含 x-cmd 工具的大卷每次重启全量递归);trustedHosts 注入条件放宽为 patch 不存在或仍为空模板(dsh 首启生成),已有内容跳过且绝不覆盖用户文件,并同步 README 双语与 .env.example 说明。
PJ568 3 місяців тому
батько
коміт
ed03519db6
5 змінених файлів з 45 додано та 12 видалено
  1. 5 2
      .env.example
  2. 4 0
      DESIGN.md
  3. 1 1
      README.md
  4. 1 1
      README.zh.md
  5. 34 8
      entrypoint.sh

+ 5 - 2
.env.example

@@ -14,9 +14,12 @@ DEEPSEEK_API_KEY=sk-xxxxxxxxxxxxxxxx
 # Host-facing port (optional, default 3080; change it if the local port is already in use)
 DSH_WEB_PORT=3080
 # 额外受信 Host(可选,逗号分隔;公网域名/自定义域名访问时填写,
+# 注入 profile 的 cordis.patch.yml。仅当该文件不存在或仍为空模板时自动注入,
+# 已维护则跳过(请直接编辑该文件)。默认靠 Caddy 改写 loopback 已覆盖常规访问)
 # Additional trusted hosts (optional, comma-separated; fill in for public/custom domain access,
-# 注入 profile 的 cordis.patch.yml。默认靠 Caddy 改写 loopback 已覆盖常规访问)
-# injected into the profile's cordis.patch.yml. The Caddy loopback rewrite covers regular access by default)
+# injected into the profile's cordis.patch.yml. Auto-injected only when the file is absent or still
+# the empty template; user-maintained files are skipped (edit the file directly).
+# The Caddy loopback rewrite covers regular access by default)
 #DSH_TRUSTED_HOSTS=dsh.example.com,example.org
 # dsh 版本(构建期,可选;默认 0.1.0-rc.6,改后需 --build 重建镜像)
 # dsh version (build-time, optional; default 0.1.0-rc.6; rebuild with --build after changing)

+ 4 - 0
DESIGN.md

@@ -216,4 +216,8 @@ docker compose up -d --build  # 或 docker compose up -d(用 GHCR 镜像时)
 | 数据卷布局 | bind mount 从 /home/node/.dsh 扩为整个 /home/node | x-cmd 安装于 $HOME/.x-cmd.root(硬编码 HOME 路径),需要 HOME 整体持久化;.dsh 数据兼容保留 |
 | defaults 内容 | 增加 AGENTS.md(容器环境指引,dsh 会话自动加载) | agent 需要知道容器环境限制与 x-cmd 用法 |
 | 上游版本手动升级(改 ARG 后 --build) | 新增 `upstream-check.yml` 定时任务(每日 UTC 03:00 + workflow_dispatch)与 `scripts/`(check-upstream / should-upgrade / upgrade-dsh / smoke-test / open-issue / check-issue-gate / close-stale-issues):检测 `@deepseek-ai/dsh` dist-tags.latest 更新后自动提升 Dockerfile/compose/README 双语中的版本号,构建冒烟测试(镜像构建、dsh --version 校验、basic auth 401/200、双进程 RUNNING)通过则推送 master(触发 GHCR 自动构建),失败则创建 Issue(同版本去重);存在同版本未关闭失败 Issue 时定时任务跳过自动升级(npm 包版本不可变,重试必失败;仅 schedule 生效,手动触发绕过供人工重试环境性失败),升级成功后自动关闭过时失败 Issue | 需求:dsh 上游自动跟进;失败不推送、master 保持已验收状态;冒烟跳过 x-cmd 首启下载(预置假 x);DESIGN.md 历史内容不做自动替换 |
+| supervisord.conf | 无 `[rpcinterface:supervisor]` 段 | 补齐该段 | 缺段时 `docker exec supervisorctl status` 报 "did not recognize the supervisor namespace commands"(socket 通但 RPC 命名空间未注册),冒烟测试进程检查实际从未通过 |
+| 冒烟测试范围 | 仅 basic auth 401/200 + 双进程 RUNNING | 新增特权 API fence 断言(伪造 Host 直连容器内 dsh 的 `POST /api` 应 403;经 Caddy 伪造 Host/Origin 应被改写为 loopback、返回 400/404 而非 403)+ trustedHosts 注入断言;进程检查改为轮询等待(dsh 首启初始化慢于 Caddy) | 安全关键路径需回归:实测 dsh 特权 RPC 端点为 `POST /api`(GET 404,坏 payload 400/404),fence 即 DESIGN §2.1 所述 loopback 来源检查 |
+| chown 属主修正 | 每次启动无条件 `chown -R node:node /home/node` | 仅当根目录属主非 1000:1000 时全量修正,否则跳过 | 数据卷含 x-cmd 工具(数万文件),全量递归拖慢每次重启 |
+| trustedHosts 注入条件 | 仅当 `cordis.patch.yml` 不存在时注入 | 不存在**或为空模板**(顶层数组 `[]`,即 dsh 首启生成)时注入;已有内容/不可解析时跳过并提示 | dsh 首启自动生成空模板 patch(initProfile 对已存在文件不覆盖),用户首启未设变量、后设变量重启需能自动注入;已维护的 patch 绝不覆盖 |
 

+ 1 - 1
README.md

@@ -42,7 +42,7 @@ After startup, open `http://<host>:3080` in a browser (the port is controlled by
 | `DSH_AUTH_PASSWORD` | Yes | None | Basic Auth plaintext password (bcrypt hash generated automatically at container startup) |
 | `DEEPSEEK_API_KEY` | Yes | None | DeepSeek API Key (referenced by the provider via apiKeyEnv) |
 | `DSH_WEB_PORT` | No | `3080` | Host port exposed to the outside (change it when it conflicts with an existing service) |
-| `DSH_TRUSTED_HOSTS` | No | Empty | Comma-separated extra trusted hosts, injected into the profile's `cordis.patch.yml`; by default Caddy rewrites Host/Origin to loopback, which covers normal access |
+| `DSH_TRUSTED_HOSTS` | No | Empty | Comma-separated extra trusted hosts, injected into the profile's `cordis.patch.yml` (only when the file is absent or still the empty template; user-maintained files are skipped — edit the file directly); by default Caddy rewrites Host/Origin to loopback, which covers normal access |
 | `DSH_VERSION` | No (build-time) | `0.1.0-rc.6` | dsh version; rebuild with `--build` after changing it |
 
 > `.env` contains passwords and the API Key — never commit it to the repository.

+ 1 - 1
README.zh.md

@@ -42,7 +42,7 @@ docker build --build-arg DSH_VERSION=0.1.0-rc.6 -t dsh-web:latest .
 | `DSH_AUTH_PASSWORD` | 是 | 无 | basic auth 明文密码(容器启动时自动生成 bcrypt 哈希) |
 | `DEEPSEEK_API_KEY` | 是 | 无 | DeepSeek API Key(provider 经 apiKeyEnv 引用) |
 | `DSH_WEB_PORT` | 否 | `3080` | 宿主机对外端口(与已有服务冲突时修改) |
-| `DSH_TRUSTED_HOSTS` | 否 | 空 | 逗号分隔的额外受信 Host,注入 profile 的 `cordis.patch.yml`;默认靠 Caddy 改写 Host/Origin 为 loopback 已覆盖常规访问 |
+| `DSH_TRUSTED_HOSTS` | 否 | 空 | 逗号分隔的额外受信 Host,注入 profile 的 `cordis.patch.yml`(仅当该文件不存在或仍为空模板时自动注入;已维护则跳过,请直接编辑该文件);默认靠 Caddy 改写 Host/Origin 为 loopback 已覆盖常规访问 |
 | `DSH_VERSION` | 否(构建期) | `0.1.0-rc.6` | dsh 版本,修改后需 `--build` 重建 |
 
 > `.env` 含密码与 API Key,禁止提交入库。

+ 34 - 8
entrypoint.sh

@@ -43,17 +43,36 @@ if [ ! -f "$DHS_HOME/AGENTS.md" ]; then
   log "已初始化 AGENTS.md(容器环境指引)"
 fi
 
-# 2. trustedHosts 注入 cordis.patch.yml(仅当文件不存在;已存在则视为用户维护)
-#    profile 其余文件(package.json/pnpm-workspace.yaml)由 dsh 首启自动补全
+# 2. trustedHosts 注入 cordis.patch.yml
+#    profile 其余文件(package.json/pnpm-workspace.yaml)由 dsh 首启自动补全,
+#    且 dsh 的 initProfile 对已存在文件绝不覆盖("Existing files are never touched"),
+#    故此处预写安全。
+#    注入条件:patch 不存在,或为 dsh 首启生成的空模板(顶层数组 [])。
+#    已有任何条目(用户维护)或不可解析时跳过——绝不覆盖用户文件。
 #
-# 2. Inject trustedHosts into cordis.patch.yml (only when absent; existing files are treated as user-maintained)
-#    The rest of the profile (package.json/pnpm-workspace.yaml) is auto-completed by dsh on first run
+# 2. Inject trustedHosts into cordis.patch.yml
+#    The rest of the profile (package.json/pnpm-workspace.yaml) is auto-completed by dsh on first run,
+#    and dsh's initProfile never touches existing files ("Existing files are never touched"),
+#    so pre-writing here is safe.
+#    Injection happens when the patch is absent, or when it is the empty template dsh generates
+#    on first run (top-level array []). Any existing entries (user-maintained) or an unparseable
+#    file cause a skip — user files are never overwritten.
 if [ -n "${DSH_TRUSTED_HOSTS:-}" ]; then
   PROFILE_DIR="$DHS_HOME/profiles/web"
   PATCH="$PROFILE_DIR/cordis.patch.yml"
-  if [ ! -f "$PATCH" ]; then
+  INJECT=1
+  if [ -f "$PATCH" ]; then
+    PATCH_LEN="$(yq '. | length' "$PATCH" 2>/dev/null || true)"
+    if [ "$PATCH_LEN" != "0" ]; then
+      INJECT=0
+      log "cordis.patch.yml 已有内容,跳过 trustedHosts 注入(用户维护;若需变更请直接编辑该文件)"
+    fi
+  fi
+  if [ "$INJECT" = 1 ]; then
     mkdir -p "$PROFILE_DIR"
     {
+      echo "# 由 dsh-web entrypoint 自动注入(DSH_TRUSTED_HOSTS)"
+      echo "# Auto-injected by the dsh-web entrypoint (DSH_TRUSTED_HOSTS)"
       echo "- id: connection"
       echo "  config:"
       echo "    trustedHosts:"
@@ -63,15 +82,22 @@ if [ -n "${DSH_TRUSTED_HOSTS:-}" ]; then
       done
     } > "$PATCH"
     log "已注入 trustedHosts: $DSH_TRUSTED_HOSTS"
-  else
-    log "cordis.patch.yml 已存在,跳过 trustedHosts 注入(用户维护)"
   fi
 fi
 
 # 3. 修正数据卷属主(bind mount 挂整个 /home/node,首启属主可能是 root)
+#    仅当根目录属主非 1000 时全量修正;后续启动跳过——数据卷含 x-cmd 工具,
+#    全量递归 chown 在大卷上会拖慢启动
 #
 # 3. Fix data volume ownership (the whole /home/node is bind-mounted; owner may be root on first run)
-chown -R node:node /home/node
+#    Only fix recursively when the root owner is not 1000; later starts skip it —
+#    the volume holds x-cmd tools and a full recursive chown slows startup on large volumes
+if [ "$(stat -c '%u:%g' /home/node 2>/dev/null || echo '0:0')" != "1000:1000" ]; then
+  chown -R node:node /home/node
+  log "已修正数据卷属主(uid:gid → 1000:1000)"
+else
+  log "数据卷属主正确,跳过 chown"
+fi
 # ---- x-cmd:agent 自行配置运行环境的工具(幂等安装 + 系统 PATH 接入)----
 # agent 的 bash 工具环境是白名单(PATH 固定为 /usr/local/sbin:/usr/local/bin:/usr/bin,无 HOME),
 # 因此:1) 生成 HOME 注入的 x wrapper;2) 把已 use 包的 shim 软链进 /usr/local/bin。