diff options
author | Neal H. Walfield <neal@pep.foundation> | 2023-05-12 12:22:22 +0200 |
---|---|---|
committer | Neal H. Walfield <neal@pep.foundation> | 2023-05-12 13:29:03 +0200 |
commit | c1894b180ef3fea4d066f1fad242bd9bc6e2503c (patch) | |
tree | 29ab6ae8fc9c1d2b6398246e174638e47f26ff1b | |
parent | abf2d1060c906522fa48985c488f015cd6bc3097 (diff) |
autocrypt: Account for multi-byte characters when parsing a string.
- When parsing a string, don't use a byte slice. It may contain
multi-byte characters.
- Fixes #1012.
-rw-r--r-- | autocrypt/src/lib.rs | 34 |
1 files changed, 25 insertions, 9 deletions
diff --git a/autocrypt/src/lib.rs b/autocrypt/src/lib.rs index 7412214c..ba88fcef 100644 --- a/autocrypt/src/lib.rs +++ b/autocrypt/src/lib.rs @@ -300,11 +300,11 @@ impl AutocryptHeaders { .splitn(2, '=') .collect::<Vec<&str>>(); - let (key, value) : (String, String) = if pair.len() == 1 { + let (key, value) : (&str, String) = if pair.len() == 1 { // No value... - (pair[0].trim_matches(' ').into(), "".into()) + (pair[0].trim_matches(' '), "".into()) } else { - (pair[0].trim_matches(' ').into(), + (pair[0].trim_matches(' '), pair[1].trim_matches(' ').into()) }; @@ -317,14 +317,14 @@ impl AutocryptHeaders { } } - let critical = !key.is_empty() && &key[0..1] == "_"; + let (critical, key) = if let Some(key) = key.strip_prefix('_') { + (true, key) + } else { + (false, key) + }; header.attributes.push(Attribute { critical, - key: if critical { - key[1..].to_string() - } else { - key - }, + key: key.to_string(), value, }); } @@ -1130,4 +1130,20 @@ mod test { ]; let _ = AutocryptHeaders::from_bytes(&data); } + + #[test] + fn issue_1012() { + let data: Vec<u8> = vec![ + 0x41, 0x75, 0x74, 0x6f, 0x63, 0x72, 0x79, 0x70, 0x74, 0x2d, 0x47, 0x6f, 0x73, 0x73, 0x69, + 0x70, 0x3a, 0x20, 0xc8, 0x84, 0x01, 0x42, 0x04, 0x0a, 0x00, 0x00, 0x00, 0x25, 0x25, 0x25, + 0x25, 0x42, 0x25, 0x3f, 0x21, 0x25, 0x25, 0x25, 0x25, 0x25, 0x25, 0x25, 0x25, 0x25, 0x25, + 0x25, 0x25, 0x25, 0x25, 0x25, 0x25, 0x00, 0x40, 0x25, 0x25, 0x25, 0x25, 0x25, 0x25, 0x25, + 0x25, 0x22, 0x6b, 0x25, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x25, 0x25, 0x25, 0x25, 0x25, + 0x25, 0x25, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x25, 0x25, 0x25, 0x25, 0x25, + 0x25, 0x25, 0x25, 0x25, 0x25, 0x25, 0x25, + ]; + + AutocryptHeaders::from_bytes(&data).expect("parses"); + } } |