Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python

fix: don't merge default policy in jiuwenbox

zzixuan099abcce430e
diff --git a/jiuwenbox/src/jiuwenbox/bundled_configs.py b/jiuwenbox/src/jiuwenbox/bundled_configs.py
index 0fa77cd036..e577935cdb 100644
--- a/jiuwenbox/src/jiuwenbox/bundled_configs.py
+++ b/jiuwenbox/src/jiuwenbox/bundled_configs.py
@@ -30,11 +30,12 @@ def default_policy_path() -> Path:
def base_policy_path() -> Path:
- """ policy 路径 (框架 default, 随 wheel 升级只读).
+ """Bundled policy path (framework default, read-only, upgraded with the wheel).
- Windows: ``windows-policy.yaml``; 其它平台 (Linux): ``default-policy.yaml``.
- box-server 读 ``base_policy_path()`` (基底 default) + ``JIUWENBOX_POLICY_PATH``
- (用户副本 user_config) 合并, 不生成合并文件 (见 policy_merge).
+ Windows: ``windows-policy.yaml``. Other platforms: ``default-policy.yaml``.
+ On Windows, box-server merges this base with the sparse user copy at
+ ``JIUWENBOX_POLICY_PATH``. On Linux, ``JIUWENBOX_POLICY_PATH`` replaces
+ the base entirely (no merge).
"""
name = "windows-policy.yaml" if sys.platform == "win32" else "default-policy.yaml"
return configs_dir() / name
diff --git a/jiuwenbox/src/jiuwenbox/server/policy_reader.py b/jiuwenbox/src/jiuwenbox/server/policy_reader.py
index 38574b8e92..31f9efab2a 100644
--- a/jiuwenbox/src/jiuwenbox/server/policy_reader.py
+++ b/jiuwenbox/src/jiuwenbox/server/policy_reader.py
@@ -153,17 +153,18 @@ class PolicyReader:
return base_policy_path()
def load_policy(self) -> SecurityPolicy:
- """读基底 (框架 default, 打包随 wheel) + 副本 (用户 user_config) 合并.
-
- - 基底: ``base_policy_path()`` (windows-policy.yaml / default-policy.yaml),
- 随 wheel 升级, 提供 default 值; 热更新场景新字段经此生效.
- - 副本: ``self.policy_path`` (``JIUWENBOX_POLICY_PATH`` env 指向 workspace 下
- 稀疏 user_config, 只存用户可配字段, 用 policy 字段名 e.g.
- ``windows.filesystem.allow_read`` / ``windows.network.egress.allowed_domains``).
- 副本不存在 → 只读基底 (退化为无 user_config).
- - 合并: ``policy_engine.merge_policy(基底, 副本)`` — dict 深合并, list 追加去重
- (用户白名单叠加基底必需集, 不丢); 不生成合并文件 (与 jiuwenclaw config.yaml
- template+override 机制对齐, 但用 list 追加语义而非替换).
+ """Load the server root policy.
+
+ Windows: merge bundled ``windows-policy.yaml`` (base) with the sparse
+ user copy at ``self.policy_path`` (``JIUWENBOX_POLICY_PATH``). Dict
+ fields deep-merge; list fields are appended and de-duplicated so a
+ user allowlist cannot drop the base required set. Copy missing /
+ unreadable → base only.
+
+ Other platforms: ``JIUWENBOX_POLICY_PATH`` is a complete replacement
+ (e.g. ``enterprise-policy.yaml``). It is loaded as-is and is **not**
+ merged with ``default-policy.yaml``. Missing / unreadable files fall
+ back to the bundled base.
"""
base_path = base_policy_path()
try:
@@ -187,7 +188,6 @@ class PolicyReader:
):
return _resolve_tool_paths(base_policy)
- # 有副本: 合并基底 + 副本 (副本用户配置叠加基底; list 追加, dict 深合并).
try:
override_data = yaml.safe_load(read_policy_text(self.policy_path)) or {}
except (OSError, yaml.YAMLError, UnicodeDecodeError) as exc:
@@ -204,9 +204,14 @@ class PolicyReader:
if not isinstance(override_data, dict) or not override_data:
return _resolve_tool_paths(base_policy)
- return _resolve_tool_paths(
- self.policy_engine.merge_policy(base_policy, override_data)
- )
+ # 基底+副本 list 并集只服务 Windows 稀疏 user_config. Linux 上
+ # JIUWENBOX_POLICY_PATH 指向完整 policy (enterprise-policy.yaml 等),
+ # 合并会把 default-policy.yaml 的 /home 等 list 项带进沙箱.
+ if sys.platform == "win32":
+ return _resolve_tool_paths(
+ self.policy_engine.merge_policy(base_policy, override_data)
+ )
+ return _resolve_tool_paths(SecurityPolicy.model_validate(override_data))
def load_policy_from_file(self, path: Path) -> SecurityPolicy:
"""从单文件加载 (不合并, 用于 per-sandbox policy 文件)."""
diff --git a/jiuwenbox/tests/test_policy_reader.py b/jiuwenbox/tests/test_policy_reader.py
new file mode 100644
index 0000000000..64b06df3d8
--- /dev/null
+++ b/jiuwenbox/tests/test_policy_reader.py
@@ -0,0 +1,81 @@
+# Copyright (c) Huawei Technologies Co., Ltd. 2026. All rights reserved.
+"""Unit tests for PolicyReader.load_policy platform merge vs replace."""
+
+from __future__ import annotations
+
+from pathlib import Path
+
+import pytest
+import yaml
+
+from jiuwenbox.server.policy_reader import PolicyReader
+
+
+_BASE_POLICY = {
+ "version": 1,
+ "name": "bundled-base",
+ "filesystem_policy": {
+ "directories": [{"path": "/home", "permissions": "0777"}],
+ "read_write": ["/home"],
+ },
+}
+
+_OVERRIDE_POLICY = {
+ "version": 1,
+ "name": "enterprise-override",
+ "filesystem_policy": {
+ "directories": [{"path": "/tmp", "permissions": "1777"}],
+ "read_write": ["/tmp"],
+ },
+}
+
+
+def _dir_paths(policy) -> list[str]:
+ return [
+ item if isinstance(item, str) else item.path
+ for item in policy.filesystem_policy.directories
+ ]
+
+
+def policy_files(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> tuple[Path, Path]:
+ base_path = tmp_path / "default-policy.yaml"
+ override_path = tmp_path / "enterprise-policy.yaml"
+ base_path.write_text(yaml.safe_dump(_BASE_POLICY), encoding="utf-8")
+ override_path.write_text(yaml.safe_dump(_OVERRIDE_POLICY), encoding="utf-8")
+ monkeypatch.setattr(
+ "jiuwenbox.server.policy_reader.base_policy_path",
+ lambda: base_path,
+ )
+ return base_path, override_path
+
+
+def test_linux_load_policy_replaces_base(policy_files, monkeypatch: pytest.MonkeyPatch):
+ _base_path, override_path = policy_files
+ monkeypatch.setattr("jiuwenbox.server.policy_reader.sys.platform", "linux")
+
+ policy = PolicyReader(policy_path=override_path).load_policy()
+
+ assert policy.name == "enterprise-override"
+ assert _dir_paths(policy) == ["/tmp"]
+ assert policy.filesystem_policy.read_write == ["/tmp"]
+
+
+def test_windows_load_policy_merges_base(policy_files, monkeypatch: pytest.MonkeyPatch):
+ _base_path, override_path = policy_files
+ monkeypatch.setattr("jiuwenbox.server.policy_reader.sys.platform", "win32")
+
+ policy = PolicyReader(policy_path=override_path).load_policy()
+
+ assert _dir_paths(policy) == ["/home", "/tmp"]
+ assert policy.filesystem_policy.read_write == ["/home", "/tmp"]
+
+
+def test_missing_override_falls_back_to_base(policy_files, monkeypatch: pytest.MonkeyPatch):
+ base_path, _override_path = policy_files
+ monkeypatch.setattr("jiuwenbox.server.policy_reader.sys.platform", "linux")
+
+ policy = PolicyReader(policy_path=base_path.parent / "missing.yaml").load_policy()
+
+ assert policy.name == "bundled-base"
+ assert _dir_paths(policy) == ["/home"]