[verified] sanitize peer chat/control/bidi before store and TUI render
Some checks failed
ci / test (pull_request) Failing after 2m2s
Some checks failed
ci / test (pull_request) Failing after 2m2s
Strip C0 (except tab/newline), DEL, C1, and bidi/format marks at ingest and render; keep ZWJ; lossy UTF-8 stays U+FFFD. Reject the same set in fil names. Docs match the policy.
This commit is contained in:
parent
cb91ba2b78
commit
e3738c51f7
9 changed files with 181 additions and 5 deletions
44
src/sanitize.rs
Normal file
44
src/sanitize.rs
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
//! 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()
|
||||
}
|
||||
Loading…
Reference in a new issue