-
Notifications
You must be signed in to change notification settings - Fork 20
[Repo Assist] refactor(rust-guard): extract is_any_trusted_actor helper and collapse URL fallback loop #4260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -940,7 +940,8 @@ pub(crate) fn extract_repo_from_github_url(url: &str) -> Option<String> { | |
|
|
||
| /// Extract repository full name from a response item | ||
| /// Tries multiple fields in order: full_name, repository.full_name, | ||
| /// base.repo.full_name, head.repo.full_name, html_url parsing | ||
| /// base.repo.full_name, head.repo.full_name, then URL parsing from | ||
| /// repository_url, html_url, and url. | ||
| /// Returns empty string if no repo info found | ||
| pub fn extract_repo_from_item(item: &Value) -> String { | ||
| // Direct full_name (repositories) | ||
|
|
@@ -973,23 +974,12 @@ pub fn extract_repo_from_item(item: &Value) -> String { | |
| { | ||
| return name.to_string(); | ||
| } | ||
| // repository_url parsing for search endpoints | ||
| if let Some(url) = item.get("repository_url").and_then(|v| v.as_str()) { | ||
| if let Some(repo_id) = extract_repo_from_github_url(url) { | ||
| return repo_id; | ||
| } | ||
| } | ||
| // html_url parsing as last resort - extract owner/repo from URLs like: | ||
| // https://github.com/owner/repo/pull/123 or https://github.com/owner/repo/issues/456 | ||
| if let Some(url) = item.get("html_url").and_then(|v| v.as_str()) { | ||
| if let Some(repo_id) = extract_repo_from_github_url(url) { | ||
| return repo_id; | ||
| } | ||
| } | ||
| // Generic URL field fallback | ||
| if let Some(url) = item.get("url").and_then(|v| v.as_str()) { | ||
| if let Some(repo_id) = extract_repo_from_github_url(url) { | ||
| return repo_id; | ||
| // URL field fallback (repository_url for search results, html_url / url as generic fallbacks) | ||
| for field in &["repository_url", "html_url", "url"] { | ||
| if let Some(url) = item.get(field).and_then(|v| v.as_str()) { | ||
| if let Some(repo_id) = extract_repo_from_github_url(url) { | ||
| return repo_id; | ||
| } | ||
| } | ||
| } | ||
| String::new() | ||
|
|
@@ -1278,11 +1268,7 @@ pub fn has_author_association(item: &Value) -> bool { | |
| /// Users in the trusted_users list are also elevated to approved integrity. | ||
| pub fn author_association_floor(item: &Value, scope: &str, ctx: &PolicyContext) -> Vec<String> { | ||
| let author_login = extract_author_login(item); | ||
| if !author_login.is_empty() | ||
| && (is_trusted_first_party_bot(author_login) | ||
| || is_configured_trusted_bot(author_login, ctx) | ||
| || is_trusted_user(author_login, ctx)) | ||
| { | ||
| if !author_login.is_empty() && is_any_trusted_actor(author_login, ctx) { | ||
| return writer_integrity(scope, ctx); | ||
| } | ||
|
|
||
|
|
@@ -1476,10 +1462,7 @@ pub fn pr_integrity( | |
| ); | ||
| // Elevate trusted bots and trusted users | ||
| let enriched_floor = if let Some(ref login) = facts.author_login { | ||
| if is_trusted_first_party_bot(login) | ||
| || is_configured_trusted_bot(login, ctx) | ||
| || is_trusted_user(login, ctx) | ||
| { | ||
| if is_any_trusted_actor(login, ctx) { | ||
| max_integrity( | ||
| repo_full_name, | ||
| enriched_floor, | ||
|
|
@@ -1772,6 +1755,14 @@ pub fn is_trusted_user(username: &str, ctx: &PolicyContext) -> bool { | |
| username_in_list(username, &ctx.trusted_users) | ||
| } | ||
|
|
||
| /// Returns `true` if `username` belongs to any trusted actor tier: | ||
| /// first-party bots, gateway-configured bots, or trusted users. | ||
| pub(crate) fn is_any_trusted_actor(username: &str, ctx: &PolicyContext) -> bool { | ||
| is_trusted_first_party_bot(username) | ||
| || is_configured_trusted_bot(username, ctx) | ||
| || is_trusted_user(username, ctx) | ||
| } | ||
|
Comment on lines
+1758
to
+1764
|
||
|
|
||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
|
|
@@ -1781,6 +1772,20 @@ mod tests { | |
| PolicyContext::default() | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_is_any_trusted_actor_tiers_and_negative() { | ||
| let ctx = PolicyContext { | ||
| trusted_bots: vec!["custom-bot".to_string()], | ||
| trusted_users: vec!["trusted-human".to_string()], | ||
| ..Default::default() | ||
| }; | ||
|
|
||
| assert!(is_any_trusted_actor("github-actions[bot]", &ctx)); | ||
| assert!(is_any_trusted_actor("custom-bot", &ctx)); | ||
| assert!(is_any_trusted_actor("trusted-human", &ctx)); | ||
| assert!(!is_any_trusted_actor("random-user", &ctx)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_collaborator_permission_floor_admin() { | ||
| let ctx = test_ctx(); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extract_repo_from_itemnow clearly falls back acrossrepository_url,html_url, andurl, but the function-level doc comment above still describes onlyhtml_urlparsing. Please update the doc comment to match the actual fallback fields/order (and consider matching the existingextract_number_from_urlloop style by avoiding the unnecessary*fieldderef for consistency).