Handle clients sending malformed Return-Path fields.

This commit is contained in:
Mauro D
2022-06-02 13:13:58 +00:00
parent 0be5ed4a41
commit 1e8dfd31cf
+30 -3
View File
@@ -14,6 +14,8 @@ use crate::{parsers::message::MessageStream, HeaderValue};
pub fn parse_id<'x>(stream: &mut MessageStream<'x>) -> HeaderValue<'x> {
let mut token_start: usize = 0;
let mut token_end: usize = 0;
let mut token_invalid_start: usize = 0; // Handle broken clients
let mut token_invalid_end: usize = 0; // Handle broken clients
let mut is_id_part = false;
let mut ids = Vec::new();
@@ -31,7 +33,15 @@ pub fn parse_id<'x>(stream: &mut MessageStream<'x>) -> HeaderValue<'x> {
_ => {
return match ids.len() {
1 => HeaderValue::Text(ids.pop().unwrap()),
0 => HeaderValue::Empty,
0 => {
if token_invalid_start > 0 {
HeaderValue::Text(String::from_utf8_lossy(
&stream.data[token_invalid_start - 1..token_invalid_end],
))
} else {
HeaderValue::Empty
}
}
_ => HeaderValue::TextList(ids),
};
}
@@ -52,13 +62,18 @@ pub fn parse_id<'x>(stream: &mut MessageStream<'x>) -> HeaderValue<'x> {
}
}
b' ' | b'\t' | b'\r' => continue,
_ => (),
_ => {}
}
if is_id_part {
if token_start == 0 {
token_start = stream.pos;
}
token_end = stream.pos;
} else {
if token_invalid_start == 0 {
token_invalid_start = stream.pos;
}
token_invalid_end = stream.pos;
}
}
@@ -102,6 +117,17 @@ mod tests {
"<1234 @ local(blah) .machine .example>\n",
vec!["1234 @ local(blah) .machine .example"],
),
("<>\n", vec![""]),
// Malformed Ids should be parsed anyway
(
"malformed@id.machine.example\n",
vec!["malformed@id.machine.example"],
),
(
" malformed2@id.machine.example \t \n",
vec!["malformed2@id.machine.example"],
),
(" m \n", vec!["m"]),
];
for input in inputs {
@@ -114,7 +140,8 @@ mod tests {
assert!(input.1.len() == 1, "Failed to parse '{:?}'", input.0);
assert_eq!(id, input.1[0], "Failed to parse '{:?}'", input.0);
}
_ => panic!("Unexpected result"),
HeaderValue::Empty if input.1[0].is_empty() => {}
result => panic!("Unexpected result: {:?}", result),
}
}
}