""" Run a SkillShield scan in CI and report the result. This is what the SkillShield GitHub Action runs. It can also be used from any CI system with ``python -m skillshield.ci``. Settings come from environment variables, so CI inputs never have to be pasted into a shell command: SKILLSHIELD_PATHS Files or directories to scan, one per line (default ".") SKILLSHIELD_FAIL_ON Fail at this risk level or above: critical, high, medium, low or none (default "high") SKILLSHIELD_MIN_SEVERITY Ignore findings below this severity (default "info") SKILLSHIELD_SARIF_FILE Write a SARIF 2.1.0 file here (optional) SKILLSHIELD_WORKSPACE Report paths relative to this directory (default: GITHUB_WORKSPACE, or the current directory) When running in GitHub Actions it also writes a job summary (GITHUB_STEP_SUMMARY) and step outputs (GITHUB_OUTPUT). Exit codes: 0 pass, 1 a skill is at or above the fail-on level, 2 a path does not exist, a file could not be read, or a setting is invalid. """ from __future__ import annotations import json import os import sys from pathlib import Path from skillshield.models import BatchScanReport, SkillScanResult, missing_source_note from skillshield.rules import Severity from skillshield.sarif import report_to_sarif from skillshield.scanner import ( COMPILED_PYTHON_EXTENSIONS, SKILL_EXTENSIONS, SkillScanner, _dir_is_skipped, ) LEVELS = ["SAFE", "LOW", "MEDIUM", "HIGH", "CRITICAL"] FAIL_ON = {"none": None, "low": "LOW", "medium": "MEDIUM", "high": "HIGH", "critical": "CRITICAL"} SEVERITIES = {s.value.lower(): s for s in Severity} # The job summary is written to GITHUB_STEP_SUMMARY, which GitHub Actions caps # at 1 MiB per step; an oversized summary fails to upload. A scan of a large # repository can skip or flag thousands of files, so every list in the summary # shows a bounded sample and keeps the total count. MAX_UNSCANNED_LISTED = 20 MAX_FLAGGED_LISTED = 50 MAX_ERRORS_LISTED = 50 class SettingError(ValueError): """A setting has a value SkillShield does not accept.""" def _rank(level: str) -> int: return LEVELS.index(level) if level in LEVELS else -1 def _relative(path: Path, workspace: Path) -> str: try: return path.resolve().relative_to(workspace.resolve()).as_posix() except ValueError: return path.as_posix() def _normalised(path: Path) -> Path: """A stable identity for a path, without changing the path used to read it.""" try: return path.resolve() except (OSError, RuntimeError): # A broken link or a symlink loop is reported as a missing path. return path def _is_scanned(path: Path, root: Path) -> bool: """Whether a file under root belongs in a CI scan. Compiled Python is collected wherever it sits: bytecode is what a reader skips, and a directory name or a submitted pyvenv.cfg must not hide it. """ suffix = path.suffix.lower() if suffix in COMPILED_PYTHON_EXTENSIONS: return True current = root for part in path.relative_to(root).parts[:-1]: current = current / part if _dir_is_skipped(current): return False return suffix in SKILL_EXTENSIONS def _scan_paths( paths: list[str], workspace: Path, min_severity: Severity = Severity.INFO ) -> tuple[list[SkillScanResult], list[str], list[str]]: """Scan each path once and collect the files the directory policy skipped.""" scanner = SkillScanner(min_severity=min_severity) results: list[SkillScanResult] = [] missing: list[str] = [] skipped: list[Path] = [] scanned: set[Path] = set() for raw in paths: p = Path(raw) if not p.is_absolute(): p = workspace / p # Keep p for reading: its suffix decides how the file is scanned. Only # key uses the resolved path, so aliases still count as one file. key = _normalised(p) if key.is_dir(): all_files = sorted(f for f in key.rglob("*") if f.is_file()) keys = {f: _normalised(f) for f in all_files} files = [f for f in all_files if keys[f] not in scanned and _is_scanned(f, key)] selected = {keys[f] for f in files} # A file an earlier input already scanned is not skipped: the # report must not call a file unscanned while its result is listed. skipped.extend( f for f in all_files if keys[f] not in selected and keys[f] not in scanned ) elif key.is_file(): files = [] if key in scanned else [p] else: missing.append(raw) continue for f in files: scanned.add(_normalised(f)) result = scanner.scan_file(f) # scan_file names a result by file name only; use the path relative # to the workspace so findings point at the right file. result.skill_name = _relative(f, workspace) results.append(result) return ( results, missing, list( dict.fromkeys(_relative(f, workspace) for f in skipped if _normalised(f) not in scanned) ), ) def scan_paths( paths: list[str], workspace: Path, min_severity: Severity = Severity.INFO ) -> tuple[list[SkillScanResult], list[str]]: """Scan each path. Returns the results and the paths that do not exist.""" results, missing, _ = _scan_paths(paths, workspace, min_severity) return results, missing def decide(results: list[SkillScanResult], missing: list[str], fail_on: str | None) -> int: """Return the exit code for these results.""" if missing or any(r.risk_level == "ERROR" for r in results): return 2 if fail_on is None: return 0 threshold = _rank(fail_on) return 1 if any(_rank(r.risk_level) >= threshold for r in results) else 0 def highest_level(results: list[SkillScanResult]) -> str: """The most severe risk level among the results that were read.""" read = [r for r in results if r.risk_level != "ERROR"] if not read: return "SAFE" return max((r.risk_level for r in read), key=_rank) def _in_tooling_dir(name: str, workspace: Path) -> bool: """Whether a skipped path sits in a tooling or dependency directory. The directories are checked on disk, not by name alone: a skill can ship its own `.venv/` to pass off payload files as an environment, and only a directory that carries `pyvenv.cfg` is a real one. This is the same rule the scanner applies to the files it reads. """ current = workspace for part in name.split("/")[:-1]: current = current / part if _dir_is_skipped(current): return True return False def _visible_unscanned(unscanned: list[str], workspace: Path) -> list[str]: """The skipped paths worth showing first, bounded for the job summary. Tooling and dependency directories come last, so a payload outside them still reaches the list on a repository whose .git alone could fill it. """ visible = [name for name in unscanned if not _in_tooling_dir(name, workspace)] hidden = [name for name in unscanned if _in_tooling_dir(name, workspace)] return (visible + hidden)[:MAX_UNSCANNED_LISTED] def summary_markdown( results: list[SkillScanResult], missing: list[str], fail_on: str | None, exit_code: int, unscanned: list[str] | None = None, workspace: Path | None = None, ) -> str: """A Markdown summary of the scan, for the GitHub job summary.""" verdict = {0: "Passed", 1: "Failed", 2: "Error"}[exit_code] threshold = fail_on or "never" flagged = [r for r in results if r.risk_level not in ("SAFE", "ERROR")] errors = [r for r in results if r.risk_level == "ERROR"] unscanned = unscanned or [] lines = [ f"## SkillShield scan: {verdict}", "", f"Scanned {len(results)} file(s). Fails at: {threshold.lower()}.", "", "| Result | Count |", "|---|---|", f"| Highest risk level | {highest_level(results)} |", f"| Flagged files | {len(flagged)} |", f"| Unreadable files | {len(errors)} |", f"| Missing paths | {len(missing)} |", f"| Unscanned files | {len(unscanned)} |", "", ] if flagged: lines += ["| File | Risk | Score | Findings |", "|---|---|---|---|"] ordered = sorted(flagged, key=lambda r: (-_rank(r.risk_level), r.skill_name)) for r in ordered[:MAX_FLAGGED_LISTED]: ids = ", ".join(f.rule_id for f in r.findings) lines.append(f"| `{r.skill_name}` | {r.risk_level} | {r.risk_score:.0f} | {ids} |") omitted_flagged = len(ordered) - MAX_FLAGGED_LISTED if omitted_flagged > 0: lines.append(f"| ... and {omitted_flagged} more, not listed here | | | |") lines.append("") for r in errors[:MAX_ERRORS_LISTED]: lines.append(f"- Could not read `{r.skill_name}`: {r.error}") omitted_errors = len(errors) - MAX_ERRORS_LISTED if omitted_errors > 0: lines.append(f"- ... and {omitted_errors} more, not listed here") for m in missing[:MAX_ERRORS_LISTED]: lines.append(f"- Path does not exist: `{m}`") omitted_missing = len(missing) - MAX_ERRORS_LISTED if omitted_missing > 0: lines.append(f"- ... and {omitted_missing} more, not listed here") if unscanned: shown = _visible_unscanned(unscanned, workspace or Path(".")) lines += ["Files not scanned:", ""] for name in shown: lines.append(f"- `{name}`") omitted = len(unscanned) - len(shown) if omitted > 0: lines.append(f"- ... and {omitted} more, not listed here") lines.append("") return "\n".join(lines).rstrip() + "\n" def console_report( results: list[SkillScanResult], missing: list[str], exit_code: int, unscanned: list[str] | None = None, sarif_file: str | None = None, ) -> str: """Plain text lines for the job log, with GitHub annotations for flagged files.""" lines = [] annotated = 0 unread = 0 # Annotate the worst files first, in the order the summary lists them, so a # bounded log never spends its slots on the mildest findings. ordered = sorted( results, key=lambda r: (r.risk_level != "ERROR", -_rank(r.risk_level), r.skill_name) ) for r in ordered: if r.risk_level == "ERROR": if unread >= MAX_ERRORS_LISTED: continue unread += 1 lines.append( f"::error file={r.skill_name}::SkillShield could not read this file: {r.error}" ) elif r.risk_level != "SAFE": if annotated >= MAX_FLAGGED_LISTED: continue annotated += 1 ids = ", ".join(f"{f.rule_id} {f.name}" for f in r.findings) note = missing_source_note(r) if note: ids = f"{ids}; {note}" level = r.risk_level kind = "error" if exit_code == 1 and level in ("HIGH", "CRITICAL") else "warning" line = r.findings[0].line_numbers[0] if r.findings and r.findings[0].line_numbers else 1 lines.append( f"::{kind} file={r.skill_name},line={line},title=SkillShield {level}::{ids}" ) flagged_total = sum(1 for r in results if r.risk_level not in ("SAFE", "ERROR")) if flagged_total > annotated: lines.append( f"::warning::SkillShield flagged {flagged_total} file(s); the {annotated} worst " f"are annotated here and in the job summary, the other " f"{flagged_total - annotated} are counted only." ) unread_total = sum(1 for r in results if r.risk_level == "ERROR") if unread_total > unread: lines.append( f"::warning::SkillShield could not read {unread_total} file(s); the first " f"{unread} are annotated here, the other {unread_total - unread} are counted only." ) for m in missing[:MAX_ERRORS_LISTED]: lines.append(f"::error::SkillShield: path does not exist: {m}") if len(missing) > MAX_ERRORS_LISTED: lines.append( f"::warning::SkillShield: {len(missing)} paths do not exist; the first " f"{MAX_ERRORS_LISTED} are annotated here, the other " f"{len(missing) - MAX_ERRORS_LISTED} are counted only." ) if unscanned: where = "the job summary and SARIF report" if sarif_file else "the job summary" lines.append( f"::warning::SkillShield skipped {len(unscanned)} file(s) it could not scan; " f"they are listed in {where}." ) lines.append( f"SkillShield scanned {len(results)} file(s); highest risk level {highest_level(results)}." ) return "\n".join(lines) def _write(path: str | None, text: str, mode: str = "w") -> None: if path: with open(path, mode, encoding="utf-8") as fh: fh.write(text) def read_settings(env: dict[str, str]) -> dict: """Read and check the settings from environment variables.""" paths = [p.strip() for p in env.get("SKILLSHIELD_PATHS", ".").splitlines() if p.strip()] fail_on_raw = env.get("SKILLSHIELD_FAIL_ON", "high").strip().lower() or "high" if fail_on_raw not in FAIL_ON: raise SettingError(f"fail-on must be one of {', '.join(FAIL_ON)}; got {fail_on_raw!r}") sev_raw = env.get("SKILLSHIELD_MIN_SEVERITY", "info").strip().lower() or "info" if sev_raw not in SEVERITIES: raise SettingError(f"min-severity must be one of {', '.join(SEVERITIES)}; got {sev_raw!r}") workspace = env.get("SKILLSHIELD_WORKSPACE") or env.get("GITHUB_WORKSPACE") or os.getcwd() return { "paths": paths or ["."], "fail_on": FAIL_ON[fail_on_raw], "min_severity": SEVERITIES[sev_raw], "sarif_file": env.get("SKILLSHIELD_SARIF_FILE", "").strip() or None, "workspace": Path(workspace), } def run(env: dict[str, str]) -> int: """Run a scan with the given environment and return the exit code.""" try: s = read_settings(env) except SettingError as e: print(f"::error::SkillShield: {e}") return 2 results, missing, skipped = _scan_paths(s["paths"], s["workspace"], s["min_severity"]) exit_code = decide(results, missing, s["fail_on"]) if s["sarif_file"]: report = BatchScanReport(results=results, metadata={"unscanned": skipped}) _write(s["sarif_file"], json.dumps(report_to_sarif(report), indent=2)) print(console_report(results, missing, exit_code, skipped, s["sarif_file"])) _write( env.get("GITHUB_STEP_SUMMARY"), summary_markdown(results, missing, s["fail_on"], exit_code, skipped, s["workspace"]), "a", ) flagged = sum(1 for r in results if r.risk_level not in ("SAFE", "ERROR")) outputs = { "risk-level": highest_level(results), "scanned": str(len(results)), "flagged": str(flagged), "findings": str(sum(len(r.findings) for r in results)), "sarif-file": s["sarif_file"] or "", "exit-code": str(exit_code), } _write(env.get("GITHUB_OUTPUT"), "".join(f"{k}={v}\n" for k, v in outputs.items()), "a") return exit_code def main() -> None: sys.exit(run(dict(os.environ))) if __name__ == "__main__": main()