| 1 | import re |
| 2 | from pathlib import Path |
| 3 | |
| 4 | |
| 5 | ROOT = Path(__file__).resolve().parents[1] |
| 6 | WORKFLOW = ROOT / ".github" / "workflows" / "security.yml" |
| 7 | # AGENTS.md is the canonical agent-guidance file; CLAUDE.md is a one-line |
| 8 | # pointer (`@AGENTS.md`) so anything Claude Code-shaped reads the same source. |
| 9 | AGENTS = ROOT / "AGENTS.md" |
| 10 | |
| 11 | |
| 12 | def _workflow_text() -> str: |
| 13 | return WORKFLOW.read_text(encoding="utf-8") |
| 14 | |
| 15 | |
| 16 | def test_security_workflow_exists() -> None: |
| 17 | assert WORKFLOW.is_file() |
| 18 | |
| 19 | |
| 20 | def test_security_workflow_runs_dependency_audit_as_blocking_check() -> None: |
| 21 | text = _workflow_text() |
| 22 | dependency_audit_job = text.split("dependency-audit:", 1)[1].split("secret-scan:", 1)[0] |
| 23 | |
| 24 | assert "dependency-audit:" in text |
| 25 | assert "uv audit --locked" in dependency_audit_job |
| 26 | assert "continue-on-error: true" not in dependency_audit_job |
| 27 | |
| 28 | |
| 29 | def test_security_workflow_runs_secret_scan_for_pull_requests_and_main_pushes() -> None: |
| 30 | text = _workflow_text() |
| 31 | secret_scan_job = text.split("secret-scan:", 1)[1].split("sast-scan:", 1)[0] |
| 32 | |
| 33 | assert "secret-scan:" in text |
| 34 | assert "pull_request:" in text |
| 35 | assert "push:" in text |
| 36 | assert "workflow_dispatch:" in text |
| 37 | assert "branches:\n - main" in text |
| 38 | assert "trufflesecurity/trufflehog" in secret_scan_job |
| 39 | action_release = re.search( |
| 40 | r"uses: trufflesecurity/trufflehog@[0-9a-f]{40} # v(\d+\.\d+\.\d+)", |
| 41 | secret_scan_job, |
| 42 | ) |
| 43 | assert action_release is not None |
| 44 | assert f"version: {action_release.group(1)}" in secret_scan_job |
| 45 | assert "extra_args: --results=verified" in secret_scan_job |
| 46 | assert "continue-on-error: true" not in secret_scan_job |
| 47 | assert "if: github.event_name" not in secret_scan_job |
| 48 | assert "path: ./" not in secret_scan_job |
| 49 | |
| 50 | |
| 51 | def test_security_workflow_runs_sast_without_unused_code_scanning_permission() -> None: |
| 52 | text = _workflow_text() |
| 53 | sast_job = text.split("sast-scan:", 1)[1] |
| 54 | |
| 55 | assert "semgrep/semgrep@sha256:" in sast_job |
| 56 | assert "semgrep scan --config=p/default" in sast_job |
| 57 | assert "SEMGREP_SEND_METRICS: off" in sast_job |
| 58 | assert "continue-on-error: true" in sast_job |
| 59 | assert "contents: read" in sast_job |
| 60 | assert "security-events: write" not in sast_job |
| 61 | assert "--sarif" not in sast_job |
| 62 | assert "upload-sarif" not in sast_job |
| 63 | |
| 64 | |
| 65 | def test_agent_guidance_mentions_secret_hygiene() -> None: |
| 66 | text = AGENTS.read_text(encoding="utf-8") |
| 67 | |
| 68 | assert "Security hygiene" in text |
| 69 | assert "Never commit real API keys" in text |
| 70 | assert "skills/last30days/scripts/lib/env.py" in text |
| 71 | assert "fixtures" in text |
| 72 |