Merge pull request 'sanitize peer chat/control/bidi before store and TUI render' (#21) from wt/t_6ddd495e into main
All checks were successful
ci / test (push) Successful in 3m42s
All checks were successful
ci / test (push) Successful in 3m42s
Reviewed-on: #21
This commit is contained in:
commit
4457c7330c
9 changed files with 181 additions and 5 deletions
|
|
@ -273,7 +273,7 @@ fil <xfer_id_hex>\n<filename>\n<sha256_hex>\n<idx>/<total>\n<raw bytes>
|
|||
```
|
||||
|
||||
- `xfer_id` — 16 random bytes (32 hex)
|
||||
- `filename` — 1–128 bytes, no `/`, NUL, newline, `.`, or `..`
|
||||
- `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)
|
||||
- `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,7 +285,13 @@ is a chat line `[file] name (N bytes)`, never the raw frame.
|
|||
|
||||
### Chat
|
||||
|
||||
UTF-8 (this implementation does not enforce UTF-8). Displayed as a line.
|
||||
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.
|
||||
No length limit beyond the frame cap.
|
||||
|
||||
## Invite (out of band)
|
||||
|
|
|
|||
|
|
@ -52,6 +52,17 @@ 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,
|
||||
|
|
|
|||
|
|
@ -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],
|
||||
|
|
|
|||
|
|
@ -34,6 +34,15 @@ 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];
|
||||
|
|
|
|||
93
tests/sanitize.rs
Normal file
93
tests/sanitize.rs
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
//! 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"));
|
||||
}
|
||||
Loading…
Reference in a new issue