From be0fe3cf24f3ae5e108f4b4389bee3a37848685b Mon Sep 17 00:00:00 2001 From: G26Karthik Date: Wed, 11 Nov 2026 05:47:33 +0530 Subject: [PATCH] Keep the control connection open after a 501 reply FtpCodec::decode returned line parser errors as decoder errors. Framed ends the stream after a decoder error, so the control loop saw the connection as closed after sending the 501. Return the parse result as the item instead and let handle_control_channel_error decide whether to close. --- src/server/controlchan/codecs.rs | 8 +++++--- src/server/controlchan/control_loop.rs | 2 +- tests/invalid_parameter.rs | 22 ++++++++++++++++++++++ 3 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 tests/invalid_parameter.rs diff --git a/src/server/controlchan/codecs.rs b/src/server/controlchan/codecs.rs index c8cb030e..60271ff1 100644 --- a/src/server/controlchan/codecs.rs +++ b/src/server/controlchan/codecs.rs @@ -21,17 +21,19 @@ impl FtpCodec { } impl Decoder for FtpCodec { - type Item = Command; + // A malformed line is an item, not a decoder error: Framed ends the stream after a decoder + // error, and the control loop would then close the connection. + type Item = Result; type Error = ControlChanError; // Here we decode the incoming bytes into a meaningful command. We'll split on newlines, and // parse the resulting line using `Command::parse()`. This method will be called by tokio. - fn decode(&mut self, buf: &mut BytesMut) -> Result, Self::Error> { + fn decode(&mut self, buf: &mut BytesMut) -> Result, Self::Error> { if let Some(newline_offset) = buf[self.next_index..].iter().position(|b| *b == b'\n') { let newline_index = newline_offset + self.next_index; let line = buf.split_to(newline_index + 1); self.next_index = 0; - Ok(Some(line_parser::parse(line)?)) + Ok(Some(line_parser::parse(line).map_err(ControlChanError::from))) } else { self.next_index = buf.len(); Ok(None) diff --git a/src/server/controlchan/control_loop.rs b/src/server/controlchan/control_loop.rs index 996315b4..aef92a33 100644 --- a/src/server/controlchan/control_loop.rs +++ b/src/server/controlchan/control_loop.rs @@ -233,7 +233,7 @@ where tokio::select! { cmd = command_source.next() => { match cmd { - Some(cmd_result) => incoming = Some(cmd_result.map(Event::Command)), + Some(cmd_result) => incoming = Some(cmd_result.flatten().map(Event::Command)), None => { slog::info!(logger, "Control connection was closed."); incoming = Some(Ok(Event::InternalMsg(ControlChanMsg::ExitControlLoop))) diff --git a/tests/invalid_parameter.rs b/tests/invalid_parameter.rs new file mode 100644 index 00000000..87be4376 --- /dev/null +++ b/tests/invalid_parameter.rs @@ -0,0 +1,22 @@ +#![allow(missing_docs)] + +pub mod common; + +use common::{read_from_server, send_to_server, tcp_connect}; + +#[tokio::test(flavor = "current_thread")] +async fn session_survives_invalid_parameter() { + common::initialize().await; + + let stream = tcp_connect().await.unwrap(); + let mut buffer = vec![0_u8; 1024]; + assert_eq!(read_from_server(&mut buffer, &stream).await, "220 Welcome test\r\n"); + + send_to_server("REST abc\r\n", &stream).await; + assert_eq!(read_from_server(&mut buffer, &stream).await, "501 Invalid Parameter\r\n"); + + send_to_server("NOOP\r\n", &stream).await; + assert_eq!(read_from_server(&mut buffer, &stream).await, "200 Successfully did nothing\r\n"); + + common::finalize().await; +}