-
-
Notifications
You must be signed in to change notification settings - Fork 0
370 lines (326 loc) · 15.9 KB
/
Copy pathstatic-analysis-gate.yml
File metadata and controls
370 lines (326 loc) · 15.9 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
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
# SPDX-License-Identifier: MPL-2.0
# Static Analysis Gate — Required by branch protection rules.
# Runs panic-attack and hypatia, deposits findings for gitbot-fleet learning.
name: Static Analysis Gate
on:
pull_request:
branches: ['**']
push:
branches: [main, master]
permissions:
contents: read
jobs:
# ---------------------------------------------------------------------------
# Job 1: panic-attack assail
# ---------------------------------------------------------------------------
panic-attack-assail:
name: panic-attack assail
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
fetch-depth: 0
- name: Install panic-attack (if available)
id: install
run: |
# Try to fetch the latest release binary from the org
PA_URL="https://github.com/hyperpolymath/panic-attack/releases/latest/download/panic-attack-linux-x86_64"
if curl -fsSL --head "$PA_URL" >/dev/null 2>&1; then
curl -fsSL -o /usr/local/bin/panic-attack "$PA_URL"
chmod +x /usr/local/bin/panic-attack
echo "installed=true" >> "$GITHUB_OUTPUT"
else
echo "::notice::panic-attack binary not available — skipping assail"
echo "installed=false" >> "$GITHUB_OUTPUT"
fi
- name: Run panic-attack assail
id: assail
if: steps.install.outputs.installed == 'true'
run: |
set +e
panic-attack assail --format json . > panic-attack-findings.json
PA_EXIT=$?
set -e
# Same defect class as the Hypatia job below: `2>&1` folded the
# scanner's stderr into the JSON payload, so every jq parse failed,
# every count silently became 0 via `|| echo 0`, and "Fail on critical
# findings" could never fire on any input. Keep stderr on the log.
if [ ! -s panic-attack-findings.json ]; then
echo "[]" > panic-attack-findings.json
fi
# Deliberately a WARNING, not a failure. panic-attack is a downloaded
# release binary whose exit-code and output contract are not verified
# here, and it has no confirmed --exit-zero equivalent, so we surface a
# malformed payload in the log rather than block on an unverified tool.
# Promote to `exit 1` (as the Hypatia job does) once that contract is
# confirmed -- see the follow-up issue linked from this PR.
if ! jq -e 'type == "array"' panic-attack-findings.json >/dev/null 2>&1; then
echo "::warning::panic-attack output is not a JSON array (exit ${PA_EXIT}); counts below are unreliable"
fi
# Parse finding counts
TOTAL=$(jq '. | length' panic-attack-findings.json 2>/dev/null || echo 0)
CRITICAL=$(jq '[.[] | select(.severity == "critical")] | length' panic-attack-findings.json 2>/dev/null || echo 0)
HIGH=$(jq '[.[] | select(.severity == "high")] | length' panic-attack-findings.json 2>/dev/null || echo 0)
MEDIUM=$(jq '[.[] | select(.severity == "medium")] | length' panic-attack-findings.json 2>/dev/null || echo 0)
LOW=$(jq '[.[] | select(.severity == "low")] | length' panic-attack-findings.json 2>/dev/null || echo 0)
echo "total=$TOTAL" >> "$GITHUB_OUTPUT"
echo "critical=$CRITICAL" >> "$GITHUB_OUTPUT"
echo "high=$HIGH" >> "$GITHUB_OUTPUT"
echo "medium=$MEDIUM" >> "$GITHUB_OUTPUT"
echo "low=$LOW" >> "$GITHUB_OUTPUT"
echo "exit_code=$PA_EXIT" >> "$GITHUB_OUTPUT"
- name: Emit check annotations
if: steps.install.outputs.installed == 'true'
run: |
# Convert JSON findings into GitHub Actions annotations
# Findings carry no `.message` (keys: action,file,line,reason,rule_module,
# severity,type), so every annotation read "null". `.file` is an absolute
# runner path, which GitHub cannot anchor to the diff, so it is made
# workspace-relative here.
jq -r --arg ws "$GITHUB_WORKSPACE" '.[] | select(.file != null) |
(.file | ltrimstr($ws + "/")) as $f |
(.reason // .message // .type // "finding") as $m |
if .severity == "critical" then
"::error file=\($f),line=\(.line // 1)::[panic-attack] \($m)"
elif .severity == "high" then
"::error file=\($f),line=\(.line // 1)::[panic-attack] \($m)"
else
"::warning file=\($f),line=\(.line // 1)::[panic-attack] \($m)"
end
' panic-attack-findings.json || true
- name: Write step summary
if: steps.install.outputs.installed == 'true'
run: |
cat <<EOF >> "$GITHUB_STEP_SUMMARY"
## panic-attack assail Results
| Severity | Count |
|----------|-------|
| Critical | ${{ steps.assail.outputs.critical }} |
| High | ${{ steps.assail.outputs.high }} |
| Medium | ${{ steps.assail.outputs.medium }} |
| Low | ${{ steps.assail.outputs.low }} |
| **Total**| ${{ steps.assail.outputs.total }} |
EOF
- name: Create stub findings (when panic-attack unavailable)
if: steps.install.outputs.installed != 'true'
run: |
echo "[]" > panic-attack-findings.json
echo "## panic-attack assail" >> "$GITHUB_STEP_SUMMARY"
echo "" >> "$GITHUB_STEP_SUMMARY"
echo "Skipped: panic-attack not available in this environment." >> "$GITHUB_STEP_SUMMARY"
- name: Upload panic-attack findings
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: panic-attack-findings
path: panic-attack-findings.json
retention-days: 90
- name: Fail on critical findings
if: steps.install.outputs.installed == 'true' && steps.assail.outputs.critical > 0
run: |
echo "::error::panic-attack found ${{ steps.assail.outputs.critical }} critical issue(s) — blocking merge"
exit 1
# ---------------------------------------------------------------------------
# Job 2: hypatia-scan
# ---------------------------------------------------------------------------
hypatia-scan:
name: Hypatia neurosymbolic scan
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
fetch-depth: 0
- name: Setup Elixir for Hypatia scanner
id: beam
continue-on-error: true
uses: erlef/setup-beam@54075bcc5e249e4758d363f27d099f55d843f124 # v1.18.2
with:
elixir-version: '1.19.4'
otp-version: '28.3'
- name: Clone and build Hypatia
id: build
continue-on-error: true
run: |
git clone https://github.com/hyperpolymath/hypatia.git "$HOME/hypatia" 2>/dev/null || true
if [ -f "$HOME/hypatia/mix.exs" ]; then
cd "$HOME/hypatia"
if [ ! -f hypatia ] && [ ! -f hypatia-v2 ]; then
mix deps.get
mix escript.build
fi
echo "ready=true" >> "$GITHUB_OUTPUT"
else
echo "::notice::Hypatia scanner not available — skipping scan"
echo "ready=false" >> "$GITHUB_OUTPUT"
fi
- name: Run Hypatia scan
id: scan
if: steps.build.outputs.ready == 'true'
run: |
set +e
HYPATIA_FORMAT=json "$HOME/hypatia/hypatia-cli.sh" scan . --exit-zero > hypatia-findings.json
HYP_EXIT=$?
set -e
# --exit-zero is Hypatia's own documented CI recipe (lib/hypatia/cli.ex),
# for exactly this case: "use in CI when a downstream step gates on
# severity counts". Findings go to stdout, the one-line summary to
# stderr, and the process exits 0 unless the SCANNER itself failed.
#
# Do NOT redirect stderr into the payload with `2>&1`: that folds the
# summary line into the JSON, so every parse fails, the old `[]`
# fallback substituted a clean result, CRITICAL was always 0, and the
# gate below could never fire on any input. Keep stderr on the log.
if [ "$HYP_EXIT" -ne 0 ]; then
echo "::error::Hypatia scanner execution failed with exit ${HYP_EXIT}"
exit "$HYP_EXIT"
fi
# `jq empty` is NOT sufficient -- it succeeds on any valid JSON,
# including a bare string, object or null. Assert the array.
if [ ! -s hypatia-findings.json ] || ! jq -e 'type == "array"' hypatia-findings.json >/dev/null; then
echo "::error::Hypatia did not produce a valid JSON findings array"
exit 1
fi
TOTAL=$(jq '. | length' hypatia-findings.json 2>/dev/null || echo 0)
CRITICAL=$(jq '[.[] | select(.severity == "critical")] | length' hypatia-findings.json 2>/dev/null || echo 0)
HIGH=$(jq '[.[] | select(.severity == "high")] | length' hypatia-findings.json 2>/dev/null || echo 0)
MEDIUM=$(jq '[.[] | select(.severity == "medium")] | length' hypatia-findings.json 2>/dev/null || echo 0)
LOW=$(jq '[.[] | select(.severity == "low")] | length' hypatia-findings.json 2>/dev/null || echo 0)
echo "total=$TOTAL" >> "$GITHUB_OUTPUT"
echo "critical=$CRITICAL" >> "$GITHUB_OUTPUT"
echo "high=$HIGH" >> "$GITHUB_OUTPUT"
echo "medium=$MEDIUM" >> "$GITHUB_OUTPUT"
echo "low=$LOW" >> "$GITHUB_OUTPUT"
- name: Emit check annotations
if: steps.build.outputs.ready == 'true'
run: |
# Findings carry no `.message` (keys: action,file,line,reason,rule_module,
# severity,type), so every annotation read "null". `.file` is an absolute
# runner path, which GitHub cannot anchor to the diff, so it is made
# workspace-relative here.
jq -r --arg ws "$GITHUB_WORKSPACE" '.[] | select(.file != null) |
(.file | ltrimstr($ws + "/")) as $f |
(.reason // .message // .type // "finding") as $m |
if .severity == "critical" then
"::error file=\($f),line=\(.line // 1)::[hypatia] \($m)"
elif .severity == "high" then
"::error file=\($f),line=\(.line // 1)::[hypatia] \($m)"
else
"::warning file=\($f),line=\(.line // 1)::[hypatia] \($m)"
end
' hypatia-findings.json || true
- name: Write step summary
if: steps.build.outputs.ready == 'true'
run: |
cat <<EOF >> "$GITHUB_STEP_SUMMARY"
## Hypatia Scan Results
| Severity | Count |
|----------|-------|
| Critical | ${{ steps.scan.outputs.critical }} |
| High | ${{ steps.scan.outputs.high }} |
| Medium | ${{ steps.scan.outputs.medium }} |
| Low | ${{ steps.scan.outputs.low }} |
| **Total**| ${{ steps.scan.outputs.total }} |
EOF
- name: Create stub findings (when Hypatia unavailable)
if: steps.build.outputs.ready != 'true'
run: |
echo "[]" > hypatia-findings.json
echo "## Hypatia Scan" >> "$GITHUB_STEP_SUMMARY"
echo "" >> "$GITHUB_STEP_SUMMARY"
echo "Skipped: Hypatia scanner not available in this environment." >> "$GITHUB_STEP_SUMMARY"
- name: Upload hypatia findings
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: hypatia-findings
path: hypatia-findings.json
retention-days: 90
- name: Fail on critical security findings
if: steps.build.outputs.ready == 'true' && steps.scan.outputs.critical > 0
run: |
echo "::error::Hypatia found ${{ steps.scan.outputs.critical }} critical security issue(s) — blocking merge"
exit 1
# ---------------------------------------------------------------------------
# Job 3: deposit-findings (combines + archives for gitbot-fleet)
# ---------------------------------------------------------------------------
deposit-findings:
name: Deposit findings for gitbot-fleet
runs-on: ubuntu-latest
needs: [panic-attack-assail, hypatia-scan]
if: always()
steps:
- name: Download panic-attack findings
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v4
with:
name: panic-attack-findings
path: findings/
- name: Download hypatia findings
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v4
with:
name: hypatia-findings
path: findings/
- name: Combine findings into unified report
id: combine
run: |
PA_FILE="findings/panic-attack-findings.json"
HYP_FILE="findings/hypatia-findings.json"
# Ensure both files exist and are valid JSON arrays
for f in "$PA_FILE" "$HYP_FILE"; do
if [ ! -s "$f" ] || ! jq empty "$f" 2>/dev/null; then
echo "[]" > "$f"
fi
done
# Tag each finding with its source scanner
jq '[.[] | . + {"scanner": "panic-attack"}]' "$PA_FILE" > /tmp/pa-tagged.json
jq '[.[] | . + {"scanner": "hypatia"}]' "$HYP_FILE" > /tmp/hyp-tagged.json
# Build unified report envelope
jq -n \
--arg repo "${{ github.repository }}" \
--arg sha "${{ github.sha }}" \
--arg ref "${{ github.ref }}" \
--arg run_id "${{ github.run_id }}" \
--arg ts "$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
--slurpfile pa /tmp/pa-tagged.json \
--slurpfile hyp /tmp/hyp-tagged.json \
'{
schema_version: "1.0.0",
repository: $repo,
commit_sha: $sha,
ref: $ref,
run_id: $run_id,
timestamp: $ts,
findings: ($pa[0] + $hyp[0])
}' > findings/unified-findings.json
TOTAL=$(jq '.findings | length' findings/unified-findings.json)
CRITICAL=$(jq '[.findings[] | select(.severity == "critical")] | length' findings/unified-findings.json)
HIGH=$(jq '[.findings[] | select(.severity == "high")] | length' findings/unified-findings.json)
MEDIUM=$(jq '[.findings[] | select(.severity == "medium")] | length' findings/unified-findings.json)
LOW=$(jq '[.findings[] | select(.severity == "low")] | length' findings/unified-findings.json)
echo "total=$TOTAL" >> "$GITHUB_OUTPUT"
echo "critical=$CRITICAL" >> "$GITHUB_OUTPUT"
echo "high=$HIGH" >> "$GITHUB_OUTPUT"
echo "medium=$MEDIUM" >> "$GITHUB_OUTPUT"
echo "low=$LOW" >> "$GITHUB_OUTPUT"
- name: Upload unified findings (fleet scanner picks these up)
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: unified-findings
path: findings/unified-findings.json
retention-days: 90
- name: Write deposit summary
run: |
cat <<EOF >> "$GITHUB_STEP_SUMMARY"
## Unified Findings Deposit
**Repository:** ${{ github.repository }}
**Commit:** \`${{ github.sha }}\`
**Deposited at:** $(date -u +"%Y-%m-%d %H:%M:%S UTC")
| Severity | Count |
|----------|-------|
| Critical | ${{ steps.combine.outputs.critical }} |
| High | ${{ steps.combine.outputs.high }} |
| Medium | ${{ steps.combine.outputs.medium }} |
| Low | ${{ steps.combine.outputs.low }} |
| **Total**| ${{ steps.combine.outputs.total }} |
Findings saved as \`unified-findings\` artifact.
The gitbot-fleet scanner will ingest these on its next pass.
EOF