Skip to content

Commit 12ef86c

Browse files
authored
fix(cli): keep policy and provider diagnostics readable (#3444)
Reuse character-safe truncation for policy history errors so a multibyte character cannot panic the table renderer. Distinguish unavailable provider-profile YAML from an absent profile, display a bounded diagnostic, and preserve strict serialization and redacted object navigation. Cover the actual CLI renderer and TUI display/navigation paths, including invalid and absent profiles, Unicode input, and redacted errors. Signed-off-by: Shiju <shiju@nvidia.com>
1 parent cfcc373 commit 12ef86c

3 files changed

Lines changed: 268 additions & 14 deletions

File tree

‎crates/openshell-cli/src/run.rs‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5940,7 +5940,7 @@ fn print_policy_revision_table(revisions: &[openshell_core::proto::SandboxPolicy
59405940
&rev.policy_hash
59415941
};
59425942
let error_short = if rev.load_error.len() > 40 {
5943-
format!("{}...", &rev.load_error[..40])
5943+
truncate_status_field(&rev.load_error, 40)
59445944
} else {
59455945
rev.load_error.clone()
59465946
};
@@ -6605,6 +6605,29 @@ mod tests {
66056605
assert!(unknown[0].get("sandbox").is_none());
66066606
}
66076607

6608+
#[test]
6609+
fn policy_revision_table_handles_unicode_load_errors() {
6610+
// Stored diagnostics can contain Unicode paths. Byte 40 splits the
6611+
// character in the 40-scalar case; longer errors must also remain safe.
6612+
let revisions = [
6613+
String::new(),
6614+
"a".repeat(40),
6615+
"a".repeat(41),
6616+
format!("{}é", "a".repeat(39)),
6617+
format!("{}éz", "a".repeat(39)),
6618+
]
6619+
.into_iter()
6620+
.map(|load_error| SandboxPolicyRevision {
6621+
version: 1,
6622+
status: PolicyStatus::Failed as i32,
6623+
load_error,
6624+
..Default::default()
6625+
})
6626+
.collect::<Vec<_>>();
6627+
6628+
super::print_policy_revision_table(&revisions);
6629+
}
6630+
66086631
#[test]
66096632
fn service_endpoint_json_has_raw_fields_and_normalized_url() {
66106633
let response = ServiceEndpointResponse {

‎crates/openshell-tui/src/app.rs‎

Lines changed: 223 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,26 @@ fn apply_discovered_provider(form: &mut CreateProviderForm, discovered: Discover
420420
// Provider detail view (Get)
421421
// ---------------------------------------------------------------------------
422422

423+
/// Availability of strictly serialized provider profile YAML in the detail view.
424+
pub enum ProviderProfileYaml {
425+
/// The provider has no associated profile.
426+
Absent,
427+
/// The profile serialized successfully.
428+
Valid(String),
429+
/// A profile exists, but its YAML serialization failed.
430+
Invalid,
431+
}
432+
433+
impl ProviderProfileYaml {
434+
/// Return YAML only when strict profile serialization succeeded.
435+
pub fn yaml(&self) -> Option<&str> {
436+
match self {
437+
Self::Valid(yaml) => Some(yaml),
438+
Self::Absent | Self::Invalid => None,
439+
}
440+
}
441+
}
442+
423443
pub struct ProviderDetailView {
424444
pub name: String,
425445
pub provider_id: String,
@@ -430,7 +450,7 @@ pub struct ProviderDetailView {
430450
pub show_raw_provider: bool,
431451
pub raw_profile_scroll: usize,
432452
pub raw_provider_scroll: usize,
433-
pub raw_profile_yaml: Option<String>,
453+
pub raw_profile_yaml: ProviderProfileYaml,
434454
pub raw_provider_yaml: String,
435455
pub profile_name: Option<String>,
436456
pub profile_category: Option<String>,
@@ -2933,7 +2953,7 @@ impl App {
29332953
KeyCode::Esc | KeyCode::Enter => {
29342954
self.provider_detail = None;
29352955
}
2936-
KeyCode::Char('y') if detail.raw_profile_yaml.is_some() => {
2956+
KeyCode::Char('y') if detail.raw_profile_yaml.yaml().is_some() => {
29372957
detail.show_raw_profile = !detail.show_raw_profile;
29382958
detail.show_raw_provider = false;
29392959
detail.raw_profile_scroll = 0;
@@ -2946,7 +2966,7 @@ impl App {
29462966
KeyCode::Char('j') | KeyCode::Down if detail.show_raw_profile => {
29472967
let max_scroll = detail
29482968
.raw_profile_yaml
2949-
.as_ref()
2969+
.yaml()
29502970
.map_or(0, |raw| raw.lines().count().saturating_sub(1));
29512971
detail.raw_profile_scroll = (detail.raw_profile_scroll + 1).min(max_scroll);
29522972
}
@@ -3441,9 +3461,12 @@ impl App {
34413461
},
34423462
);
34433463

3444-
let raw_profile_yaml = profile.and_then(|profile| {
3464+
let raw_profile_yaml = profile.map_or(ProviderProfileYaml::Absent, |profile| {
34453465
let dto = ProviderTypeProfile::from_proto(profile);
3446-
openshell_providers::profile_to_yaml(&dto).ok()
3466+
// Serializer errors may echo authored values. Keep the failure
3467+
// distinct from absence without retaining potentially secret text.
3468+
openshell_providers::profile_to_yaml(&dto)
3469+
.map_or(ProviderProfileYaml::Invalid, ProviderProfileYaml::Valid)
34473470
});
34483471

34493472
ProviderDetailView {
@@ -3660,6 +3683,201 @@ mod tests {
36603683
KeyEvent::new(code, KeyModifiers::NONE)
36613684
}
36623685

3686+
fn detail_mcp_profile(versions: &[&str]) -> openshell_core::proto::ProviderProfile {
3687+
openshell_core::proto::ProviderProfile {
3688+
id: "mcp-example".to_string(),
3689+
display_name: "MCP Example".to_string(),
3690+
endpoints: vec![openshell_core::proto::NetworkEndpoint {
3691+
host: "mcp.example.com".to_string(),
3692+
port: 443,
3693+
protocol: "mcp".to_string(),
3694+
mcp: Some(openshell_core::proto::McpOptions {
3695+
versions: versions.iter().map(ToString::to_string).collect(),
3696+
..Default::default()
3697+
}),
3698+
..Default::default()
3699+
}],
3700+
..Default::default()
3701+
}
3702+
}
3703+
3704+
fn detail_app(profile: Option<openshell_core::proto::ProviderProfile>) -> App {
3705+
let mut app = test_app();
3706+
let provider = openshell_core::proto::Provider {
3707+
metadata: Some(openshell_core::proto::ObjectMeta {
3708+
id: "provider-id".to_string(),
3709+
name: "example-provider".to_string(),
3710+
..Default::default()
3711+
}),
3712+
r#type: "mcp-example".to_string(),
3713+
credentials: HashMap::from([("API_KEY".to_string(), "test-only-secret".to_string())]),
3714+
..Default::default()
3715+
};
3716+
app.provider_entries.push(ProviderListEntry {
3717+
provider: provider.clone(),
3718+
profile,
3719+
});
3720+
app.provider_detail = Some(app.provider_detail_from_provider(&provider));
3721+
app
3722+
}
3723+
3724+
fn render_provider_detail(app: &App) -> String {
3725+
let mut terminal = ratatui::Terminal::new(ratatui::backend::TestBackend::new(100, 36))
3726+
.expect("test terminal");
3727+
terminal
3728+
.draw(|frame| crate::ui::create_provider::draw_detail(frame, app, frame.size()))
3729+
.expect("provider detail renders");
3730+
terminal
3731+
.backend()
3732+
.buffer()
3733+
.content()
3734+
.iter()
3735+
.map(ratatui::buffer::Cell::symbol)
3736+
.collect()
3737+
}
3738+
3739+
#[tokio::test]
3740+
async fn provider_detail_absent_profile_keeps_object_yaml_available() {
3741+
let mut app = detail_app(None);
3742+
let detail = app.provider_detail.as_ref().expect("detail view");
3743+
assert!(matches!(
3744+
detail.raw_profile_yaml,
3745+
ProviderProfileYaml::Absent
3746+
));
3747+
let text = render_provider_detail(&app);
3748+
assert!(text.contains("Profile: <none> (legacy/unprofiled provider)"));
3749+
assert!(!text.contains("Profile YAML unavailable"));
3750+
assert!(!text.contains("[y]"));
3751+
3752+
app.handle_provider_detail_key(key(KeyCode::Char('y')));
3753+
assert!(
3754+
!app.provider_detail
3755+
.as_ref()
3756+
.expect("detail view")
3757+
.show_raw_profile
3758+
);
3759+
app.handle_provider_detail_key(key(KeyCode::Char('o')));
3760+
let text = render_provider_detail(&app);
3761+
assert!(text.contains("Provider Object YAML"));
3762+
assert!(text.contains("<redacted>"));
3763+
assert!(!text.contains("test-only-secret"));
3764+
}
3765+
3766+
#[tokio::test]
3767+
async fn provider_detail_valid_profile_preserves_yaml_navigation() {
3768+
let profile = detail_mcp_profile(&["2025-11-25"]);
3769+
let expected_yaml =
3770+
openshell_providers::profile_to_yaml(&ProviderTypeProfile::from_proto(&profile))
3771+
.expect("valid profile YAML");
3772+
let mut app = detail_app(Some(profile));
3773+
assert_eq!(
3774+
app.provider_detail
3775+
.as_ref()
3776+
.expect("detail view")
3777+
.raw_profile_yaml
3778+
.yaml(),
3779+
Some(expected_yaml.as_str())
3780+
);
3781+
assert!(render_provider_detail(&app).contains("[y] Profile YAML"));
3782+
app.handle_provider_detail_key(key(KeyCode::Char('y')));
3783+
let text = render_provider_detail(&app);
3784+
assert!(text.contains("Provider Profile YAML"));
3785+
assert!(text.contains("mcp-example"));
3786+
assert!(!text.contains("test-only-secret"));
3787+
app.handle_provider_detail_key(key(KeyCode::Char('j')));
3788+
assert_eq!(
3789+
app.provider_detail
3790+
.as_ref()
3791+
.expect("detail view")
3792+
.raw_profile_scroll,
3793+
1
3794+
);
3795+
app.handle_provider_detail_key(key(KeyCode::Char('k')));
3796+
assert_eq!(
3797+
app.provider_detail
3798+
.as_ref()
3799+
.expect("detail view")
3800+
.raw_profile_scroll,
3801+
0
3802+
);
3803+
app.handle_provider_detail_key(key(KeyCode::Char('o')));
3804+
assert!(
3805+
app.provider_detail
3806+
.as_ref()
3807+
.expect("detail view")
3808+
.show_raw_provider
3809+
);
3810+
app.handle_provider_detail_key(key(KeyCode::Char('y')));
3811+
let detail = app.provider_detail.as_ref().expect("detail view");
3812+
assert!(detail.show_raw_profile);
3813+
assert!(!detail.show_raw_provider);
3814+
app.handle_provider_detail_key(key(KeyCode::Esc));
3815+
assert!(
3816+
!app.provider_detail
3817+
.as_ref()
3818+
.expect("detail view")
3819+
.show_raw_profile
3820+
);
3821+
app.handle_provider_detail_key(key(KeyCode::Esc));
3822+
assert!(app.provider_detail.is_none());
3823+
}
3824+
3825+
#[tokio::test]
3826+
async fn provider_detail_invalid_profile_remains_visible_without_yaml_action() {
3827+
for versions in [vec!["2025-11-25", "2025-11-25"], vec!["latest"]] {
3828+
let mut app = detail_app(Some(detail_mcp_profile(&versions)));
3829+
let detail = app.provider_detail.as_ref().expect("detail view");
3830+
assert!(matches!(
3831+
detail.raw_profile_yaml,
3832+
ProviderProfileYaml::Invalid
3833+
));
3834+
assert_eq!(detail.profile_name.as_deref(), Some("MCP Example"));
3835+
let text = render_provider_detail(&app);
3836+
assert!(text.contains("example-provider"));
3837+
assert!(text.contains("Profile YAML unavailable: serialization failed."));
3838+
assert!(text.contains("Correct the profile at its source, then reopen this view."));
3839+
assert!(!text.contains("legacy/unprofiled"));
3840+
assert!(!text.contains("[y]"));
3841+
app.handle_provider_detail_key(key(KeyCode::Char('y')));
3842+
assert!(
3843+
!app.provider_detail
3844+
.as_ref()
3845+
.expect("detail view")
3846+
.show_raw_profile
3847+
);
3848+
app.handle_provider_detail_key(key(KeyCode::Char('o')));
3849+
let text = render_provider_detail(&app);
3850+
assert!(text.contains("Provider Object YAML"));
3851+
assert!(text.contains("<redacted>"));
3852+
assert!(!text.contains("test-only-secret"));
3853+
}
3854+
}
3855+
3856+
#[tokio::test]
3857+
async fn provider_detail_multibyte_profile_error_is_bounded_and_redacted() {
3858+
let malformed = "秘密é🚧\nprivate-profile-value".repeat(2048);
3859+
let profile = detail_mcp_profile(&[malformed.as_str()]);
3860+
let error =
3861+
openshell_providers::profile_to_yaml(&ProviderTypeProfile::from_proto(&profile))
3862+
.expect_err("malformed revision must fail strict serialization");
3863+
assert!(error.to_string().contains("秘密"));
3864+
let app = detail_app(Some(profile));
3865+
assert!(matches!(
3866+
app.provider_detail
3867+
.as_ref()
3868+
.expect("detail view")
3869+
.raw_profile_yaml,
3870+
ProviderProfileYaml::Invalid
3871+
));
3872+
let text = render_provider_detail(&app);
3873+
assert!(text.contains("Profile YAML unavailable: serialization failed."));
3874+
assert!(!text.contains("秘密"));
3875+
assert!(!text.contains("private-profile-value"));
3876+
assert!(!text.contains("test-only-secret"));
3877+
let ordinary_failure = detail_app(Some(detail_mcp_profile(&["latest"])));
3878+
assert_eq!(text, render_provider_detail(&ordinary_failure));
3879+
}
3880+
36633881
#[tokio::test]
36643882
async fn create_provider_enter_with_empty_profile_catalog_is_recoverable() {
36653883
let mut app = test_app();

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

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ use ratatui::layout::{Constraint, Direction, Layout, Rect};
66
use ratatui::text::{Line, Span};
77
use ratatui::widgets::{Block, Borders, Clear, Padding, Paragraph};
88

9-
use crate::app::{App, CreateProviderPhase, ProviderKeyField, UpdateProviderField};
9+
use crate::app::{
10+
App, CreateProviderPhase, ProviderKeyField, ProviderProfileYaml, UpdateProviderField,
11+
};
1012

1113
use indexmap::IndexMap;
1214
use std::ops::Range;
@@ -602,7 +604,7 @@ pub fn draw_detail(frame: &mut Frame<'_>, app: &App, area: Rect) {
602604

603605
let title = if detail.show_raw_provider {
604606
" Provider Object YAML "
605-
} else if detail.show_raw_profile {
607+
} else if detail.show_raw_profile && detail.raw_profile_yaml.yaml().is_some() {
606608
" Provider Profile YAML "
607609
} else {
608610
" Provider Detail "
@@ -629,13 +631,12 @@ pub fn draw_detail(frame: &mut Frame<'_>, app: &App, area: Rect) {
629631
return;
630632
}
631633

632-
if detail.show_raw_profile {
634+
if detail.show_raw_profile
635+
&& let Some(raw) = detail.raw_profile_yaml.yaml()
636+
{
633637
draw_raw_yaml(
634638
frame,
635-
detail
636-
.raw_profile_yaml
637-
.as_deref()
638-
.unwrap_or("No provider profile is available for this provider."),
639+
raw,
639640
detail.raw_profile_scroll,
640641
"Summary",
641642
"y",
@@ -676,6 +677,18 @@ pub fn draw_detail(frame: &mut Frame<'_>, app: &App, area: Rect) {
676677
t.status_warn,
677678
)));
678679
}
680+
if matches!(detail.raw_profile_yaml, ProviderProfileYaml::Invalid) {
681+
// Fixed diagnostics bound the display and never expose values echoed by
682+
// the serializer, even when malformed profile fields contain secrets.
683+
lines.push(Line::from(Span::styled(
684+
"Profile YAML unavailable: serialization failed.",
685+
t.status_warn,
686+
)));
687+
lines.push(Line::from(Span::styled(
688+
"Correct the profile at its source, then reopen this view.",
689+
t.status_warn,
690+
)));
691+
}
679692
if let Some(description) = &detail.profile_description {
680693
lines.push(Line::from(vec![
681694
Span::styled("Description: ", t.muted),
@@ -704,7 +717,7 @@ pub fn draw_detail(frame: &mut Frame<'_>, app: &App, area: Rect) {
704717
Span::styled("[o]", t.key_hint),
705718
Span::styled(" Object YAML ", t.muted),
706719
];
707-
if detail.raw_profile_yaml.is_some() {
720+
if detail.raw_profile_yaml.yaml().is_some() {
708721
hint_spans.extend([
709722
Span::styled("[y]", t.key_hint),
710723
Span::styled(" Profile YAML ", t.muted),

0 commit comments

Comments
 (0)