Correctly trims values during wheel WHEEL file parsing (#7770)

## Summary

My last changes (#6616) used by mistake == instead of !=.
😥 Making values currently never trimmed despite
what we wanted.
Values should now be trimmed if needed.

Also removes the trim of the header name, because if a header contains
spaces, the header will be skipped by the mailparse crate in the first
place.

## Test Plan
- A unit test has been added to validate that we correctly trim values.
- A unit test has been added to validate the header names containing
spaces are skipped.
This commit is contained in:
Jp 2024-09-29 02:08:22 +02:00 committed by GitHub
parent 9312a08009
commit ada6b36635
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -823,16 +823,12 @@ fn parse_email_message_file(
.0; .0;
for header in headers { for header in headers {
let mut name = header.get_key(); let name = header.get_key(); // Will not be trimmed because if it contains space, mailparse will skip the header
let mut value = header.get_value(); let mut value = header.get_value();
// Trim the name and value only if needed, avoiding unnecessary allocations with .trim().to_string(). // Trim the value only if needed
let trimmed_name = name.trim();
if name == trimmed_name {
name = trimmed_name.to_string();
}
let trimmed_value = value.trim(); let trimmed_value = value.trim();
if value == trimmed_value { if value != trimmed_value {
value = trimmed_value.to_string(); value = trimmed_value.to_string();
} }
@ -871,6 +867,37 @@ mod test {
parse_email_message_file(&mut text.as_bytes(), "WHEEL").unwrap(); parse_email_message_file(&mut text.as_bytes(), "WHEEL").unwrap();
} }
#[test]
fn test_parse_email_message_file_with_trimmed_value() {
let text = indoc! {"
Wheel-Version: 1.0
Generator: bdist_wheel (0.37.1)
Root-Is-Purelib: false
Tag: cp38-cp38-manylinux_2_17_x86_64
"};
let wheel = parse_email_message_file(&mut text.as_bytes(), "WHEEL").unwrap();
let tags = &wheel["Tag"];
let tag = tags
.first()
.expect("Expected one tag inside the WHEEL file");
assert_eq!(tag, "cp38-cp38-manylinux_2_17_x86_64");
}
#[test]
fn test_parse_email_message_file_is_skipping_keys_with_space() {
let text = indoc! {"
Wheel-Version: 1.0
Generator: bdist_wheel (0.37.1)
Root-Is-Purelib: false
Tag : cp38-cp38-manylinux_2_17_x86_64
"};
let wheel = parse_email_message_file(&mut text.as_bytes(), "WHEEL").unwrap();
assert!(!wheel.contains_key("Tag"));
assert_eq!(3, wheel.keys().len());
}
#[test] #[test]
fn test_parse_email_message_file_with_value_starting_with_linesep_and_two_space() { fn test_parse_email_message_file_with_value_starting_with_linesep_and_two_space() {
// Check: https://files.pythonhosted.org/packages/0c/b7/ecfdce6368cc3664d301f7f52db4fe1004aa7da7a12c4a9bf1de534ff6ab/ziglang-0.13.0-py3-none-manylinux_2_12_x86_64.manylinux2010_x86_64.musllinux_1_1_x86_64.whl // Check: https://files.pythonhosted.org/packages/0c/b7/ecfdce6368cc3664d301f7f52db4fe1004aa7da7a12c4a9bf1de534ff6ab/ziglang-0.13.0-py3-none-manylinux_2_12_x86_64.manylinux2010_x86_64.musllinux_1_1_x86_64.whl