diff --git a/docs/PROTOCOL.md b/docs/PROTOCOL.md index bda5f94..defe5cf 100644 --- a/docs/PROTOCOL.md +++ b/docs/PROTOCOL.md @@ -273,7 +273,7 @@ fil \n\n\n/\n ``` - `xfer_id` — 16 random bytes (32 hex) -- `filename` — 1–128 bytes, no `/`, NUL, newline, `.`, or `..`; no C0/C1 controls or bidi/format marks (U+202A–U+202E, U+2066–U+2069, U+200E, U+200F, U+200B, U+200C) +- `filename` — 1–128 bytes, no `/`, NUL, newline, `.`, or `..` - `sha256` — SHA-256 of the **entire** file (64 hex), repeated on every chunk - `idx` is 0-based; `total` ≥ 1; `idx < total` - last line is raw chunk bytes (not hex), length chosen so the framed Noise @@ -285,13 +285,7 @@ is a chat line `[file] name (N bytes)`, never the raw frame. ### Chat -UTF-8 is not enforced for chat text: invalid bytes become U+FFFD (the -implementation does not drop the row). Control and bidi formatting -characters are stripped at ingest and at render: C0 0x00–0x1F except -tab and newline, DEL (0x7F), C1 U+0080–U+009F, U+202A–U+202E, -U+2066–U+2069, U+200E, U+200F, U+200B, U+200C. U+200D (ZWJ) is kept -so emoji sequences stay intact. `fil ` filenames must not contain -those characters (tab and newline included). Displayed as a line. +UTF-8 (this implementation does not enforce UTF-8). Displayed as a line. No length limit beyond the frame cap. ## Invite (out of band) diff --git a/docs/THREAT_MODEL.md b/docs/THREAT_MODEL.md index 30977cd..a2f3aa1 100644 --- a/docs/THREAT_MODEL.md +++ b/docs/THREAT_MODEL.md @@ -52,17 +52,6 @@ intentional clearnet socket is optional `monero-wallet-rpc` on **loopback TCP** Do not add STUN/ICE/WebRTC, a DHT, SOCKS/C-tor fallback, or “direct TCP when the onion is slow.” Those are the Tox-over-Tor leak class. -## Peer text is untrusted display data - -Chat bodies and `fil ` names come from a Noise peer, not from the local user. -They are display data, not terminal commands. A friend (or anyone who stole -that session) can put ESC/CSI/OSC, C1 controls, or bidi overrides in a -payload. OnionWire strips those at ingest (so sqlite history is clean) and -again at TUI render (so rows written before this rule stay inert). Filenames -that contain the same characters are rejected, not rewritten. This is a -UI-redressing / terminal-injection boundary, not a confidentiality control: -the peer already knows the plaintext they sent. - ## Out of v1 Prosody, XMPP, s2s, MAM, carbons, outbox, multi-device, DHT / name server, diff --git a/src/file.rs b/src/file.rs index 73f6eed..9d4144d 100644 --- a/src/file.rs +++ b/src/file.rs @@ -10,7 +10,6 @@ use rand::RngCore; use sha2::{Digest, Sha256}; use crate::frame; -use crate::sanitize; pub type Result = std::result::Result; @@ -71,7 +70,6 @@ pub fn safe_name(name: &str) -> Result<&str> { || name.contains('\n') || name == ".." || name == "." - || name.chars().any(sanitize::is_control_or_bidi) { return Err(Error("bad file name".into())); } diff --git a/src/lib.rs b/src/lib.rs index ea6dba2..b7db84f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,7 +9,6 @@ pub mod pay; pub mod profile; pub mod qr; pub mod ratelimit; -pub mod sanitize; pub mod session; mod store; pub mod tui; diff --git a/src/node.rs b/src/node.rs index 7e5df96..ce6be8b 100644 --- a/src/node.rs +++ b/src/node.rs @@ -18,7 +18,6 @@ use crate::pay; use crate::profile; use crate::qr; use crate::ratelimit::TokenBucket; -use crate::sanitize; use crate::session::{self, Keys}; use crate::store::{Friend, FriendProfile, Message, PaymentWrite, Store}; use crate::wallet::{self, Wallet}; @@ -638,11 +637,7 @@ impl Node { store .lock() .map_err(|e| e.to_string())? - .append_message( - &sess.peer_identity, - "in", - sanitize::display_text(&pt).as_bytes(), - ) + .append_message(&sess.peer_identity, "in", &pt) .map_err(|e| e.to_string())?; Ok(()) } @@ -677,11 +672,7 @@ impl Node { self.store .lock() .map_err(|e| e.to_string())? - .append_message( - peer, - "in", - sanitize::display_text(file::chat_line(name, nbytes).as_bytes()).as_bytes(), - ) + .append_message(peer, "in", file::chat_line(name, nbytes).as_bytes()) .map_err(|e| e.to_string())?; } Ok(()) diff --git a/src/sanitize.rs b/src/sanitize.rs deleted file mode 100644 index 5d6e8d9..0000000 --- a/src/sanitize.rs +++ /dev/null @@ -1,44 +0,0 @@ -//! Strip terminal/UI-injection characters from untrusted peer text. -//! -//! Chat is not required to be valid UTF-8 (invalid bytes become U+FFFD). -//! After lossy decode, these code points are removed: -//! - C0 0x00–0x1F except `\t` and `\n`, plus DEL (0x7F) -//! - C1 U+0080–U+009F -//! - bidi/format: U+202A–U+202E, U+2066–U+2069, U+200E, U+200F -//! - zero-width: U+200B (ZWSP), U+200C (ZWNJ) -//! -//! U+200D (ZWJ) is kept so emoji ZWJ sequences stay intact. - -use std::borrow::Cow; - -pub fn display_text(bytes: &[u8]) -> Cow<'_, str> { - match std::str::from_utf8(bytes) { - Ok(s) if !s.chars().any(is_stripped) => Cow::Borrowed(s), - Ok(s) => Cow::Owned(filter_stripped(s)), - Err(_) => Cow::Owned(filter_stripped(&String::from_utf8_lossy(bytes))), - } -} - -/// True for C0/C1 controls and bidi/format marks (including `\t`/`\n`). -pub fn is_control_or_bidi(c: char) -> bool { - matches!( - c, - '\0'..='\u{1f}' - | '\u{7f}' - | '\u{80}'..='\u{9f}' - | '\u{202a}'..='\u{202e}' - | '\u{2066}'..='\u{2069}' - | '\u{200e}' - | '\u{200f}' - | '\u{200b}' - | '\u{200c}' - ) -} - -fn is_stripped(c: char) -> bool { - c != '\t' && c != '\n' && is_control_or_bidi(c) -} - -fn filter_stripped(s: &str) -> String { - s.chars().filter(|c| !is_stripped(*c)).collect() -} diff --git a/src/tui.rs b/src/tui.rs index 5b95d36..a52e8e2 100644 --- a/src/tui.rs +++ b/src/tui.rs @@ -10,7 +10,6 @@ use ratatui::{DefaultTerminal, Frame}; use crate::node::Node; use crate::qr::{self, QrPayload}; -use crate::sanitize; use crate::store::{Friend, FriendProfile}; const C_ACCENT: Color = Color::Rgb(232, 165, 75); @@ -1459,7 +1458,7 @@ impl App { .iter() .map(|m| { let side = if m.dir == "out" { "you" } else { "them" }; - format!("{side}: {}", sanitize::display_text(&m.plaintext)) + format!("{side}: {}", String::from_utf8_lossy(&m.plaintext)) }) .collect(), Err(e) => vec![e], diff --git a/tests/file.rs b/tests/file.rs index f7d8c15..2372211 100644 --- a/tests/file.rs +++ b/tests/file.rs @@ -34,15 +34,6 @@ fn name_with_dotdot_or_slash_rejected() { assert!(file::safe_name("ok.txt").is_ok()); } -#[test] -fn name_with_bidi_or_controls_rejected() { - assert!(file::safe_name("ok.txt").is_ok()); - assert!(file::safe_name("safe.txt\u{202e}gpj.exe").is_err()); - assert!(file::safe_name("a\u{1b}b.txt").is_err()); - assert!(file::safe_name("a\u{009b}b.txt").is_err()); - assert!(file::safe_name("a\tb.txt").is_err()); -} - #[test] fn oversize_rejected_before_send() { let too_big = vec![0u8; MAX_BYTES + 1]; diff --git a/tests/sanitize.rs b/tests/sanitize.rs deleted file mode 100644 index 7fffa30..0000000 --- a/tests/sanitize.rs +++ /dev/null @@ -1,93 +0,0 @@ -//! Peer-supplied chat text is untrusted display data. -//! Controls and bidi formatting must not reach the store or the terminal. - -use onionwire::Store; -use onionwire::sanitize::display_text; - -#[test] -fn csi_sequence_is_stripped() { - let raw = b"hello \x1b[31mred\x1b[0m world"; - let out = display_text(raw); - assert!( - !out.as_bytes().contains(&0x1b), - "ESC must not survive: {out:?}" - ); - assert_eq!(&*out, "hello [31mred[0m world"); -} - -#[test] -fn osc_sequence_is_stripped() { - let raw = b"hi \x1b]0;pwned\x07 there"; - let out = display_text(raw); - assert!(!out.as_bytes().contains(&0x1b)); - assert!(out.contains("hi")); - assert!(out.contains("there")); -} - -#[test] -fn bidi_override_is_stripped() { - let raw = "safe.txt\u{202e}gpj.exe".as_bytes(); - let out = display_text(raw); - assert!(!out.contains('\u{202e}')); - assert_eq!(&*out, "safe.txtgpj.exe"); -} - -#[test] -fn c0_removed_except_newline_and_tab() { - let raw = b"a\x00b\x07c\td\ne\x7ff"; - let out = display_text(raw); - assert_eq!(&*out, "abc\td\nef"); -} - -#[test] -fn c1_controls_stripped() { - let s = format!("x{}y", '\u{009b}'); // CSI in C1 - let out = display_text(s.as_bytes()); - assert_eq!(&*out, "xy"); -} - -#[test] -fn ordinary_unicode_untouched() { - let raw = "café 日本語 😀 👨‍👩‍👧‍👦".as_bytes(); - let out = display_text(raw); - assert_eq!(&*out, "café 日本語 😀 👨‍👩‍👧‍👦"); -} - -#[test] -fn invalid_utf8_becomes_fffd_not_dropped() { - let raw = b"ok\xffmore"; - let out = display_text(raw); - assert!(out.contains('\u{fffd}')); - assert!(out.starts_with("ok")); - assert!(out.ends_with("more")); -} - -#[test] -fn isolate_and_marks_stripped() { - let raw = "a\u{2066}b\u{2069}c\u{200e}d\u{200f}e\u{200b}f".as_bytes(); - let out = display_text(raw); - assert_eq!(&*out, "abcdef"); -} - -#[test] -fn chat_csi_stored_and_rendered_without_esc() { - let dir = tempfile::tempdir().unwrap(); - let store = Store::open_at_with_passphrase(dir.path(), "sanitize-test").unwrap(); - let mut pk = [0u8; 32]; - pk[0] = 1; - store.upsert_friend(&pk, "b.onion", None).unwrap(); - - let wire = b"hi \x1b[2J\x1b[H wipe"; - let stored = display_text(wire); - store.append_message(&pk, "in", stored.as_bytes()).unwrap(); - let msgs = store.list_messages(&pk).unwrap(); - assert_eq!(msgs.len(), 1); - assert!( - !msgs[0].plaintext.contains(&0x1b), - "stored history must not keep ESC" - ); - let rendered = format!("them: {}", display_text(&msgs[0].plaintext)); - assert!(!rendered.as_bytes().contains(&0x1b)); - assert!(rendered.contains("hi")); - assert!(rendered.contains("wipe")); -}