Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
223 changes: 223 additions & 0 deletions crates/openshell-tui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1315,6 +1315,16 @@ fn render_policy_lines(
// L7 allow rules.
for l7 in &ep.rules {
if let Some(allow) = &l7.allow {
if ep.protocol.eq_ignore_ascii_case("mcp") {
render_mcp_rule(
&mut lines,
"Allow",
&allow.method,
allow.params.get("name"),
t,
);
continue;
}
let method = if allow.method.is_empty() {
"*"
} else {
Expand All @@ -1334,6 +1344,18 @@ fn render_policy_lines(
}
}

if ep.protocol.eq_ignore_ascii_case("mcp") {
for deny in &ep.deny_rules {
render_mcp_rule(
&mut lines,
"Deny",
&deny.method,
deny.params.get("name"),
t,
);
}
}

// Access preset (if set instead of explicit rules).
if ep.access != 0 && ep.rules.is_empty() {
lines.push(Line::from(vec![
Expand Down Expand Up @@ -1371,6 +1393,37 @@ fn render_policy_lines(
lines
}

fn render_mcp_rule(
lines: &mut Vec<ratatui::text::Line<'static>>,
action: &str,
method: &str,
tool: Option<&openshell_core::proto::L7QueryMatcher>,
theme: &theme::Theme,
) {
use ratatui::text::{Line, Span};

let method = if method.is_empty() {
if tool.is_some() { "tools/call" } else { "*" }
} else {
method
};
lines.push(Line::from(vec![
Span::styled(format!(" {action}: "), theme.muted),
Span::styled(method.to_string(), theme.text),
]));
if let Some(tool) = tool {
for name in std::iter::once(&tool.glob)
.filter(|name| !name.is_empty())
.chain(&tool.any)
{
lines.push(Line::from(vec![
Span::styled(" Tool: ", theme.muted),
Span::styled(name.clone(), theme.text),
]));
}
}
}

// ---------------------------------------------------------------------------
// Animation helper
// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -3444,3 +3497,173 @@ mod sandbox_notes_tests {
assert_eq!(sandbox_notes(&sandbox, String::new()), "");
}
}

#[cfg(test)]
mod policy_render_tests {
use super::*;

fn policy_lines(yaml: &str) -> Vec<ratatui::text::Line<'static>> {
let policy = openshell_policy::parse_sandbox_policy(yaml).expect("valid policy");
render_policy_lines(&policy, &theme::Theme::dark())
}

#[test]
fn mcp_tool_allowlist_is_visible_one_tool_per_line() {
let lines = policy_lines(
r"
version: 1
network_policies:
review:
endpoints:
- host: host.openshell.internal
port: 32443
protocol: mcp
enforcement: enforce
rules:
- allow:
method: tools/call
tool:
any:
- local_gh_issue_view
- local_gh_pr_checks
- local_gh_pr_details
- local_gh_pr_diff
- local_gh_pr_list
- local_gh_pr_view
- local_gh_repo_view
- local_gh_run_failed_logs
- local_gh_run_view
- local_gh_source_blob
- local_gh_source_tree
- local_gh_version
",
);
let tools: Vec<_> = lines
.iter()
.map(ToString::to_string)
.filter_map(|line| line.strip_prefix(" Tool: ").map(str::to_string))
.collect();
assert_eq!(
tools,
[
"local_gh_issue_view",
"local_gh_pr_checks",
"local_gh_pr_details",
"local_gh_pr_diff",
"local_gh_pr_list",
"local_gh_pr_view",
"local_gh_repo_view",
"local_gh_run_failed_logs",
"local_gh_run_view",
"local_gh_source_blob",
"local_gh_source_tree",
"local_gh_version",
]
);
assert!(
lines
.iter()
.any(|line| line.to_string() == " Allow: tools/call")
);
assert!(
!lines
.iter()
.any(|line| line.to_string().contains("tools/call *"))
);

let mut buffer = ratatui::buffer::Buffer::empty(ratatui::layout::Rect::new(0, 0, 60, 30));
ratatui::widgets::Widget::render(
ratatui::widgets::Paragraph::new(lines),
buffer.area,
&mut buffer,
);
for tool in tools {
assert!(buffer.content.chunks(60).any(|row| {
row.iter()
.map(ratatui::buffer::Cell::symbol)
.collect::<String>()
.contains(&tool)
}));
}
}

#[test]
fn mcp_methods_and_allow_deny_selectors_preserve_their_scope() {
let lines = policy_lines(
r"
version: 1
network_policies:
review:
endpoints:
- host: mcp.example.com
port: 443
protocol: mcp
mcp:
allow_all_known_mcp_methods: true
rules:
- allow:
method: tools/list
- allow:
tool: read_source
- allow:
method: tools/call
params:
name:
any: [read_*, list_*]
deny_rules:
- tool: read_private
- method: tools/call
params:
name:
any: [read_secret_*, list_private]
- method: resources/read
profile:
endpoints:
- host: profile.example.com
port: 443
protocol: mcp
mcp:
allow_all_known_mcp_methods: true
rules:
- allow: {}
deny_rules:
- {}
rest:
endpoints:
- host: api.example.com
port: 443
protocol: rest
rules:
- allow: {method: GET, path: /repos/**}
",
);
let text: Vec<_> = lines.iter().map(ToString::to_string).collect();
assert!(text.iter().any(|line| line == " Allow: *"));
assert!(text.iter().any(|line| line == " Deny: *"));
let mcp_start = text
.iter()
.position(|line| line == " Allow: tools/list")
.unwrap();
assert_eq!(
&text[mcp_start..mcp_start + 12],
[
" Allow: tools/list",
" Allow: tools/call",
" Tool: read_source",
" Allow: tools/call",
" Tool: read_*",
" Tool: list_*",
" Deny: tools/call",
" Tool: read_private",
" Deny: tools/call",
" Tool: read_secret_*",
" Tool: list_private",
" Deny: resources/read",
]
);
assert!(
text.iter()
.any(|line| line == " Allow: GET /repos/**")
);
}
}
Loading