Validate UI trees at decode time and simplify display elements (#59518)

## Why

UI tree limits previously applied during rendering, silently dropping content
beyond the budgets. Validate the whole JSON tree before rendering so oversized
inputs are rejected, including content outside the visible line.

## What changed

- Add a `UiTree` string parser that limits JSON to 64 KiB, nesting depth to 16,
  non-null nodes to 256, and total string content to 16 KiB of UTF-8.
- Represent text as string leaves or `text` elements with children. Remove node
  keys, `spacer`, and `divider` elements.
- Require an explicit `row` gap as a non-negative JavaScript-safe integer measured
  in terminal columns. Bound rendered spacing by the viewport and skip empty
  children when inserting gaps.
- Update footer content and built-in status lines to use the revised tree format.

## Testing

Add coverage for malformed elements and gaps, whole-tree size boundaries, URL
length limits, large-gap rendering, and bounded UTF-8 prefixes for directly
constructed text. Update hyperlink and footer clipping tests for the new format.

GitOrigin-RevId: 3c00881d3e7f2d79d11aa188f64f0725cd47ee68
This commit is contained in:
Joe Savona
2026-11-04 23:31:55 +00:00
committed by copyberry
parent 08511817f3
commit 47172f064f
7 changed files with 211 additions and 173 deletions
+54
View File
@@ -0,0 +1,54 @@
//! Enforce the input byte limit before parsing and the UI tree limits after deserialization.
use super::MAX_GAP;
use super::MAX_TEXT_BYTES;
use super::UiNode;
use super::UiTree;
use serde::Deserialize;
use serde::Deserializer;
use serde::de::Error;
impl std::str::FromStr for UiTree {
type Err = serde_json::Error;
fn from_str(text: &str) -> Result<Self, Self::Err> {
if text.len() > 64 * 1024 {
return Err(Self::Err::custom("UI tree JSON exceeds 64 KiB"));
}
let root: Option<UiNode> = serde_json::from_str(text)?;
let mut pending: Vec<_> = root.iter().map(|node| (node, 0)).collect();
let mut nodes = 0;
let mut text_bytes = 0;
while let Some((node, depth)) = pending.pop() {
if depth > 16 {
return Err(Self::Err::custom("UI tree exceeds maximum depth 16"));
}
nodes += 1;
if nodes > 256 {
return Err(Self::Err::custom("UI tree exceeds 256 non-null nodes"));
}
match node {
UiNode::String(text) => {
text_bytes += text.len();
if text_bytes > MAX_TEXT_BYTES {
return Err(Self::Err::custom("UI tree exceeds 16 KiB of text"));
}
}
UiNode::Text { children, .. }
| UiNode::Link { children, .. }
| UiNode::Row { children, .. } => {
pending.extend(children.iter().flatten().map(|child| (child, depth + 1)));
}
}
}
Ok(Self(root))
}
}
pub(super) fn gap<'de, D: Deserializer<'de>>(de: D) -> Result<u64, D::Error> {
let gap = u64::deserialize(de)?;
if gap > MAX_GAP {
return Err(D::Error::custom("gap exceeds the maximum safe integer"));
}
Ok(gap)
}
+38 -70
View File
@@ -1,5 +1,6 @@
//! Render the visible prefix of a horizontal display tree into one terminal line.
use super::MAX_TEXT_BYTES;
use super::UiNode;
use super::UiTree;
use crate::history_cell::sanitize_user_text;
@@ -8,27 +9,14 @@ use crate::terminal_hyperlinks::HyperlinkLine;
use crate::terminal_hyperlinks::TerminalHyperlink;
use ratatui::style::Modifier;
const MAX_DEPTH: usize = 16;
const MAX_NODES: usize = 256;
const MAX_TEXT_BYTES: usize = 16 * 1024;
struct Budget {
nodes: usize,
text_bytes: usize,
}
impl UiNode {
impl UiTree {
/// Render one row. On overflow, fit an unlinked ellipsis inside the supplied width.
pub(crate) fn render_line(&self, width: u16) -> HyperlinkLine {
let width = usize::from(width);
if width == 0 {
let Some(root) = self.0.as_ref().filter(|_| width > 0) else {
return HyperlinkLine::default();
}
let mut budget = Budget {
nodes: MAX_NODES,
text_bytes: MAX_TEXT_BYTES,
};
let mut line = node_line(Some(self), /*depth*/ 0, &mut budget).unwrap_or_default();
let mut line = node_line(root, width);
if line.width() > width {
line.line = truncate_line_to_width(line.line, width - 1);
line.clip_hyperlinks_to_width(line.width());
@@ -38,30 +26,18 @@ impl UiNode {
}
}
fn node_line(node: Option<&UiNode>, depth: usize, budget: &mut Budget) -> Option<HyperlinkLine> {
if budget.nodes == 0 {
return None;
}
budget.nodes -= 1;
if depth > MAX_DEPTH {
return None;
}
match node? {
UiNode::Text { props, .. } => {
let raw = &props.text;
if budget.text_bytes == 0 && !raw.is_empty() {
return None;
}
let end = raw.floor_char_boundary(raw.len().min(budget.text_bytes));
let first = raw[..end].split('\n').next().unwrap_or_default();
budget.text_bytes -= first.len();
let visible = sanitize_user_text(first.into()).replace('\t', " ");
Some(visible.into())
fn node_line(node: &UiNode, width: usize) -> HyperlinkLine {
match node {
UiNode::String(raw) => {
// Built-in values are constructed directly, bypassing the wire decoder.
let raw = &raw[..raw.floor_char_boundary(MAX_TEXT_BYTES)];
let first = raw.split('\n').next().unwrap_or_default();
sanitize_user_text(first.into())
.replace('\t', " ")
.into()
}
UiNode::Link {
props, children, ..
} => {
let mut line = row_line(children, /*gap*/ 0, depth, budget)?;
UiNode::Link { props, children } => {
let mut line = row_line(children, /*gap*/ 0, width);
for span in &mut line.line.spans {
span.style = span.style.add_modifier(Modifier::UNDERLINED);
}
@@ -69,48 +45,40 @@ fn node_line(node: Option<&UiNode>, depth: usize, budget: &mut Budget) -> Option
if width > 0 {
line.hyperlinks = vec![TerminalHyperlink::web(0..width, props.href.0.clone())];
}
Some(line)
line
}
UiNode::Row {
props, children, ..
} => {
// A spacing unit is four points; a terminal cell is eight points across.
let gap = (props.gap.unwrap_or(0.0).clamp(0.0, 32.0) / 2.0).ceil() as usize;
row_line(children, gap, depth, budget)
}
UiNode::Spacer { .. } => Some(" ".into()),
UiNode::Divider { .. } => Some("│".into()),
UiNode::Row { props, children } => row_line(children, props.gap, width),
UiNode::Text { children, .. } => row_line(children, /*gap*/ 0, width),
}
}
fn row_line(
children: &[UiTree],
gap: usize,
depth: usize,
budget: &mut Budget,
) -> Option<HyperlinkLine> {
let mut result: Option<HyperlinkLine> = None;
fn row_line(children: &[Option<UiNode>], gap: u64, width: usize) -> HyperlinkLine {
let mut result = HyperlinkLine::default();
let mut used = 0;
for child in children {
if budget.nodes == 0 {
break;
}
let Some(child) = node_line(child.as_ref(), depth + 1, budget) else {
for child in children.iter().flatten() {
let child = node_line(child, width);
let child_width = child.width();
if child_width == 0 {
continue;
};
let spacing = if result.is_some() { gap } else { 0 };
let line = result.get_or_insert_default();
if spacing > 0 {
line.line.push_span(" ".repeat(spacing));
}
let offset = used + spacing;
used = offset + child.width();
line.line.spans.extend(child.line.spans);
line.hyperlinks
if used > 0 && gap > 0 {
// One column past the viewport is sufficient to detect overflow.
let spacing = gap.min((width + 1 - used) as u64) as usize;
result.line.push_span(" ".repeat(spacing));
used += spacing;
}
let offset = used;
result.line.spans.extend(child.line.spans);
result
.hyperlinks
.extend(child.hyperlinks.into_iter().map(|mut link| {
link.columns = link.columns.start + offset..link.columns.end + offset;
link
}));
used += child_width;
if used > width {
break;
}
}
result
}