Skip to content

Commit 1ad52f5

Browse files
committed
fix(ci): make the code-scanning gate able to see alerts, and fail closed
The codeql-gate has never checked anything. It declared no permissions block, so it inherited the workflow's contents:read and the code-scanning alert API answered 403. gh writes the API error BODY to stdout, so ALERTS=$(gh api '.../code-scanning/alerts?state=open' --jq 'length' 2>/dev/null || echo "0") left ALERTS holding {"message":"Resource not accessible by integration",...,"status":"403"}0 every [ ... -gt ... ] then failed as a non-integer comparison, the if took its false branch, and the step printed "CodeQL gate passed (0 alerts)" and exited 0. Observed in the log of a GREEN run (34700514102, job 103571322660) while the repository had an open high-severity alert. The `|| echo "0"` reads as defensive and is the exact opposite: it converts "I cannot see the alerts" into "there are no alerts", which is the one direction a security gate must never fail in. Grant the job security-events:read (and actions:read, which the CodeQL-run poll needs for the same reason), and replace the fallback with a helper that treats a non-zero gh exit or a non-numeric body as a hard failure. The helper returns rather than exits because it runs inside a command substitution, where an exit would end only the subshell and let the caller continue with an empty count -- the same silent pass in a new costume; both call sites check the status. tests/test_security_gate_fail_closed.sh pins both halves and fails with six assertions against the previous workflow. Also bumps graph-ui's vitest to ^4.1.11 (GHSA-82fw-gwwq-j7x9, path traversal via @vitest/mocker, CVSS 5.9). Development scope, and graph-ui ships in no release artifact, so nothing released was ever exposed -- but it was the sole open alert and the gate above is worthless while it stands unresolved. Raising the floor past the vulnerable range also stops a fresh install resolving back into it. graph-ui: npm ci clean, 47 tests in 12 files pass on 4.1.11. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 9da4d66 commit 1ad52f5

7 files changed

Lines changed: 241 additions & 51 deletions

File tree

‎.github/workflows/_security.yml‎

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ jobs:
4141
codeql-gate:
4242
runs-on: ubuntu-latest
4343
timeout-minutes: 240
44+
# Without security-events:read this job inherits the workflow's
45+
# contents:read, the code-scanning alert API answers 403, and the gate
46+
# below reported "0 alerts" on every run it has ever made. actions:read is
47+
# what the CodeQL-run poll above needs for the same reason.
48+
permissions:
49+
contents: read
50+
security-events: read
51+
actions: read
4452
steps:
4553
- name: Wait for CodeQL on current commit (max 150 min)
4654
env:
@@ -77,11 +85,43 @@ jobs:
7785
env:
7886
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
7987
run: |
88+
# FAIL CLOSED. The previous form was `gh api ... 2>/dev/null || echo "0"`,
89+
# which looks defensive and is the opposite. gh writes the API error
90+
# BODY to stdout, so a 403 made ALERTS the string
91+
# {"message":"Resource not accessible by integration",...,"status":"403"}0
92+
# every `[` comparison then failed as non-integer, the `if` took its
93+
# false branch, and the step printed "CodeQL gate passed (0 alerts)"
94+
# and exited 0. That is exactly how a security gate should NEVER fail:
95+
# silently, in the direction of "everything is fine". An unreadable
96+
# alert list is now an error, not a zero.
97+
# NOTE: this runs in a command substitution, so it must `return`, not
98+
# `exit` -- an `exit` here would only end the subshell and let the
99+
# caller continue with an empty count, recreating the very bug this
100+
# replaces. The callers below check the status explicitly.
101+
count_open_alerts() {
102+
local body status
103+
body=$(gh api 'repos/${{ github.repository }}/code-scanning/alerts?state=open' \
104+
--jq 'length' 2>/dev/null)
105+
status=$?
106+
if [ $status -ne 0 ]; then
107+
echo "BLOCKED: cannot read code scanning alerts (gh exit $status)." >&2
108+
echo " Refusing to treat an unreadable alert list as zero." >&2
109+
echo " Most likely cause: the job lacks security-events:read." >&2
110+
return 1
111+
fi
112+
case "$body" in
113+
''|*[!0-9]*)
114+
echo "BLOCKED: alert count was not a number: ${body:-<empty>}" >&2
115+
return 1
116+
;;
117+
esac
118+
printf '%s' "$body"
119+
}
80120
echo "Waiting 60s for alert API to settle..."
81121
sleep 60
82-
ALERTS=$(gh api 'repos/${{ github.repository }}/code-scanning/alerts?state=open' --jq 'length' 2>/dev/null || echo "0")
122+
ALERTS=$(count_open_alerts) || exit 1
83123
sleep 15
84-
ALERTS2=$(gh api 'repos/${{ github.repository }}/code-scanning/alerts?state=open' --jq 'length' 2>/dev/null || echo "0")
124+
ALERTS2=$(count_open_alerts) || exit 1
85125
[ "$ALERTS" -lt "$ALERTS2" ] && ALERTS=$ALERTS2
86126
if [ "$ALERTS" -gt 0 ]; then
87127
echo "BLOCKED: $ALERTS open alert(s)"

‎.github/workflows/pr.yml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ concurrency:
2222
jobs:
2323
security:
2424
uses: ./.github/workflows/_security.yml
25+
# The called job requests security-events:read to count code-scanning
26+
# alerts; a reusable workflow cannot request more than its caller grants,
27+
# so withholding this here fails the whole run at startup.
28+
permissions:
29+
contents: read
30+
security-events: read
31+
actions: read
2532
secrets: inherit
2633

2734
lint:

‎.github/workflows/release.yml‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ jobs:
4444
# ── Security (starts immediately, blocks only verify) ───────────
4545
security:
4646
uses: ./.github/workflows/_security.yml
47+
# The called job requests security-events:read to count code-scanning
48+
# alerts; a reusable workflow cannot request more than its caller grants,
49+
# so withholding this here fails the whole run at startup.
50+
permissions:
51+
contents: read
52+
security-events: read
53+
actions: read
4754
secrets: inherit
4855

4956
# ── 0. Preflight: refuse malformed dispatch inputs ──────────────

‎graph-ui/package-lock.json‎

Lines changed: 48 additions & 48 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎graph-ui/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
"tailwindcss": "^4.1.0",
3838
"typescript": "^5.7.0",
3939
"vite": "^6.4.3",
40-
"vitest": "^4.1.0"
40+
"vitest": "^4.1.11"
4141
},
4242
"overrides": {
4343
"form-data": ">=4.0.6",

‎scripts/test.sh‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,9 @@ bash "$ROOT/tests/test_windows_bundle_contract.sh"
218218
echo "=== Step 0e2: activation refusal diagnostic contract ==="
219219
bash "$ROOT/tests/test_activation_diagnostic_contract.sh"
220220

221+
echo "=== Step 0e3: security gate fail-closed contract ==="
222+
bash "$ROOT/tests/test_security_gate_fail_closed.sh"
223+
221224
echo "=== Step 0f: tree-sitter runtime Makefile dependencies ==="
222225
bash "$ROOT/tests/test_makefile_ts_runtime_dependencies.sh"
223226

0 commit comments

Comments
 (0)