Skip to content

Commit fe28775

Browse files
ci(workflow): adopt hardened hypatia-scan from hyperpolymath/hypatia#237 (#8)
Catch-up propagation. Baseline added as 2nd commit after first run. Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
1 parent af04e70 commit fe28775

1 file changed

Lines changed: 106 additions & 20 deletions

File tree

.github/workflows/hypatia-scan.yml

Lines changed: 106 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ on:
1313

1414
permissions:
1515
contents: read
16-
# security-events: read lets the built-in GITHUB_TOKEN query this
17-
# repo\'s own Dependabot alerts via the Hypatia DependabotAlerts rule.
18-
security-events: read
16+
# `pull-requests: write` is needed for the "Comment on PR with findings"
17+
# step to POST a results summary. Note: on Dependabot PRs the token is
18+
# downgraded to read-only regardless, so that step is also marked
19+
# continue-on-error below.
20+
pull-requests: write
1921

2022
jobs:
2123
scan:
@@ -24,19 +26,25 @@ jobs:
2426

2527
steps:
2628
- name: Checkout repository
27-
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
29+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v4
2830
with:
2931
fetch-depth: 0 # Full history for better pattern analysis
3032

3133
- name: Setup Elixir for Hypatia scanner
32-
uses: erlef/setup-beam@e6d7c94229049569db56a7ad5a540c051a010af9 # v1.18.2
34+
uses: erlef/setup-beam@fc68ffb90438ef2936bbb3251622353b3dcb2f93 # v1.18.2
3335
with:
3436
elixir-version: '1.19.4'
3537
otp-version: '28.3'
3638

37-
- name: Clone Hypatia
39+
- name: Clone Hypatia (or use checkout when scanning hypatia itself)
3840
run: |
39-
if [ ! -d "$HOME/hypatia" ]; then
41+
# When scanning hypatia from inside hypatia, point $HOME/hypatia
42+
# at the PR/branch checkout instead of cloning main — otherwise
43+
# CLI changes can never pass their own gate (the scanner binary
44+
# would always come from main and ignore new flags).
45+
if [ "${{ github.repository }}" = "hyperpolymath/hypatia" ]; then
46+
ln -sfn "${GITHUB_WORKSPACE}" "$HOME/hypatia"
47+
elif [ ! -d "$HOME/hypatia" ]; then
4048
git clone https://github.com/hyperpolymath/hypatia.git "$HOME/hypatia"
4149
fi
4250
@@ -51,11 +59,39 @@ jobs:
5159
5260
- name: Run Hypatia scan
5361
id: scan
62+
env:
63+
# Suppress the "Warning: Dependabot alerts unavailable: GITHUB_TOKEN
64+
# not set" line so the run is silent-warning-free. The token is
65+
# read-only by default and only used to query Dependabot alerts.
66+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5467
run: |
5568
echo "Scanning repository: ${{ github.repository }}"
5669
57-
# Run scanner (exits non-zero when findings exist — suppress to continue)
58-
HYPATIA_FORMAT=json "$HOME/hypatia/hypatia-cli.sh" scan . > hypatia-findings.json || true
70+
# Run scanner with --exit-zero so a findings-found exit-1 does
71+
# NOT short-circuit the rest of this step under `set -e`. The
72+
# downstream "Check for critical or high-severity issues" step
73+
# is the explicit gate. See hyperpolymath/hypatia#213.
74+
#
75+
# Guard against the scanner producing no output (a crash, an
76+
# unknown flag, etc.): if hypatia-findings.json is empty or
77+
# missing after the run, fall back to "[]" so the jq calls
78+
# below don't 9 the whole gate. We surface stderr so the
79+
# underlying scanner failure is still visible in the log.
80+
set +e
81+
HYPATIA_FORMAT=json "$HOME/hypatia/hypatia-cli.sh" scan . --exit-zero \
82+
> hypatia-findings.json 2> hypatia-scan.stderr
83+
SCAN_EXIT=$?
84+
set -e
85+
echo "Scanner exit: $SCAN_EXIT"
86+
if [ -s hypatia-scan.stderr ]; then
87+
echo "--- scanner stderr ---"
88+
cat hypatia-scan.stderr
89+
echo "--- end stderr ---"
90+
fi
91+
if ! jq empty hypatia-findings.json 2>/dev/null; then
92+
echo "Scanner did not produce valid JSON; defaulting to empty findings."
93+
echo "[]" > hypatia-findings.json
94+
fi
5995
6096
# Count findings
6197
FINDING_COUNT=$(jq '. | length' hypatia-findings.json 2>/dev/null || echo 0)
@@ -77,7 +113,7 @@ jobs:
77113
echo "- Medium: $MEDIUM" >> $GITHUB_STEP_SUMMARY
78114
79115
- name: Upload findings artifact
80-
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
116+
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
81117
with:
82118
name: hypatia-findings
83119
path: hypatia-findings.json
@@ -87,6 +123,8 @@ jobs:
87123
if: steps.scan.outputs.findings_count > 0
88124
env:
89125
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
126+
FLEET_PUSH_TOKEN: ${{ secrets.HYPATIA_DISPATCH_PAT }}
127+
FLEET_DISPATCH_TOKEN: ${{ secrets.HYPATIA_DISPATCH_PAT }}
90128
GITHUB_REPOSITORY: ${{ github.repository }}
91129
GITHUB_SHA: ${{ github.sha }}
92130
run: |
@@ -96,21 +134,63 @@ jobs:
96134
FLEET_DIR="/tmp/gitbot-fleet-$$"
97135
git clone https://github.com/hyperpolymath/gitbot-fleet.git "$FLEET_DIR"
98136
99-
# Run submission script
100-
bash "$FLEET_DIR/scripts/submit-finding.sh" hypatia-findings.json
137+
# Run submission script. Pass the findings path as ABSOLUTE —
138+
# submit-finding.sh cd's into its own working dir before reading
139+
# the file, so a relative path would resolve to the wrong place
140+
# and the script fails with "No such file or directory".
141+
bash "$FLEET_DIR/scripts/submit-finding.sh" "$GITHUB_WORKSPACE/hypatia-findings.json"
101142
102143
# Cleanup
103144
rm -rf "$FLEET_DIR"
104145
105146
echo "✅ Finding submission complete"
106147
107-
- name: Check for critical issues
108-
if: steps.scan.outputs.critical > 0
148+
- name: Check for critical or high-severity issues
149+
if: steps.scan.outputs.critical > 0 || steps.scan.outputs.high > 0
109150
run: |
110-
echo "⚠️ Critical security issues found!"
111-
echo "Review hypatia-findings.json for details"
112-
# Don't fail the build yet - just warn
113-
# exit 1
151+
echo "Total critical/high: ${{ steps.scan.outputs.critical }} critical, ${{ steps.scan.outputs.high }} high"
152+
153+
# Baseline-aware gate: pre-existing accepted findings live in
154+
# .hypatia-baseline.json (committed). New critical/high findings
155+
# not in the baseline still fail the build. Findings are matched
156+
# on (severity, rule_module, type, file) tuple with absolute
157+
# build paths normalised to repo-relative.
158+
if [ -f .hypatia-baseline.json ]; then
159+
# Normalise + project the FINDING IDENTITY tuple from the current
160+
# scan. Identity is (severity, rule_module, type, file) — `action`
161+
# is remediation guidance that can legitimately drift between
162+
# scanner versions (e.g. "flag" -> "create_branch") and is NOT
163+
# part of what makes two findings the same.
164+
jq '[ .[] | select(.severity == "critical" or .severity == "high")
165+
| {severity, rule_module, type,
166+
file: (.file | sub("^/home/runner/work/[^/]+/[^/]+/"; "")
167+
| sub("^/github/workspace/"; "")) } ]' \
168+
hypatia-findings.json > findings-current.json
169+
170+
# Subtract baseline. A current finding is "new" iff there's no
171+
# baseline element with the same identity tuple. Baseline entries
172+
# may include extra fields (e.g. `action`); strip them before the
173+
# comparison so legacy baselines keep working.
174+
jq --slurpfile base .hypatia-baseline.json \
175+
'($base[0] | map({severity, rule_module, type, file})) as $bk
176+
| map(. as $f | select(($bk | any(. == $f)) | not))' \
177+
findings-current.json > findings-new.json
178+
new_count=$(jq 'length' findings-new.json)
179+
180+
if [ "$new_count" -gt 0 ]; then
181+
echo "::error::$new_count new critical/high finding(s) outside the baseline:"
182+
jq -r '.[] | " [\(.severity)] \(.rule_module)/\(.type) — \(.file)"' findings-new.json
183+
echo
184+
echo "If these are intentional, regenerate .hypatia-baseline.json:"
185+
echo " jq '[.[] | select(.severity == \"critical\" or .severity == \"high\") | {severity, rule_module, type, file}] | sort_by(.severity, .rule_module, .type, .file)' hypatia-findings.json > .hypatia-baseline.json"
186+
exit 1
187+
fi
188+
echo "All critical/high findings present in baseline — gate passes."
189+
else
190+
echo "No .hypatia-baseline.json — failing on any critical/high (legacy behaviour)."
191+
echo "Review hypatia-findings.json for details"
192+
exit 1
193+
fi
114194
115195
- name: Generate scan report
116196
run: |
@@ -147,8 +227,14 @@ jobs:
147227
cat hypatia-report.md >> $GITHUB_STEP_SUMMARY
148228
149229
- name: Comment on PR with findings
230+
# Dependabot PRs always run with a read-only token regardless of the
231+
# workflow's declared permissions, so the createComment call below
232+
# would 403 on every dep-bump PR. The PR comment is informational
233+
# (the check result is already visible in the PR UI); we don't want
234+
# its absence to block merge.
150235
if: github.event_name == 'pull_request' && steps.scan.outputs.findings_count > 0
151-
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v7
236+
continue-on-error: true
237+
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v7
152238
with:
153239
script: |
154240
const fs = require('fs');
@@ -178,4 +264,4 @@ jobs:
178264
repo: context.repo.repo,
179265
issue_number: context.issue.number,
180266
body: comment
181-
});
267+
});

0 commit comments

Comments
 (0)