From 2463da2645d4706b6ead350a500975a6c197e41a Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Mon, 2 Nov 2026 10:38:10 -0600 Subject: [PATCH] Phase 3b (#3456): plain-output contract statement + terminal_text strips Cf Contract (#3455): a plain-text field is row-safe only with no structure character (Cc + U+2028/U+2029) and no format character (shared Cf set). Disposition by surface kind: row renderers escape, stripping sanitizers strip, ingest validators refuse. Shared set home: id-core unicode.rs, Unicode 16. Fix: world_ssh terminal_text also strips crate::is_format_char (was Cc-only), with regression tests. All other inventory sites verified conformant or deliberately-local; web/identity sanitize_name filed as a follow-up (partial Cf strip). --- pkgs/id/src/commands/text.rs | 34 ++++++++++++++++++++++++++++++++++ pkgs/id/src/world_ssh.rs | 24 ++++++++++++++++++++++-- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/pkgs/id/src/commands/text.rs b/pkgs/id/src/commands/text.rs index 4eafaaf74d..b3af78ac04 100644 --- a/pkgs/id/src/commands/text.rs +++ b/pkgs/id/src/commands/text.rs @@ -33,6 +33,40 @@ //! A quoted field is a JSON string literal, so a consumer that needs the //! original bytes can parse it as JSON. Values with no structure-breaking //! character are the common case and stay unwrapped, exactly as before. +//! +//! # The plain-output contract (design #3455, Phase 3b) +//! +//! A plain-text field is row-safe only if it contains no character that can +//! act on the terminal. There are exactly two such classes: +//! +//! * **Structure characters**: control characters (category Cc, matched by +//! `char::is_control`) plus the line/paragraph separators `U+2028` / +//! `U+2029`, which are not controls but which some consumers treat as line +//! breaks. +//! * **Format characters**: category Cf, per the crate's one shared set +//! (`crate::is_format_char`). These are invisible or reorder the text +//! around them (bidi overrides/isolates, zero-width characters), so they +//! can falsify a row's columns at the terminal even from inside quotes. +//! +//! Every plain renderer in `pkgs/id` accounts for both classes; the +//! disposition differs by surface kind: +//! +//! * **Row renderers escape**: [`plain_field`] (this module) and the +//! `key=value` renderer (`crate::commands::group`) quote an affected value and +//! render each offending character as `\uXXXX`, so the value stays complete +//! and JSON-recoverable but no raw actor reaches the terminal. +//! * **Stripping sanitizers strip**: `crate::world::sanitize_display_name`, +//! `crate::world::sanitize_chat` and the SSH world's terminal gate drop both +//! classes (chat keeps `ZWJ`/`ZWNJ` so legitimate emoji survive; the SSH +//! gate keeps `LF`/`TAB` so terminal layout survives). +//! * **Ingest validators refuse**: directory names/descriptions, invite +//! names and petnames refuse both classes at the boundary, so no new +//! stored value carries one. +//! +//! No surface re-derives the character set: the single definition is +//! `crate::is_format_char`, the complete Unicode 16 general-category Cf +//! set, defined once in `id-core` (`crates/id-core/src/unicode.rs`) and +//! shared by validation and rendering. use std::fmt::Display; diff --git a/pkgs/id/src/world_ssh.rs b/pkgs/id/src/world_ssh.rs index 9ae4db4b61..ab35d719fe 100644 --- a/pkgs/id/src/world_ssh.rs +++ b/pkgs/id/src/world_ssh.rs @@ -1792,10 +1792,15 @@ impl Typing { } /// Terminal text from the world: control characters are dropped, so text from -/// one participant cannot drive another participant's terminal. +/// one participant cannot drive another participant's terminal. Unicode +/// format characters (Cf, per the shared `crate::is_format_char` set) are +/// dropped for the same reason: a bidi override or zero-width character in +/// one participant's text could otherwise reorder or falsify the terminal +/// output around it (design #3455). The accepted cost is that `ZWJ`-joined +/// emoji render as separate glyphs here; the stored text is untouched. fn terminal_text(data: &str) -> String { data.chars() - .filter(|ch| !ch.is_control() || matches!(ch, '\n' | '\t')) + .filter(|ch| (!ch.is_control() || matches!(ch, '\n' | '\t')) && !crate::is_format_char(*ch)) .collect::() .replace('\n', "\r\n") } @@ -2955,6 +2960,21 @@ mod tests { ); } + #[test] + fn terminal_text_drops_format_characters_but_keeps_layout() { + // Bidi overrides and zero-width characters never reach the terminal, + // even without any control character present (#3455 Phase 3b). + assert_eq!(terminal_text("a\u{202e}b"), "ab"); + assert_eq!(terminal_text("a\u{200b}b"), "ab"); + assert_eq!(terminal_text("a\u{feff}b"), "ab"); + // The shared Cf set is the gate, not a bidi-only subset. + assert!(!terminal_text("\u{00ad}").chars().any(|c| c == '\u{00ad}')); + // Layout survivors are unchanged: LF/TAB are kept (as CRLF/TAB). + assert_eq!(terminal_text("a\nb\tc"), "a\r\nb\tc"); + // Ordinary text, including non-joiner emoji bases, is byte-identical. + assert_eq!(terminal_text("naïve-✦サーバー"), "naïve-✦サーバー"); + } + #[test] fn world_line_buffer_handles_fragmented_packets_and_crlf_once() { let mut input = WorldLineBuffer::default();