From fcadccfbab3bb63829cf696aca253ffdf68886b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Sch=C3=A4fer?= Date: Tue, 30 Apr 2024 18:16:27 +0200 Subject: [PATCH] parsers::vcard: allow linebreaks in binval data [RFC 2426][1] says: > The binary data MUST be encoded using the "B" encoding format. > Long lines of encoded binary data SHOULD BE folded to 75 characters > using the folding method defined in [MIME-DIR]. That implies that whitespace may occur in binval data and we thus must be able to parse this correctly. [1]: https://datatracker.ietf.org/doc/html/rfc2426#section-2.4.1 --- parsers/src/vcard.rs | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/parsers/src/vcard.rs b/parsers/src/vcard.rs index 11170b7..7f41507 100644 --- a/parsers/src/vcard.rs +++ b/parsers/src/vcard.rs @@ -14,7 +14,7 @@ //! see [`vcard_update`][crate::vcard_update] module. use crate::iq::{IqGetPayload, IqResultPayload, IqSetPayload}; -use crate::util::text_node_codecs::{Base64, Codec, Text}; +use crate::util::text_node_codecs::{Codec, Text, WhitespaceAwareBase64}; use crate::{ns, Error}; use minidom::Element; @@ -44,7 +44,7 @@ generate_element!( Binval, "BINVAL", VCARD, text: ( /// The actual data. - data: Base64 + data: WhitespaceAwareBase64 ) ); @@ -131,4 +131,34 @@ mod tests { assert_eq!(photo.type_.data, "image/jpeg".to_string()); assert_eq!(photo.binval.data, bytes); } + + #[test] + fn test_vcard_with_linebreaks() { + // Test xml stolen from https://xmpp.org/extensions/xep-0153.html#example-5 + // extended to use a multi-line base64 string as is allowed as per RFC 2426 + let test_vcard = r" + 1476-06-09 + + Italy + Verona + + + + JulietCapulet + jcapulet@shakespeare.lit + + image/jpeg + Zm9v +Cg== + + "; + + let test_vcard = Element::from_str(&test_vcard).expect("Failed to parse XML"); + let test_vcard = VCard::try_from(test_vcard).expect("Failed to parse vCard"); + + let photo = test_vcard.photo.expect("No photo found"); + + assert_eq!(photo.type_.data, "image/jpeg".to_string()); + assert_eq!(photo.binval.data, b"foo\n"); + } }