fix: reject malformed peer connection types instead of panicking

Peer::new_from_message, NewPeer::new_from_message and the listener's
parse_peer_init_message parsed an untrusted connection-type string with
.parse().unwrap(), panicking on anything other than P/F/D. Reachable from:
- a malicious server via ConnectToPeer (kills a thread-pool worker), and
- any peer connecting to our listener (kills the accept loop).

new_from_message now returns Option and callers drop malformed messages;
the listener rejects the connection. Added a regression test for truncated
TransferRequest parsing, now safe via the bounds-checked readers.
This commit is contained in:
michel
2026-07-18 14:19:50 +02:00
parent 689d8b5de1
commit 00e4036ac2
4 changed files with 55 additions and 14 deletions
@@ -9,7 +9,9 @@ impl MessageHandler<ServerMessage> for ConnectToPeerHandler {
18
}
fn handle(&self, message: &mut Message, sender: Sender<ServerMessage>) {
let peer = Peer::new_from_message(message);
sender.send(ServerMessage::ConnectToPeer(peer)).unwrap();
let Some(peer) = Peer::new_from_message(message) else {
return;
};
let _ = sender.send(ServerMessage::ConnectToPeer(peer));
}
}