Merge branch 'main' into wt/t_fc02d8b6
All checks were successful
ci / test (pull_request) Successful in 3m2s
All checks were successful
ci / test (pull_request) Successful in 3m2s
This commit is contained in:
commit
dcb98132e2
13 changed files with 552 additions and 21 deletions
|
|
@ -12,6 +12,7 @@ use rand::RngCore;
|
|||
use sha2::{Digest, Sha256};
|
||||
|
||||
use crate::frame;
|
||||
use crate::sanitize;
|
||||
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
|
|
@ -79,6 +80,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;
|
||||
|
|
|
|||
26
src/loc.rs
26
src/loc.rs
|
|
@ -27,7 +27,7 @@ pub fn sign(identity_sk: &[u8], onion: &str, ts: i64) -> Result<Loc> {
|
|||
.try_into()
|
||||
.map_err(|_| Error("identity secret key must be 32 bytes".into()))?;
|
||||
let sk = SigningKey::from_bytes(&sk_bytes);
|
||||
let sig = sk.sign(&sign_msg(onion, ts)).to_bytes().to_vec();
|
||||
let sig = sk.sign(&sign_msg_v2(onion, ts)).to_bytes().to_vec();
|
||||
Ok(Loc {
|
||||
onion: onion.to_string(),
|
||||
ts,
|
||||
|
|
@ -50,8 +50,12 @@ pub fn verify(identity_pk: &[u8], loc: &Loc) -> bool {
|
|||
let Ok(vk) = VerifyingKey::from_bytes(&pk) else {
|
||||
return false;
|
||||
};
|
||||
vk.verify(&sign_msg(&loc.onion, loc.ts), &Signature::from_bytes(&sig))
|
||||
let signature = Signature::from_bytes(&sig);
|
||||
vk.verify(&sign_msg_v2(&loc.onion, loc.ts), &signature)
|
||||
.is_ok()
|
||||
|| vk
|
||||
.verify(&sign_msg_v1(&loc.onion, loc.ts), &signature)
|
||||
.is_ok()
|
||||
}
|
||||
|
||||
pub fn encode(loc: &Loc) -> Vec<u8> {
|
||||
|
|
@ -77,13 +81,29 @@ pub fn decode(pt: &[u8]) -> Option<Loc> {
|
|||
Some(Loc { onion, ts, sig })
|
||||
}
|
||||
|
||||
fn sign_msg(onion: &str, ts: i64) -> Vec<u8> {
|
||||
fn sign_msg_v2(onion: &str, ts: i64) -> Vec<u8> {
|
||||
tagged(
|
||||
b"onionwire-loc-v1",
|
||||
&[onion.as_bytes(), ts.to_string().as_bytes()],
|
||||
)
|
||||
}
|
||||
|
||||
fn sign_msg_v1(onion: &str, ts: i64) -> Vec<u8> {
|
||||
let mut msg = Vec::from(onion.as_bytes());
|
||||
msg.push(b'\n');
|
||||
msg.extend_from_slice(ts.to_string().as_bytes());
|
||||
msg
|
||||
}
|
||||
|
||||
fn tagged(tag: &[u8], fields: &[&[u8]]) -> Vec<u8> {
|
||||
let mut msg = Vec::from(tag);
|
||||
for f in fields {
|
||||
msg.push(0);
|
||||
msg.extend_from_slice(f);
|
||||
}
|
||||
msg
|
||||
}
|
||||
|
||||
fn to_hex(bytes: &[u8]) -> String {
|
||||
bytes.iter().map(|b| format!("{b:02x}")).collect()
|
||||
}
|
||||
|
|
|
|||
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};
|
||||
|
|
@ -638,7 +639,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(())
|
||||
}
|
||||
|
|
@ -673,7 +678,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(())
|
||||
|
|
|
|||
59
src/pay.rs
59
src/pay.rs
|
|
@ -179,7 +179,10 @@ pub fn sign_invoice(
|
|||
ts: i64,
|
||||
) -> Result<Invoice> {
|
||||
check_invoice_fields(amount_atomic, address, memo)?;
|
||||
let sig = sign_bytes(identity_sk, &inv_sign_msg(amount_atomic, address, memo, ts))?;
|
||||
let sig = sign_bytes(
|
||||
identity_sk,
|
||||
&inv_sign_msg_v2(amount_atomic, address, memo, ts),
|
||||
)?;
|
||||
Ok(Invoice {
|
||||
amount_atomic: amount_atomic.to_string(),
|
||||
address: address.to_string(),
|
||||
|
|
@ -195,7 +198,11 @@ pub fn verify_invoice(identity_pk: &[u8], inv: &Invoice) -> bool {
|
|||
}
|
||||
verify_bytes(
|
||||
identity_pk,
|
||||
&inv_sign_msg(&inv.amount_atomic, &inv.address, &inv.memo, inv.ts),
|
||||
&inv_sign_msg_v2(&inv.amount_atomic, &inv.address, &inv.memo, inv.ts),
|
||||
&inv.sig,
|
||||
) || verify_bytes(
|
||||
identity_pk,
|
||||
&inv_sign_msg_v1(&inv.amount_atomic, &inv.address, &inv.memo, inv.ts),
|
||||
&inv.sig,
|
||||
)
|
||||
}
|
||||
|
|
@ -243,7 +250,10 @@ pub fn sign_receipt(
|
|||
ts: i64,
|
||||
) -> Result<Receipt> {
|
||||
check_receipt_fields(txid, amount_atomic, address)?;
|
||||
let sig = sign_bytes(identity_sk, &rcp_sign_msg(txid, amount_atomic, address, ts))?;
|
||||
let sig = sign_bytes(
|
||||
identity_sk,
|
||||
&rcp_sign_msg_v2(txid, amount_atomic, address, ts),
|
||||
)?;
|
||||
Ok(Receipt {
|
||||
txid: txid.to_string(),
|
||||
amount_atomic: amount_atomic.to_string(),
|
||||
|
|
@ -259,7 +269,11 @@ pub fn verify_receipt(identity_pk: &[u8], rcp: &Receipt) -> bool {
|
|||
}
|
||||
verify_bytes(
|
||||
identity_pk,
|
||||
&rcp_sign_msg(&rcp.txid, &rcp.amount_atomic, &rcp.address, rcp.ts),
|
||||
&rcp_sign_msg_v2(&rcp.txid, &rcp.amount_atomic, &rcp.address, rcp.ts),
|
||||
&rcp.sig,
|
||||
) || verify_bytes(
|
||||
identity_pk,
|
||||
&rcp_sign_msg_v1(&rcp.txid, &rcp.amount_atomic, &rcp.address, rcp.ts),
|
||||
&rcp.sig,
|
||||
)
|
||||
}
|
||||
|
|
@ -316,7 +330,19 @@ fn check_receipt_fields(txid: &str, amount_atomic: &str, address: &str) -> Resul
|
|||
check_address(address)
|
||||
}
|
||||
|
||||
fn inv_sign_msg(amount_atomic: &str, address: &str, memo: &str, ts: i64) -> Vec<u8> {
|
||||
fn inv_sign_msg_v2(amount_atomic: &str, address: &str, memo: &str, ts: i64) -> Vec<u8> {
|
||||
tagged(
|
||||
b"onionwire-inv-v1",
|
||||
&[
|
||||
amount_atomic.as_bytes(),
|
||||
address.as_bytes(),
|
||||
memo.as_bytes(),
|
||||
ts.to_string().as_bytes(),
|
||||
],
|
||||
)
|
||||
}
|
||||
|
||||
fn inv_sign_msg_v1(amount_atomic: &str, address: &str, memo: &str, ts: i64) -> Vec<u8> {
|
||||
let mut msg = Vec::from(amount_atomic.as_bytes());
|
||||
msg.push(b'\n');
|
||||
msg.extend_from_slice(address.as_bytes());
|
||||
|
|
@ -327,7 +353,19 @@ fn inv_sign_msg(amount_atomic: &str, address: &str, memo: &str, ts: i64) -> Vec<
|
|||
msg
|
||||
}
|
||||
|
||||
fn rcp_sign_msg(txid: &str, amount_atomic: &str, address: &str, ts: i64) -> Vec<u8> {
|
||||
fn rcp_sign_msg_v2(txid: &str, amount_atomic: &str, address: &str, ts: i64) -> Vec<u8> {
|
||||
tagged(
|
||||
b"onionwire-rcp-v1",
|
||||
&[
|
||||
txid.as_bytes(),
|
||||
amount_atomic.as_bytes(),
|
||||
address.as_bytes(),
|
||||
ts.to_string().as_bytes(),
|
||||
],
|
||||
)
|
||||
}
|
||||
|
||||
fn rcp_sign_msg_v1(txid: &str, amount_atomic: &str, address: &str, ts: i64) -> Vec<u8> {
|
||||
let mut msg = Vec::from(txid.as_bytes());
|
||||
msg.push(b'\n');
|
||||
msg.extend_from_slice(amount_atomic.as_bytes());
|
||||
|
|
@ -338,6 +376,15 @@ fn rcp_sign_msg(txid: &str, amount_atomic: &str, address: &str, ts: i64) -> Vec<
|
|||
msg
|
||||
}
|
||||
|
||||
fn tagged(tag: &[u8], fields: &[&[u8]]) -> Vec<u8> {
|
||||
let mut msg = Vec::from(tag);
|
||||
for f in fields {
|
||||
msg.push(0);
|
||||
msg.extend_from_slice(f);
|
||||
}
|
||||
msg
|
||||
}
|
||||
|
||||
fn sign_bytes(identity_sk: &[u8], msg: &[u8]) -> Result<Vec<u8>> {
|
||||
let sk_bytes: [u8; 32] = identity_sk
|
||||
.try_into()
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ pub fn sign(
|
|||
.map_err(|_| Error("identity secret key must be 32 bytes".into()))?;
|
||||
let sk = SigningKey::from_bytes(&sk_bytes);
|
||||
let sig = sk
|
||||
.sign(&sign_msg(display_name, bio, xmr_addr, ts))
|
||||
.sign(&sign_msg_v2(display_name, bio, xmr_addr, ts))
|
||||
.to_bytes()
|
||||
.to_vec();
|
||||
Ok(Profile {
|
||||
|
|
@ -73,11 +73,18 @@ pub fn verify(identity_pk: &[u8], prf: &Profile) -> bool {
|
|||
let Ok(vk) = VerifyingKey::from_bytes(&pk) else {
|
||||
return false;
|
||||
};
|
||||
let signature = Signature::from_bytes(&sig);
|
||||
vk.verify(
|
||||
&sign_msg(&prf.display_name, &prf.bio, &prf.xmr_addr, prf.ts),
|
||||
&Signature::from_bytes(&sig),
|
||||
&sign_msg_v2(&prf.display_name, &prf.bio, &prf.xmr_addr, prf.ts),
|
||||
&signature,
|
||||
)
|
||||
.is_ok()
|
||||
|| vk
|
||||
.verify(
|
||||
&sign_msg_v1(&prf.display_name, &prf.bio, &prf.xmr_addr, prf.ts),
|
||||
&signature,
|
||||
)
|
||||
.is_ok()
|
||||
}
|
||||
|
||||
pub fn encode(prf: &Profile) -> Vec<u8> {
|
||||
|
|
@ -131,7 +138,19 @@ fn check_fields(display_name: &str, bio: &str, xmr_addr: &str) -> Result<()> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn sign_msg(display_name: &str, bio: &str, xmr_addr: &str, ts: i64) -> Vec<u8> {
|
||||
fn sign_msg_v2(display_name: &str, bio: &str, xmr_addr: &str, ts: i64) -> Vec<u8> {
|
||||
tagged(
|
||||
b"onionwire-prf-v1",
|
||||
&[
|
||||
display_name.as_bytes(),
|
||||
bio.as_bytes(),
|
||||
xmr_addr.as_bytes(),
|
||||
ts.to_string().as_bytes(),
|
||||
],
|
||||
)
|
||||
}
|
||||
|
||||
fn sign_msg_v1(display_name: &str, bio: &str, xmr_addr: &str, ts: i64) -> Vec<u8> {
|
||||
let mut msg = Vec::from(display_name.as_bytes());
|
||||
msg.push(b'\n');
|
||||
msg.extend_from_slice(bio.as_bytes());
|
||||
|
|
@ -142,6 +161,15 @@ fn sign_msg(display_name: &str, bio: &str, xmr_addr: &str, ts: i64) -> Vec<u8> {
|
|||
msg
|
||||
}
|
||||
|
||||
fn tagged(tag: &[u8], fields: &[&[u8]]) -> Vec<u8> {
|
||||
let mut msg = Vec::from(tag);
|
||||
for f in fields {
|
||||
msg.push(0);
|
||||
msg.extend_from_slice(f);
|
||||
}
|
||||
msg
|
||||
}
|
||||
|
||||
fn to_hex(bytes: &[u8]) -> String {
|
||||
bytes.iter().map(|b| format!("{b:02x}")).collect()
|
||||
}
|
||||
|
|
|
|||
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