Skip to content

Commit 0973a46

Browse files
committed
fix: make vCard parsing more robust in case of trailing newlines
Contacts should be added only if there is an END:VCARD detected, not because we found the end of file.
1 parent e22d980 commit 0973a46

File tree

1 file changed

+36
-13
lines changed
  • deltachat-contact-tools/src

1 file changed

+36
-13
lines changed

deltachat-contact-tools/src/lib.rs

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -206,22 +206,21 @@ pub fn parse_vcard(vcard: &str) -> Vec<VcardContact> {
206206
} else if let Some(rev) = vcard_property(line, "rev") {
207207
datetime.get_or_insert(rev);
208208
} else if line.eq_ignore_ascii_case("END:VCARD") {
209+
let (authname, addr) =
210+
sanitize_name_and_addr(display_name.unwrap_or(""), addr.unwrap_or(""));
211+
212+
contacts.push(VcardContact {
213+
authname,
214+
addr,
215+
key: key.map(|s| s.to_string()),
216+
profile_image: photo.map(|s| s.to_string()),
217+
timestamp: datetime
218+
.context("No timestamp in vcard")
219+
.and_then(parse_datetime),
220+
});
209221
break;
210222
}
211223
}
212-
213-
let (authname, addr) =
214-
sanitize_name_and_addr(display_name.unwrap_or(""), addr.unwrap_or(""));
215-
216-
contacts.push(VcardContact {
217-
authname,
218-
addr,
219-
key: key.map(|s| s.to_string()),
220-
profile_image: photo.map(|s| s.to_string()),
221-
timestamp: datetime
222-
.context("No timestamp in vcard")
223-
.and_then(parse_datetime),
224-
});
225224
}
226225

227226
contacts
@@ -540,6 +539,30 @@ END:VCARD",
540539
assert_eq!(contacts.len(), 1);
541540
}
542541

542+
#[test]
543+
fn test_vcard_with_trailing_newline() {
544+
let contacts = parse_vcard(
545+
"BEGIN:VCARD\r
546+
VERSION:4.0\r
547+
FN:Alice Wonderland\r
548+
N:Wonderland;Alice;;;Ms.\r
549+
GENDER:W\r
550+
EMAIL;TYPE=work:alice@example.com\r
551+
KEY;TYPE=PGP;ENCODING=b:[base64-data]\r
552+
REV:20240418T184242Z\r
553+
END:VCARD\r
554+
\r",
555+
);
556+
557+
assert_eq!(contacts[0].addr, "alice@example.com".to_string());
558+
assert_eq!(contacts[0].authname, "Alice Wonderland".to_string());
559+
assert_eq!(contacts[0].key, Some("[base64-data]".to_string()));
560+
assert_eq!(contacts[0].profile_image, None);
561+
assert_eq!(*contacts[0].timestamp.as_ref().unwrap(), 1713465762);
562+
563+
assert_eq!(contacts.len(), 1);
564+
}
565+
543566
#[test]
544567
fn test_make_and_parse_vcard() {
545568
let contacts = [

0 commit comments

Comments
 (0)