feat: drop unknown typed frames instead of storing as chat
Some checks failed
ci / test (push) Has been cancelled
release / aarch64 (push) Failing after 2m8s

Classify decrypted plaintext before append_message so loc/prf/inv/rcp/png
and unknown xxx[space] prefixes never land in the message log.
This commit is contained in:
Sirius DevOps 2026-09-10 14:18:06 -04:00
commit c9d85d9ae5
No known key found for this signature in database
4 changed files with 105 additions and 18 deletions

39
src/dispatch.rs Normal file
View file

@ -0,0 +1,39 @@
//! Classify decrypted application plaintext before it hits the message log.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Kind {
Chat,
Loc,
Profile,
Invoice,
Receipt,
Ping,
Drop,
}
pub fn classify(pt: &[u8]) -> Kind {
if pt.starts_with(b"loc ") {
return Kind::Loc;
}
if pt.starts_with(b"prf ") {
return Kind::Profile;
}
if pt.starts_with(b"inv ") {
return Kind::Invoice;
}
if pt.starts_with(b"rcp ") {
return Kind::Receipt;
}
if pt.starts_with(b"png ") {
return Kind::Ping;
}
if pt.len() >= 4
&& pt[0].is_ascii_lowercase()
&& pt[1].is_ascii_lowercase()
&& pt[2].is_ascii_lowercase()
&& pt[3] == b' '
{
return Kind::Drop;
}
Kind::Chat
}

View file

@ -1,3 +1,4 @@
pub mod dispatch;
pub mod frame;
pub mod hs;
pub mod loc;

View file

@ -9,6 +9,7 @@ use futures::io::{AsyncRead, AsyncWrite};
use tor_cell::relaycell::msg::Connected;
use tor_hsservice::{RunningOnionService, handle_rend_requests};
use crate::dispatch::{self, Kind};
use crate::frame;
use crate::hs::{self, Client, HS_PORT};
use crate::loc;
@ -308,26 +309,33 @@ impl Node {
.map_err(session_err)?;
let ct = frame::read_frame(stream).await.map_err(|e| e.to_string())?;
let pt = sess.decrypt(&ct).map_err(session_err)?;
if let Some(loc) = loc::decode(&pt) {
let applied = store.lock().map_err(|e| e.to_string())?.apply_loc(
&sess.peer_identity,
&loc.onion,
loc.ts,
&loc.sig,
);
match applied {
Ok(true) => {}
Ok(false) => eprintln!("loc dropped (bad sig, stale ts, or unknown friend)"),
Err(e) => return Err(e.to_string()),
match dispatch::classify(&pt) {
Kind::Loc => {
if let Some(loc) = loc::decode(&pt) {
let applied = store.lock().map_err(|e| e.to_string())?.apply_loc(
&sess.peer_identity,
&loc.onion,
loc.ts,
&loc.sig,
);
match applied {
Ok(true) => {}
Ok(false) => eprintln!("loc dropped (bad sig, stale ts, or unknown friend)"),
Err(e) => return Err(e.to_string()),
}
}
Ok(())
}
return Ok(());
Kind::Chat => {
store
.lock()
.map_err(|e| e.to_string())?
.append_message(&sess.peer_identity, "in", &pt)
.map_err(|e| e.to_string())?;
Ok(())
}
Kind::Profile | Kind::Invoice | Kind::Receipt | Kind::Ping | Kind::Drop => Ok(()),
}
store
.lock()
.map_err(|e| e.to_string())?
.append_message(&sess.peer_identity, "in", &pt)
.map_err(|e| e.to_string())?;
Ok(())
}
}