Skip to content

Commit

Permalink
Fixed panic parsing broken HTMLs
Browse files Browse the repository at this point in the history
  • Loading branch information
mdecimus committed Jan 15, 2025
1 parent 6bb468c commit 4838e5f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ members = [
[profile.dev]
opt-level = 0
debug = 1
codegen-units = 4
#codegen-units = 4
lto = false
incremental = true
panic = 'unwind'
Expand Down
27 changes: 24 additions & 3 deletions crates/smtp/src/queue/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>(),
Limit = max_in_flight,
Expand Down Expand Up @@ -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::<Vec<_>>(),
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::<Vec<_>>(),
Limit = max_in_flight,
);
}
Expand Down
23 changes: 16 additions & 7 deletions crates/spam-filter/src/modules/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -201,10 +201,14 @@ pub fn html_to_tokens(input: &str) -> Vec<HtmlToken> {
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;
}
Expand All @@ -226,8 +230,13 @@ pub fn html_to_tokens(input: &str) -> Vec<HtmlToken> {
}

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' => {
Expand Down

0 comments on commit 4838e5f

Please sign in to comment.