From 6f45aa9937602f3d03cd83cfb5fb3c541fac4adf Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Thu, 12 Dec 2024 11:31:51 +0000 Subject: fix: defend against empty`tags` prevent a panic when tags are of an unexpect length --- src/bin/git_remote_nostr/push.rs | 15 +++++++++++---- src/bin/git_remote_nostr/utils.rs | 6 +++--- src/bin/ngit/sub_commands/list.rs | 6 +++--- src/lib/git/mod.rs | 2 +- src/lib/git_events.rs | 19 +++++++++++++------ src/lib/login/user.rs | 9 +++++---- 6 files changed, 36 insertions(+), 21 deletions(-) diff --git a/src/bin/git_remote_nostr/push.rs b/src/bin/git_remote_nostr/push.rs index 6116fe2..4066312 100644 --- a/src/bin/git_remote_nostr/push.rs +++ b/src/bin/git_remote_nostr/push.rs @@ -1010,7 +1010,8 @@ async fn get_merged_proposals_info( .iter() .filter(|e| { e.tags.iter().any(|t| { - t.as_slice()[0].eq("commit") + t.as_slice().len() > 1 + && t.as_slice()[0].eq("commit") && t.as_slice()[1].eq(&parent.id().to_string()) }) }) @@ -1034,7 +1035,9 @@ async fn get_merged_proposals_info( .iter() .filter(|e| { e.tags.iter().any(|t| { - t.as_slice()[0].eq("commit") && t.as_slice()[1].eq(&commit_hash.to_string()) + t.as_slice().len() > 1 + && t.as_slice()[0].eq("commit") + && t.as_slice()[1].eq(&commit_hash.to_string()) }) }) .collect::>() @@ -1228,7 +1231,11 @@ async fn get_proposal_and_revision_root_from_patch( git_repo: &Repo, patch: &Event, ) -> Result<(EventId, Option)> { - let proposal_or_revision = if patch.tags.iter().any(|t| t.as_slice()[1].eq("root")) { + let proposal_or_revision = if patch + .tags + .iter() + .any(|t| t.as_slice().len() > 1 && t.as_slice()[1].eq("root")) + { patch.clone() } else { let proposal_or_revision_id = EventId::parse( @@ -1260,7 +1267,7 @@ async fn get_proposal_and_revision_root_from_patch( if proposal_or_revision .tags .iter() - .any(|t| t.as_slice()[1].eq("revision-root")) + .any(|t| t.as_slice().len() > 1 && t.as_slice()[1].eq("revision-root")) { Ok(( EventId::parse( diff --git a/src/bin/git_remote_nostr/utils.rs b/src/bin/git_remote_nostr/utils.rs index b54ce6b..5efb6e0 100644 --- a/src/bin/git_remote_nostr/utils.rs +++ b/src/bin/git_remote_nostr/utils.rs @@ -128,9 +128,9 @@ pub async fn get_open_proposals( .iter() .filter(|e| { status_kinds().contains(&e.kind) - && e.tags - .iter() - .any(|t| t.as_slice()[1].eq(&proposal.id.to_string())) + && e.tags.iter().any(|t| { + t.as_slice().len() > 1 && t.as_slice()[1].eq(&proposal.id.to_string()) + }) }) .collect::>() .first() diff --git a/src/bin/ngit/sub_commands/list.rs b/src/bin/ngit/sub_commands/list.rs index b7bfd24..ee015ef 100644 --- a/src/bin/ngit/sub_commands/list.rs +++ b/src/bin/ngit/sub_commands/list.rs @@ -77,9 +77,9 @@ pub async fn launch() -> Result<()> { .iter() .filter(|e| { status_kinds().contains(&e.kind) - && e.tags - .iter() - .any(|t| t.as_slice()[1].eq(&proposal.id.to_string())) + && e.tags.iter().any(|t| { + t.as_slice().len() > 1 && t.as_slice()[1].eq(&proposal.id.to_string()) + }) }) .collect::>() .first() diff --git a/src/lib/git/mod.rs b/src/lib/git/mod.rs index 0615213..7a7ad5d 100644 --- a/src/lib/git/mod.rs +++ b/src/lib/git/mod.rs @@ -850,7 +850,7 @@ fn git_sig_to_tag_vec(sig: &git2::Signature) -> Vec { fn extract_sig_from_patch_tags<'a>(tags: &'a Tags, tag_name: &str) -> Result> { let v = tags .iter() - .find(|t| t.as_slice()[0].eq(tag_name)) + .find(|t| !t.as_slice().is_empty() && t.as_slice()[0].eq(tag_name)) .context(format!("tag '{tag_name}' not present in patch"))? .as_slice(); if v.len() != 5 { diff --git a/src/lib/git_events.rs b/src/lib/git_events.rs index 10194bb..17fae8c 100644 --- a/src/lib/git_events.rs +++ b/src/lib/git_events.rs @@ -18,7 +18,7 @@ pub fn tag_value(event: &Event, tag_name: &str) -> Result { Ok(event .tags .iter() - .find(|t| t.as_slice()[0].eq(tag_name)) + .find(|t| !t.as_slice().is_empty() && t.as_slice()[0].eq(tag_name)) .context(format!("tag '{tag_name}'not present"))? .as_slice()[1] .clone()) @@ -59,7 +59,11 @@ pub fn status_kinds() -> Vec { } pub fn event_is_patch_set_root(event: &Event) -> bool { - event.kind.eq(&Kind::GitPatch) && event.tags.iter().any(|t| t.as_slice()[1].eq("root")) + event.kind.eq(&Kind::GitPatch) + && event + .tags + .iter() + .any(|t| t.as_slice().len() > 1 && t.as_slice()[1].eq("root")) } pub fn event_is_revision_root(event: &Event) -> bool { @@ -67,7 +71,7 @@ pub fn event_is_revision_root(event: &Event) -> bool { && event .tags .iter() - .any(|t| t.as_slice()[1].eq("revision-root")) + .any(|t| t.as_slice().len() > 1 && t.as_slice()[1].eq("revision-root")) } pub fn patch_supports_commit_ids(event: &Event) -> bool { @@ -75,7 +79,7 @@ pub fn patch_supports_commit_ids(event: &Event) -> bool { && event .tags .iter() - .any(|t| t.as_slice()[0].eq("commit-pgp-sig")) + .any(|t| !t.as_slice().is_empty() && t.as_slice()[0].eq("commit-pgp-sig")) } #[allow(clippy::too_many_arguments)] @@ -473,11 +477,14 @@ pub fn event_is_cover_letter(event: &nostr::Event) -> bool { // [PATCH v1 0/n ] or // [PATCH subsystem v2 0/n ] event.kind.eq(&Kind::GitPatch) - && event.tags.iter().any(|t| t.as_slice()[1].eq("root")) && event .tags .iter() - .any(|t| t.as_slice()[1].eq("cover-letter")) + .any(|t| t.as_slice().len() > 1 && t.as_slice()[1].eq("root")) + && event + .tags + .iter() + .any(|t| t.as_slice().len() > 1 && t.as_slice()[1].eq("cover-letter")) } pub fn commit_msg_from_patch(patch: &nostr::Event) -> Result { diff --git a/src/lib/login/user.rs b/src/lib/login/user.rs index c13cdf5..de4a2d9 100644 --- a/src/lib/login/user.rs +++ b/src/lib/login/user.rs @@ -187,10 +187,11 @@ pub fn extract_user_relays(public_key: &nostr::PublicKey, events: &[nostr::Event .tags .iter() .filter(|t| { - t.kind() - .eq(&nostr::TagKind::SingleLetter(SingleLetterTag::lowercase( - Alphabet::R, - ))) + t.as_slice().len() > 1 + && t.kind() + .eq(&nostr::TagKind::SingleLetter(SingleLetterTag::lowercase( + Alphabet::R, + ))) }) .map(|t| UserRelayRef { url: t.as_slice()[1].clone(), -- cgit v1.2.3