From 4838e5faeda2e52d476a714ffcb60ce4f4d1e91b Mon Sep 17 00:00:00 2001 From: mdecimus Date: Wed, 15 Jan 2025 10:10:02 +0100 Subject: [PATCH] Fixed panic parsing broken HTMLs --- Cargo.toml | 2 +- crates/smtp/src/queue/manager.rs | 27 +++++++++++++++++++++++--- crates/spam-filter/src/modules/html.rs | 23 +++++++++++++++------- 3 files changed, 41 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index bd71ca41..9d0d8357 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,7 +23,7 @@ members = [ [profile.dev] opt-level = 0 debug = 1 -codegen-units = 4 +#codegen-units = 4 lto = false incremental = true panic = 'unwind' diff --git a/crates/smtp/src/queue/manager.rs b/crates/smtp/src/queue/manager.rs index ee852206..e481f8cf 100644 --- a/crates/smtp/src/queue/manager.rs +++ b/crates/smtp/src/queue/manager.rs @@ -144,8 +144,16 @@ impl Queue { Total = queue_events.len(), Details = self .on_hold - .keys() - .copied() + .values() + .fold([0, 0, 0], |mut acc, v| { + match v { + OnHold::InFlight => acc[0] += 1, + OnHold::ConcurrencyLimited { .. } => acc[1] += 1, + OnHold::Locked { .. } => acc[2] += 1, + } + acc + }) + .into_iter() .map(trc::Value::from) .collect::>(), Limit = max_in_flight, @@ -176,7 +184,20 @@ impl Queue { Queue(trc::QueueEvent::BackPressure), Reason = "Queue outbound processing capacity for this node exceeded.", Total = queue_events.len(), - Details = self.on_hold.keys().copied().map(trc::Value::from).collect::>(), + Details = self + .on_hold + .values() + .fold([0, 0, 0], |mut acc, v| { + match v { + OnHold::InFlight => acc[0] += 1, + OnHold::ConcurrencyLimited { .. } => acc[1] += 1, + OnHold::Locked { .. } => acc[2] += 1, + } + acc + }) + .into_iter() + .map(trc::Value::from) + .collect::>(), Limit = max_in_flight, ); } diff --git a/crates/spam-filter/src/modules/html.rs b/crates/spam-filter/src/modules/html.rs index 309237e0..ffe89378 100644 --- a/crates/spam-filter/src/modules/html.rs +++ b/crates/spam-filter/src/modules/html.rs @@ -4,7 +4,7 @@ * SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-SEL */ - use mail_parser::decoders::html::add_html_token; +use mail_parser::decoders::html::add_html_token; #[derive(Debug, Eq, PartialEq, Clone)] pub enum HtmlToken { @@ -201,10 +201,14 @@ pub fn html_to_tokens(input: &str) -> Vec { match ch { b'>' if !in_quote => { if !value.is_empty() { - attributes.last_mut().unwrap().1 = - String::from_utf8(value) - .unwrap_or_default() - .into(); + let value = + String::from_utf8(value).unwrap_or_default(); + if let Some((_, v)) = attributes.last_mut() { + *v = value.into(); + } else { + // Broken attribute + attributes.push((0, Some(value))); + } } break 'outer; } @@ -226,8 +230,13 @@ pub fn html_to_tokens(input: &str) -> Vec { } if !value.is_empty() { - attributes.last_mut().unwrap().1 = - String::from_utf8(value).unwrap_or_default().into(); + let value = String::from_utf8(value).unwrap_or_default(); + if let Some((_, v)) = attributes.last_mut() { + *v = value.into(); + } else { + // Broken attribute + attributes.push((0, Some(value))); + } } } b' ' | b'\t' | b'\r' | b'\n' => {