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