Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,13 @@ updates:
directory: "/" # Location of package manifests
schedule:
interval: "daily"
groups:
codeql:
applies-to: version-updates
patterns:
- "github/codeql-action"
- "github/codeql-action/*"
- package-ecosystem: "uv"
directory: "/.github/format-tools"
schedule:
interval: "weekly"
8 changes: 8 additions & 0 deletions .github/format-tools/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[project]
name = "action-detect-relevant-changes-format-tools"
version = "0.0.0"
requires-python = "==3.12.*"
dependencies = ["uv==0.12.1", "pyyaml==6.0.3", "yamllint==1.38.0"]

[tool.uv]
package = false
86 changes: 86 additions & 0 deletions .github/format-tools/uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 25 additions & 5 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ on:
permissions:
contents: read
pull-requests: read
security-events: write

jobs:
setup:
Expand Down Expand Up @@ -61,6 +60,7 @@ jobs:
persist-credentials: false
- uses: raven-actions/actionlint@3d39aea434753780c3b3d4a1a31c854b4dbf49d7 # v2
with:
version: "1.7.12"
# Pass explicit file globs so actionlint does not need to discover
# the project root from GITHUB_WORKSPACE (which doesn't contain
# .github/workflows/ when checkout used a path: subdirectory).
Expand All @@ -82,13 +82,24 @@ jobs:
path: ${{ needs.setup.outputs.checkout_path }}
repository: ${{ needs.setup.outputs.repo }}
persist-credentials: false
- name: Checkout trusted format tools
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
repository: ${{ github.repository }}
ref: ${{ github.workflow_sha }}
path: workflow-tools
sparse-checkout: .github/format-tools
persist-credentials: false
- name: Install uv
uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3
- name: Install yamllint
run: uv tool install yamllint
with:
python-version: "3.12"
version-file: workflow-tools/.github/format-tools/pyproject.toml
- name: Run yamllint
working-directory: ${{ needs.setup.outputs.checkout_path }}
run: yamllint .
run: >-
uv run --project "$GITHUB_WORKSPACE/workflow-tools/.github/format-tools"
--locked --no-build yamllint .

guardrail-test:
needs: setup
Expand All @@ -101,9 +112,13 @@ jobs:
path: ${{ needs.setup.outputs.checkout_path }}
repository: ${{ needs.setup.outputs.repo }}
persist-credentials: false
- uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3
with:
python-version: "3.12"
version-file: ${{ needs.setup.outputs.checkout_path }}/.github/format-tools/pyproject.toml
- name: Test guardrail alert logic
working-directory: ${{ needs.setup.outputs.checkout_path }}
run: python3 -m unittest discover -s tests -p 'test_guardrail_audit_alert.py' -v
run: uv run --project .github/format-tools --locked --no-build python -m unittest discover -s tests -v

markdown-check:
needs: setup
Expand All @@ -128,6 +143,11 @@ jobs:

codeql:
needs: setup
permissions:
actions: read
contents: read
pull-requests: read
security-events: write
if: >
always() && needs.setup.result == 'success' && (
github.event_name != 'pull_request' ||
Expand Down
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,9 @@ repos:
hooks:
- id: markdownlint-cli2
args: [--fix]
- repo: https://github.com/rhysd/actionlint
rev: v1.7.12
hooks:
- id: actionlint
entry: bash scripts/pre-commit/actionlint.sh
verbose: true
9 changes: 9 additions & 0 deletions scripts/pre-commit/actionlint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash
set -euo pipefail

if [[ "$(uname -s)" == Darwin ]]; then
printf '%s\n' "actionlint: ShellCheck skipped on Darwin due to actionlint's pipe deadlock; CI runs it on Linux." >&2
exec actionlint -shellcheck= "$@"
fi

exec actionlint "$@"
51 changes: 51 additions & 0 deletions tests/test_codeql_pins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
"""Verify that coupled CodeQL actions stay on one immutable release."""

from __future__ import annotations

import re
import unittest
from pathlib import Path

import yaml


def codeql_references(source: str) -> list[tuple[str, str]]:
"""Return complete CodeQL action references from parsed workflow steps."""
workflows = yaml.safe_load(source)
references = []
for job in workflows.get("jobs", {}).values():
for step in job.get("steps", []):
uses = step.get("uses")
if isinstance(uses, str) and uses.startswith("github/codeql-action/"):
action, separator, reference = uses.removeprefix(
"github/codeql-action/"
).partition("@")
references.append((action, reference if separator else ""))
return references


class CodeQLPinTests(unittest.TestCase):
"""Reject independent component updates and mutable CodeQL references."""

def test_one_immutable_release(self) -> None:
"""Require init, analyze, and any other CodeQL actions to share a commit."""
directory = Path(__file__).resolve().parents[1] / ".github/workflows"
files = [*directory.glob("*.yaml"), *directory.glob("*.yml")]
references = [
reference for path in files for reference in codeql_references(path.read_text())
]
self.assertTrue({"init", "analyze"} <= {name for name, _ in references})
self.assertTrue(all(re.fullmatch(r"[0-9a-f]{40}", ref) for _, ref in references))
self.assertEqual(len({ref for _, ref in references}), 1)

def test_quoted_uses_key_is_checked(self) -> None:
"""Quoted YAML keys still expose every CodeQL reference to validation."""
references = codeql_references(
"""jobs:
codeql:
steps:
- "uses": github/codeql-action/upload-sarif@v4
"""
)
self.assertEqual(references, [("upload-sarif", "v4")])
self.assertFalse(re.fullmatch(r"[0-9a-f]{40}", references[0][1]))
Loading