diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2024-12-12 11:31:51 +0000 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2024-12-12 11:32:48 +0000 |
| commit | 6f45aa9937602f3d03cd83cfb5fb3c541fac4adf (patch) | |
| tree | c64277a455b0b8c07c4e501a0716f16365b88c33 /src | |
| parent | 2b49a548169bf9bc3a3ef667a7e952e31e878bab (diff) | |
fix: defend against empty`tags`
prevent a panic when tags are of an unexpect length
Diffstat (limited to 'src')
| -rw-r--r-- | src/bin/git_remote_nostr/push.rs | 15 | ||||
| -rw-r--r-- | src/bin/git_remote_nostr/utils.rs | 6 | ||||
| -rw-r--r-- | src/bin/ngit/sub_commands/list.rs | 6 | ||||
| -rw-r--r-- | src/lib/git/mod.rs | 2 | ||||
| -rw-r--r-- | src/lib/git_events.rs | 19 | ||||
| -rw-r--r-- | 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( | |||
| 1010 | .iter() | 1010 | .iter() |
| 1011 | .filter(|e| { | 1011 | .filter(|e| { |
| 1012 | e.tags.iter().any(|t| { | 1012 | e.tags.iter().any(|t| { |
| 1013 | t.as_slice()[0].eq("commit") | 1013 | t.as_slice().len() > 1 |
| 1014 | && t.as_slice()[0].eq("commit") | ||
| 1014 | && t.as_slice()[1].eq(&parent.id().to_string()) | 1015 | && t.as_slice()[1].eq(&parent.id().to_string()) |
| 1015 | }) | 1016 | }) |
| 1016 | }) | 1017 | }) |
| @@ -1034,7 +1035,9 @@ async fn get_merged_proposals_info( | |||
| 1034 | .iter() | 1035 | .iter() |
| 1035 | .filter(|e| { | 1036 | .filter(|e| { |
| 1036 | e.tags.iter().any(|t| { | 1037 | e.tags.iter().any(|t| { |
| 1037 | t.as_slice()[0].eq("commit") && t.as_slice()[1].eq(&commit_hash.to_string()) | 1038 | t.as_slice().len() > 1 |
| 1039 | && t.as_slice()[0].eq("commit") | ||
| 1040 | && t.as_slice()[1].eq(&commit_hash.to_string()) | ||
| 1038 | }) | 1041 | }) |
| 1039 | }) | 1042 | }) |
| 1040 | .collect::<Vec<&Event>>() | 1043 | .collect::<Vec<&Event>>() |
| @@ -1228,7 +1231,11 @@ async fn get_proposal_and_revision_root_from_patch( | |||
| 1228 | git_repo: &Repo, | 1231 | git_repo: &Repo, |
| 1229 | patch: &Event, | 1232 | patch: &Event, |
| 1230 | ) -> Result<(EventId, Option<EventId>)> { | 1233 | ) -> Result<(EventId, Option<EventId>)> { |
| 1231 | let proposal_or_revision = if patch.tags.iter().any(|t| t.as_slice()[1].eq("root")) { | 1234 | let proposal_or_revision = if patch |
| 1235 | .tags | ||
| 1236 | .iter() | ||
| 1237 | .any(|t| t.as_slice().len() > 1 && t.as_slice()[1].eq("root")) | ||
| 1238 | { | ||
| 1232 | patch.clone() | 1239 | patch.clone() |
| 1233 | } else { | 1240 | } else { |
| 1234 | let proposal_or_revision_id = EventId::parse( | 1241 | let proposal_or_revision_id = EventId::parse( |
| @@ -1260,7 +1267,7 @@ async fn get_proposal_and_revision_root_from_patch( | |||
| 1260 | if proposal_or_revision | 1267 | if proposal_or_revision |
| 1261 | .tags | 1268 | .tags |
| 1262 | .iter() | 1269 | .iter() |
| 1263 | .any(|t| t.as_slice()[1].eq("revision-root")) | 1270 | .any(|t| t.as_slice().len() > 1 && t.as_slice()[1].eq("revision-root")) |
| 1264 | { | 1271 | { |
| 1265 | Ok(( | 1272 | Ok(( |
| 1266 | EventId::parse( | 1273 | 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( | |||
| 128 | .iter() | 128 | .iter() |
| 129 | .filter(|e| { | 129 | .filter(|e| { |
| 130 | status_kinds().contains(&e.kind) | 130 | status_kinds().contains(&e.kind) |
| 131 | && e.tags | 131 | && e.tags.iter().any(|t| { |
| 132 | .iter() | 132 | t.as_slice().len() > 1 && t.as_slice()[1].eq(&proposal.id.to_string()) |
| 133 | .any(|t| t.as_slice()[1].eq(&proposal.id.to_string())) | 133 | }) |
| 134 | }) | 134 | }) |
| 135 | .collect::<Vec<&nostr::Event>>() | 135 | .collect::<Vec<&nostr::Event>>() |
| 136 | .first() | 136 | .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<()> { | |||
| 77 | .iter() | 77 | .iter() |
| 78 | .filter(|e| { | 78 | .filter(|e| { |
| 79 | status_kinds().contains(&e.kind) | 79 | status_kinds().contains(&e.kind) |
| 80 | && e.tags | 80 | && e.tags.iter().any(|t| { |
| 81 | .iter() | 81 | t.as_slice().len() > 1 && t.as_slice()[1].eq(&proposal.id.to_string()) |
| 82 | .any(|t| t.as_slice()[1].eq(&proposal.id.to_string())) | 82 | }) |
| 83 | }) | 83 | }) |
| 84 | .collect::<Vec<&nostr::Event>>() | 84 | .collect::<Vec<&nostr::Event>>() |
| 85 | .first() | 85 | .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<String> { | |||
| 850 | fn extract_sig_from_patch_tags<'a>(tags: &'a Tags, tag_name: &str) -> Result<git2::Signature<'a>> { | 850 | fn extract_sig_from_patch_tags<'a>(tags: &'a Tags, tag_name: &str) -> Result<git2::Signature<'a>> { |
| 851 | let v = tags | 851 | let v = tags |
| 852 | .iter() | 852 | .iter() |
| 853 | .find(|t| t.as_slice()[0].eq(tag_name)) | 853 | .find(|t| !t.as_slice().is_empty() && t.as_slice()[0].eq(tag_name)) |
| 854 | .context(format!("tag '{tag_name}' not present in patch"))? | 854 | .context(format!("tag '{tag_name}' not present in patch"))? |
| 855 | .as_slice(); | 855 | .as_slice(); |
| 856 | if v.len() != 5 { | 856 | 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<String> { | |||
| 18 | Ok(event | 18 | Ok(event |
| 19 | .tags | 19 | .tags |
| 20 | .iter() | 20 | .iter() |
| 21 | .find(|t| t.as_slice()[0].eq(tag_name)) | 21 | .find(|t| !t.as_slice().is_empty() && t.as_slice()[0].eq(tag_name)) |
| 22 | .context(format!("tag '{tag_name}'not present"))? | 22 | .context(format!("tag '{tag_name}'not present"))? |
| 23 | .as_slice()[1] | 23 | .as_slice()[1] |
| 24 | .clone()) | 24 | .clone()) |
| @@ -59,7 +59,11 @@ pub fn status_kinds() -> Vec<Kind> { | |||
| 59 | } | 59 | } |
| 60 | 60 | ||
| 61 | pub fn event_is_patch_set_root(event: &Event) -> bool { | 61 | pub fn event_is_patch_set_root(event: &Event) -> bool { |
| 62 | event.kind.eq(&Kind::GitPatch) && event.tags.iter().any(|t| t.as_slice()[1].eq("root")) | 62 | event.kind.eq(&Kind::GitPatch) |
| 63 | && event | ||
| 64 | .tags | ||
| 65 | .iter() | ||
| 66 | .any(|t| t.as_slice().len() > 1 && t.as_slice()[1].eq("root")) | ||
| 63 | } | 67 | } |
| 64 | 68 | ||
| 65 | pub fn event_is_revision_root(event: &Event) -> bool { | 69 | pub fn event_is_revision_root(event: &Event) -> bool { |
| @@ -67,7 +71,7 @@ pub fn event_is_revision_root(event: &Event) -> bool { | |||
| 67 | && event | 71 | && event |
| 68 | .tags | 72 | .tags |
| 69 | .iter() | 73 | .iter() |
| 70 | .any(|t| t.as_slice()[1].eq("revision-root")) | 74 | .any(|t| t.as_slice().len() > 1 && t.as_slice()[1].eq("revision-root")) |
| 71 | } | 75 | } |
| 72 | 76 | ||
| 73 | pub fn patch_supports_commit_ids(event: &Event) -> bool { | 77 | pub fn patch_supports_commit_ids(event: &Event) -> bool { |
| @@ -75,7 +79,7 @@ pub fn patch_supports_commit_ids(event: &Event) -> bool { | |||
| 75 | && event | 79 | && event |
| 76 | .tags | 80 | .tags |
| 77 | .iter() | 81 | .iter() |
| 78 | .any(|t| t.as_slice()[0].eq("commit-pgp-sig")) | 82 | .any(|t| !t.as_slice().is_empty() && t.as_slice()[0].eq("commit-pgp-sig")) |
| 79 | } | 83 | } |
| 80 | 84 | ||
| 81 | #[allow(clippy::too_many_arguments)] | 85 | #[allow(clippy::too_many_arguments)] |
| @@ -473,11 +477,14 @@ pub fn event_is_cover_letter(event: &nostr::Event) -> bool { | |||
| 473 | // [PATCH v1 0/n ] or | 477 | // [PATCH v1 0/n ] or |
| 474 | // [PATCH subsystem v2 0/n ] | 478 | // [PATCH subsystem v2 0/n ] |
| 475 | event.kind.eq(&Kind::GitPatch) | 479 | event.kind.eq(&Kind::GitPatch) |
| 476 | && event.tags.iter().any(|t| t.as_slice()[1].eq("root")) | ||
| 477 | && event | 480 | && event |
| 478 | .tags | 481 | .tags |
| 479 | .iter() | 482 | .iter() |
| 480 | .any(|t| t.as_slice()[1].eq("cover-letter")) | 483 | .any(|t| t.as_slice().len() > 1 && t.as_slice()[1].eq("root")) |
| 484 | && event | ||
| 485 | .tags | ||
| 486 | .iter() | ||
| 487 | .any(|t| t.as_slice().len() > 1 && t.as_slice()[1].eq("cover-letter")) | ||
| 481 | } | 488 | } |
| 482 | 489 | ||
| 483 | pub fn commit_msg_from_patch(patch: &nostr::Event) -> Result<String> { | 490 | pub fn commit_msg_from_patch(patch: &nostr::Event) -> Result<String> { |
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 | |||
| 187 | .tags | 187 | .tags |
| 188 | .iter() | 188 | .iter() |
| 189 | .filter(|t| { | 189 | .filter(|t| { |
| 190 | t.kind() | 190 | t.as_slice().len() > 1 |
| 191 | .eq(&nostr::TagKind::SingleLetter(SingleLetterTag::lowercase( | 191 | && t.kind() |
| 192 | Alphabet::R, | 192 | .eq(&nostr::TagKind::SingleLetter(SingleLetterTag::lowercase( |
| 193 | ))) | 193 | Alphabet::R, |
| 194 | ))) | ||
| 194 | }) | 195 | }) |
| 195 | .map(|t| UserRelayRef { | 196 | .map(|t| UserRelayRef { |
| 196 | url: t.as_slice()[1].clone(), | 197 | url: t.as_slice()[1].clone(), |