SMTP transport panics on an EHLO reply line consisting only of whitespace
Description
ServerInfo::from_response (src/transport/smtp/extension.rs) panics when a line of the EHLO reply is non-empty but consists only of whitespace. The line passes the line.is_empty() check, and line.split_whitespace().next().unwrap() then unwraps None.
The EHLO reply comes from the network peer. Before STARTTLS (or on a plaintext connection) it is unauthenticated, so besides a malicious server, anyone on the path can trigger the panic.
Details
for line in response.message() {
if line.is_empty() {
continue;
}
let mut split = line.split_whitespace();
match split.next().unwrap() { // None for " "A reply such as 250-mail.example\r\n250 \r\r\n parses into the lines ["mail.example", "\r"] (or other whitespace-only text), which reaches the unwrap().
PoC
use lettre::transport::smtp::{extension::ServerInfo, response::Response};
let response: Response = "250-mail.example\r\n250 \r\r\n".parse().unwrap();
ServerInfo::from_response(&response); // panics: called `Option::unwrap()` on a `None` valueFound by fuzzing Response parsing + ServerInfo::from_response with cargo-fuzz (libFuzzer). Reproduced on 0.11.22, 0.11.23 and current master (94b63ac).
Impact
Denial of service of the sending task: with the default panic = "unwind" the task sending the email (e.g. a tokio task using AsyncSmtpTransport) is aborted and remaining messages are not sent; with panic = "abort" the whole process terminates. No memory-safety impact.
Suggested fix
Skip whitespace-only lines the same way as empty ones (patch below, with a regression test). We are happy to open a PR once you are ready.
diff --git a/src/transport/smtp/extension.rs b/src/transport/smtp/extension.rs
index ff5b4c4..a541ac3 100644
--- a/src/transport/smtp/extension.rs
+++ b/src/transport/smtp/extension.rs
@@ -136,12 +136,12 @@ impl ServerInfo {
let mut features: HashSet<Extension> = HashSet::new();
for line in response.message() {
- if line.is_empty() {
+ let mut split = line.split_whitespace();
+ let Some(keyword) = split.next() else {
continue;
- }
+ };
- let mut split = line.split_whitespace();
- match split.next().unwrap() {
+ match keyword {
"8BITMIME" => {
features.insert(Extension::EightBitMime);
}
@@ -412,4 +412,14 @@ mod test {
assert!(server_info2.supports_auth_mechanism(Mechanism::Plain));
assert!(!server_info2.supports_feature(Extension::StartTls));
}
+
+ #[test]
+ fn test_serverinfo_whitespace_only_line() {
+ let response: Response = "250-mail.example\r\n250 \r\r\n".parse().unwrap();
+
+ let server_info = ServerInfo::from_response(&response).unwrap();
+
+ assert_eq!(server_info.name(), "mail.example");
+ assert!(server_info.features.is_empty());
+ }
}