Skip to content
Merged
Show file tree
Hide file tree
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
34 changes: 34 additions & 0 deletions pkgs/id/src/commands/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
24 changes: 22 additions & 2 deletions pkgs/id/src/world_ssh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<String>()
.replace('\n', "\r\n")
}
Expand Down Expand Up @@ -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();
Expand Down
Loading