Skip to content

Commit 15b99c9

Browse files
committed
Always use IP of control conn for data conn
Rather than using the IP provided by the server in the response to PASV, use the IP of the server we are connected to. This is more secure since we won't connect to an arbitrary endpoint provided by the server. It also works better when the server is behind a NAT and not configured properly to provide its public IP in PASV responses. See also: GHSA-69rc-qfx4-h683
1 parent 635da8f commit 15b99c9

File tree

1 file changed

+5
-16
lines changed

1 file changed

+5
-16
lines changed

src/ftp.rs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -264,28 +264,17 @@ impl FtpStream {
264264
.ok_or_else(|| FtpError::InvalidResponse(format!("Invalid PASV response: {}", line)))
265265
.and_then(|caps| {
266266
// If the regex matches we can be sure groups contains numbers
267-
let (oct1, oct2, oct3, oct4) = (
268-
caps[1].parse::<u8>().unwrap(),
269-
caps[2].parse::<u8>().unwrap(),
270-
caps[3].parse::<u8>().unwrap(),
271-
caps[4].parse::<u8>().unwrap(),
272-
);
273267
let (msb, lsb) = (
274268
caps[5].parse::<u8>().unwrap(),
275269
caps[6].parse::<u8>().unwrap(),
276270
);
277271
let port = ((msb as u16) << 8) + lsb as u16;
278272

279-
use std::net::{IpAddr, Ipv4Addr};
280-
281-
let ip = if (oct1, oct2, oct3, oct4) == (0, 0, 0, 0) {
282-
self.get_ref()
283-
.peer_addr()
284-
.map_err(FtpError::ConnectionError)?
285-
.ip()
286-
} else {
287-
IpAddr::V4(Ipv4Addr::new(oct1, oct2, oct3, oct4))
288-
};
273+
let ip = self
274+
.get_ref()
275+
.peer_addr()
276+
.map_err(FtpError::ConnectionError)?
277+
.ip();
289278
Ok(SocketAddr::new(ip, port))
290279
})
291280
}

0 commit comments

Comments
 (0)