Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

CI: CodeQL advanced setup that analyses only the languages a PR touches (#11998)

Daniel Hanfdbc8676a372
diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml
new file mode 100644
index 00000000000..8020a266801
--- /dev/null
+++ b/.github/workflows/codeql.yml
@@ -0,0 +1,113 @@
+# CodeQL code scanning (advanced setup; replaces the repository's default setup).
+#
+# Coverage matches default setup: the same four languages, the default query suite and the
+# remote threat model. Every push to main and the weekly schedule analyse all four, which keeps
+# main's baseline current for each language. GitHub warns against path filters on push for
+# CodeQL, since each analysis is compared with the previous commit's, so push has none.
+#
+# What changes is pull requests: a PR analyses only the languages whose files it touches. A
+# language the PR does not touch cannot gain or lose an alert, and its analysis on main stays
+# the reference. Anything the changes job cannot classify (the file list fails to load, more
+# than 3000 files, or this workflow itself changes) analyses all four.
+name: CodeQL
+
+on:
+ push:
+ branches: [main]
+ pull_request:
+ branches: [main]
+ schedule:
+ - cron: "17 4 * * 1"
+ workflow_dispatch:
+
+# One group per commit on main (a queued run in a shared group is cancelled by the next push, so a
+# merge burst would leave commits unanalysed); per branch / PR elsewhere, where a newer push
+# supersedes the older run.
+concurrency:
+ group: ${{ github.workflow }}-${{ github.ref }}-${{ github.ref == 'refs/heads/main' && github.sha || '' }}
+ cancel-in-progress: ${{ github.event_name == 'pull_request' }}
+
+permissions:
+ contents: read
+
+jobs:
+ changes:
+ name: Languages to analyse
+ runs-on: ubuntu-latest
+ timeout-minutes: 5
+ permissions:
+ contents: read
+ pull-requests: read
+ outputs:
+ languages: ${{ steps.pick.outputs.languages }}
+ steps:
+ - name: Pick languages
+ id: pick
+ env:
+ GH_TOKEN: ${{ github.token }}
+ EVENT: ${{ github.event_name }}
+ REPO: ${{ github.repository }}
+ PR: ${{ github.event.pull_request.number }}
+ run: |
+ set -uo pipefail
+ all='["actions","javascript-typescript","python","rust"]'
+ if [ "$EVENT" != "pull_request" ]; then
+ echo "languages=$all" >> "$GITHUB_OUTPUT"
+ echo "$EVENT: analysing all languages"
+ exit 0
+ fi
+ # The files API returns at most 3000 files; at that size, or on any error, analyse all.
+ if ! files=$(gh api --paginate "repos/$REPO/pulls/$PR/files?per_page=100" --jq '.[] | .filename, (.previous_filename // empty)'); then
+ echo "languages=$all" >> "$GITHUB_OUTPUT"
+ echo "could not list the PR's files: analysing all languages"
+ exit 0
+ fi
+ # grep reads a here-string, not a pipe: with pipefail, grep -q closing a pipe early
+ # makes the writer's SIGPIPE turn a match into a miss on long file lists.
+ count=$(grep -c . <<< "$files" || true)
+ if [ "$count" -ge 3000 ] || grep -qxE '\.github/workflows/codeql\.ya?ml|\.github/codeql/.*' <<< "$files"; then
+ echo "languages=$all" >> "$GITHUB_OUTPUT"
+ echo "$count files or a CodeQL config change: analysing all languages"
+ exit 0
+ fi
+ langs=()
+ has() { grep -qiE "$1" <<< "$files"; }
+ has '^\.github/(workflows|actions)/|(^|/)action\.ya?ml$' && langs+=(actions)
+ has '\.(js|jsx|mjs|cjs|ts|tsx|mts|cts|vue|html?|svelte)$|(^|/)(package(-lock)?\.json|tsconfig[^/]*\.json|jsconfig\.json|pnpm-lock\.yaml|yarn\.lock|bun\.lockb?)$' && langs+=(javascript-typescript)
+ has '\.pyi?$' && langs+=(python)
+ has '\.rs$|(^|/)Cargo\.(toml|lock)$' && langs+=(rust)
+ if [ "${#langs[@]}" -eq 0 ]; then
+ json='[]'
+ else
+ json=$(printf '"%s",' "${langs[@]}")
+ json="[${json%,}]"
+ fi
+ echo "languages=$json" >> "$GITHUB_OUTPUT"
+ echo "$count changed files: analysing $json"
+
+ analyze:
+ name: Analyze (${{ matrix.language }})
+ needs: changes
+ if: needs.changes.outputs.languages != '[]'
+ runs-on: ubuntu-latest
+ timeout-minutes: 60
+ permissions:
+ actions: read
+ contents: read
+ packages: read
+ security-events: write
+ strategy:
+ fail-fast: false
+ matrix:
+ language: ${{ fromJSON(needs.changes.outputs.languages) }}
+ steps:
+ - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
+ with:
+ persist-credentials: false
+ - uses: github/codeql-action/init@2892aa5e19bbd11bc0cff5427e3b750a04d9e3c2 # v4.38.2
+ with:
+ languages: ${{ matrix.language }}
+ build-mode: none
+ - uses: github/codeql-action/analyze@2892aa5e19bbd11bc0cff5427e3b750a04d9e3c2 # v4.38.2
+ with:
+ category: "/language:${{ matrix.language }}"
diff --git a/tests/security/test_codeql_language_picker.py b/tests/security/test_codeql_language_picker.py
new file mode 100644
index 00000000000..94aa96be3b2
--- /dev/null
+++ b/tests/security/test_codeql_language_picker.py
@@ -0,0 +1,110 @@
+# SPDX-License-Identifier: AGPL-3.0-only
+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0
+"""The CodeQL workflow's language picker, run for real with a fake ``gh`` on PATH.
+
+A pull request analyses only the languages its files touch, so a picker that misses a
+language silently drops CodeQL coverage for that change. Every push, schedule and dispatch
+must analyse all four.
+"""
+
+from __future__ import annotations
+
+import json
+import os
+import shutil
+import subprocess
+import sys
+from pathlib import Path
+
+import pytest
+import yaml
+
+ROOT = Path(__file__).resolve().parents[2]
+WORKFLOW = ROOT / ".github" / "workflows" / "codeql.yml"
+ALL = ["actions", "javascript-typescript", "python", "rust"]
+
+pytestmark = pytest.mark.skipif(
+ sys.platform == "win32" or shutil.which("bash") is None, reason = "needs bash"
+)
+
+
+def _script() -> str:
+ doc = yaml.safe_load(WORKFLOW.read_text(encoding = "utf-8"))
+ return doc["jobs"]["changes"]["steps"][0]["run"]
+
+
+def _pick(
+ tmp_path: Path,
+ files: list[str] | None,
+ event: str = "pull_request",
+) -> list[str]:
+ bin_dir = tmp_path / "bin"
+ bin_dir.mkdir(exist_ok = True)
+ listing = tmp_path / "files.txt"
+ gh = bin_dir / "gh"
+ if files is None:
+ gh.write_text("#!/bin/bash\nexit 1\n")
+ else:
+ listing.write_text("".join(f + "\n" for f in files))
+ gh.write_text(f"#!/bin/bash\ncat '{listing}'\n")
+ gh.chmod(0o755)
+ out = tmp_path / "out.txt"
+ out.write_text("")
+ env = dict(
+ os.environ,
+ PATH = f"{bin_dir}{os.pathsep}{os.environ['PATH']}",
+ GITHUB_OUTPUT = str(out),
+ EVENT = event,
+ REPO = "unslothai/unsloth",
+ PR = "1",
+ )
+ subprocess.run(["bash", "-c", _script()], env = env, check = True, capture_output = True)
+ return json.loads(out.read_text().strip().split("=", 1)[1])
+
+
[email protected]("event", ["push", "schedule", "workflow_dispatch"])
+def test_non_pull_request_events_analyse_everything(tmp_path, event):
+ assert _pick(tmp_path, ["README.md"], event) == ALL
+
+
+ "files, expected",
+ [
+ (["unsloth/models/llama.py"], ["python"]),
+ (["studio/backend/stubs.pyi"], ["python"]),
+ (["studio/frontend/src/app.tsx"], ["javascript-typescript"]),
+ (["studio/frontend/package.json"], ["javascript-typescript"]),
+ (["studio/frontend/tsconfig.app.json"], ["javascript-typescript"]),
+ (["studio/frontend/index.html"], ["javascript-typescript"]),
+ (["studio/src-tauri/src/main.rs"], ["rust"]),
+ (["studio/src-tauri/Cargo.lock"], ["rust"]),
+ ([".github/workflows/lint-ci.yml"], ["actions"]),
+ ([".github/actions/setup/action.yml"], ["actions"]),
+ (["README.md", "docs/guide.md", "studio/frontend/public/logo.png"], []),
+ (["a.py", "b.ts", "c.rs", ".github/workflows/x.yml"], ALL),
+ # A rename lists both names; the old one alone still counts.
+ (["docs/moved.txt", "unsloth/old_name.py"], ["python"]),
+ ],
+)
+def test_pull_requests_analyse_the_languages_they_touch(tmp_path, files, expected):
+ assert _pick(tmp_path, files) == expected
+
+
+def test_a_codeql_config_change_analyses_everything(tmp_path):
+ assert _pick(tmp_path, [".github/workflows/codeql.yml"]) == ALL
+ assert _pick(tmp_path, [".github/codeql/codeql-config.yml"]) == ALL
+
+
+def test_an_unreadable_file_list_analyses_everything(tmp_path):
+ assert _pick(tmp_path, None) == ALL
+
+
+def test_the_api_file_limit_analyses_everything(tmp_path):
+ assert _pick(tmp_path, [f"docs/{i}.md" for i in range(3000)]) == ALL
+
+
+def test_an_early_match_in_a_long_list_is_not_lost(tmp_path):
+ """grep -q stops reading at its first match; fed from a pipe under pipefail, the
+ writer's SIGPIPE used to turn that match into a miss."""
+ files = ["first.py"] + [f"docs/{'x' * 200}/{i}.md" for i in range(2998)]
+ assert _pick(tmp_path, files) == ["python"]