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.
93 lines
2.6 KiB
Rust
93 lines
2.6 KiB
Rust
//! 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"));
|
|
}
|