Repository navigation
[ci] Run CodeQL security analysis on every PR #347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
wangtiance
merged 2 commits into
ThinkFlowLab:main
from
wangtiance:codex/ci-pr-security-scan
Nov 16, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| name: CodeQL | ||
|
|
||
| on: | ||
| pull_request: | ||
| push: | ||
| branches: [main] | ||
| workflow_dispatch: | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| concurrency: | ||
| group: ${{ github.workflow }}-${{ github.event_name }}-${{ github.event.pull_request.number || github.run_id }} | ||
| cancel-in-progress: ${{ github.event_name == 'pull_request' }} | ||
|
|
||
| jobs: | ||
| analyze: | ||
| name: Analyze (${{ matrix.language }}) | ||
| runs-on: ubuntu-22.04 | ||
| timeout-minutes: 30 | ||
| permissions: | ||
| contents: read | ||
| actions: read | ||
| security-events: write | ||
| strategy: | ||
| fail-fast: false | ||
| matrix: | ||
| language: [python, javascript-typescript] | ||
| steps: | ||
| - uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0 | ||
| with: | ||
| persist-credentials: false | ||
| - name: Initialize CodeQL / 初始化安全扫描 | ||
| uses: github/codeql-action/init@33d7b85da1a04171e211befe2c739303b9a4c066 # v4.39.3 | ||
| with: | ||
| languages: ${{ matrix.language }} | ||
| build-mode: none | ||
| queries: security-extended | ||
| - name: Analyze and upload results / 扫描并上传结果 | ||
| uses: github/codeql-action/analyze@33d7b85da1a04171e211befe2c739303b9a4c066 # v4.39.3 | ||
| with: | ||
| category: /language:${{ matrix.language }} | ||
| wait-for-processing: true | ||
|
|
||
| # Stable required-check candidate, independent of docs-only release skips. | ||
| checks: | ||
| name: CodeQL / checks | ||
| if: always() | ||
| needs: analyze | ||
| runs-on: ubuntu-22.04 | ||
| timeout-minutes: 5 | ||
| steps: | ||
| - name: Require all analyses / 要求全部语言扫描成功 | ||
| env: | ||
| ANALYSIS_RESULT: ${{ needs.analyze.result }} | ||
| run: >- | ||
| python -c 'import os, sys; result = os.environ.get("ANALYSIS_RESULT", "missing"); | ||
| print("CodeQL analysis / 安全扫描: " + result); sys.exit(0 if result == "success" else 1)' | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| """Keep every PR scanned, and execute the stable security aggregate command.""" | ||
| import os | ||
| from pathlib import Path | ||
| import re | ||
| import shlex | ||
| import subprocess | ||
| import sys | ||
|
|
||
| import pytest | ||
| import yaml | ||
|
|
||
|
|
||
| pytestmark = pytest.mark.ready | ||
| ROOT = Path(__file__).resolve().parents[1] | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def workflow(): | ||
| return yaml.safe_load((ROOT / '.github/workflows/codeql.yml').read_text(encoding='utf-8')) | ||
|
|
||
|
|
||
| def test_every_pr_including_docs_and_forks_has_security_checks(workflow): | ||
| # PyYAML's YAML 1.1 loader parses GitHub's `on` key as boolean True. | ||
| events = workflow[True] | ||
| assert events['pull_request'] is None | ||
| assert events['push'] == {'branches': ['main']} | ||
| assert 'workflow_dispatch' in events | ||
| assert 'pull_request_target' not in events | ||
| assert workflow['name'] == 'CodeQL' | ||
| assert workflow['permissions'] == {'contents': 'read'} | ||
| analyze = workflow['jobs']['analyze'] | ||
| assert 'if' not in analyze and 'needs' not in analyze | ||
| assert {'python', 'javascript-typescript'} <= set(analyze['strategy']['matrix']['language']) | ||
| assert analyze['strategy']['fail-fast'] is False | ||
| assert analyze['timeout-minutes'] == 30 | ||
|
|
||
|
|
||
| def test_source_analysis_uses_pinned_actions_and_limited_permissions(workflow): | ||
| job = workflow['jobs']['analyze'] | ||
| assert job['permissions'] == {'contents': 'read', 'actions': 'read', 'security-events': 'write'} | ||
| steps = job['steps'] | ||
| assert len(steps) == 3 | ||
| assert steps[0]['with']['persist-credentials'] is False | ||
| assert all(re.fullmatch(r'[^@]+@[0-9a-f]{40}', step['uses']) for step in steps) | ||
| initialize, analyze = steps[1:] | ||
| assert initialize['uses'].startswith('github/codeql-action/init@') | ||
| assert analyze['uses'].startswith('github/codeql-action/analyze@') | ||
| assert initialize['uses'].split('@')[1] == analyze['uses'].split('@')[1] | ||
| assert initialize['with'] == { | ||
| 'languages': '${{ matrix.language }}', 'build-mode': 'none', 'queries': 'security-extended', | ||
| } | ||
| assert analyze['with'] == {'category': '/language:${{ matrix.language }}', 'wait-for-processing': True} | ||
| assert not any('run' in step or 'continue-on-error' in step or 'if' in step for step in steps) | ||
| assert 'secrets.' not in str(job) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize('status', ['success', 'failure', 'cancelled', 'skipped', 'timed_out', '', 'unknown', None]) | ||
| def test_actual_aggregate_command_rejects_every_unsettled_result(workflow, status): | ||
| job = workflow['jobs']['checks'] | ||
| assert job['name'] == 'CodeQL / checks' | ||
| assert job['if'] == 'always()' and job['needs'] == 'analyze' | ||
| assert 'continue-on-error' not in job | ||
| step = job['steps'][0] | ||
| assert step['env'] == {'ANALYSIS_RESULT': '${{ needs.analyze.result }}'} | ||
| assert 'if' not in step and 'continue-on-error' not in step | ||
| args = shlex.split(step['run']) | ||
| assert args[:2] == ['python', '-c'] | ||
| env = {**os.environ, 'PYTHONIOENCODING': 'utf-8'} | ||
| env.pop('ANALYSIS_RESULT', None) | ||
| if status is not None: | ||
| env['ANALYSIS_RESULT'] = status | ||
| result = subprocess.run([sys.executable, *args[1:]], env=env, | ||
| capture_output=True, text=True, encoding='utf-8', timeout=10) | ||
| assert (result.returncode == 0) == (status == 'success'), result.stderr | ||
| assert 'CodeQL analysis' in result.stdout |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When PRs increase and pushes are frequent, old commits keep occupying runners, causing new checks to queue up. Configuring concurrency and cancel-in-progress: true for the same PR can cancel outdated runs and free up runners. It is also recommended to cover CodeQL, CI, and DSH plugin checks at the same time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in 629e03a