libverify-github 0.11.0

GitHub connector for libverify SDLC verification
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
//! Repository posture evidence collection for GitHub repositories.
//!
//! Collects repository-level security configuration signals:
//! CODEOWNERS, SECURITY.md, secret scanning, vulnerability scanning,
//! and branch protection settings.

use libverify_core::evidence::{CodeownersEntry, EvidenceGap, EvidenceState, RepositoryPosture};

use crate::client::GitHubClient;

/// Candidate paths for the CODEOWNERS file (GitHub resolves in this order).
const CODEOWNERS_PATHS: &[&str] = &["CODEOWNERS", "docs/CODEOWNERS", ".github/CODEOWNERS"];

/// Candidate paths for the security policy file.
const SECURITY_POLICY_PATHS: &[&str] = &["SECURITY.md", "docs/SECURITY.md", ".github/SECURITY.md"];

/// Disclosure keywords that indicate a responsible disclosure process.
const DISCLOSURE_KEYWORDS: &[&str] = &[
    "responsible disclosure",
    "coordinated disclosure",
    "vulnerability report",
    "report a vulnerability",
    "security@",
    "hackerone",
    "bugcrowd",
    "bug bounty",
];

/// Collect repository posture evidence from the GitHub API.
///
/// Fetches CODEOWNERS, SECURITY.md, and repository settings to populate
/// `RepositoryPosture`. Uses `ref_sha` for file lookups. Falls back to
/// `EvidenceState::Missing` when the API call fails entirely, or
/// `EvidenceState::Partial` when some data could not be collected.
pub fn collect_repository_posture(
    client: &GitHubClient,
    owner: &str,
    repo: &str,
    ref_sha: &str,
) -> EvidenceState<RepositoryPosture> {
    let mut gaps = Vec::new();

    // Run independent API calls concurrently using scoped threads.
    // Each call hits different endpoints so there's no contention.
    let (
        codeowners_entries,
        security_policy,
        settings_result,
        dep_tool,
        permissions_result,
        tag_protection,
    ) = std::thread::scope(|s| {
        let h_codeowners = s.spawn(|| collect_codeowners(client, owner, repo, ref_sha));
        let h_security = s.spawn(|| collect_security_policy(client, owner, repo, ref_sha));
        let h_settings = s.spawn(|| collect_repo_settings(client, owner, repo));
        let h_dep_tool = s.spawn(|| collect_dependency_update_tool(client, owner, repo, ref_sha));
        let h_permissions = s.spawn(|| collect_permissions_info(client, owner, repo));
        let h_tag = s.spawn(|| collect_tag_protection(client, owner, repo));

        (
            h_codeowners.join().unwrap(),
            h_security.join().unwrap(),
            h_settings.join().unwrap(),
            h_dep_tool.join().unwrap(),
            h_permissions.join().unwrap(),
            h_tag.join().unwrap(),
        )
    });

    let (security_policy_present, security_policy_has_disclosure) = security_policy;

    let (
        security_analysis_available,
        secret_scanning_enabled,
        secret_push_protection_enabled,
        vulnerability_scanning_enabled,
        code_scanning_enabled,
        default_branch_protected,
        enforce_admins,
        dismiss_stale_reviews,
    ) = match settings_result {
        Ok(settings) => (
            settings.security_analysis_available,
            settings.secret_scanning,
            settings.push_protection,
            settings.dependabot,
            settings.code_scanning,
            settings.branch_protected,
            settings.enforce_admins,
            settings.dismiss_stale_reviews,
        ),
        Err(e) => {
            gaps.push(EvidenceGap::CollectionFailed {
                source: "github".to_string(),
                subject: "repository settings".to_string(),
                detail: format!("{e:#}"),
            });
            (false, false, false, false, false, false, false, false)
        }
    };

    let (default_workflow_permissions, admin_count, direct_collaborator_count) =
        match permissions_result {
            Ok(info) => (
                info.default_workflow_permissions,
                info.admin_count,
                info.direct_collaborator_count,
            ),
            Err(e) => {
                gaps.push(EvidenceGap::CollectionFailed {
                    source: "github".to_string(),
                    subject: "permissions info".to_string(),
                    detail: format!("{e:#}"),
                });
                (String::new(), 0, 0)
            }
        };

    let posture = RepositoryPosture {
        codeowners_entries,
        security_analysis_available,
        secret_scanning_enabled,
        secret_push_protection_enabled,
        vulnerability_scanning_enabled,
        code_scanning_enabled,
        security_policy_present,
        security_policy_has_disclosure,
        default_branch_protected,
        enforce_admins,
        dismiss_stale_reviews,
        dependency_update_tool_configured: dep_tool,
        default_workflow_permissions,
        admin_count,
        direct_collaborator_count,
        tag_protection_enabled: tag_protection,
        ..Default::default()
    };

    if gaps.is_empty() {
        EvidenceState::complete(posture)
    } else {
        EvidenceState::partial(posture, gaps)
    }
}

/// Parse CODEOWNERS file content into entries.
fn collect_codeowners(
    client: &GitHubClient,
    owner: &str,
    repo: &str,
    ref_sha: &str,
) -> Vec<CodeownersEntry> {
    for path in CODEOWNERS_PATHS {
        if let Ok(content) = client.get_file_content(owner, repo, path, ref_sha) {
            return parse_codeowners(&content);
        }
    }
    Vec::new()
}

/// Parse CODEOWNERS content into structured entries.
fn parse_codeowners(content: &str) -> Vec<CodeownersEntry> {
    content
        .lines()
        .filter(|line| {
            let trimmed = line.trim();
            !trimmed.is_empty() && !trimmed.starts_with('#')
        })
        .filter_map(|line| {
            let parts: Vec<&str> = line.split_whitespace().collect();
            if parts.len() >= 2 {
                Some(CodeownersEntry {
                    pattern: parts[0].to_string(),
                    owners: parts[1..].iter().map(|s| s.to_string()).collect(),
                })
            } else {
                None
            }
        })
        .collect()
}

/// Check for SECURITY.md and whether it describes a disclosure process.
fn collect_security_policy(
    client: &GitHubClient,
    owner: &str,
    repo: &str,
    ref_sha: &str,
) -> (bool, bool) {
    for path in SECURITY_POLICY_PATHS {
        if let Ok(content) = client.get_file_content(owner, repo, path, ref_sha) {
            let lower = content.to_lowercase();
            let has_disclosure = DISCLOSURE_KEYWORDS.iter().any(|kw| lower.contains(kw));
            return (true, has_disclosure);
        }
    }
    (false, false)
}

/// Repository settings response (subset of GET /repos/{owner}/{repo}).
#[derive(serde::Deserialize)]
struct RepoResponse {
    #[serde(default)]
    default_branch: String,
    #[serde(default)]
    security_and_analysis: Option<SecurityAndAnalysis>,
}

#[derive(serde::Deserialize)]
struct SecurityAndAnalysis {
    #[serde(default)]
    secret_scanning: Option<SecurityFeature>,
    #[serde(default)]
    secret_scanning_push_protection: Option<SecurityFeature>,
    #[serde(default)]
    dependabot_security_updates: Option<SecurityFeature>,
}

#[derive(serde::Deserialize)]
struct SecurityFeature {
    status: String,
}

/// Branch protection API response (subset).
#[derive(serde::Deserialize)]
struct BranchProtectionResponse {
    #[serde(default)]
    enforce_admins: Option<EnforceAdmins>,
    #[serde(default)]
    required_pull_request_reviews: Option<RequiredPullRequestReviews>,
}

#[derive(serde::Deserialize)]
struct EnforceAdmins {
    enabled: bool,
}

#[derive(serde::Deserialize)]
struct RequiredPullRequestReviews {
    #[serde(default)]
    dismiss_stale_reviews: bool,
}

/// Result of collecting repository settings, including any evidence gaps
/// that occurred during collection (e.g. insufficient API permissions).
struct RepoSettingsResult {
    security_analysis_available: bool,
    secret_scanning: bool,
    push_protection: bool,
    dependabot: bool,
    code_scanning: bool,
    branch_protected: bool,
    enforce_admins: bool,
    dismiss_stale_reviews: bool,
}

/// Fetch repository settings from the GitHub REST API.
fn collect_repo_settings(
    client: &GitHubClient,
    owner: &str,
    repo: &str,
) -> anyhow::Result<RepoSettingsResult> {
    let path = format!("/repos/{owner}/{repo}");
    let body = client.get(&path)?;
    let resp: RepoResponse = serde_json::from_str(&body)?;
    let security_analysis_available = resp.security_and_analysis.is_some();

    let (secret_scanning, push_protection, dependabot) = match resp.security_and_analysis.as_ref() {
        Some(sa) => (
            sa.secret_scanning
                .as_ref()
                .is_some_and(|f| f.status == "enabled"),
            sa.secret_scanning_push_protection
                .as_ref()
                .is_some_and(|f| f.status == "enabled"),
            sa.dependabot_security_updates
                .as_ref()
                .is_some_and(|f| f.status == "enabled"),
        ),
        None => (false, false, false),
    };

    // Code scanning: check if any code-scanning analyses exist (non-empty = enabled)
    let code_scanning = client
        .get(&format!(
            "/repos/{owner}/{repo}/code-scanning/analyses?per_page=1"
        ))
        .map(|body| {
            serde_json::from_str::<Vec<serde_json::Value>>(&body)
                .map(|v| !v.is_empty())
                .unwrap_or(false)
        })
        .unwrap_or(false);

    // Branch protection: parse response for detailed fields
    let default_branch = &resp.default_branch;
    let protection_url = format!("/repos/{owner}/{repo}/branches/{default_branch}/protection");
    let (branch_protected, enforce_admins, dismiss_stale_reviews) =
        match client.get(&protection_url) {
            Ok(bp_body) => {
                let bp: BranchProtectionResponse =
                    serde_json::from_str(&bp_body).unwrap_or(BranchProtectionResponse {
                        enforce_admins: None,
                        required_pull_request_reviews: None,
                    });
                let enforce = bp.enforce_admins.as_ref().is_some_and(|ea| ea.enabled);
                let dismiss = bp
                    .required_pull_request_reviews
                    .as_ref()
                    .is_some_and(|pr| pr.dismiss_stale_reviews);
                (true, enforce, dismiss)
            }
            Err(_) => (false, false, false),
        };

    Ok(RepoSettingsResult {
        security_analysis_available,
        secret_scanning,
        push_protection,
        dependabot,
        code_scanning,
        branch_protected,
        enforce_admins,
        dismiss_stale_reviews,
    })
}

/// Dependency update tool config file paths to check.
const DEPENDABOT_PATH: &str = ".github/dependabot.yml";
const RENOVATE_PATHS: &[&str] = &[
    "renovate.json",
    "renovate.json5",
    ".renovaterc",
    ".renovaterc.json",
];

/// Check whether a dependency update tool is configured.
fn collect_dependency_update_tool(
    client: &GitHubClient,
    owner: &str,
    repo: &str,
    ref_sha: &str,
) -> bool {
    // Check Dependabot
    if client
        .get_file_content(owner, repo, DEPENDABOT_PATH, ref_sha)
        .is_ok()
    {
        return true;
    }
    // Check Renovate
    for path in RENOVATE_PATHS {
        if client.get_file_content(owner, repo, path, ref_sha).is_ok() {
            return true;
        }
    }
    false
}

/// Permissions info collected from the GitHub API.
struct PermissionsInfo {
    default_workflow_permissions: String,
    admin_count: u32,
    direct_collaborator_count: u32,
}

/// Collect workflow permissions and collaborator info.
fn collect_permissions_info(
    client: &GitHubClient,
    owner: &str,
    repo: &str,
) -> anyhow::Result<PermissionsInfo> {
    // GET /repos/{owner}/{repo} includes default_branch_permissions in some API versions
    // For workflow permissions, we use the Actions permissions endpoint
    let default_workflow_permissions = client
        .get(&format!(
            "/repos/{owner}/{repo}/actions/permissions/workflow"
        ))
        .ok()
        .and_then(|body| {
            serde_json::from_str::<serde_json::Value>(&body)
                .ok()
                .and_then(|v| v["default_workflow_permissions"].as_str().map(String::from))
        })
        .unwrap_or_default();

    // GET /repos/{owner}/{repo}/collaborators?affiliation=direct
    let (admin_count, direct_collaborator_count) = client
        .get(&format!(
            "/repos/{owner}/{repo}/collaborators?affiliation=direct&per_page=100"
        ))
        .ok()
        .and_then(|body| serde_json::from_str::<Vec<serde_json::Value>>(&body).ok())
        .map(|collabs| {
            let admins = collabs
                .iter()
                .filter(|c| c["permissions"]["admin"].as_bool().unwrap_or(false))
                .count() as u32;
            let direct = collabs.len() as u32;
            (admins, direct)
        })
        .unwrap_or((0, 0));

    Ok(PermissionsInfo {
        default_workflow_permissions,
        admin_count,
        direct_collaborator_count,
    })
}

/// Check whether tag protection rules exist.
fn collect_tag_protection(client: &GitHubClient, owner: &str, repo: &str) -> bool {
    // Tag protection rules API requires admin access; returns 404 if no rules or no permission
    client
        .get(&format!("/repos/{owner}/{repo}/tags/protection"))
        .ok()
        .and_then(|body| serde_json::from_str::<Vec<serde_json::Value>>(&body).ok())
        .is_some_and(|rules| !rules.is_empty())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse_codeowners_basic() {
        let content = "# Global owners\n* @org/team\n/src/auth/ @alice @bob\n";
        let entries = parse_codeowners(content);
        assert_eq!(entries.len(), 2);
        assert_eq!(entries[0].pattern, "*");
        assert_eq!(entries[0].owners, vec!["@org/team"]);
        assert_eq!(entries[1].pattern, "/src/auth/");
        assert_eq!(entries[1].owners, vec!["@alice", "@bob"]);
    }

    #[test]
    fn parse_codeowners_empty_and_comments() {
        let content = "# comment\n\n  # another comment\n";
        let entries = parse_codeowners(content);
        assert!(entries.is_empty());
    }

    #[test]
    fn parse_codeowners_single_column_skipped() {
        let content = "*.rs\n";
        let entries = parse_codeowners(content);
        assert!(entries.is_empty(), "single-column lines have no owners");
    }

    #[test]
    fn disclosure_keywords_are_lowercase() {
        for kw in DISCLOSURE_KEYWORDS {
            assert_eq!(
                *kw,
                kw.to_lowercase(),
                "keyword must be lowercase for case-insensitive matching"
            );
        }
    }
}