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
+17
View File
@@ -174,3 +174,20 @@ impl Transfer {
}
}
}
#[cfg(test)]
mod tests {
use super::*;
// A truncated TransferRequest from an untrusted peer must parse to defaults
// rather than panic (the read_* primitives are bounds-checked).
#[test]
fn transfer_new_from_truncated_message_does_not_panic() {
let mut message = Message::new_with_data(vec![1, 0, 0]);
let transfer = Transfer::new_from_message(&mut message);
assert_eq!(transfer.direction, 0);
assert_eq!(transfer.token, 0);
assert_eq!(transfer.filename, "");
assert_eq!(transfer.size, 0);
}
}