Skip to content

fix(security): write tool guard rules atomically so a torn save cannot disable the guard - #2706

Closed
TrueFurina wants to merge 1 commit into
TencentCloud:developfrom
TrueFurina:fix/atomic-tool-guard-rules-write
Closed

TrueFurina wants to merge 1 commit into
TencentCloud:developfrom
TrueFurina:fix/atomic-tool-guard-rules-write

Conversation

@TrueFurina

Copy link
Copy Markdown

问题

~/.octop/security/tool_guard/dangerous_shell_commands.yaml 是用户可编辑的安全规则,读写在 src/octop/infra/agents/security/tool_guard_rules.py:

    def save_text(self, content: str) -> tuple[int, list[str]]:
        rules, errors = validate_rules_yaml(content)
        if errors:
            return 0, errors
        self.ensure_seeded()
        self.rules_file.write_text(content, encoding="utf-8")
        return len(rules), []

ensure_seeded() 与 reset_to_bundled() 同样用 write_text。这是「先截断、再写入」:磁盘满、断电、进程被杀、Windows 上杀毒/索引占用,都会把文件留在半截状态。

与其他配置文件的区别在于失败方向。运行时用 octop_harness.security.tool_guard.rule_guardian.load_rules_from_yaml 读这个文件:

        except (OSError, yaml.YAMLError) as exc:
            logger.warning("Failed to load guard rules from %s: %s", yaml_path, exc)
            return []

解析失败只写一行 warning,然后返回空规则表。也就是说一次被中断的保存不会报错给用户,而是让危险命令守卫静默失效——RuleBasedToolGuardian 拿到 0 条规则,之后 bash 等工具的危险模式匹配全部放行。更糟的是 ensure_seeded() 只在文件不存在时播种,文件存在但已损坏时不会自愈,进程重启后守卫依旧为空。

保存接口是 PUT /security/tool-guard/rules/raw(src/octop/api/routers/security.py:183-201),保存成功后立即 registry.reload_octop_harnesss(),所以损坏文件会在同一次请求里被立刻加载成「0 条规则」。

改动

  1. src/octop/infra/agents/security/tool_guard_rules.py:新增模块内私有 _write_rules_text,用同目录临时文件 + os.replace 换入;ensure_seeded / save_text / reset_to_bundled 三处写入改走它。写入或换入失败时删除临时文件并抛原异常(BaseException 一并覆盖 KeyboardInterrupt / SystemExit),旧规则保持可用。POSIX 下沿用既有文件权限,新文件按进程 umask 取 0o666 & ~umask,与 write_text 原先产生的权限一致。
    except BaseException:  # also covers KeyboardInterrupt / SystemExit
        with contextlib.suppress(OSError):
            os.unlink(tmp_name)
        raise
  1. tests/unit/test_tool_guard_rules_store.py:新增 test_failed_save_keeps_previous_rules——先播种默认规则,把 os.replace 换成抛 OSError,断言异常抛出、文件内容与播种时逐字一致、目录里没有 .tmp 残留。用例沿用同文件既有的 tmp_path fixture 风格。

验证

  • 基线(develop 原文件 + 新用例):1 failed, 3 passed,失败信息 Failed: DID NOT RAISE OSError——原实现根本不经过 os.replace,既不报错、又把旧规则覆盖掉。
  • 补丁后:同一文件 4 passed(含既有的播种、非法 YAML 拒绝、正常保存并刷新目录三个用例)。
  • 失效方向实证:把规则文件写成无法解析的内容后调用真实加载器,得到 rules_loaded=0 guardian_rule_count=0,即解析失败会静默退化为「没有规则」。
  • ruff check 与 ruff format --check(lint.isort.known-first-party=["octop"])对两个文件均通过。
  • 未运行完整测试套件:本机为干净环境,只安装了该用例所需的依赖。

限制

  • 只保证单次写入的原子性,不引入跨进程锁:并发保存仍是后写者胜,但每个读者只会看到某一份完整、可解析的规则。
  • 不修复「文件已损坏」的历史状态:ensure_seeded 的语义(只在文件缺失时播种)保持不变,损坏文件仍需要用户保存或重置一次。
  • 读侧「解析失败即空规则」的 fail-open 行为在 octop-harness 中,不在本仓库范围内,本 PR 只在写入侧消除该前提。

@jubaoliang jubaoliang closed this Nov 1, 2026
TrueFurina added a commit to TrueFurina/Octop that referenced this pull request Nov 2, 2026
- TencentCloud#2495 fix(oauth): preserve existing query params on authorization_endpoint (TencentCloud#2391)
- TencentCloud#2529 fix(execute_env): fold admin env into local_shell backend (close TencentCloud#179)
- TencentCloud#2543 [CLOSED BY AUTHOR] accidental empty blob commit - re-opening with fix
- TencentCloud#2570 fix(core): guard POSIX-only os.chmod/os.umask on Windows (TencentCloud#2370)
- TencentCloud#2571 fix(tls): guard POSIX-only os.chmod on Windows (TencentCloud#2370)
- TencentCloud#2572 fix(core): guard final POSIX-only os.chmod call sites (TencentCloud#2370)
- TencentCloud#2573 fix(experts): guard POSIX-only os.chmod in skill scripts (TencentCloud#2370)
- TencentCloud#2575 fix(agents): reuse _token_int in _usage_has_segments for safe coercion
- TencentCloud#2580 fix(shell): guard bare int() in shell_ws resize handler
- TencentCloud#2605 fix(xlsx): suppress PermissionError during soffice shim temp cleanup
- TencentCloud#2648 fix(security): prevent path-traversal in tccli OAuth helper --profile
- TencentCloud#2665 fix(octop): guard credential marker reads and backup file read with OSError
- TencentCloud#2692 fix(cli): read shell rc file as UTF-8 when installing completion
- TencentCloud#2706 fix(security): write tool guard rules atomically so a torn save cannot disable the guard
- TencentCloud#2731 fix(gateway): keep TLS verification when sending the Feishu greeting
- TencentCloud#2596 fix(clinical): avoid TOCTOU in atomic_write temp cleanup
- TencentCloud#2597 fix(service): suppress OSError in _write_managed_file finally cleanup
- TencentCloud#2599 fix(skill-manager): use unlink(missing_ok=True) to close TOCTOU in restore
- TencentCloud#2600 fix(users): add missing_ok=True to avatar unlink in delete_profile_avatar
- TencentCloud#2601 fix(api): add missing_ok=True to daily-memory delete unlink
- TencentCloud#2604 fix(agently): add missing_ok=True to confirmation-file unlinks
- TencentCloud#2606 fix(ldap): narrow _safe_unbind suppress(Exception) to LDAPException
- TencentCloud#2607 fix(skills): narrow suppress(Exception) around proc.wait to specific errors
- TencentCloud#2635 fix(cli): narrow suppress(Exception) in _close_cli_session to OSError+RuntimeError
- TencentCloud#2636 fix(feishu): narrow suppress(Exception) in creator timeout finally to OSError
- TencentCloud#2637 fix(gateway): narrow suppress(Exception) in usage_record to OSError
- TencentCloud#2639 fix(terminal): narrow suppress(Exception) in agent-fallback lookup to OSError
- TencentCloud#2640 fix(backend): narrow suppress(Exception) in probe.py docker path to OSError+RuntimeError
- TencentCloud#2641 fix(browse): narrow _close_backend suppress(Exception) to OSError+RuntimeError
- TencentCloud#2643 fix(backup): add timeout=900 to pg_dump and pg_restore subprocess.run
- TencentCloud#2645 fix(pptx): add missing_ok=True to all bare unlink/rmdir in clean.py
- TencentCloud#2646 fix(soffice): add missing_ok=True to gcc shim temp-file cleanup in docx and pptx skills
- TencentCloud#2647 fix(composer): narrow suppress(Exception) in write_files adelete to OSError+RuntimeError
- TencentCloud#2652 Add encoding=utf-8 to pdf skill scripts and timeout to ollama pkill
- TencentCloud#2690 fix(backup): unlink symlinked/junctioned dirs during replace import
- TencentCloud#2693 fix(env): write ~/.octop/env atomically
- TencentCloud#2478 fix(cli): guard bundled import when --force clears site-packages (TencentCloud#2458)
- TencentCloud#2541 guard seed_bundled so init --force never wipes data then crashes (TencentCloud#2458)
- TencentCloud#2566 fix(cron): add in-process idempotency guard against duplicate deliveries (TencentCloud#2401)
- TencentCloud#2577 fix(config): guard bare int() on http_port and database port parsing
- TencentCloud#2578 fix(voice): guard bare int() on tencent voice_type coercion
- TencentCloud#2585 fix(cli): guard remaining bare asyncio.run() in skills.py and launch.py
- TencentCloud#2586 fix(utils): specify encoding=utf-8 on subprocess text=True calls
- TencentCloud#2588 fix(infra): add encoding=utf-8 to subprocess text=True in control
- TencentCloud#2593 fix(experts): add encoding=utf-8 to macro read_text calls
- TencentCloud#2653 office skills: add explicit encoding=utf-8 to open() in PDF and validator scripts
- TencentCloud#2654 patrol_server.py: add explicit encoding=utf-8 to JSONL file reader
- TencentCloud#2656 Guard Path.iterdir() with OSError in browser_media and skill_package_store
- TencentCloud#2661 fix(octop): guard glob/rglob with OSError in expert catalog discovery
- TencentCloud#2664 fix(octop): guard slim_control endpoint read with OSError and JSON parse errors
- TencentCloud#2170 docs: complete the ~/.octop layout tree and note the wizard password location
- TencentCloud#2369 docs: fix wizard password path in .env.example (post-TencentCloud#2174)
- TencentCloud#2480 fix(thread-artifacts): prefer tool result path over request args (TencentCloud#2464)
- TencentCloud#2484 Fix active-turn flag leak with TTL-based stale eviction (issue TencentCloud#2456)
- TencentCloud#2490 fix(cli): handle EXDEV when backup output crosses filesystems
- TencentCloud#2494 fix(grep_safety): search only target file path, not parent dir siblings (TencentCloud#2352)
- TencentCloud#2509 fix(sso): only treat same-origin opener as Octop SSO popup (TencentCloud#2347)
- TencentCloud#2519 fix(provider-probe): skip disabled models when picking default probe target (TencentCloud#2312)
- TencentCloud#2526 fix(knowledge): emit citations only for hits that made it into context (TencentCloud#1490)
- TencentCloud#2527 fix(hitl): isolate pending approvals by thread, not just session (TencentCloud#1897)
- TencentCloud#2539 fix(message_keys): group-chat sessions key by room not by sender (TencentCloud#2067)
- TencentCloud#2542 return 409 PROVIDER_NAME_TAKEN on duplicate provider creation (TencentCloud#1967)
- TencentCloud#2548 fix(skillhub): retry HTTP request on transient CDN IPv4 failure (TencentCloud#1819)
- TencentCloud#2562 fix(cron): preserve interval next_run_time across rebuilds (TencentCloud#2292)
- TencentCloud#2581 fix(gateway): use time.monotonic() for bot scan-poll deadline loop
- TencentCloud#2592 fix(core): make cleanup and datetime parsing resilient
- TencentCloud#2631 fix(users): add ignore_errors=True to checkpoint rmtree
- TencentCloud#2699 fix(media): serialize inbound attachment path allocation and upload
- TencentCloud#2714 fix(gateway): parse creator events from a text-mode subprocess pipe
- TencentCloud#2720 fix(preferences): keep bookmarks with upper-case URL scheme
TrueFurina added a commit to TrueFurina/Octop that referenced this pull request Nov 2, 2026
- TencentCloud#2495 fix(oauth): preserve existing query params on authorization_endpoint (TencentCloud#2391)
- TencentCloud#2529 fix(execute_env): fold admin env into local_shell backend (close TencentCloud#179)
- TencentCloud#2570 fix(core): guard POSIX-only os.chmod/os.umask on Windows (TencentCloud#2370)
- TencentCloud#2571 fix(tls): guard POSIX-only os.chmod on Windows (TencentCloud#2370)
- TencentCloud#2572 fix(core): guard final POSIX-only os.chmod call sites (TencentCloud#2370)
- TencentCloud#2573 fix(experts): guard POSIX-only os.chmod in skill scripts (TencentCloud#2370)
- TencentCloud#2575 fix(agents): reuse _token_int in _usage_has_segments for safe coercion
- TencentCloud#2580 fix(shell): guard bare int() in shell_ws resize handler
- TencentCloud#2605 fix(xlsx): suppress PermissionError during soffice shim temp cleanup
- TencentCloud#2648 fix(security): prevent path-traversal in tccli OAuth helper --profile
- TencentCloud#2665 fix(octop): guard credential marker reads and backup file read with OSError
- TencentCloud#2692 fix(cli): read shell rc file as UTF-8 when installing completion
- TencentCloud#2706 fix(security): write tool guard rules atomically so a torn save cannot disable the guard
- TencentCloud#2731 fix(gateway): keep TLS verification when sending the Feishu greeting
- TencentCloud#2596 fix(clinical): avoid TOCTOU in atomic_write temp cleanup
- TencentCloud#2597 fix(service): suppress OSError in _write_managed_file finally cleanup
- TencentCloud#2599 fix(skill-manager): use unlink(missing_ok=True) to close TOCTOU in restore
- TencentCloud#2600 fix(users): add missing_ok=True to avatar unlink in delete_profile_avatar
- TencentCloud#2601 fix(api): add missing_ok=True to daily-memory delete unlink
- TencentCloud#2604 fix(agently): add missing_ok=True to confirmation-file unlinks
- TencentCloud#2606 fix(ldap): narrow _safe_unbind suppress(Exception) to LDAPException
- TencentCloud#2607 fix(skills): narrow suppress(Exception) around proc.wait to specific errors
- TencentCloud#2635 fix(cli): narrow suppress(Exception) in _close_cli_session to OSError+RuntimeError
- TencentCloud#2636 fix(feishu): narrow suppress(Exception) in creator timeout finally to OSError
- TencentCloud#2637 fix(gateway): narrow suppress(Exception) in usage_record to OSError
- TencentCloud#2639 fix(terminal): narrow suppress(Exception) in agent-fallback lookup to OSError
- TencentCloud#2640 fix(backend): narrow suppress(Exception) in probe.py docker path to OSError+RuntimeError
- TencentCloud#2641 fix(browse): narrow _close_backend suppress(Exception) to OSError+RuntimeError
- TencentCloud#2643 fix(backup): add timeout=900 to pg_dump and pg_restore subprocess.run
- TencentCloud#2645 fix(pptx): add missing_ok=True to all bare unlink/rmdir in clean.py
- TencentCloud#2646 fix(soffice): add missing_ok=True to gcc shim temp-file cleanup in docx and pptx skills
- TencentCloud#2647 fix(composer): narrow suppress(Exception) in write_files adelete to OSError+RuntimeError
- TencentCloud#2652 Add encoding=utf-8 to pdf skill scripts and timeout to ollama pkill
- TencentCloud#2690 fix(backup): unlink symlinked/junctioned dirs during replace import
- TencentCloud#2693 fix(env): write ~/.octop/env atomically
- TencentCloud#2478 fix(cli): guard bundled import when --force clears site-packages (TencentCloud#2458)
- TencentCloud#2541 guard seed_bundled so init --force never wipes data then crashes (TencentCloud#2458)
- TencentCloud#2566 fix(cron): add in-process idempotency guard against duplicate deliveries (TencentCloud#2401)
- TencentCloud#2577 fix(config): guard bare int() on http_port and database port parsing
- TencentCloud#2578 fix(voice): guard bare int() on tencent voice_type coercion
- TencentCloud#2585 fix(cli): guard remaining bare asyncio.run() in skills.py and launch.py
- TencentCloud#2586 fix(utils): specify encoding=utf-8 on subprocess text=True calls
- TencentCloud#2588 fix(infra): add encoding=utf-8 to subprocess text=True in control
- TencentCloud#2593 fix(experts): add encoding=utf-8 to macro read_text calls
- TencentCloud#2653 office skills: add explicit encoding=utf-8 to open() in PDF and validator scripts
- TencentCloud#2654 patrol_server.py: add explicit encoding=utf-8 to JSONL file reader
- TencentCloud#2656 Guard Path.iterdir() with OSError in browser_media and skill_package_store
- TencentCloud#2661 fix(octop): guard glob/rglob with OSError in expert catalog discovery
- TencentCloud#2664 fix(octop): guard slim_control endpoint read with OSError and JSON parse errors
- TencentCloud#2170 docs: complete the ~/.octop layout tree and note the wizard password location
- TencentCloud#2369 docs: fix wizard password path in .env.example (post-TencentCloud#2174)
- TencentCloud#2480 fix(thread-artifacts): prefer tool result path over request args (TencentCloud#2464)
- TencentCloud#2484 Fix active-turn flag leak with TTL-based stale eviction (issue TencentCloud#2456)
- TencentCloud#2490 fix(cli): handle EXDEV when backup output crosses filesystems
- TencentCloud#2494 fix(grep_safety): search only target file path, not parent dir siblings (TencentCloud#2352)
- TencentCloud#2509 fix(sso): only treat same-origin opener as Octop SSO popup (TencentCloud#2347)
- TencentCloud#2519 fix(provider-probe): skip disabled models when picking default probe target (TencentCloud#2312)
- TencentCloud#2522 fix(search-probe): make Kimi probe model configurable via env (TencentCloud#1926)
- TencentCloud#2526 fix(knowledge): emit citations only for hits that made it into context (TencentCloud#1490)
- TencentCloud#2527 fix(hitl): isolate pending approvals by thread, not just session (TencentCloud#1897)
- TencentCloud#2539 fix(message_keys): group-chat sessions key by room not by sender (TencentCloud#2067)
- TencentCloud#2542 return 409 PROVIDER_NAME_TAKEN on duplicate provider creation (TencentCloud#1967)
- TencentCloud#2548 fix(skillhub): retry HTTP request on transient CDN IPv4 failure (TencentCloud#1819)
- TencentCloud#2562 fix(cron): preserve interval next_run_time across rebuilds (TencentCloud#2292)
- TencentCloud#2581 fix(gateway): use time.monotonic() for bot scan-poll deadline loop
- TencentCloud#2592 fix(core): make cleanup and datetime parsing resilient
- TencentCloud#2631 fix(users): add ignore_errors=True to checkpoint rmtree
- TencentCloud#2699 fix(media): serialize inbound attachment path allocation and upload
- TencentCloud#2714 fix(gateway): parse creator events from a text-mode subprocess pipe
- TencentCloud#2720 fix(preferences): keep bookmarks with upper-case URL scheme
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants