From b6fa05ddcfd917ca2f4a68e9003ec223614edd8b Mon Sep 17 00:00:00 2001 From: Lakshman Patel Date: Tue, 1 Dec 2026 17:54:49 +0530 Subject: [PATCH] fix(ci): keep workflows in source archives and scan pull requests GitHub source tarballs honour export-ignore, and OpenSSF Scorecard reads workflow files from those tarballs. Marking .github/ as export-ignore made every tarball-based check see zero workflows: SAST scored 0 and Token-Permissions, Pinned-Dependencies, Dangerous-Workflow, Packaging and Signed-Releases came back inconclusive. Drop that rule and add a check-attr regression test. CodeQL now also runs on pull_request so analyses exist for PR head commits, which is what Scorecard's SAST check inspects. --- .gitattributes | 5 +- .github/workflows/codeql.yml | 9 ++-- internal/testaudit/archive_hygiene_test.go | 63 ++++++++++++++++++++++ 3 files changed, 73 insertions(+), 4 deletions(-) create mode 100644 internal/testaudit/archive_hygiene_test.go diff --git a/.gitattributes b/.gitattributes index 3342e8f9..a1ff8127 100644 --- a/.gitattributes +++ b/.gitattributes @@ -73,7 +73,10 @@ benchmarks/data/** linguist-vendored *.whl binary # --- Source archive hygiene (excluded from `git archive`) ----------------- -.github export-ignore +# `.github/` must stay in archives. GitHub's generated source tarballs honour +# export-ignore, and OpenSSF Scorecard reads workflow files from that tarball: +# excluding it makes SAST, Token-Permissions, Pinned-Dependencies and +# Dangerous-Workflow see zero workflows and score them 0/inconclusive. .shared-templates export-ignore .gitattributes export-ignore .gitignore export-ignore diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index 0698e486..641942a7 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -1,11 +1,14 @@ name: CodeQL -# Static analysis for Go. Runs on every push to main (so Scorecard's SAST -# check sees analyses for default-branch commits) and weekly to pick up new -# CodeQL queries without waiting for a code change. +# Static analysis for Go. Runs on pull requests and every push to main, so +# findings are caught before merge and Scorecard's SAST check sees analyses +# for both PR head commits and default-branch commits. Runs weekly to pick up +# new CodeQL queries without waiting for a code change. on: push: branches: [main] + pull_request: + branches: [main] schedule: - cron: "30 4 * * 1" workflow_dispatch: diff --git a/internal/testaudit/archive_hygiene_test.go b/internal/testaudit/archive_hygiene_test.go new file mode 100644 index 00000000..b1daa5ff --- /dev/null +++ b/internal/testaudit/archive_hygiene_test.go @@ -0,0 +1,63 @@ +package testaudit + +import ( + "archive/tar" + "bytes" + "compress/gzip" + "errors" + "io" + "os" + "os/exec" + "path/filepath" + "testing" +) + +// GitHub's generated source tarballs honour export-ignore from .gitattributes, +// and tarball-based scanners (OpenSSF Scorecard) read workflow files from +// those archives. Marking .github/ as export-ignore therefore drops every +// workflow, making SAST, Token-Permissions, Pinned-Dependencies and +// Dangerous-Workflow see zero workflows and score 0 or inconclusive. +// +// `git check-attr` does not resolve directory patterns the way archive +// generation does, so this test inspects a real `git archive HEAD`. +func TestSourceArchivesIncludeCI(t *testing.T) { + root := repoRoot(t) + + if _, err := os.Stat(filepath.Join(root, ".git")); err != nil { + t.Skip("not a git checkout") + } + if _, err := exec.LookPath("git"); err != nil { + t.Skip("git not installed") + } + + var buf bytes.Buffer + cmd := exec.Command("git", "-C", root, "archive", "--format=tar.gz", "HEAD") + cmd.Stdout = &buf + if err := cmd.Run(); err != nil { + t.Fatalf("git archive HEAD: %v", err) + } + + gz, err := gzip.NewReader(&buf) + if err != nil { + t.Fatalf("gzip.NewReader: %v", err) + } + defer gz.Close() + + tr := tar.NewReader(gz) + for { + hdr, err := tr.Next() + if errors.Is(err, io.EOF) { + break + } + if err != nil { + t.Fatalf("tar reader: %v", err) + } + if hdr.Name == ".github/workflows/ci.yml" { + return + } + } + + t.Fatal(".github/workflows/ci.yml is missing from `git archive HEAD`; " + + "an export-ignore rule in .gitattributes hides CI configuration from " + + "source archives and tarball-based scanners") +}