diff --git a/.github/workflows/security.yml b/.github/workflows/security.yml new file mode 100644 index 0000000..67ef3ab --- /dev/null +++ b/.github/workflows/security.yml @@ -0,0 +1,143 @@ +name: Security + +on: + push: + branches: ['**'] + pull_request: + branches: [main, master] + schedule: + # Weekly, so advisories published after the last push still get reported. + - cron: '23 5 * * 1' + workflow_dispatch: {} + +jobs: + secrets: + name: Secrets (gitleaks) + runs-on: ubuntu-latest + permissions: + contents: read + steps: + - name: Checkout code + uses: actions/checkout@v7 + with: + # Full history, so a secret that was committed and later removed is still found. + fetch-depth: 0 + submodules: false + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: stable + # gitleaks runs at a pinned version. go run checks the module against the Go checksum + # database. Known false positives go in .gitleaks.toml at the repo root, which gitleaks reads + # on its own. + - name: Scan git history for secrets + run: go run github.com/zricethezav/gitleaks/v8@v8.30.1 git --no-banner --redact --exit-code 1 . + + code: + name: Code (Semgrep) + runs-on: ubuntu-latest + permissions: + contents: read + steps: + - name: Checkout code + uses: actions/checkout@v7 + with: + submodules: false + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: '3.12' + - name: Install Semgrep + run: pip install semgrep==1.182.1 + # Fails on ERROR findings from Semgrep's default rules. WARNING findings are not gated. + - name: Scan code + run: semgrep scan --config p/default --severity ERROR --error --metrics=off . + + dependencies: + name: Dependencies (Trivy) + runs-on: ubuntu-latest + permissions: + contents: read + env: + TRIVY_VERSION: '0.76.1' + steps: + - name: Checkout code + uses: actions/checkout@v7 + with: + submodules: false + - name: Set up Node + uses: actions/setup-node@v4 + with: + node-version: 24 + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: '3.12' + - name: Install uv + run: pip install uv==0.13.5 + - name: Install Trivy + # Downloaded from the release and checked against its published checksums. The + # aquasecurity/trivy-action version tags were repointed to malicious commits in March 2026, + # so that action is not used. + shell: bash + run: | + archive="trivy_${TRIVY_VERSION}_Linux-64bit.tar.gz" + base_url="https://github.com/aquasecurity/trivy/releases/download/v${TRIVY_VERSION}" + + curl --fail --location --retry 5 --retry-delay 2 \ + --output "$RUNNER_TEMP/$archive" "$base_url/$archive" + curl --fail --location --retry 5 --retry-delay 2 \ + --output "$RUNNER_TEMP/trivy_checksums.txt" "$base_url/trivy_${TRIVY_VERSION}_checksums.txt" + + (cd "$RUNNER_TEMP" && grep -F " $archive" trivy_checksums.txt > trivy_checksum.txt \ + && sha256sum -c trivy_checksum.txt) + + tar -xzf "$RUNNER_TEMP/$archive" -C "$RUNNER_TEMP" trivy + echo "$RUNNER_TEMP" >> "$GITHUB_PATH" + "$RUNNER_TEMP/trivy" --version + # Trivy audits lockfiles and skips bare manifests. For each manifest with no lockfile in its + # own folder or any parent folder, resolve one here so its dependencies get scanned. These + # lockfiles are made only for this run and are not committed. + - name: Resolve manifests that have no lockfile + shell: bash + run: | + has_lockfile() { + local dir=$1 + shift + while true; do + for lockfile in "$@"; do + if [ -f "$dir/$lockfile" ]; then + return 0 + fi + done + if [ "$dir" = "." ]; then + return 1 + fi + dir=$(dirname "$dir") + done + } + + while IFS= read -r -d '' manifest; do + dir=$(dirname "$manifest") + if ! has_lockfile "$dir" package-lock.json yarn.lock pnpm-lock.yaml; then + echo "Resolving npm dependencies in $dir" + (cd "$dir" && npm install --package-lock-only --ignore-scripts --no-audit --no-fund) + fi + done < <(find . -name node_modules -prune -o -name package.json -print0) + + while IFS= read -r -d '' manifest; do + dir=$(dirname "$manifest") + if ! has_lockfile "$dir" uv.lock poetry.lock Pipfile.lock; then + echo "Resolving Python dependencies in $dir" + (cd "$dir" && uv lock) + fi + done < <(find . -name node_modules -prune -o -name .venv -prune -o -name pyproject.toml -print0) + # Fails on HIGH and CRITICAL vulnerabilities that have a fixed version. Vulnerabilities with no + # fix are left out, because nothing can be changed to clear them. Development dependencies are + # included, since build and test tooling runs in CI. + # --offline-scan: Maven Central rate-limits the lookups Trivy makes for pom.xml, so Maven + # dependencies are read from their declared versions only. Transitive ones are not resolved. + - name: Scan dependencies for known vulnerabilities + run: >- + trivy fs --scanners vuln --severity HIGH,CRITICAL --ignore-unfixed --include-dev-deps + --offline-scan --exit-code 1 --timeout 15m .