| 1 | from pathlib import Path |
| 2 | |
| 3 | import yaml |
| 4 | |
| 5 | |
| 6 | ROOT = Path(__file__).resolve().parents[1] |
| 7 | WORKFLOW = ROOT / ".github" / "workflows" / "scorecard.yml" |
| 8 | |
| 9 | |
| 10 | def _workflow_text() -> str: |
| 11 | return WORKFLOW.read_text(encoding="utf-8") |
| 12 | |
| 13 | |
| 14 | def test_scorecard_workflow_exists() -> None: |
| 15 | assert WORKFLOW.is_file() |
| 16 | |
| 17 | |
| 18 | def test_scorecard_runs_ossf_action_and_uploads_sarif() -> None: |
| 19 | text = _workflow_text() |
| 20 | |
| 21 | assert "ossf/scorecard-action" in text |
| 22 | assert "github/codeql-action/upload-sarif" in text |
| 23 | assert "results_format: sarif" in text |
| 24 | |
| 25 | |
| 26 | def test_scorecard_reads_workflows_excluded_from_archives() -> None: |
| 27 | workflow = yaml.safe_load(_workflow_text()) |
| 28 | scorecard_steps = [ |
| 29 | step |
| 30 | for step in workflow["jobs"]["analysis"]["steps"] |
| 31 | if step.get("uses", "").split("@")[0] == "ossf/scorecard-action" |
| 32 | ] |
| 33 | |
| 34 | assert len(scorecard_steps) == 1 |
| 35 | assert scorecard_steps[0]["with"].get("file_mode") == "git" |
| 36 | |
| 37 | |
| 38 | def test_scorecard_runs_on_schedule_and_default_branch() -> None: |
| 39 | text = _workflow_text() |
| 40 | |
| 41 | # A weekly schedule surfaces security-health regressions even with no code |
| 42 | # changes; push-to-main keeps the score fresh on every merge. |
| 43 | assert "schedule:" in text |
| 44 | assert "cron:" in text |
| 45 | assert "branches:" in text |
| 46 | assert "- main" in text |
| 47 | |
| 48 | |
| 49 | def test_scorecard_requests_minimal_permissions() -> None: |
| 50 | text = _workflow_text() |
| 51 | |
| 52 | # Top-level is read-only; only the analysis job widens what it needs. |
| 53 | assert "permissions: read-all" in text |
| 54 | # Job-level permissions fully replace the top-level block, so the reads |
| 55 | # checkout and Scorecard require must be listed explicitly or they default |
| 56 | # to none and every run fails. |
| 57 | assert "contents: read" in text |
| 58 | assert "actions: read" in text |
| 59 | assert "security-events: write" in text |
| 60 | assert "id-token: write" in text |
| 61 | |
| 62 | |
| 63 | def test_scorecard_documents_advisory_policy() -> None: |
| 64 | text = _workflow_text() |
| 65 | |
| 66 | assert "advisory-first" in text.lower() |
| 67 | assert "never blocks merges" in text.lower() |
| 68 |