1use anyhow::{Context, Result, bail};
17
18pub fn normalize_sshsig_armor(pem: &str) -> String {
26 let body: String = pem
27 .lines()
28 .filter(|line| !line.starts_with("-----"))
29 .map(str::trim)
30 .collect();
31 let mut normalized = String::from("-----BEGIN SSH SIGNATURE-----\n");
32 for chunk in body.as_bytes().chunks(70) {
33 normalized.push_str(&String::from_utf8_lossy(chunk));
35 normalized.push('\n');
36 }
37 normalized.push_str("-----END SSH SIGNATURE-----\n");
38 normalized
39}
40
41pub fn split_signed_commit(raw: &[u8]) -> Result<Option<(Vec<u8>, String)>> {
55 #[derive(PartialEq)]
56 enum Header {
57 Signature,
58 OtherSignature,
59 Kept,
60 }
61
62 let text = std::str::from_utf8(raw).context("commit object is not UTF-8")?;
63 let Some((headers, body)) = text.split_once("\n\n") else {
64 bail!("malformed commit object: no header/body separator");
65 };
66
67 let mut kept_headers: Vec<&str> = Vec::new();
68 let mut signature_lines: Vec<&str> = Vec::new();
69 let mut current = Header::Kept;
70 for line in headers.split('\n') {
71 if current == Header::Signature
74 && let Some(continuation) = line.strip_prefix(' ')
75 {
76 signature_lines.push(continuation);
77 } else if let Some(first) = line.strip_prefix("gpgsig ") {
78 current = Header::Signature;
79 signature_lines.push(first);
80 } else if line.starts_with("gpgsig")
81 || (current == Header::OtherSignature && line.starts_with(' '))
82 {
83 current = Header::OtherSignature;
84 } else {
85 current = Header::Kept;
86 kept_headers.push(line);
87 }
88 }
89
90 if signature_lines.is_empty() {
91 return Ok(None);
92 }
93
94 let mut payload = kept_headers.join("\n").into_bytes();
95 payload.extend_from_slice(b"\n\n");
96 payload.extend_from_slice(body.as_bytes());
97
98 let mut pem = signature_lines.join("\n");
99 pem.push('\n');
100 Ok(Some((payload, pem)))
101}
102
103#[must_use]
109pub fn committer_identity(commit: &[u8]) -> Option<String> {
110 let text = std::str::from_utf8(commit).ok()?;
111 let headers = text.split_once("\n\n").map_or(text, |(headers, _)| headers);
112 let line = headers
113 .split('\n')
114 .find_map(|line| line.strip_prefix("committer "))?;
115 let open = line.rfind('<')?;
117 let close = line[open..].find('>')? + open;
118 Some(line[open + 1..close].to_string())
119}
120
121#[must_use]
135pub fn committer_did(commit: &[u8]) -> Option<String> {
136 let identity = committer_identity(commit)?;
137 if !identity.starts_with("did:") {
138 return None;
139 }
140 let did = claimed_did(&identity);
141 if did.is_empty() {
142 return None;
143 }
144 Some(did.to_string())
145}
146
147fn claimed_did(value: &str) -> &str {
154 let reference = value.split('#').next().unwrap_or(value);
155 if affinidi_did_common::scid::scid_src(reference).is_some() {
156 return reference;
157 }
158 value.split(['#', '?', '/']).next().unwrap_or(value)
159}
160
161#[must_use]
165pub fn same_signer(a: &str, b: &str) -> bool {
166 affinidi_did_common::scid::same_reference(a, b)
167}
168
169#[must_use]
172pub fn signer_identity(did: &str) -> String {
173 let did = did.split('#').next().unwrap_or(did);
174 affinidi_did_common::scid::strip_scid_src(did).into_owned()
175}
176
177#[must_use]
185pub fn signer_did(commit: &[u8]) -> Option<String> {
186 trailer_did(commit).or_else(|| committer_did(commit))
187}
188
189#[must_use]
192pub fn conflicting_signer_dids(commit: &[u8]) -> Option<(String, String)> {
193 let trailer = trailer_did(commit)?;
194 let committer = committer_did(commit)?;
195 (!same_signer(&trailer, &committer)).then_some((trailer, committer))
196}
197
198const SIGNER_DID_KEY: &str = "Signed-by-DID";
200
201const COMMENT_PREFIX: char = '#';
208
209const GIT_GENERATED_PREFIXES: [&str; 2] = ["Signed-off-by: ", "(cherry picked from commit "];
217
218fn trailer_did(commit: &[u8]) -> Option<String> {
236 let text = std::str::from_utf8(commit).ok()?;
237 let (_, message) = text.split_once("\n\n")?;
238 let value = last_trailer_value(message, SIGNER_DID_KEY)?;
239 let value = value.trim_ascii();
240 if !value.starts_with("did:") {
241 return None;
242 }
243 Some(claimed_did(value).to_string())
246}
247
248fn last_trailer_value(message: &str, key: &str) -> Option<String> {
257 let mut lines: Vec<&str> = message.split('\n').collect();
258 if lines.last().is_some_and(|line| line.is_empty()) {
260 lines.pop();
261 }
262 let before_subject = lines.iter().take_while(|line| is_blank(line)).count();
270 let lines = &lines[before_subject..];
271
272 let start = trailer_block_start(lines)?;
273
274 let mut value: Option<String> = None;
275 let mut open: Option<bool> = None;
280 for line in &lines[start..] {
281 if open.is_some() && line.starts_with(|c: char| c.is_ascii_whitespace()) {
282 if open == Some(true)
283 && let Some(value) = value.as_mut()
284 {
285 value.push('\n');
288 value.push_str(line);
289 }
290 continue;
291 }
292 match separator_pos(line) {
293 Some(position) => {
294 let matched = line[..position].trim_ascii().eq_ignore_ascii_case(key);
295 if matched {
296 value = Some(line[position + 1..].to_string());
297 }
298 open = Some(matched);
299 }
300 None => open = None,
301 }
302 }
303 value.map(|value| unfold(value.trim_ascii()))
305}
306
307fn unfold(value: &str) -> String {
315 let mut unfolded = String::with_capacity(value.len());
316 let mut rest = value;
317 while let Some(newline) = rest.find('\n') {
318 unfolded.push_str(&rest[..newline]);
319 unfolded.push(' ');
320 rest = rest[newline + 1..].trim_ascii_start();
321 }
322 unfolded.push_str(rest);
323 unfolded.trim_ascii().to_string()
324}
325
326fn trailer_block_start(lines: &[&str]) -> Option<usize> {
335 let end_of_title = lines
339 .iter()
340 .position(|line| !line.starts_with(COMMENT_PREFIX) && is_blank(line))?;
341
342 let mut recognized_prefix = false;
343 let mut trailer_lines = 0_usize;
344 let mut non_trailer_lines = 0_usize;
345 let mut possible_continuation_lines = 0_usize;
348 let mut only_spaces = true;
349
350 for index in (end_of_title..lines.len()).rev() {
351 let line = lines[index];
352 if line.starts_with(COMMENT_PREFIX) {
353 non_trailer_lines += possible_continuation_lines;
354 possible_continuation_lines = 0;
355 continue;
356 }
357 if is_blank(line) {
358 if only_spaces {
359 continue;
360 }
361 non_trailer_lines += possible_continuation_lines;
362 if recognized_prefix && trailer_lines * 3 >= non_trailer_lines {
363 return Some(index + 1);
364 }
365 if trailer_lines > 0 && non_trailer_lines == 0 {
366 return Some(index + 1);
367 }
368 return None;
369 }
370 only_spaces = false;
371
372 if GIT_GENERATED_PREFIXES
373 .iter()
374 .any(|prefix| line.starts_with(prefix))
375 {
376 trailer_lines += 1;
377 possible_continuation_lines = 0;
378 recognized_prefix = true;
379 } else if separator_pos(line).is_some() {
380 trailer_lines += 1;
381 possible_continuation_lines = 0;
382 } else if line.starts_with(|c: char| c.is_ascii_whitespace()) {
388 possible_continuation_lines += 1;
389 } else {
390 non_trailer_lines += 1 + possible_continuation_lines;
391 possible_continuation_lines = 0;
392 }
393 }
394 None
395}
396
397fn separator_pos(line: &str) -> Option<usize> {
405 let mut whitespace_found = false;
406 for (offset, c) in line.char_indices() {
407 if c == ':' {
408 return (offset >= 1).then_some(offset);
409 }
410 if !whitespace_found && (c.is_ascii_alphanumeric() || c == '-') {
411 continue;
412 }
413 if offset != 0 && (c == ' ' || c == '\t') {
414 whitespace_found = true;
415 continue;
416 }
417 return None;
418 }
419 None
420}
421
422fn is_blank(line: &str) -> bool {
424 line.trim_ascii().is_empty()
425}
426
427#[cfg(test)]
428mod tests {
429 #![allow(clippy::unwrap_used)]
430
431 use super::*;
432
433 fn commit_with_committer(committer: &str) -> String {
434 format!(
435 "tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904\n\
436 author A U Thor <a@example.com> 1700000000 +0000\n\
437 committer {committer} 1700000000 +0000\n\
438 \n\
439 a message\n"
440 )
441 }
442
443 #[test]
444 fn a_did_committer_yields_the_bare_did() {
445 let commit = commit_with_committer("Alice <did:webvh:QmAbc:example.com#key-0>");
446 assert_eq!(
447 committer_did(commit.as_bytes()).unwrap(),
448 "did:webvh:QmAbc:example.com",
449 "the fragment names the key, not the identity the registry knows"
450 );
451 }
452
453 #[test]
454 fn a_did_without_a_fragment_survives_intact() {
455 let commit = commit_with_committer("Alice <did:webvh:QmAbc:example.com>");
456 assert_eq!(
457 committer_did(commit.as_bytes()).unwrap(),
458 "did:webvh:QmAbc:example.com"
459 );
460 }
461
462 #[test]
463 fn a_plain_email_committer_claims_no_did() {
464 let commit = commit_with_committer("Alice <alice@example.com>");
465 assert!(committer_did(commit.as_bytes()).is_none());
466 assert_eq!(
467 committer_identity(commit.as_bytes()).unwrap(),
468 "alice@example.com",
469 "the identity is still reported, so the failure can name it"
470 );
471 }
472
473 #[test]
474 fn a_body_line_cannot_impersonate_the_committer_header() {
475 let commit = "tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904\n\
478 author A U Thor <a@example.com> 1700000000 +0000\n\
479 committer A U Thor <alice@example.com> 1700000000 +0000\n\
480 \n\
481 committer Evil <did:webvh:QmEvil:attacker.example> 1700000000 +0000\n";
482 assert!(
483 committer_did(commit.as_bytes()).is_none(),
484 "a DID in the message body must not be read as the committer"
485 );
486 }
487
488 #[test]
489 fn a_display_name_containing_an_angle_bracket_does_not_truncate() {
490 let commit = commit_with_committer("A <script> Thor <did:webvh:QmAbc:example.com#key-1>");
491 assert_eq!(
492 committer_did(commit.as_bytes()).unwrap(),
493 "did:webvh:QmAbc:example.com"
494 );
495 }
496
497 #[test]
498 fn a_signed_commits_payload_still_exposes_the_committer() {
499 let commit = commit_with_committer("Alice <did:webvh:QmAbc:example.com#key-0>");
502 let (headers, body) = commit.split_once("\n\n").unwrap();
503 let signed = format!(
504 "{headers}\ngpgsig -----BEGIN SSH SIGNATURE-----\n \
505 AAAA\n -----END SSH SIGNATURE-----\n\n{body}"
506 );
507 let (payload, _) = split_signed_commit(signed.as_bytes()).unwrap().unwrap();
508 assert_eq!(
509 committer_did(&payload).unwrap(),
510 "did:webvh:QmAbc:example.com"
511 );
512 }
513
514 fn signed_with_extra_headers(extra: &str, after_signature: bool) -> (String, String) {
517 let head = "tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904\n\
518 author A U Thor <a@example.com> 1700000000 +0000\n\
519 committer A U Thor <a@example.com> 1700000000 +0000";
520 let signature = "gpgsig -----BEGIN SSH SIGNATURE-----\n AAAA\n -----END SSH SIGNATURE-----";
521 let headers = if after_signature {
522 format!("{head}\n{signature}\n{extra}")
523 } else {
524 format!("{head}\n{extra}\n{signature}")
525 };
526 (
527 format!("{headers}\n\na message\n"),
528 format!("{head}\n\na message\n"),
529 )
530 }
531
532 #[test]
533 fn other_gpgsig_headers_are_removed_from_the_payload_like_git() {
534 for extra in [
538 "gpgsig-sha256 -----BEGIN PGP SIGNATURE-----\n AAAA\n -----END PGP SIGNATURE-----",
539 "gpgsig-sha256 ",
540 "gpgsigx junk",
541 "gpgsig-foo bar",
542 "gpgsig",
543 ] {
544 for after_signature in [false, true] {
545 let (commit, expected) = signed_with_extra_headers(extra, after_signature);
546 let (payload, pem) = split_signed_commit(commit.as_bytes()).unwrap().unwrap();
547 assert_eq!(
548 String::from_utf8(payload).unwrap(),
549 expected,
550 "{extra:?} (after the signature: {after_signature})"
551 );
552 assert_eq!(
553 pem, "-----BEGIN SSH SIGNATURE-----\nAAAA\n-----END SSH SIGNATURE-----\n",
554 "the other header must not leak into the signature"
555 );
556 }
557 }
558 }
559
560 #[test]
561 fn a_header_that_merely_contains_gpgsig_is_kept() {
562 let (commit, _) = signed_with_extra_headers("x-gpgsig junk", false);
565 let (payload, _) = split_signed_commit(commit.as_bytes()).unwrap().unwrap();
566 assert!(
567 String::from_utf8(payload)
568 .unwrap()
569 .contains("\nx-gpgsig junk\n")
570 );
571 }
572
573 #[test]
574 fn a_header_after_another_signature_is_kept() {
575 let (commit, expected) =
578 signed_with_extra_headers("gpgsig-sha256 first\n second\nencoding UTF-8", true);
579 let (payload, _) = split_signed_commit(commit.as_bytes()).unwrap().unwrap();
580 let expected = expected.replace("\n\na message", "\nencoding UTF-8\n\na message");
581 assert_eq!(String::from_utf8(payload).unwrap(), expected);
582 }
583
584 #[test]
585 fn a_commit_signed_only_by_another_header_is_unsigned() {
586 let commit = "tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904\n\
589 committer A U Thor <a@example.com> 1700000000 +0000\n\
590 gpgsig-sha256 -----BEGIN SSH SIGNATURE-----\n AAAA\n\
591 \n\
592 a message\n";
593 assert!(split_signed_commit(commit.as_bytes()).unwrap().is_none());
594 }
595
596 fn commit_with_trailer(committer: &str, trailer: &str) -> String {
597 format!(
598 "tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904\n\
599 author A U Thor <a@example.com> 1700000000 +0000\n\
600 committer {committer} 1700000000 +0000\n\
601 \n\
602 a message\n\
603 \n\
604 {trailer}\n"
605 )
606 }
607
608 #[test]
609 fn a_did_scid_claim_keeps_its_src_locator() {
610 let commit = commit_with_trailer(
611 "Alice <alice@example.com>",
612 "Signed-by-DID: did:scid:vh:1:QmScid?src=dids.example.org/alice#key-0",
613 );
614 assert_eq!(
615 signer_did(commit.as_bytes()).unwrap(),
616 "did:scid:vh:1:QmScid?src=dids.example.org/alice"
617 );
618 for (claim, did) in [
620 (
621 "did:scid:vh:1:QmScid?versionId=2#key-0",
622 "did:scid:vh:1:QmScid",
623 ),
624 (
625 "did:scid:vh:1:QmScid?src=a.example&x=1#key-0",
626 "did:scid:vh:1:QmScid",
627 ),
628 (
629 "did:webvh:Qm:a.example?versionId=2#key-0",
630 "did:webvh:Qm:a.example",
631 ),
632 ] {
633 let commit = commit_with_trailer(
634 "Alice <alice@example.com>",
635 &format!("Signed-by-DID: {claim}"),
636 );
637 assert_eq!(signer_did(commit.as_bytes()).unwrap(), did, "{claim}");
638 }
639 }
640
641 #[test]
642 fn a_did_scid_trailer_and_committer_agree_with_and_without_src() {
643 let commit = commit_with_trailer(
644 "Alice <did:scid:vh:1:QmScid#key-0>",
645 "Signed-by-DID: did:scid:vh:1:QmScid?src=dids.example.org#key-0",
646 );
647 assert_eq!(conflicting_signer_dids(commit.as_bytes()), None);
648 let commit = commit_with_trailer(
650 "Alice <did:webvh:QmScid:dids.example.org#key-0>",
651 "Signed-by-DID: did:scid:vh:1:QmScid?src=dids.example.org#key-0",
652 );
653 assert!(conflicting_signer_dids(commit.as_bytes()).is_some());
654 }
655
656 #[test]
657 fn signer_identity_drops_only_a_did_scid_src() {
658 assert_eq!(
659 signer_identity("did:scid:vh:1:QmScid?src=a.example#key-0"),
660 "did:scid:vh:1:QmScid"
661 );
662 assert_eq!(
663 signer_identity("did:webvh:Qm:a.example"),
664 "did:webvh:Qm:a.example"
665 );
666 assert!(same_signer(
667 "did:scid:vh:1:QmScid?src=a.example",
668 "did:scid:vh:1:QmScid"
669 ));
670 }
671
672 #[test]
673 fn signer_did_prefers_trailer_over_committer() {
674 let commit = commit_with_trailer(
675 "Alice <did:webvh:QmOld:old.example#key-0>",
676 "Signed-by-DID: did:webvh:QmNew:new.example#key-0",
677 );
678 assert_eq!(
679 signer_did(commit.as_bytes()).unwrap(),
680 "did:webvh:QmNew:new.example",
681 "trailer must take precedence over committer email"
682 );
683 }
684
685 #[test]
686 fn signer_did_falls_back_to_committer_for_legacy_commits() {
687 let commit = commit_with_committer("Alice <did:webvh:QmAbc:example.com#key-0>");
688 assert_eq!(
689 signer_did(commit.as_bytes()).unwrap(),
690 "did:webvh:QmAbc:example.com",
691 "legacy commits with DID in committer email must still work"
692 );
693 }
694
695 #[test]
696 fn signer_did_reads_trailer_with_normal_email_committer() {
697 let commit = commit_with_trailer(
698 "Alice <alice@example.com>",
699 "Signed-by-DID: did:webvh:QmAbc:example.com#key-0",
700 );
701 assert_eq!(
702 signer_did(commit.as_bytes()).unwrap(),
703 "did:webvh:QmAbc:example.com",
704 );
705 }
706
707 #[test]
708 fn signer_did_returns_none_without_did_anywhere() {
709 let commit = commit_with_committer("Alice <alice@example.com>");
710 assert!(signer_did(commit.as_bytes()).is_none());
711 }
712
713 #[test]
714 fn trailer_strips_fragment() {
715 let commit = commit_with_trailer(
716 "Alice <alice@example.com>",
717 "Signed-by-DID: did:webvh:QmAbc:example.com#key-1",
718 );
719 assert_eq!(
720 signer_did(commit.as_bytes()).unwrap(),
721 "did:webvh:QmAbc:example.com",
722 );
723 }
724
725 #[test]
726 fn trailer_ignores_non_did_values() {
727 let commit = commit_with_trailer("Alice <alice@example.com>", "Signed-by-DID: not-a-did");
728 assert!(signer_did(commit.as_bytes()).is_none());
729 }
730
731 #[test]
732 fn signer_did_ignores_body_line_outside_final_trailer_block() {
733 let commit = "tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904\n\
734 author A U Thor <a@example.com> 1700000000 +0000\n\
735 committer Alice <alice@example.com> 1700000000 +0000\n\
736 \n\
737 This line only discusses a trailer.\n\
738 Signed-by-DID: did:webvh:QmBody:example.com#key-0\n\
739 \n\
740 final prose, not a trailer block\n";
741 assert!(signer_did(commit.as_bytes()).is_none());
742 }
743
744 #[test]
745 fn signer_did_reads_final_trailer_block_only() {
746 let commit = "tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904\n\
747 author A U Thor <a@example.com> 1700000000 +0000\n\
748 committer Alice <alice@example.com> 1700000000 +0000\n\
749 \n\
750 Signed-by-DID: did:webvh:QmBody:ignored.example#key-0\n\
751 \n\
752 body text\n\
753 \n\
754 Signed-off-by: Alice <alice@example.com>\n\
755 Signed-by-DID: did:webvh:QmTrailer:example.com#key-0\n";
756 assert_eq!(
757 signer_did(commit.as_bytes()).unwrap(),
758 "did:webvh:QmTrailer:example.com"
759 );
760 }
761
762 #[test]
763 fn conflicting_signer_dids_reports_trailer_and_committer_disagreement() {
764 let commit = commit_with_trailer(
765 "Alice <did:webvh:QmCommitter:example.com#key-0>",
766 "Signed-by-DID: did:webvh:QmTrailer:example.com#key-0",
767 );
768 assert_eq!(
769 conflicting_signer_dids(commit.as_bytes()).unwrap(),
770 (
771 "did:webvh:QmTrailer:example.com".to_string(),
772 "did:webvh:QmCommitter:example.com".to_string(),
773 )
774 );
775 }
776
777 fn commit_with_body(body: &str) -> String {
784 format!(
785 "tree 4b825dc642cb6eb9a060e54bf8d69288fbee4904\n\
786 author A U Thor <a@example.com> 1700000000 +0000\n\
787 committer Alice <alice@example.com> 1700000000 +0000\n\
788 \n\
789 {body}"
790 )
791 }
792
793 const DID: &str = "did:webvh:QmA:example.com";
794
795 #[test]
796 fn a_trailer_in_a_mixed_paragraph_is_not_a_claim() {
797 let commit = commit_with_body(&format!(
803 "subject\n\nprose about the change\nSigned-by-DID: {DID}\n"
804 ));
805 assert!(trailer_did(commit.as_bytes()).is_none());
806 }
807
808 #[test]
809 fn a_trailer_in_the_title_paragraph_is_not_a_claim() {
810 let commit = commit_with_body(&format!("Signed-by-DID: {DID}\n"));
814 assert!(trailer_did(commit.as_bytes()).is_none());
815 let commit = commit_with_body(&format!("subject\nSigned-by-DID: {DID}\n"));
816 assert!(trailer_did(commit.as_bytes()).is_none());
817 }
818
819 #[test]
820 fn whitespace_before_the_colon_still_names_the_trailer() {
821 for gap in ["", " ", " ", "\t", " \t"] {
824 let commit = commit_with_body(&format!("subject\n\nSigned-by-DID{gap}: {DID}#key-0\n"));
825 assert_eq!(
826 trailer_did(commit.as_bytes()).as_deref(),
827 Some(DID),
828 "gap {gap:?} must not hide the claim"
829 );
830 }
831 }
832
833 #[test]
834 fn the_trailer_key_is_matched_case_insensitively() {
835 for key in [
837 "Signed-by-DID",
838 "signed-by-did",
839 "SIGNED-BY-DID",
840 "Signed-By-Did",
841 ] {
842 let commit = commit_with_body(&format!("subject\n\n{key}: {DID}#key-0\n"));
843 assert_eq!(
844 trailer_did(commit.as_bytes()).as_deref(),
845 Some(DID),
846 "key {key:?} must be recognized"
847 );
848 }
849 }
850
851 #[test]
852 fn a_folded_trailer_value_is_unfolded_like_git() {
853 let commit = commit_with_body(&format!("subject\n\nSigned-by-DID: {DID}\n and more\n"));
858 assert_eq!(
859 trailer_did(commit.as_bytes()).as_deref(),
860 Some("did:webvh:QmA:example.com and more")
861 );
862 let commit = commit_with_body(&format!("subject\n\nSigned-by-DID: {DID} \n continued\n"));
867 assert_eq!(
868 trailer_did(commit.as_bytes()).as_deref(),
869 Some("did:webvh:QmA:example.com continued")
870 );
871 }
872
873 #[test]
874 fn a_git_generated_trailer_unlocks_the_25_percent_allowance() {
875 let commit = commit_with_body(&format!(
878 "subject\n\nn1\nn2\nn3\nSigned-off-by: A U Thor <a@example.com>\nSigned-by-DID: {DID}\n"
879 ));
880 assert_eq!(trailer_did(commit.as_bytes()).as_deref(), Some(DID));
881 let commit = commit_with_body(&format!(
883 "subject\n\nn1\nn2\nn3\nn4\nn5\nn6\nn7\n\
884 Signed-off-by: A U Thor <a@example.com>\nSigned-by-DID: {DID}\n"
885 ));
886 assert!(trailer_did(commit.as_bytes()).is_none());
887 }
888
889 #[test]
890 fn a_cherry_pick_line_unlocks_the_allowance_without_a_separator() {
891 let commit = commit_with_body(&format!(
895 "subject\n\nprose\n\
896 (cherry picked from commit 0123456789abcdef0123456789abcdef01234567)\n\
897 Signed-by-DID: {DID}\n"
898 ));
899 assert_eq!(trailer_did(commit.as_bytes()).as_deref(), Some(DID));
900 }
901
902 #[test]
903 fn a_line_whose_colon_comes_first_defeats_the_block() {
904 let commit = commit_with_body(&format!(
907 "subject\n\n: did:webvh:QmEvil:attacker.example\nSigned-by-DID: {DID}\n"
908 ));
909 assert!(trailer_did(commit.as_bytes()).is_none());
910 }
911
912 #[test]
913 fn the_last_trailer_wins_even_when_its_value_is_not_a_did() {
914 let commit = commit_with_body(
918 "subject\n\nSigned-by-DID: did:webvh:QmFirst:example.com\nSigned-by-DID: see below\n",
919 );
920 assert!(trailer_did(commit.as_bytes()).is_none());
921 }
922
923 #[test]
924 fn blank_lines_before_the_subject_are_skipped_like_git() {
925 for prefix in ["\n", " \n", "\t\n", "\n\n"] {
931 let commit = commit_with_body(&format!("{prefix}Signed-by-DID: {DID}\n"));
932 assert!(
933 trailer_did(commit.as_bytes()).is_none(),
934 "with prefix {prefix:?} the trailer line is the subject git shows"
935 );
936 }
937 let commit = commit_with_body(&format!("\nsubject\n\nSigned-by-DID: {DID}\n"));
939 assert_eq!(trailer_did(commit.as_bytes()).as_deref(), Some(DID));
940 }
941
942 #[test]
943 fn comment_lines_do_not_count_against_the_block() {
944 let commit = commit_with_body(&format!("subject\n\n# a comment\nSigned-by-DID: {DID}\n"));
946 assert_eq!(trailer_did(commit.as_bytes()).as_deref(), Some(DID));
947 }
948}