[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
|
|
@ -10,6 +10,7 @@ use rand::RngCore;
|
|||
use sha2::{Digest, Sha256};
|
||||
|
||||
use crate::frame;
|
||||
use crate::sanitize;
|
||||
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
|
|
@ -70,6 +71,7 @@ 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()));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ pub mod pay;
|
|||
pub mod profile;
|
||||
pub mod qr;
|
||||
pub mod ratelimit;
|
||||
pub mod sanitize;
|
||||
pub mod session;
|
||||
mod store;
|
||||
pub mod tui;
|
||||
|
|
|
|||
13
src/node.rs
13
src/node.rs
|
|
@ -18,6 +18,7 @@ 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};
|
||||
|
|
@ -637,7 +638,11 @@ impl Node {
|
|||
store
|
||||
.lock()
|
||||
.map_err(|e| e.to_string())?
|
||||
.append_message(&sess.peer_identity, "in", &pt)
|
||||
.append_message(
|
||||
&sess.peer_identity,
|
||||
"in",
|
||||
sanitize::display_text(&pt).as_bytes(),
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -672,7 +677,11 @@ impl Node {
|
|||
self.store
|
||||
.lock()
|
||||
.map_err(|e| e.to_string())?
|
||||
.append_message(peer, "in", file::chat_line(name, nbytes).as_bytes())
|
||||
.append_message(
|
||||
peer,
|
||||
"in",
|
||||
sanitize::display_text(file::chat_line(name, nbytes).as_bytes()).as_bytes(),
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
}
|
||||
Ok(())
|
||||
|
|
|
|||
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()
|
||||
}
|
||||
|
|
@ -10,6 +10,7 @@ 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);
|
||||
|
|
@ -1458,7 +1459,7 @@ impl App {
|
|||
.iter()
|
||||
.map(|m| {
|
||||
let side = if m.dir == "out" { "you" } else { "them" };
|
||||
format!("{side}: {}", String::from_utf8_lossy(&m.plaintext))
|
||||
format!("{side}: {}", sanitize::display_text(&m.plaintext))
|
||||
})
|
||||
.collect(),
|
||||
Err(e) => vec![e],
|
||||
|
|
|
|||
Loading…
Reference in a new issue