|
1 | | -# Reusable: security-static + CodeQL gate. |
| 1 | +# Reusable: security-static + license gate. |
| 2 | +# CodeQL is not here: PRs do not run it. It runs on push to main, weekly, and |
| 3 | +# inside dry-run.yml / release.yml, which call codeql.yml directly. |
2 | 4 | # Runs independently from lint/test/build — does not block the main pipeline. |
3 | 5 | # Affects overall workflow success status. |
4 | 6 | name: Security Gate |
|
52 | 54 | GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
53 | 55 | run: scripts/audit-license-provenance.py |
54 | 56 |
|
55 | | - codeql-gate: |
56 | | - if: ${{ inputs.lanes == 'all' || contains(fromJSON(inputs.lanes), 'codeql-gate') }} |
57 | | - runs-on: ubuntu-latest |
58 | | - timeout-minutes: 240 |
59 | | - # Without security-events:read this job inherits the workflow's |
60 | | - # contents:read, the code-scanning alert API answers 403, and the gate |
61 | | - # below reported "0 alerts" on every run it has ever made. actions:read is |
62 | | - # what the CodeQL-run poll above needs for the same reason. |
63 | | - permissions: |
64 | | - contents: read |
65 | | - security-events: read |
66 | | - actions: read |
67 | | - steps: |
68 | | - - name: Wait for CodeQL on current commit (max 150 min) |
69 | | - env: |
70 | | - GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
71 | | - run: | |
72 | | - # On pull_request events github.sha is the synthetic merge commit; |
73 | | - # CodeQL runs are recorded against the PR head SHA. |
74 | | - CURRENT_SHA="${{ github.event.pull_request.head.sha || github.sha }}" |
75 | | - echo "Waiting for CodeQL to complete on $CURRENT_SHA..." |
76 | | - # 300 attempts x 30s = 150 min. The previous budget was 90 x 30s = 45 min, |
77 | | - # which is shorter than CodeQL actually takes on this repository: a measured |
78 | | - # run on PR #1426 (head 7b72652a) completed with conclusion=success after |
79 | | - # 124 min, 7 minutes AFTER this gate had already given up. That marked at |
80 | | - # least five contributor PRs red for a scan that passed. The job's own |
81 | | - # timeout-minutes is 240, so 150 still leaves headroom. |
82 | | - for attempt in $(seq 1 300); do |
83 | | - LATEST=$(gh api "repos/${{ github.repository }}/actions/workflows/codeql.yml/runs?head_sha=$CURRENT_SHA&per_page=1" \ |
84 | | - --jq '.workflow_runs[] | "\(.conclusion) \(.status)"' 2>/dev/null | head -1 || echo "") |
85 | | - if [ -z "$LATEST" ]; then |
86 | | - echo " $attempt/300: no run yet..."; sleep 30; continue |
87 | | - fi |
88 | | - CONCLUSION=$(echo "$LATEST" | cut -d' ' -f1) |
89 | | - STATUS=$(echo "$LATEST" | cut -d' ' -f2) |
90 | | - if [ "$STATUS" = "completed" ] && [ "$CONCLUSION" = "success" ]; then |
91 | | - echo "=== CodeQL passed ==="; exit 0 |
92 | | - elif [ "$STATUS" = "completed" ]; then |
93 | | - echo "BLOCKED: CodeQL $CONCLUSION"; exit 1 |
94 | | - fi |
95 | | - echo " $attempt/300: $STATUS..."; sleep 30 |
96 | | - done |
97 | | - echo "BLOCKED: CodeQL timeout"; exit 1 |
98 | | -
|
99 | | - - name: Check for open code scanning alerts |
100 | | - env: |
101 | | - GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
102 | | - run: | |
103 | | - # FAIL CLOSED. The previous form was `gh api ... 2>/dev/null || echo "0"`, |
104 | | - # which looks defensive and is the opposite. gh writes the API error |
105 | | - # BODY to stdout, so a 403 made ALERTS the string |
106 | | - # {"message":"Resource not accessible by integration",...,"status":"403"}0 |
107 | | - # every `[` comparison then failed as non-integer, the `if` took its |
108 | | - # false branch, and the step printed "CodeQL gate passed (0 alerts)" |
109 | | - # and exited 0. That is exactly how a security gate should NEVER fail: |
110 | | - # silently, in the direction of "everything is fine". An unreadable |
111 | | - # alert list is now an error, not a zero. |
112 | | - # NOTE: this runs in a command substitution, so it must `return`, not |
113 | | - # `exit` -- an `exit` here would only end the subshell and let the |
114 | | - # caller continue with an empty count, recreating the very bug this |
115 | | - # replaces. The callers below check the status explicitly. |
116 | | - count_open_alerts() { |
117 | | - local body status |
118 | | - body=$(gh api 'repos/${{ github.repository }}/code-scanning/alerts?state=open' \ |
119 | | - --jq 'length' 2>/dev/null) |
120 | | - status=$? |
121 | | - if [ $status -ne 0 ]; then |
122 | | - echo "BLOCKED: cannot read code scanning alerts (gh exit $status)." >&2 |
123 | | - echo " Refusing to treat an unreadable alert list as zero." >&2 |
124 | | - echo " Most likely cause: the job lacks security-events:read." >&2 |
125 | | - return 1 |
126 | | - fi |
127 | | - case "$body" in |
128 | | - ''|*[!0-9]*) |
129 | | - echo "BLOCKED: alert count was not a number: ${body:-<empty>}" >&2 |
130 | | - return 1 |
131 | | - ;; |
132 | | - esac |
133 | | - printf '%s' "$body" |
134 | | - } |
135 | | - echo "Waiting 60s for alert API to settle..." |
136 | | - sleep 60 |
137 | | - ALERTS=$(count_open_alerts) || exit 1 |
138 | | - sleep 15 |
139 | | - ALERTS2=$(count_open_alerts) || exit 1 |
140 | | - [ "$ALERTS" -lt "$ALERTS2" ] && ALERTS=$ALERTS2 |
141 | | - if [ "$ALERTS" -gt 0 ]; then |
142 | | - echo "BLOCKED: $ALERTS open alert(s)" |
143 | | - gh api 'repos/${{ github.repository }}/code-scanning/alerts?state=open' \ |
144 | | - --jq '.[] | " #\(.number) [\(.rule.security_severity_level // .rule.severity)] \(.rule.id) — \(.most_recent_instance.location.path):\(.most_recent_instance.location.start_line)"' 2>/dev/null || true |
145 | | - exit 1 |
146 | | - fi |
147 | | - echo "=== CodeQL gate passed (0 alerts) ===" |
0 commit comments