Merge branch 'main' into wt/t_fc02d8b6
All checks were successful
ci / test (pull_request) Successful in 3m2s

This commit is contained in:
sirius 2026-09-12 04:11:00 +00:00
commit dcb98132e2
13 changed files with 552 additions and 21 deletions

200
tests/app_sig.rs Normal file
View file

@ -0,0 +1,200 @@
//! Domain-separated application-frame signatures (loc/prf/inv/rcp).
//!
//! Encode still emits typed prefixes and LF-joined fields. Only the signed
//! bytes change: tagged `0x00`-separated form on emit, dual-accept of the
//! legacy untagged LF layout on verify.
use ed25519_dalek::{Signer, SigningKey};
use onionwire::loc::{self, Loc};
use onionwire::pay::{self, Invoice, Receipt};
use onionwire::profile::{self, Profile};
use rand::rngs::OsRng;
const MAINNET_STD: &str = "4AdUndXHHZ6cfufTMvppY6JwXNouMBzSkbLYfpAV5Usx3skxNgYeYTRj5UzqtReoS44qo9mtmXCqY45DJ852K5Jv2684Rge";
fn keypair() -> (SigningKey, [u8; 32]) {
let sk = SigningKey::generate(&mut OsRng);
let pk = sk.verifying_key().to_bytes();
(sk, pk)
}
#[test]
fn tagged_loc_roundtrip() {
let (sk, pk) = keypair();
let loc = loc::sign(&sk.to_bytes(), "abc.onion", 42).unwrap();
let parsed = loc::decode(&loc::encode(&loc)).expect("loc frame");
assert!(loc::verify(&pk, &parsed));
assert_eq!(parsed.onion, "abc.onion");
assert_eq!(parsed.ts, 42);
}
#[test]
fn tagged_prf_roundtrip() {
let (sk, pk) = keypair();
let prf = profile::sign(&sk.to_bytes(), "Ali", "hi", "", 2_000_000_000).unwrap();
let parsed = profile::decode(&profile::encode(&prf)).expect("prf frame");
assert!(profile::verify(&pk, &parsed));
assert_eq!(parsed.display_name, "Ali");
assert_eq!(parsed.bio, "hi");
assert_eq!(parsed.xmr_addr, "");
assert_eq!(parsed.ts, 2_000_000_000);
}
#[test]
fn tagged_inv_roundtrip() {
let (sk, pk) = keypair();
let inv = pay::sign_invoice(&sk.to_bytes(), "120000000000", MAINNET_STD, "coffee", 42).unwrap();
let parsed = pay::decode_invoice(&pay::encode_invoice(&inv)).expect("inv frame");
assert!(pay::verify_invoice(&pk, &parsed));
assert_eq!(parsed.amount_atomic, "120000000000");
assert_eq!(parsed.address, MAINNET_STD);
assert_eq!(parsed.memo, "coffee");
assert_eq!(parsed.ts, 42);
}
#[test]
fn tagged_rcp_roundtrip() {
let (sk, pk) = keypair();
let rcp = pay::sign_receipt(&sk.to_bytes(), "deadbeef", "1", MAINNET_STD, 99).unwrap();
let parsed = pay::decode_receipt(&pay::encode_receipt(&rcp)).expect("rcp frame");
assert!(pay::verify_receipt(&pk, &parsed));
assert_eq!(parsed.txid, "deadbeef");
assert_eq!(parsed.amount_atomic, "1");
assert_eq!(parsed.address, MAINNET_STD);
assert_eq!(parsed.ts, 99);
}
#[test]
fn legacy_untagged_loc_still_verifies() {
let (sk, pk) = keypair();
let onion = "abc.onion";
let ts = 42i64;
let mut msg = Vec::from(onion.as_bytes());
msg.push(b'\n');
msg.extend_from_slice(ts.to_string().as_bytes());
let loc = Loc {
onion: onion.into(),
ts,
sig: sk.sign(&msg).to_bytes().to_vec(),
};
assert!(
loc::verify(&pk, &loc),
"legacy loc LF layout must still verify"
);
}
#[test]
fn legacy_untagged_prf_still_verifies() {
let (sk, pk) = keypair();
let ts = 2_000_000_000i64;
let mut msg = Vec::from(b"Ali".as_slice());
msg.push(b'\n');
msg.extend_from_slice(b"hi");
msg.push(b'\n');
msg.extend_from_slice(b"");
msg.push(b'\n');
msg.extend_from_slice(ts.to_string().as_bytes());
let prf = Profile {
display_name: "Ali".into(),
bio: "hi".into(),
xmr_addr: String::new(),
ts,
sig: sk.sign(&msg).to_bytes().to_vec(),
};
assert!(
profile::verify(&pk, &prf),
"legacy prf LF layout must still verify"
);
}
#[test]
fn legacy_untagged_inv_still_verifies() {
let (sk, pk) = keypair();
let ts = 42i64;
let mut msg = Vec::from(b"1".as_slice());
msg.push(b'\n');
msg.extend_from_slice(MAINNET_STD.as_bytes());
msg.push(b'\n');
msg.extend_from_slice(b"coffee");
msg.push(b'\n');
msg.extend_from_slice(ts.to_string().as_bytes());
let inv = Invoice {
amount_atomic: "1".into(),
address: MAINNET_STD.into(),
memo: "coffee".into(),
ts,
sig: sk.sign(&msg).to_bytes().to_vec(),
};
assert!(
pay::verify_invoice(&pk, &inv),
"legacy inv LF layout must still verify"
);
}
#[test]
fn legacy_untagged_rcp_still_verifies() {
let (sk, pk) = keypair();
let ts = 99i64;
let mut msg = Vec::from(b"deadbeef".as_slice());
msg.push(b'\n');
msg.extend_from_slice(b"1");
msg.push(b'\n');
msg.extend_from_slice(MAINNET_STD.as_bytes());
msg.push(b'\n');
msg.extend_from_slice(ts.to_string().as_bytes());
let rcp = Receipt {
txid: "deadbeef".into(),
amount_atomic: "1".into(),
address: MAINNET_STD.into(),
ts,
sig: sk.sign(&msg).to_bytes().to_vec(),
};
assert!(
pay::verify_receipt(&pk, &rcp),
"legacy rcp LF layout must still verify"
);
}
/// prf and rcp both sign four fields. Without a type tag, a profile whose
/// fields are also a valid receipt (name=txid, bio=amount, xmr=address)
/// verifies as a receipt under the same key.
#[test]
fn prf_signature_does_not_verify_as_rcp() {
let (sk, pk) = keypair();
let ts = 42i64;
let prf = profile::sign(&sk.to_bytes(), "deadbeef", "1", MAINNET_STD, ts).unwrap();
assert!(profile::verify(&pk, &prf));
let rcp = Receipt {
txid: "deadbeef".into(),
amount_atomic: "1".into(),
address: MAINNET_STD.into(),
ts,
sig: prf.sig.clone(),
};
assert!(
!pay::verify_receipt(&pk, &rcp),
"prf-shaped payload must not verify as rcp"
);
}
/// inv field order is amount/address/memo/ts; rcp is txid/amount/address/ts.
/// Reinterpret the invoice fields in order as a receipt and the signature
/// must not verify (and field checks fail closed on a non-decimal amount).
#[test]
fn inv_signature_does_not_verify_as_rcp() {
let (sk, pk) = keypair();
let ts = 42i64;
let inv = pay::sign_invoice(&sk.to_bytes(), "1", MAINNET_STD, MAINNET_STD, ts).unwrap();
assert!(pay::verify_invoice(&pk, &inv));
let rcp = Receipt {
txid: "1".into(),
amount_atomic: MAINNET_STD.into(),
address: MAINNET_STD.into(),
ts,
sig: inv.sig.clone(),
};
assert!(
!pay::verify_receipt(&pk, &rcp),
"inv-shaped payload must not verify as rcp"
);
}

View file

@ -37,6 +37,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
View 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"));
}