Skip to content

Commit 28c14f4

Browse files
committed
feat(tui): warn when a proposed endpoint is not method- and path-scoped
The draft inbox renders an endpoint's scope but never says when that scope is wide. endpoint_layer_label already tags L4 and format_allow_rule already renders an unset method or path as *, so the breadth is displayed while nothing draws a reviewer's eye to a proposal that grants far more than the request that was denied. Add scope_warning, a pure classifier beside endpoint_layer_label, and render it under the endpoint it describes in the detail popup, styled like the existing security-note line. It flags an L4 endpoint, a REST endpoint with no allow rules, and a REST allow rule that leaves the method or path unset. Protocols other than REST scope on command rather than method and path, so they are left alone rather than warned about incorrectly. This stays in the TUI rather than in generate_security_notes because the gateway's security notes feed auto-approval eligibility, so adding a case there is a policy decision rather than a presentation one. Everything needed to classify the endpoint is already on the client in proposed_rule.endpoints. Part of #1098. Signed-off-by: Vyncint Ng <115854244+vyncint@users.noreply.github.com>
1 parent 118b250 commit 28c14f4

2 files changed

Lines changed: 146 additions & 0 deletions

File tree

‎crates/openshell-tui/src/ui/sandbox_draft.rs‎

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,11 @@ pub fn draw_detail_popup(
328328
text_width,
329329
);
330330

331+
if let Some(warning) = scope_warning(ep) {
332+
let warn = t.status_warn.add_modifier(Modifier::BOLD);
333+
push_wrapped(&mut lines, " ! ", warn, warning, warn, text_width);
334+
}
335+
331336
for detail in format_endpoint_details(ep) {
332337
push_wrapped(&mut lines, " ", t.text, &detail, t.text, text_width);
333338
}
@@ -863,6 +868,36 @@ fn format_endpoint_details(endpoint: &NetworkEndpoint) -> Vec<String> {
863868
details
864869
}
865870

871+
/// Why a proposed endpoint is broader than the denial that prompted it.
872+
///
873+
/// An L4 endpoint allows the whole TCP port. A REST endpoint with no allow
874+
/// rules, or with an allow rule that leaves the method or path unset — rendered
875+
/// as `*` by `format_allow_rule` — still permits far more than the single
876+
/// request that was denied. Protocols other than REST scope on `command`
877+
/// instead, so they are left alone rather than warned about incorrectly.
878+
fn scope_warning(endpoint: &NetworkEndpoint) -> Option<&'static str> {
879+
if endpoint.protocol.trim().is_empty() {
880+
return Some(
881+
"L4 rule: allows every connection to this host and port, not only the denied request",
882+
);
883+
}
884+
if !endpoint.protocol.eq_ignore_ascii_case("rest") {
885+
return None;
886+
}
887+
if endpoint.rules.is_empty() {
888+
return Some("no method or path scope: allows every request to this host");
889+
}
890+
let unscoped = endpoint
891+
.rules
892+
.iter()
893+
.filter_map(|rule| rule.allow.as_ref())
894+
.any(|allow| allow.method.trim().is_empty() || allow.path.trim().is_empty());
895+
if unscoped {
896+
return Some("an allow rule leaves the method or path unset (*), widening the scope");
897+
}
898+
None
899+
}
900+
866901
fn endpoint_layer_label(endpoint: &NetworkEndpoint) -> &str {
867902
if endpoint.protocol.eq_ignore_ascii_case("rest") {
868903
"L7 rest"
@@ -976,6 +1011,7 @@ fn format_short_time(epoch_ms: i64) -> String {
9761011
mod tests {
9771012
use super::*;
9781013
use crate::theme::Theme;
1014+
use openshell_core::proto::{L7Rule, NetworkPolicyRule};
9791015
use ratatui::Terminal;
9801016
use ratatui::backend::TestBackend;
9811017

@@ -1363,4 +1399,112 @@ mod tests {
13631399
let rows = pack_hints(&hint_units(&chunk, &theme, true), 30);
13641400
assert!(rows.len() > 1, "hints should wrap at 30 columns");
13651401
}
1402+
1403+
// --- L4 / no-method-path scope warning ---------------------------------
1404+
1405+
fn scoped_endpoint(protocol: &str, rules: Vec<L7Rule>) -> NetworkEndpoint {
1406+
NetworkEndpoint {
1407+
host: "api.github.com".to_string(),
1408+
port: 443,
1409+
protocol: protocol.to_string(),
1410+
rules,
1411+
..Default::default()
1412+
}
1413+
}
1414+
1415+
fn allow_rule(method: &str, path: &str) -> L7Rule {
1416+
L7Rule {
1417+
allow: Some(L7Allow {
1418+
method: method.to_string(),
1419+
path: path.to_string(),
1420+
..Default::default()
1421+
}),
1422+
}
1423+
}
1424+
1425+
fn chunk_with(endpoint: NetworkEndpoint) -> PolicyChunk {
1426+
PolicyChunk {
1427+
status: "pending".to_string(),
1428+
rule_name: "allow-github".to_string(),
1429+
proposed_rule: Some(NetworkPolicyRule {
1430+
endpoints: vec![endpoint],
1431+
..Default::default()
1432+
}),
1433+
..Default::default()
1434+
}
1435+
}
1436+
1437+
/// Rebuild a matchable string from a rendered buffer.
1438+
///
1439+
/// Cells are joined row-major, so a wrapped phrase is split by row padding
1440+
/// and by the popup's own box-drawing border. Both become whitespace, then
1441+
/// whitespace collapses.
1442+
fn squash(text: &str) -> String {
1443+
text.chars()
1444+
.map(|c| {
1445+
if "│┌┐└┘─".contains(c) {
1446+
' '
1447+
} else {
1448+
c
1449+
}
1450+
})
1451+
.collect::<String>()
1452+
.split_whitespace()
1453+
.collect::<Vec<_>>()
1454+
.join(" ")
1455+
}
1456+
1457+
#[test]
1458+
fn l4_endpoint_is_flagged_as_unscoped() {
1459+
let warning = scope_warning(&scoped_endpoint("", vec![]));
1460+
assert!(warning.is_some_and(|w| w.starts_with("L4 rule:")));
1461+
}
1462+
1463+
#[test]
1464+
fn rest_endpoint_without_allow_rules_is_flagged() {
1465+
let warning = scope_warning(&scoped_endpoint("rest", vec![]));
1466+
assert!(warning.is_some_and(|w| w.contains("no method or path scope")));
1467+
}
1468+
1469+
#[test]
1470+
fn rest_allow_rule_without_method_or_path_is_flagged() {
1471+
for rule in [allow_rule("", "/repos/**"), allow_rule("GET", "")] {
1472+
let warning = scope_warning(&scoped_endpoint("rest", vec![rule]));
1473+
assert!(
1474+
warning.is_some_and(|w| w.contains("leaves the method or path unset")),
1475+
"unscoped allow rule should be flagged"
1476+
);
1477+
}
1478+
}
1479+
1480+
#[test]
1481+
fn fully_scoped_rest_endpoint_is_not_flagged() {
1482+
let endpoint = scoped_endpoint("rest", vec![allow_rule("GET", "/repos/**")]);
1483+
assert_eq!(scope_warning(&endpoint), None);
1484+
}
1485+
1486+
/// Non-REST protocols scope on `command`, so method and path say nothing
1487+
/// about how broad they are.
1488+
#[test]
1489+
fn non_rest_protocol_is_left_alone() {
1490+
assert_eq!(scope_warning(&scoped_endpoint("ssh", vec![])), None);
1491+
}
1492+
1493+
#[test]
1494+
fn scope_warning_is_rendered_in_the_detail_popup() {
1495+
let (screen, _) = render(&chunk_with(scoped_endpoint("", vec![])), 80, 24, 0);
1496+
assert!(
1497+
squash(&screen).contains("allows every connection to this host and port"),
1498+
"L4 warning should appear in the popup"
1499+
);
1500+
}
1501+
1502+
#[test]
1503+
fn a_scoped_endpoint_renders_no_warning() {
1504+
let endpoint = scoped_endpoint("rest", vec![allow_rule("GET", "/repos/**")]);
1505+
let (screen, _) = render(&chunk_with(endpoint), 80, 24, 0);
1506+
let seen = squash(&screen);
1507+
assert!(!seen.contains("allows every"), "unexpected warning: {seen}");
1508+
assert!(!seen.contains("no method or path scope"));
1509+
}
13661510
}

‎docs/sandboxes/policy-advisor.mdx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ The output shows the chunk ID, status, rationale, binary, endpoint summary, prov
232232
Endpoints: api.github.com:443 [L7 rest, allow PUT /repos/NVIDIA/OpenShell/contents/docs/**]
233233
```
234234

235+
The terminal UI flags proposals that are broader than the request that was denied. In the detail popup, `openshell term` shows a warning under any endpoint that is L4, that is REST with no allow rules, or whose allow rule leaves the method or path unset. Protocols other than REST scope on `command`, so they are not flagged.
236+
235237
Approve only when the structured rule matches the access you intend to grant:
236238

237239
```shell

0 commit comments

Comments
 (0)