|
41 | 41 | codeql-gate: |
42 | 42 | runs-on: ubuntu-latest |
43 | 43 | 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 |
44 | 52 | steps: |
45 | 53 | - name: Wait for CodeQL on current commit (max 150 min) |
46 | 54 | env: |
@@ -77,11 +85,43 @@ jobs: |
77 | 85 | env: |
78 | 86 | GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
79 | 87 | 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 | + } |
80 | 120 | echo "Waiting 60s for alert API to settle..." |
81 | 121 | 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 |
83 | 123 | 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 |
85 | 125 | [ "$ALERTS" -lt "$ALERTS2" ] && ALERTS=$ALERTS2 |
86 | 126 | if [ "$ALERTS" -gt 0 ]; then |
87 | 127 | echo "BLOCKED: $ALERTS open alert(s)" |
|
0 commit comments