Skip to content

Commit c1c8317

Browse files
ngallodevclaude
andcommitted
fix(ci): scope the CodeQL gate to this repo's CodeQL findings
The codeql-gate job failed every run on 32 open alerts. Only one was a CodeQL finding in this repository's own code; the rest were CodeQL findings in vendored third-party grammars and OpenSSF Scorecard repository-hygiene checks. Gate (.github/workflows/_security.yml): - Count only CodeQL alerts outside vendored/ and internal/cbm/vendored/. Scorecard keeps reporting in its own workflow; vendored grammar code is upstream code this repo does not patch. - On pull requests, query the PR's own analysis (ref=refs/pull/N/merge). Without a ref the API answers for the default branch, so a fix inside a PR could never clear the gate before merging. - Paginate (per_page=100, --paginate). The unpaginated call returned at most 30 alerts, so the count silently capped at 30. - Fail-closed behavior is unchanged: an unreadable alert list or a non-numeric count still blocks. tests/test_security_gate_fail_closed.sh passes. Findings fixed: - src/simhash/minhash.c (CodeQL cpp/overflowing-snprintf, alert #13): cbm_minhash_to_hex advanced its offset by snprintf's return value without checking it. Stop and terminate on a negative or truncated write. - .github/workflows/claude.yml, claude-code-review.yml (Scorecard TokenPermissions DeusData#36, DeusData#37): add a deny-all top-level `permissions: {}`; each job already grants what it needs. Diagnostics: - .github/workflows/_test.yml: upload build/c/test-logs/*.log from a failed Windows test shard. The harness prints only the last 15 lines of each failing suite, which cut off the ASan report for index_resilience on Windows. Validation: security-audit.sh and the gate fail-closed contract pass; make lint-ci lint-tidy-diff passes; scripts/test.sh --suites simhash 25 passed. The new filter evaluated against the live alert API counts only alert #13 on the default branch and 0 for refs/pull/12/merge. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent b732b86 commit c1c8317

5 files changed

Lines changed: 50 additions & 9 deletions

File tree

‎.github/workflows/_security.yml‎

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ jobs:
8686
- name: Check for open code scanning alerts
8787
env:
8888
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
89+
# A PR is judged by its own CodeQL analysis (refs/pull/N/merge);
90+
# without a ref the API answers for the default branch, so a fix in
91+
# the PR could never clear the gate before it merged.
92+
ALERT_REF: ${{ github.event_name == 'pull_request' && format('refs/pull/{0}/merge', github.event.pull_request.number) || '' }}
8993
run: |
9094
# FAIL CLOSED. The previous form was `gh api ... 2>/dev/null || echo "0"`,
9195
# which looks defensive and is the opposite. gh writes the API error
@@ -100,24 +104,38 @@ jobs:
100104
# `exit` -- an `exit` here would only end the subshell and let the
101105
# caller continue with an empty count, recreating the very bug this
102106
# replaces. The callers below check the status explicitly.
107+
# Scope: CodeQL alerts only, outside vendored third-party code.
108+
# Scorecard findings are repository-hygiene signals reported by their
109+
# own workflow, and alerts in vendored grammars/libraries are
110+
# upstream code this repo does not patch, so neither blocks a merge.
111+
# --paginate: the API returns 30 alerts per page by default, so an
112+
# unpaginated count silently stopped at 30.
113+
ALERTS_URL='repos/${{ github.repository }}/code-scanning/alerts?state=open&per_page=100'
114+
[ -n "$ALERT_REF" ] && ALERTS_URL="$ALERTS_URL&ref=$ALERT_REF"
115+
GATE_FILTER='.[] | select(.tool.name == "CodeQL")
116+
| select((.most_recent_instance.location.path // "")
117+
| test("^(internal/cbm/)?vendored/") | not)'
103118
count_open_alerts() {
104119
local body status
105-
body=$(gh api 'repos/${{ github.repository }}/code-scanning/alerts?state=open' \
106-
--jq 'length' 2>/dev/null)
120+
body=$(gh api --paginate \
121+
"$ALERTS_URL" \
122+
--jq "$GATE_FILTER | .number" 2>/dev/null)
107123
status=$?
108124
if [ $status -ne 0 ]; then
109125
echo "BLOCKED: cannot read code scanning alerts (gh exit $status)." >&2
110126
echo " Refusing to treat an unreadable alert list as zero." >&2
111127
echo " Most likely cause: the job lacks security-events:read." >&2
112128
return 1
113129
fi
114-
case "$body" in
130+
local count
131+
count=$(printf '%s' "$body" | grep -c . || true)
132+
case "$count" in
115133
''|*[!0-9]*)
116-
echo "BLOCKED: alert count was not a number: ${body:-<empty>}" >&2
134+
echo "BLOCKED: alert count was not a number: ${count:-<empty>}" >&2
117135
return 1
118136
;;
119137
esac
120-
printf '%s' "$body"
138+
printf '%s' "$count"
121139
}
122140
echo "Waiting 60s for alert API to settle..."
123141
sleep 60
@@ -127,8 +145,9 @@ jobs:
127145
[ "$ALERTS" -lt "$ALERTS2" ] && ALERTS=$ALERTS2
128146
if [ "$ALERTS" -gt 0 ]; then
129147
echo "BLOCKED: $ALERTS open alert(s)"
130-
gh api 'repos/${{ github.repository }}/code-scanning/alerts?state=open' \
131-
--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
148+
gh api --paginate \
149+
"$ALERTS_URL" \
150+
--jq "$GATE_FILTER"' | " #\(.number) [\(.rule.security_severity_level // .rule.severity)] \(.rule.id) — \(.most_recent_instance.location.path):\(.most_recent_instance.location.start_line)"' 2>/dev/null || true
132151
exit 1
133152
fi
134-
echo "=== CodeQL gate passed (0 alerts) ==="
153+
echo "=== CodeQL gate passed (0 gating CodeQL alerts) ==="

‎.github/workflows/_test.yml‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,17 @@ jobs:
484484
env:
485485
CCACHE_DIR: ${{ github.workspace }}/.ccache
486486

487+
# The harness prints only each failing suite's last 15 lines, which cuts
488+
# off sanitizer reports. Keep the full per-suite logs of a failed shard.
489+
- name: Upload test logs (on failure)
490+
if: failure()
491+
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
492+
with:
493+
name: test-logs-${{ matrix.os }}-${{ matrix.msystem }}-${{ strategy.job-index }}
494+
path: build/c/test-logs/*.log
495+
retention-days: 7
496+
if-no-files-found: ignore
497+
487498
- name: Shard manifest (completeness proof)
488499
if: always()
489500
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1

‎.github/workflows/claude-code-review.yml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ on:
1010
# - "src/**/*.js"
1111
# - "src/**/*.jsx"
1212

13+
# Deny by default; each job grants only what it needs.
14+
permissions: {}
15+
1316
jobs:
1417
claude-review:
1518
# Optional: Filter by PR author

‎.github/workflows/claude.yml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ on:
1010
pull_request_review:
1111
types: [submitted]
1212

13+
# Deny by default; each job grants only what it needs.
14+
permissions: {}
15+
1316
jobs:
1417
claude:
1518
if: |

‎src/simhash/minhash.c‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,12 @@ void cbm_minhash_to_hex(const cbm_minhash_t *fp, char *buf, int bufsize) {
290290
}
291291
int pos = 0;
292292
for (int i = 0; i < CBM_MINHASH_K; i++) {
293-
pos += snprintf(buf + pos, (size_t)(bufsize - pos), "%08x", fp->values[i]);
293+
int written = snprintf(buf + pos, (size_t)(bufsize - pos), "%08x", fp->values[i]);
294+
if (written < 0 || written >= bufsize - pos) {
295+
buf[pos] = '\0';
296+
return;
297+
}
298+
pos += written;
294299
}
295300
}
296301

0 commit comments

Comments
 (0)