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:
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user