Skip to content

refactor: extract early-return security gates from call_tool_guarded (cc 185) - #772

Merged
yvgude merged 3 commits into
yvgude:mainfrom
cedric013:refactor/call-tool-guarded-chain
Jul 9, 2026
Merged

refactor: extract early-return security gates from call_tool_guarded (cc 185)#772
yvgude merged 3 commits into
yvgude:mainfrom
cedric013:refactor/call-tool-guarded-chain

Conversation

@cedric013

Copy link
Copy Markdown
Contributor

Summary

Closes #771.

call_tool_guarded (rust/src/server/call_tool.rs) has cognitive complexity 185 — the worst hotspot in the repo — and is ~1000 lines. It chains several independent early-return security gates inline before dispatch.

This extracts the three self-contained gates into helpers returning Option<CallToolResult>:

  • guard_role_and_policy — role + context-policy-pack tool-access gating
  • guard_egress — egress / output DLP on agent writes & actions
  • guard_workflow — workflow allow-list gating

The dispatcher now calls them in the exact same order, each still short-circuiting via return Ok(blocked) on Some. guard_name/guard_args are intentionally kept inline because they are reused by the later permission-inheritance gate.

Scope: this is a conservative, security-preserving reduction of the prelude only. The risky dispatch + post-processing section (panic-catch, elicitation, finalize) is deliberately left untouched for a separate follow-up.

Test plan

  • cargo build --lib
  • cargo test --lib -- server::call_tool -> 7 passed
  • cargo test --lib -- workflow role_guard policy_guard egress -> 31 passed
  • cargo fmt --check
  • cargo clippy --lib --all-features -- -D warnings clean for call_tool.rs (only remaining error is the unrelated pre-existing rules_overhead.rs:143 lint on main, fixed by fix: silence clippy::map_unwrap_or in rules_overhead (unblocks Clippy CI) #767)

Notes for reviewers

  • Risk areas: security gates — verified the extraction preserves call order and short-circuit semantics; each gate's body was moved verbatim (return Ok(x) -> return Some(x), trailing None).
  • Backwards compatibility: behaviour unchanged.

cedric013 added 3 commits July 9, 2026 14:39
call_tool_guarded (cognitive complexity 185) chained several independent early-return security gates inline. Extract the three self-contained ones — role+policy tool-access, egress DLP, and workflow allow-list — into guard_role_and_policy / guard_egress / guard_workflow helpers returning Option<CallToolResult>. The dispatcher calls them in the exact same order (each still short-circuits via return Ok on Some). guard_name/guard_args stay inline since they are reused by the later permission-inheritance gate. Behaviour is unchanged; the risky dispatch + post-processing section is untouched.

@yvgude yvgude left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Reviewed the call_tool_guarded security gate extraction.

Verified:

  • Three guards extracted with correct Option<CallToolResult> return type:
    • guard_role_and_policy (sync) — role + context-policy-pack
    • guard_egress (sync) — egress / output DLP
    • guard_workflow (async, needs &self.workflow) — workflow allow-list
  • Gate ordering preserved: role → ctx_call resolution → egress → workflow (same short-circuit semantics)
  • Verbatim move — return Ok(x)return Some(x), trailing None
  • ctx_call inner-tool resolution correctly stays inline (shared with permission-inheritance gate)
  • Dispatch + post-processing intentionally untouched (deferred to follow-up)
  • Clippy fixes in rules_inject/content.rs, chatgpt_ws.rs, signing.rs are all minor and correct
  • LEAN-CTX.md refresh is cosmetic

Security-critical path — gate order and short-circuit behavior are the key invariants. Both are preserved.

Approved.

@yvgude
yvgude merged commit e7a4607 into yvgude:main Jul 9, 2026
26 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 9, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

refactor: call_tool_guarded is ~1000 lines (cognitive complexity 185) — extract early-return security gates

2 participants