From a05c68e15eae4a6ae60a26699a9394a77cea3d1a Mon Sep 17 00:00:00 2001 From: Francisca Ortega Date: Wed, 16 Oct 2024 08:11:33 -0300 Subject: [PATCH] add: ip verification in tcp connection --- src/client/tcp_connection.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/client/tcp_connection.rs b/src/client/tcp_connection.rs index d8cdb346..c9c7cb2d 100644 --- a/src/client/tcp_connection.rs +++ b/src/client/tcp_connection.rs @@ -67,7 +67,17 @@ impl ClientConnection for ClientTCPConnection { Ok(stream_result) => stream_result?, Err(_) => return Err(ClientError::Io(IoError::new(ErrorKind::TimedOut, format!("Error: timeout"))).into()), }; - + + //Verify that the connected IP matches the expected IP + let actual_ip = stream.peer_addr()?.ip(); + let expected_ip = self.get_server_addr(); + if actual_ip != expected_ip { + return Err(ClientError::Io(IoError::new( + ErrorKind::PermissionDenied, + format!("IP mismatch: expected {}, got {}", expected_ip, actual_ip), + )).into()); + } + // Add len of message len let msg_length: u16 = bytes.len() as u16; let tcp_bytes_length: [u8; 2] = [(msg_length >> 8) as u8, msg_length as u8];