Skip to content

Commit

Permalink
fix(host): Don't add unescaped meta headers to custom headers
Browse files Browse the repository at this point in the history
  • Loading branch information
Frederick888 committed Nov 23, 2023
1 parent ebec0d0 commit d5ecc38
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
47 changes: 43 additions & 4 deletions src/model/messaging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub const MAX_BODY_LENGTH: usize = 768 * 1024;

const HEADER_META: &str = "X-ExtEditorR";
const HEADER_LOWER_META: &str = "x-exteditorr"; // cspell: disable-line
const HEADER_LOWER_META_PREFIX: &str = "x-exteditorr-"; // cspell: disable-line
const HEADER_NORMALISED_META: &str = "X-Exteditorr"; // normalised by Thunderbird, cspell: disable-line
const HEADER_LOWER_ESCAPED_META: &str = "x-exteditorr-x-exteditorr"; // cspell: disable-line
const HEADER_PRIORITY: &str = "X-ExtEditorR-Priority";
Expand Down Expand Up @@ -215,7 +216,7 @@ impl Compose {
break;
}
if let Some((header_name, header_value)) = line.split_once(':') {
self.process_header(header_name, header_value, &mut unknown_headers, false)?;
self.process_header(header_name, header_value, &mut unknown_headers)?;
} else {
eprintln!("ExtEditorR failed to process header {line}");
}
Expand Down Expand Up @@ -290,7 +291,6 @@ impl Compose {
header_name: &str,
header_value: &str,
unknown_headers: &mut Vec<String>,
meta: bool,
) -> Result<()> {
let header_name_lower = header_name.trim().to_lowercase();
let header_value = header_value.trim();
Expand Down Expand Up @@ -359,7 +359,6 @@ impl Compose {
&format!("{HEADER_META}-{compact_header_name}"),
compact_header_value,
unknown_headers,
true,
)?;
} else {
eprintln!("ExtEditorR failed to process header {compact_header}");
Expand All @@ -372,7 +371,9 @@ impl Compose {
header_value,
));
}
_ if !meta && header_name_lower.starts_with("x-") => {
_ if header_name_lower.starts_with("x-")
&& !header_name_lower.starts_with(HEADER_LOWER_META_PREFIX) =>
{
// Thunderbird throws error if header name doesn't start with X-
self.compose_details
.custom_headers
Expand Down Expand Up @@ -1067,6 +1068,44 @@ pub mod tests {
);
}

#[test]
fn warn_unescaped_meta_headers_test() {
let eml = [
"X-ExtEditorR: Allow-X-Headers: true",
"X-ExtEditorR1: bar",
"X-ExtEditorR2-Foo: bar",
"X-ExtEditorR-Foo: bar",
"",
"This is a test.",
"",
]
.join("\r\n");
let mut request = get_blank_compose();
let responses = request.merge_from_eml(&mut eml.as_bytes(), 512).unwrap();
assert_eq!(1, responses.len());
assert_eq!(1, responses[0].warnings.len());
assert_eq!("Unknown header(s) found", responses[0].warnings[0].title);
assert_eq!(
"ExtEditorR did not recognise the following headers:\n- X-ExtEditorR-Foo",
responses[0].warnings[0].message
);
assert_eq!(2, responses[0].compose_details.custom_headers.len());
assert_eq!(
CustomHeader {
name: "X-ExtEditorR1".to_string(),
value: "bar".to_string(),
},
responses[0].compose_details.custom_headers[0]
);
assert_eq!(
CustomHeader {
name: "X-ExtEditorR2-Foo".to_string(),
value: "bar".to_string(),
},
responses[0].compose_details.custom_headers[1]
);
}

#[test]
fn delete_send_on_exit_header_test() {
let mut eml = "Subject: Hello\r\n\r\nThis is a test.\r\n".as_bytes();
Expand Down
2 changes: 1 addition & 1 deletion src/model/thunderbird.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ pub enum Newsgroups {
Multiple(Vec<String>),
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct CustomHeader {
pub name: String,
pub value: String,
Expand Down

0 comments on commit d5ecc38

Please sign in to comment.