Skip to content

fix(security): surface rule reason in sandbox block message (#408) - #425

Closed
wangtsingx wants to merge 2 commits into
AI-Shell-Team:mainfrom
openforks:fix/408-sandbox-block-message
Closed

fix(security): surface rule reason in sandbox block message (#408)#425
wangtsingx wants to merge 2 commits into
AI-Shell-Team:mainfrom
openforks:fix/408-sandbox-block-message

Conversation

@wangtsingx

@wangtsingx wangtsingx commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

概述

改动类型

  • Bug 修复

涉及范围

  • 安全模块
  • 技能 / 工具

用户可见变更

  • 沙箱命中安全规则 Block 时,消息现在展示规则的可读 reason(如 "System config changes can break the host")而非空泛的通用 level 句
  • 即使规则缺少 reason/description/confirm_message,兜底消息也会附带 rule id 和 matched paths

兼容性

  • 向后兼容? 是
  • 配置变更? 否

测试验证

  • cargo test -p aish-security -p aish-tools -p aish-llm — 757 个测试全部通过
  • 新增 test_format_security_message_shows_rule_reason_from_sandbox:验证沙箱命中路径的 rule reason 正确展示
  • 新增 test_format_security_message_fallback_includes_rule_id_and_paths:验证兜底附带 rule id 和 paths
  • 更新 assess_sandbox_result_blocks_high_risk_delete:断言 reason 出现在 reasons 首位

根因分析

沙箱命中路径 assess_sandbox_result 构造 reasons 时只放入内部诊断串("sandbox matched ...""matched paths: ..."),从未使用 PolicyRule.reason 字段。format_security_message 通过 is_internal_security_reason 过滤掉这些内部串后无可读原因可用,退化为通用 level 句。

risk.rsdegraded.rs 早已正确使用 primary_rule.reason,但沙箱命中路径遗漏了同一模式。

检查清单

  • 代码风格符合项目规范
  • 已添加必要的测试
  • 文档已更新(如需要)

🤖 AI-assisted (Claude)

Summary by CodeRabbit

  • Bug Fixes
    • Security assessments now show the matched policy’s explanation before other diagnostic details.
    • Security fallback messages now include the applicable rule and up to three affected paths.
    • Explicit policy explanations take precedence over generated fallback messages.
    • Improved security diagnostics make it easier to understand why an action was flagged.
    • Policy details are now presented consistently across assessment and fallback messages.

…-Team#408)

沙箱命中 HIGH 规则后 Block 文案退化为通用「bash 命令需要 HIGH
级安全处理」,而非规则的可读 reason。根因是 assess_sandbox_result
构造 reasons 时只放入内部诊断串("sandbox matched ..."、
"matched paths: ..."),从未使用 PolicyRule.reason 字段。
format_security_message 过滤掉内部串后无可读原因可用,退化为
通用 level 句。

修复:
- assess.rs: 将 primary_rule.reason 作为第一条可读原因插入 reasons,
  与 risk.rs / degraded.rs 已有模式一致。primary_rule 声明前移到
  reasons 构造之前以解决借用顺序问题。
- bash.rs: format_security_message 的 last-resort 兜底从裸通用句
  改为附带 rule id/name 和 matched paths(最多 3 条),确保即使
  规则缺少 reason/description/confirm_message 也能展示命中信息。

测试:3 个 crate 共 757 个测试通过,含 2 个新增测试和 1 个更新测试。

Closes AI-Shell-Team#408
@github-actions

github-actions Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Thanks for the pull request. A maintainer will review it when available.

Please keep the PR focused, explain the why in the description, and make sure local checks pass before requesting review.

Contribution guide: https://github.com/AI-Shell-Team/aish/blob/main/CONTRIBUTING.md

@coderabbitai

coderabbitai Bot commented Aug 3, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 019d9933-3c29-477e-ba11-3bbd36f45652

📥 Commits

Reviewing files that changed from the base of the PR and between 07f8b94 and bc1f3f8.

📒 Files selected for processing (1)
  • crates/aish-security/src/sandbox/assess.rs
🚧 Files skipped from review as they are similar to previous changes (1)
  • crates/aish-security/src/sandbox/assess.rs

📝 Walkthrough

Walkthrough

Sandbox assessments now place the matched policy reason first. Bash security fallback messages now include matched rule and path details. Tests cover reason precedence, rule identifiers, and matched paths.

Changes

Policy diagnostics

Layer / File(s) Summary
Sandbox assessment reasons
crates/aish-security/src/sandbox/assess.rs
The assessment prepends the matched policy rule reason to its reasons list. The high-risk delete test verifies this order.
Bash security fallback details
crates/aish-tools/src/bash/bash.rs
Fallback messages include the matched rule identifier or name and up to three matched paths. Tests verify reason precedence and detail output.

Estimated code review effort: 2 (Simple) | ~15 minutes

Possibly related PRs

Poem

A rabbit checks the policy trail,
The clearest reason leads the way.
Rule names hop through fallback text,
Three paths join the display.
Tests thump paws in proud array.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: exposing the matched policy rule reason in sandbox block messages.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
crates/aish-tools/src/bash/bash.rs (1)

1263-1297: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add coverage for the name-only and three-path fallback cases.

The test covers a rule ID and one path only. Add a case with no rule ID, a rule name, and four matched paths. Assert that the message contains the name and only the first three paths.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/aish-tools/src/bash/bash.rs` around lines 1263 - 1297, The existing
test test_format_security_message_fallback_includes_rule_id_and_paths lacks
coverage for name-only rules and path truncation. Add a test case with
matched_rule.id absent, a populated rule name, and four matched_paths; assert
format_security_message includes the rule name and only the first three paths,
excluding the fourth.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@crates/aish-tools/src/bash/bash.rs`:
- Around line 1263-1297: The existing test
test_format_security_message_fallback_includes_rule_id_and_paths lacks coverage
for name-only rules and path truncation. Add a test case with matched_rule.id
absent, a populated rule name, and four matched_paths; assert
format_security_message includes the rule name and only the first three paths,
excluding the fourth.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: ad062afe-8044-46ba-b174-5c1bb6f81e5f

📥 Commits

Reviewing files that changed from the base of the PR and between af55deb and 07f8b94.

📒 Files selected for processing (2)
  • crates/aish-security/src/sandbox/assess.rs
  • crates/aish-tools/src/bash/bash.rs

@F16shen

F16shen commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator

这个问题已经在修复中了,只是还没提PR

@F16shen F16shen closed this Aug 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants