mirror of
https://github.com/chatmail/core.git
synced 2026-04-28 19:06:35 +03:00
fix: Fix skip_smtp_greeting() (#5911)
- Skip lines starting with "220-" (w/o whitespace at the end). - Don't forget to clear the buffer before reading the next line.
This commit is contained in:
@@ -99,11 +99,12 @@ pub(crate) async fn connect_stream(
|
|||||||
async fn skip_smtp_greeting<R: tokio::io::AsyncBufReadExt + Unpin>(stream: &mut R) -> Result<()> {
|
async fn skip_smtp_greeting<R: tokio::io::AsyncBufReadExt + Unpin>(stream: &mut R) -> Result<()> {
|
||||||
let mut line = String::with_capacity(512);
|
let mut line = String::with_capacity(512);
|
||||||
loop {
|
loop {
|
||||||
|
line.clear();
|
||||||
let read = stream.read_line(&mut line).await?;
|
let read = stream.read_line(&mut line).await?;
|
||||||
if read == 0 {
|
if read == 0 {
|
||||||
bail!("Unexpected EOF while reading SMTP greeting.");
|
bail!("Unexpected EOF while reading SMTP greeting.");
|
||||||
}
|
}
|
||||||
if line.starts_with("220- ") {
|
if line.starts_with("220-") {
|
||||||
continue;
|
continue;
|
||||||
} else if line.starts_with("220 ") {
|
} else if line.starts_with("220 ") {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
@@ -207,3 +208,19 @@ async fn connect_insecure(addr: SocketAddr) -> Result<Box<dyn SessionBufStream>>
|
|||||||
let session_stream: Box<dyn SessionBufStream> = Box::new(buffered_stream);
|
let session_stream: Box<dyn SessionBufStream> = Box::new(buffered_stream);
|
||||||
Ok(session_stream)
|
Ok(session_stream)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use tokio::io::BufReader;
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||||
|
async fn test_skip_smtp_greeting() -> Result<()> {
|
||||||
|
let greeting = b"220-server261.web-hosting.com ESMTP Exim 4.96.2 #2 Sat, 24 Aug 2024 12:25:53 -0400 \r\n\
|
||||||
|
220-We do not authorize the use of this system to transport unsolicited,\r\n\
|
||||||
|
220 and/or bulk e-mail.\r\n";
|
||||||
|
let mut buffered_stream = BufReader::new(&greeting[..]);
|
||||||
|
skip_smtp_greeting(&mut buffered_stream).await
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user