-
Notifications
You must be signed in to change notification settings - Fork 0
317 lines (288 loc) · 14 KB
/
org-security-scan.yml
File metadata and controls
317 lines (288 loc) · 14 KB
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
name: Org-Wide Security Scan
on:
# Triggered by the org webhook relay when a PR is opened/updated
# or when a new repo is created in the org.
repository_dispatch:
types:
- pull_request_scan # PR opened, reopened, or synchronized
- default_branch_scan # Push directly to default branch (incl. first push to a new repo)
# Keep manual trigger for one-off or backfill scans
workflow_dispatch:
inputs:
repo:
description: 'Repo to scan (owner/repo)'
required: true
pr_number:
description: 'PR number (leave blank for default branch scan)'
required: false
sha:
description: 'Commit SHA to scan'
required: false
permissions:
contents: read
security-events: write
jobs:
# ── Resolve inputs from either dispatch source ───────────────────────────
resolve:
name: Resolve scan target
runs-on: ubuntu-latest
outputs:
repo: ${{ steps.resolve.outputs.repo }}
sha: ${{ steps.resolve.outputs.sha }}
pr_number: ${{ steps.resolve.outputs.pr_number }}
ref: ${{ steps.resolve.outputs.ref }}
steps:
- name: Resolve target
id: resolve
env:
EVENT: ${{ github.event_name }}
# repository_dispatch payloads
RD_REPO: ${{ github.event.client_payload.repo }}
RD_SHA: ${{ github.event.client_payload.sha }}
RD_PR: ${{ github.event.client_payload.pr_number }}
RD_REF: ${{ github.event.client_payload.ref }}
# workflow_dispatch inputs
WD_REPO: ${{ inputs.repo }}
WD_SHA: ${{ inputs.sha }}
WD_PR: ${{ inputs.pr_number }}
run: |
if [ "$EVENT" = "workflow_dispatch" ]; then
echo "repo=$WD_REPO" >> "$GITHUB_OUTPUT"
echo "sha=$WD_SHA" >> "$GITHUB_OUTPUT"
echo "pr_number=$WD_PR" >> "$GITHUB_OUTPUT"
echo "ref=refs/heads/main" >> "$GITHUB_OUTPUT"
else
echo "repo=$RD_REPO" >> "$GITHUB_OUTPUT"
echo "sha=$RD_SHA" >> "$GITHUB_OUTPUT"
echo "pr_number=$RD_PR" >> "$GITHUB_OUTPUT"
echo "ref=$RD_REF" >> "$GITHUB_OUTPUT"
fi
# ── Set pending commit status on the PR ──────────────────────────────────
set-pending:
name: Set pending status
needs: resolve
runs-on: ubuntu-latest
if: needs.resolve.outputs.pr_number != ''
steps:
- name: Post pending status — Semgrep
uses: actions/github-script@v7
with:
github-token: ${{ secrets.ORG_SCAN_TOKEN }}
script: |
await github.rest.repos.createCommitStatus({
owner: '${{ needs.resolve.outputs.repo }}'.split('/')[0],
repo: '${{ needs.resolve.outputs.repo }}'.split('/')[1],
sha: '${{ needs.resolve.outputs.sha }}',
state: 'pending',
context: 'security/semgrep',
description: 'Semgrep SAST scan in progress...',
});
- name: Post pending status — Trivy
uses: actions/github-script@v7
with:
github-token: ${{ secrets.ORG_SCAN_TOKEN }}
script: |
await github.rest.repos.createCommitStatus({
owner: '${{ needs.resolve.outputs.repo }}'.split('/')[0],
repo: '${{ needs.resolve.outputs.repo }}'.split('/')[1],
sha: '${{ needs.resolve.outputs.sha }}',
state: 'pending',
context: 'security/trivy',
description: 'Trivy vulnerability scan in progress...',
});
# ── Semgrep ───────────────────────────────────────────────────────────────
semgrep:
name: Semgrep SAST
needs: [resolve, set-pending]
if: always() && needs.resolve.result == 'success'
runs-on: ubuntu-latest
outputs:
findings: ${{ steps.count.outputs.findings }}
critical: ${{ steps.count.outputs.critical }}
high: ${{ steps.count.outputs.high }}
medium: ${{ steps.count.outputs.medium }}
steps:
- name: Checkout target repo
uses: actions/checkout@v4
with:
repository: ${{ needs.resolve.outputs.repo }}
token: ${{ secrets.ORG_SCAN_TOKEN }}
ref: ${{ needs.resolve.outputs.sha || needs.resolve.outputs.ref }}
fetch-depth: 0
- name: Install Semgrep
run: pip install semgrep
- name: Run Semgrep
id: semgrep
run: |
semgrep scan \
--config p/owasp-top-ten \
--config p/secrets \
--config p/javascript \
--config p/python \
--config p/golang \
--config p/docker \
--config p/terraform \
--sarif \
--output semgrep.sarif \
--no-error \
.
continue-on-error: true
- name: Count findings
id: count
run: |
if [ -f semgrep.sarif ]; then
FINDINGS=$(jq '[.runs[].results[]] | length' semgrep.sarif)
CRITICAL=$(jq '[.runs[].results[] | select(.properties.severity == "error")] | length' semgrep.sarif)
HIGH=$(jq '[.runs[].results[] | select(.properties.severity == "warning")] | length' semgrep.sarif)
MEDIUM=$(jq '[.runs[].results[] | select(.properties.severity == "note")] | length' semgrep.sarif)
else
FINDINGS=0; CRITICAL=0; HIGH=0; MEDIUM=0
fi
echo "findings=$FINDINGS" >> "$GITHUB_OUTPUT"
echo "critical=$CRITICAL" >> "$GITHUB_OUTPUT"
echo "high=$HIGH" >> "$GITHUB_OUTPUT"
echo "medium=$MEDIUM" >> "$GITHUB_OUTPUT"
- name: Upload SARIF to target repo
uses: github/codeql-action/upload-sarif@v4
if: always()
with:
sarif_file: semgrep.sarif
category: semgrep
env:
GITHUB_TOKEN: ${{ secrets.ORG_SCAN_TOKEN }}
GITHUB_REPOSITORY: ${{ needs.resolve.outputs.repo }}
- name: Update commit status — Semgrep
if: always() && needs.resolve.outputs.pr_number != ''
uses: actions/github-script@v7
with:
github-token: ${{ secrets.ORG_SCAN_TOKEN }}
script: |
const findings = parseInt('${{ steps.count.outputs.findings }}') || 0;
const critical = parseInt('${{ steps.count.outputs.critical }}') || 0;
await github.rest.repos.createCommitStatus({
owner: '${{ needs.resolve.outputs.repo }}'.split('/')[0],
repo: '${{ needs.resolve.outputs.repo }}'.split('/')[1],
sha: '${{ needs.resolve.outputs.sha }}',
state: critical > 0 ? 'failure' : findings > 0 ? 'warning' : 'success',
context: 'security/semgrep',
description: findings === 0
? 'No issues found'
: `${findings} issue(s) found (${critical} critical)`,
target_url: `https://github.com/${{ needs.resolve.outputs.repo }}/security/code-scanning`,
});
# ── Trivy ─────────────────────────────────────────────────────────────────
trivy:
name: Trivy Vulnerability Scan
needs: [resolve, set-pending]
if: always() && needs.resolve.result == 'success'
runs-on: ubuntu-latest
outputs:
findings: ${{ steps.count.outputs.findings }}
critical: ${{ steps.count.outputs.critical }}
high: ${{ steps.count.outputs.high }}
medium: ${{ steps.count.outputs.medium }}
steps:
- name: Checkout target repo
uses: actions/checkout@v4
with:
repository: ${{ needs.resolve.outputs.repo }}
token: ${{ secrets.ORG_SCAN_TOKEN }}
ref: ${{ needs.resolve.outputs.sha || needs.resolve.outputs.ref }}
- name: Run Trivy
uses: aquasecurity/trivy-action@master
with:
scan-type: fs
scan-ref: .
format: sarif
output: trivy-results.sarif
severity: CRITICAL,HIGH,MEDIUM
ignore-unfixed: true
exit-code: 0
- name: Count findings
id: count
run: |
if [ -f trivy-results.sarif ]; then
FINDINGS=$(jq '[.runs[].results[]] | length' trivy-results.sarif)
CRITICAL=$(jq '[.runs[].results[] | select(.properties.severity == "error")] | length' trivy-results.sarif)
HIGH=$(jq '[.runs[].results[] | select(.properties.severity == "warning")] | length' trivy-results.sarif)
MEDIUM=$(jq '[.runs[].results[] | select(.properties.severity == "note")] | length' trivy-results.sarif)
else
FINDINGS=0; CRITICAL=0; HIGH=0; MEDIUM=0
fi
echo "findings=$FINDINGS" >> "$GITHUB_OUTPUT"
echo "critical=$CRITICAL" >> "$GITHUB_OUTPUT"
echo "high=$HIGH" >> "$GITHUB_OUTPUT"
echo "medium=$MEDIUM" >> "$GITHUB_OUTPUT"
- name: Upload SARIF to target repo
uses: github/codeql-action/upload-sarif@v4
if: always()
with:
sarif_file: trivy-results.sarif
category: trivy
env:
GITHUB_TOKEN: ${{ secrets.ORG_SCAN_TOKEN }}
GITHUB_REPOSITORY: ${{ needs.resolve.outputs.repo }}
- name: Update commit status — Trivy
if: always() && needs.resolve.outputs.pr_number != ''
uses: actions/github-script@v7
with:
github-token: ${{ secrets.ORG_SCAN_TOKEN }}
script: |
const findings = parseInt('${{ steps.count.outputs.findings }}') || 0;
const critical = parseInt('${{ steps.count.outputs.critical }}') || 0;
await github.rest.repos.createCommitStatus({
owner: '${{ needs.resolve.outputs.repo }}'.split('/')[0],
repo: '${{ needs.resolve.outputs.repo }}'.split('/')[1],
sha: '${{ needs.resolve.outputs.sha }}',
state: critical > 0 ? 'failure' : findings > 0 ? 'warning' : 'success',
context: 'security/trivy',
description: findings === 0
? 'No vulnerabilities found'
: `${findings} vulnerability(s) found (${critical} critical)`,
target_url: `https://github.com/${{ needs.resolve.outputs.repo }}/security/code-scanning`,
});
# ── Post PR comment with combined summary ─────────────────────────────────
pr-comment:
name: Post PR summary
needs: [resolve, semgrep, trivy]
if: always() && needs.resolve.outputs.pr_number != ''
runs-on: ubuntu-latest
steps:
- name: Post or update PR comment
uses: actions/github-script@v7
with:
github-token: ${{ secrets.ORG_SCAN_TOKEN }}
script: |
const [owner, repo] = '${{ needs.resolve.outputs.repo }}'.split('/');
const pr_number = parseInt('${{ needs.resolve.outputs.pr_number }}');
const semgrepFindings = parseInt('${{ needs.semgrep.outputs.findings }}') || 0;
const semgrepCritical = parseInt('${{ needs.semgrep.outputs.critical }}') || 0;
const semgrepHigh = parseInt('${{ needs.semgrep.outputs.high }}') || 0;
const semgrepMedium = parseInt('${{ needs.semgrep.outputs.medium }}') || 0;
const trivyFindings = parseInt('${{ needs.trivy.outputs.findings }}') || 0;
const trivyCritical = parseInt('${{ needs.trivy.outputs.critical }}') || 0;
const trivyHigh = parseInt('${{ needs.trivy.outputs.high }}') || 0;
const trivyMedium = parseInt('${{ needs.trivy.outputs.medium }}') || 0;
const totalCritical = semgrepCritical + trivyCritical;
const overallIcon = totalCritical > 0 ? '🔴' : (semgrepFindings + trivyFindings) > 0 ? '🟡' : '🟢';
const overallStatus = totalCritical > 0 ? 'Action required' : (semgrepFindings + trivyFindings) > 0 ? 'Review recommended' : 'All clear';
const semgrepIcon = semgrepCritical > 0 ? '🔴' : semgrepFindings > 0 ? '🟡' : '🟢';
const trivyIcon = trivyCritical > 0 ? '🔴' : trivyFindings > 0 ? '🟡' : '🟢';
const securityUrl = `https://github.com/${owner}/${repo}/security/code-scanning`;
const runUrl = `${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}`;
const body = `<!-- security-scan-bot -->
## ${overallIcon} Security Scan Results — ${overallStatus}
| Scanner | Critical | High | Medium | Total | Status |
|---------|:--------:|:----:|:------:|:-----:|--------|
| ${semgrepIcon} Semgrep (SAST) | ${semgrepCritical} | ${semgrepHigh} | ${semgrepMedium} | ${semgrepFindings} | ${semgrepFindings === 0 ? 'Clean ✅' : 'Issues found'} |
| ${trivyIcon} Trivy (vulns + secrets) | ${trivyCritical} | ${trivyHigh} | ${trivyMedium} | ${trivyFindings} | ${trivyFindings === 0 ? 'Clean ✅' : 'Issues found'} |
${(semgrepFindings + trivyFindings) > 0 ? `> View full findings in the [Security tab](${securityUrl}).` : ''}
<sub>Scan run: [#${{ github.run_number }}](${runUrl}) · Commit: \`${{ needs.resolve.outputs.sha }}\`</sub>`;
// Find and update existing comment, or create a new one
const comments = await github.rest.issues.listComments({ owner, repo, issue_number: pr_number });
const existing = comments.data.find(c => c.body.includes('<!-- security-scan-bot -->'));
if (existing) {
await github.rest.issues.updateComment({ owner, repo, comment_id: existing.id, body });
} else {
await github.rest.issues.createComment({ owner, repo, issue_number: pr_number, body });
}