|
1 | 1 | /** |
2 | 2 | * Dependabot Check |
3 | 3 | * Verifies that Dependabot security updates are enabled on repositories |
| 4 | + * and reports the count of open/closed security alerts. |
4 | 5 | */ |
5 | 6 |
|
6 | 7 | import { TASK_TEMPLATES } from '../../../task-mappings'; |
7 | 8 | import type { IntegrationCheck } from '../../../types'; |
8 | | -import type { GitHubOrg, GitHubRepo } from '../types'; |
| 9 | +import type { GitHubDependabotAlert, GitHubOrg, GitHubRepo } from '../types'; |
9 | 10 | import { targetReposVariable } from '../variables'; |
10 | 11 |
|
| 12 | +interface AlertCounts { |
| 13 | + open: number; |
| 14 | + dismissed: number; |
| 15 | + fixed: number; |
| 16 | + total: number; |
| 17 | + bySeverity: { |
| 18 | + critical: number; |
| 19 | + high: number; |
| 20 | + medium: number; |
| 21 | + low: number; |
| 22 | + }; |
| 23 | +} |
| 24 | + |
11 | 25 | export const dependabotCheck: IntegrationCheck = { |
12 | 26 | id: 'dependabot_enabled', |
13 | 27 | name: 'Dependabot Security Updates Enabled', |
@@ -43,30 +57,128 @@ export const dependabotCheck: IntegrationCheck = { |
43 | 57 |
|
44 | 58 | ctx.log(`Checking ${repos.length} repositories for Dependabot`); |
45 | 59 |
|
| 60 | + /** |
| 61 | + * Fetch Dependabot alerts for a repository and calculate counts |
| 62 | + */ |
| 63 | + const fetchAlertCounts = async (repoFullName: string): Promise<AlertCounts | null> => { |
| 64 | + try { |
| 65 | + const alerts = await ctx.fetchAllPages<GitHubDependabotAlert>( |
| 66 | + `/repos/${repoFullName}/dependabot/alerts`, |
| 67 | + ); |
| 68 | + |
| 69 | + const counts: AlertCounts = { |
| 70 | + open: 0, |
| 71 | + dismissed: 0, |
| 72 | + fixed: 0, |
| 73 | + total: alerts.length, |
| 74 | + bySeverity: { critical: 0, high: 0, medium: 0, low: 0 }, |
| 75 | + }; |
| 76 | + |
| 77 | + for (const alert of alerts) { |
| 78 | + // Count by state |
| 79 | + if (alert.state === 'open') counts.open++; |
| 80 | + else if (alert.state === 'dismissed') counts.dismissed++; |
| 81 | + else if (alert.state === 'fixed') counts.fixed++; |
| 82 | + |
| 83 | + // Count open alerts by severity |
| 84 | + if (alert.state === 'open') { |
| 85 | + const severity = alert.security_vulnerability?.severity ?? 'low'; |
| 86 | + counts.bySeverity[severity]++; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return counts; |
| 91 | + } catch (error) { |
| 92 | + const errorStr = String(error); |
| 93 | + // 403 usually means Dependabot alerts are not enabled or no permission |
| 94 | + if (errorStr.includes('403') || errorStr.includes('Forbidden')) { |
| 95 | + ctx.log(`Cannot access Dependabot alerts for ${repoFullName} (permission denied)`); |
| 96 | + return null; |
| 97 | + } |
| 98 | + ctx.warn(`Failed to fetch Dependabot alerts for ${repoFullName}: ${errorStr}`); |
| 99 | + return null; |
| 100 | + } |
| 101 | + }; |
| 102 | + |
| 103 | + /** |
| 104 | + * Format alert counts for display |
| 105 | + */ |
| 106 | + const formatAlertSummary = (counts: AlertCounts): string => { |
| 107 | + const parts: string[] = []; |
| 108 | + |
| 109 | + if (counts.open > 0) { |
| 110 | + const severityBreakdown: string[] = []; |
| 111 | + if (counts.bySeverity.critical > 0) |
| 112 | + severityBreakdown.push(`${counts.bySeverity.critical} critical`); |
| 113 | + if (counts.bySeverity.high > 0) severityBreakdown.push(`${counts.bySeverity.high} high`); |
| 114 | + if (counts.bySeverity.medium > 0) |
| 115 | + severityBreakdown.push(`${counts.bySeverity.medium} medium`); |
| 116 | + if (counts.bySeverity.low > 0) severityBreakdown.push(`${counts.bySeverity.low} low`); |
| 117 | + |
| 118 | + parts.push(`${counts.open} open (${severityBreakdown.join(', ')})`); |
| 119 | + } else { |
| 120 | + parts.push('0 open'); |
| 121 | + } |
| 122 | + |
| 123 | + parts.push(`${counts.fixed} fixed`); |
| 124 | + parts.push(`${counts.dismissed} dismissed`); |
| 125 | + |
| 126 | + return parts.join(', '); |
| 127 | + }; |
| 128 | + |
46 | 129 | for (const repo of repos) { |
47 | 130 | const dependabotStatus = repo.security_and_analysis?.dependabot_security_updates?.status; |
48 | 131 |
|
| 132 | + // Fetch alert counts regardless of Dependabot status |
| 133 | + const alertCounts = await fetchAlertCounts(repo.full_name); |
| 134 | + |
49 | 135 | if (dependabotStatus === 'enabled') { |
| 136 | + const alertSummary = alertCounts |
| 137 | + ? `\n\nAlert Summary: ${formatAlertSummary(alertCounts)}` |
| 138 | + : ''; |
| 139 | + |
50 | 140 | ctx.pass({ |
51 | 141 | title: `Dependabot enabled on ${repo.name}`, |
52 | | - description: |
53 | | - 'Dependabot security updates are enabled and will automatically create pull requests to fix vulnerable dependencies.', |
| 142 | + description: `Dependabot security updates are enabled and will automatically create pull requests to fix vulnerable dependencies.${alertSummary}`, |
54 | 143 | resourceType: 'repository', |
55 | 144 | resourceId: repo.full_name, |
56 | 145 | evidence: { |
57 | 146 | security_and_analysis: repo.security_and_analysis, |
| 147 | + ...(alertCounts && { |
| 148 | + alerts: { |
| 149 | + open: alertCounts.open, |
| 150 | + fixed: alertCounts.fixed, |
| 151 | + dismissed: alertCounts.dismissed, |
| 152 | + total: alertCounts.total, |
| 153 | + open_by_severity: alertCounts.bySeverity, |
| 154 | + }, |
| 155 | + }), |
58 | 156 | checked_at: new Date().toISOString(), |
59 | 157 | }, |
60 | 158 | }); |
61 | 159 | } else { |
| 160 | + const alertSummary = alertCounts |
| 161 | + ? `\n\nAlert Summary: ${formatAlertSummary(alertCounts)}` |
| 162 | + : ''; |
| 163 | + |
62 | 164 | ctx.fail({ |
63 | 165 | title: `Dependabot not enabled on ${repo.name}`, |
64 | | - description: |
65 | | - 'Dependabot security updates are not enabled, leaving the repository vulnerable to known dependency exploits.', |
| 166 | + description: `Dependabot security updates are not enabled, leaving the repository vulnerable to known dependency exploits.${alertSummary}`, |
66 | 167 | resourceType: 'repository', |
67 | 168 | resourceId: repo.full_name, |
68 | 169 | severity: 'medium', |
69 | 170 | remediation: `1. Go to ${repo.html_url}/settings/security_analysis\n2. Enable "Dependabot security updates"\n3. Optionally enable "Dependabot version updates" for proactive updates`, |
| 171 | + evidence: alertCounts |
| 172 | + ? { |
| 173 | + alerts: { |
| 174 | + open: alertCounts.open, |
| 175 | + fixed: alertCounts.fixed, |
| 176 | + dismissed: alertCounts.dismissed, |
| 177 | + total: alertCounts.total, |
| 178 | + open_by_severity: alertCounts.bySeverity, |
| 179 | + }, |
| 180 | + } |
| 181 | + : undefined, |
70 | 182 | }); |
71 | 183 | } |
72 | 184 | } |
|
0 commit comments