From 7b80a1fcc317b5b7cd367b48750190a5b951a62e Mon Sep 17 00:00:00 2001 From: Samuel K Date: Wed, 18 Nov 2026 02:51:58 -0600 Subject: [PATCH 1/2] fix(ci): lint complete pull request diffs --- .github/workflows/pr-ci.yml | 11 +- hack/ci/lint_patch.go | 224 +++++++++++++++++++++++ hack/ci/lint_patch_test.go | 342 ++++++++++++++++++++++++++++++++++++ hack/ci/main.go | 4 +- 4 files changed, 579 insertions(+), 2 deletions(-) create mode 100644 hack/ci/lint_patch.go create mode 100644 hack/ci/lint_patch_test.go diff --git a/.github/workflows/pr-ci.yml b/.github/workflows/pr-ci.yml index 62da49ee05..c62bad2675 100644 --- a/.github/workflows/pr-ci.yml +++ b/.github/workflows/pr-ci.yml @@ -178,6 +178,9 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 + with: + ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || '' }} + fetch-depth: ${{ github.event_name == 'pull_request' && '0' || '1' }} - uses: ./.github/actions/relocate-tmp @@ -186,11 +189,17 @@ jobs: with: go-version-file: go.mod + - name: Generate complete pull request lint patch + id: lint-patch + if: github.event_name == 'pull_request' + run: go run ./hack/ci lint-patch >> "$GITHUB_OUTPUT" + - name: golangci-lint uses: golangci/golangci-lint-action@ba0d7d2ec06a0ea1cb5fa41b2e4a3ab91d21278a # v9 with: version: v2.14.0 - only-new-issues: true + only-new-issues: ${{ github.event_name != 'pull_request' }} + args: ${{ steps.lint-patch.outputs.args }} # Native host tests: they execute the real `gh` CLI and, on macOS, the native # keyring, so they run per host OS outside the unit tier's package list. diff --git a/hack/ci/lint_patch.go b/hack/ci/lint_patch.go new file mode 100644 index 0000000000..293c7f9a1a --- /dev/null +++ b/hack/ci/lint_patch.go @@ -0,0 +1,224 @@ +package main + +import ( + "crypto/sha256" + "encoding/json" + "errors" + "fmt" + "io" + "os" + "os/exec" + "path/filepath" + "regexp" + "strings" +) + +var ( + lintCommitID = regexp.MustCompile(`^[0-9a-fA-F]{40}$`) + lintSafePath = regexp.MustCompile( + `^(?:/[A-Za-z0-9_.-][A-Za-z0-9_./-]*|[A-Za-z]:/[A-Za-z0-9_./-]*)$`, + ) +) + +type lintPatchRef struct { + SHA string `json:"sha"` +} + +type lintPatchPullRequest struct { + Base lintPatchRef `json:"base"` + Head lintPatchRef `json:"head"` +} + +type lintPatchEvent struct { + PullRequest *lintPatchPullRequest `json:"pull_request"` +} + +type lintPatchInput struct { + repo string + eventPath string + runnerTemp string +} + +type lintPatchResult struct { + base, head, checkout, mergeBase, path, digest string +} + +func lintPatchCLI(args []string, stdout, stderr io.Writer) int { + if len(args) != 0 { + _, _ = fmt.Fprintln(stderr, "lint-patch: unexpected arguments") + return 2 + } + repo, err := os.Getwd() + if err != nil { + _, _ = fmt.Fprintln(stderr, "lint-patch: cannot determine checkout directory") + return 1 + } + result, err := generateLintPatch(lintPatchInput{ + repo: repo, eventPath: os.Getenv("GITHUB_EVENT_PATH"), + runnerTemp: os.Getenv("RUNNER_TEMP"), + }) + if err != nil { + _, _ = fmt.Fprintf(stderr, "lint-patch: %v\n", err) + return 1 + } + _, _ = fmt.Fprintf( + stderr, + "lint-patch: base=%s head=%s checkout=%s merge_base=%s patch_sha256=%s\n", + result.base, + result.head, + result.checkout, + result.mergeBase, + result.digest, + ) + if _, err := fmt.Fprintf( + stdout, + "args=--new-from-patch=%s --new=false --new-from-rev= --new-from-merge-base=\n", + result.path, + ); err != nil { + _, _ = fmt.Fprintln(stderr, "lint-patch: cannot write action output") + return 1 + } + return 0 +} + +func generateLintPatch(input lintPatchInput) (lintPatchResult, error) { + var result lintPatchResult + base, head, err := readLintPatchEvent(input.eventPath) + if err != nil { + return result, err + } + result.base, result.head = base, head + checkout, err := validateLintCheckout(input.repo, head) + if err != nil { + return result, err + } + result.checkout = checkout + mergeBase, err := uniqueLintMergeBase(input.repo, base, head) + if err != nil { + return result, err + } + result.mergeBase = mergeBase + path, digest, err := writeLintPatch(input.repo, input.runnerTemp, mergeBase, head) + if err != nil { + return result, err + } + current, err := validateLintCheckout(input.repo, head) + if err != nil || current != checkout { + return result, errors.New("checkout changed while generating pull request patch") + } + result.path, result.digest = path, digest + return result, nil +} + +func readLintPatchEvent(path string) (string, string, error) { + if path == "" { + return "", "", errors.New("missing pull request event path") + } + file, err := os.Open(path) + if err != nil { + return "", "", errors.New("cannot open pull request event") + } + defer func() { _ = file.Close() }() + var event lintPatchEvent + decoder := json.NewDecoder(file) + if err := decoder.Decode(&event); err != nil || event.PullRequest == nil { + return "", "", errors.New("invalid pull request event") + } + if err := decoder.Decode(&struct{}{}); !errors.Is(err, io.EOF) { + return "", "", errors.New("invalid pull request event") + } + base := event.PullRequest.Base.SHA + head := event.PullRequest.Head.SHA + if !lintCommitID.MatchString(base) || !lintCommitID.MatchString(head) { + return "", "", errors.New("pull request base and head must be full SHA1 hashes") + } + return strings.ToLower(base), strings.ToLower(head), nil +} + +func validateLintCheckout(repo, head string) (string, error) { + prefix, err := lintGitOutput(repo, "rev-parse", "--show-prefix") + if err != nil || prefix != "" { + return "", errors.New("checkout must be the repository root") + } + checkout, err := lintGitOutput(repo, "rev-parse", "--verify", "HEAD^{commit}") + if err != nil || checkout != head { + return "", errors.New("checkout does not match pull request head") + } + return checkout, nil +} + +func uniqueLintMergeBase(repo, base, head string) (string, error) { + output, err := lintGitOutput(repo, "merge-base", "--all", base, head) + if err != nil { + return "", errors.New("cannot compute pull request merge base") + } + parts := strings.Fields(output) + if len(parts) != 1 || !lintCommitID.MatchString(parts[0]) { + return "", errors.New("pull request must have exactly one merge base") + } + return parts[0], nil +} + +func lintGitOutput(repo string, args ...string) (string, error) { + cmd := exec.Command("git", args...) + cmd.Dir = repo + output, err := cmd.Output() + if err != nil { + return "", err + } + return strings.TrimSpace(string(output)), nil +} + +func writeLintPatch(repo, temp, mergeBase, head string) (string, string, error) { + file, path, err := openLintPatch(temp) + if err != nil { + return "", "", err + } + hash := sha256.New() + cmd := exec.Command( + "git", + "--no-pager", + "diff", + "--no-ext-diff", + "--no-textconv", + "--no-color", + "--no-relative", + "--src-prefix=a/", + "--dst-prefix=b/", + "--find-renames", + "-l0", + "--unified=3", + mergeBase, + head, + "--", + ) + cmd.Dir = repo + cmd.Stdout = io.MultiWriter(file, hash) + runErr := cmd.Run() + info, statErr := file.Stat() + closeErr := file.Close() + if runErr != nil || statErr != nil || closeErr != nil { + return "", "", errors.New("cannot generate complete pull request patch") + } + if info.Size() == 0 { + return "", "", errors.New("pull request patch is empty") + } + return path, fmt.Sprintf("%x", hash.Sum(nil)), nil +} + +func openLintPatch(temp string) (*os.File, string, error) { + if !filepath.IsAbs(temp) || filepath.Clean(temp) != temp || + !lintSafePath.MatchString(filepath.ToSlash(temp)) { + return nil, "", errors.New("RUNNER_TEMP must be a safe absolute directory") + } + dir, err := os.MkdirTemp(temp, "devsy-lint-patch-") + if err != nil { + return nil, "", errors.New("cannot create private patch directory") + } + path := filepath.Join(dir, "pull-request.patch") + file, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o600) + if err != nil { + return nil, "", errors.New("cannot create patch file") + } + return file, filepath.ToSlash(path), nil +} diff --git a/hack/ci/lint_patch_test.go b/hack/ci/lint_patch_test.go new file mode 100644 index 0000000000..5089716eee --- /dev/null +++ b/hack/ci/lint_patch_test.go @@ -0,0 +1,342 @@ +package main + +import ( + "crypto/sha256" + "encoding/json" + "fmt" + "os" + "os/exec" + "path/filepath" + "runtime" + "strings" + "testing" +) + +type lintPatchFixture struct { + repo, temp, event string + base, head, merge, ancestor string +} + +func newLintPatchFixture(t *testing.T) lintPatchFixture { + t.Helper() + repo := t.TempDir() + gitFixture(t, repo, "init", "--initial-branch=main") + gitFixture(t, repo, "config", "user.name", "Test User") + gitFixture(t, repo, "config", "user.email", "test@example.invalid") + gitFixture(t, repo, "config", "commit.gpgsign", "false") + var initial strings.Builder + for i := range 100 { + fmt.Fprintf(&initial, "original line %03d\n", i) + } + writeFixture(t, filepath.Join(repo, "shared.txt"), initial.String()) + gitFixture(t, repo, "add", "shared.txt") + gitFixture(t, repo, "commit", "-m", "initial") + ancestor := gitFixture(t, repo, "rev-parse", "HEAD") + gitFixture(t, repo, "checkout", "-b", "feature") + headContent := strings.Replace( + initial.String(), + "original line 099", + "head-only final change", + 1, + ) + writeFixture(t, filepath.Join(repo, "shared.txt"), headContent) + var large strings.Builder + for i := range 21050 { + fmt.Fprintf(&large, "feature patch line %05d\n", i) + } + writeFixture(t, filepath.Join(repo, "large.txt"), large.String()) + writeFixture(t, filepath.Join(repo, "z-last.txt"), "late-head-marker\n") + gitFixture(t, repo, "add", "shared.txt", "large.txt", "z-last.txt") + gitFixture(t, repo, "commit", "-m", "feature") + head := gitFixture(t, repo, "rev-parse", "HEAD") + gitFixture(t, repo, "checkout", "main") + writeFixture(t, filepath.Join(repo, "shared.txt"), "base-only line\n"+initial.String()) + gitFixture(t, repo, "add", "shared.txt") + gitFixture(t, repo, "commit", "-m", "base") + base := gitFixture(t, repo, "rev-parse", "HEAD") + gitFixture(t, repo, "merge", "--no-ff", "--no-edit", head) + merge := gitFixture(t, repo, "rev-parse", "HEAD") + gitFixture(t, repo, "checkout", "--detach", head) + temp := t.TempDir() + event := filepath.Join(t.TempDir(), "event.json") + writeLintEvent(t, event, base, head) + return lintPatchFixture{ + repo: repo, + temp: temp, + event: event, + base: base, + head: head, + merge: merge, + ancestor: ancestor, + } +} + +func gitFixture(t *testing.T, repo string, args ...string) string { + t.Helper() + cmd := exec.Command("git", args...) + cmd.Dir = repo + cmd.Env = append( + os.Environ(), + "GIT_CONFIG_NOSYSTEM=1", + "GIT_CONFIG_GLOBAL=/dev/null", + "GIT_TERMINAL_PROMPT=0", + ) + output, err := cmd.CombinedOutput() + if err != nil { + t.Fatalf("git %q: %v\n%s", args, err, output) + } + return strings.TrimSpace(string(output)) +} + +func writeFixture(t *testing.T, path, value string) { + t.Helper() + if err := os.WriteFile(path, []byte(value), 0o600); err != nil { + t.Fatal(err) + } +} + +func writeLintEvent(t *testing.T, path, base, head string) { + t.Helper() + event := map[string]any{"pull_request": map[string]any{ + "base": map[string]string{"sha": base}, "head": map[string]string{"sha": head}, + }} + data, err := json.Marshal(event) + if err != nil { + t.Fatal(err) + } + writeFixture(t, path, string(data)) +} + +func (f lintPatchFixture) input() lintPatchInput { + return lintPatchInput{repo: f.repo, eventPath: f.event, runnerTemp: f.temp} +} + +func TestLintPatchIncludesCompleteHeadDiff(t *testing.T) { + f := newLintPatchFixture(t) + result, err := generateLintPatch(f.input()) + if err != nil { + t.Fatal(err) + } + data, err := os.ReadFile(result.path) + if err != nil { + t.Fatal(err) + } + assertLintPatchContents(t, data, result, f) + assertHeadAndMergedFixture(t, f) +} + +func assertHeadAndMergedFixture(t *testing.T, f lintPatchFixture) { + t.Helper() + head := gitFixture(t, f.repo, "show", "HEAD:shared.txt") + if strings.HasPrefix(head, "base-only line\n") || + !strings.HasSuffix(head, "head-only final change") { + t.Fatal("head checkout does not preserve the head-side source") + } + merged := gitFixture(t, f.repo, "show", f.merge+":shared.txt") + if !strings.HasPrefix(merged, "base-only line\n") || + !strings.HasSuffix(merged, "head-only final change") { + t.Fatal("fixture merge did not retain both base and head changes") + } +} + +func assertLintPatchContents( + t *testing.T, + data []byte, + result lintPatchResult, + f lintPatchFixture, +) { + t.Helper() + patch := string(data) + if strings.Count(patch, "\n") <= 20000 || !strings.Contains(patch, "+late-head-marker\n") { + t.Fatal("patch omitted its large body or final file") + } + if !strings.Contains(patch, "+head-only final change\n") || + strings.Contains(patch, "base-only line") { + t.Fatal("patch was not computed from merge base to pull request head") + } + if !strings.Contains(patch, "@@ -97,4 +97,4 @@") { + t.Fatal("patch did not retain the head-side hunk position") + } + assertLintPatchBinding(t, data, result, f) +} + +func assertLintPatchBinding(t *testing.T, data []byte, result lintPatchResult, f lintPatchFixture) { + t.Helper() + if result.base != f.base || result.head != f.head || result.checkout != f.head || + result.mergeBase != f.ancestor { + t.Fatalf("unexpected commit binding: %+v", result) + } + if got := fmt.Sprintf("%x", sha256.Sum256(data)); got != result.digest { + t.Fatalf("patch digest = %s, want %s", result.digest, got) + } + assertLintPatchFile(t, result.path, f.temp) +} + +func assertLintPatchFile(t *testing.T, path, temp string) { + t.Helper() + assertLintPatchContainment(t, path, temp) + info, err := os.Lstat(path) + if err != nil || !info.Mode().IsRegular() { + t.Fatalf("patch is not a regular file: %v %v", info, err) + } + if runtime.GOOS != "windows" && info.Mode().Perm() != 0o600 { + t.Fatalf("patch permissions: %v", info.Mode()) + } +} + +func assertLintPatchContainment(t *testing.T, path, temp string) { + t.Helper() + rel, err := filepath.Rel(temp, path) + if err != nil || rel == "." || rel == ".." || + strings.HasPrefix(rel, ".."+string(os.PathSeparator)) { + t.Fatalf("patch is outside its temporary directory: %q %v", rel, err) + } +} + +func TestLintPatchSafePaths(t *testing.T) { + for _, test := range []struct { + path string + want bool + }{ + {"/tmp/runner-temp", true}, + {"C:/runner-temp", true}, + {"C:/", true}, + {"relative/path", false}, + {"//server/share", false}, + {"//?/C:/temp", false}, + {"C:/temp/D:/file", false}, + {"C:/temp with spaces", false}, + {"/tmp/file;unsafe", false}, + } { + if got := lintSafePath.MatchString(test.path); got != test.want { + t.Errorf("safe path %q = %t, want %t", test.path, got, test.want) + } + } + for _, path := range []string{ + "relative/path", + filepath.Join(os.TempDir(), "safe") + string(os.PathSeparator) + "..", + "//server/share", + } { + file, _, err := openLintPatch(path) + if err == nil { + _ = file.Close() + t.Errorf("accepted unsafe path %q", path) + } + } +} + +func TestLintPatchRejectsInvalidEventsAndCheckouts(t *testing.T) { + f := newLintPatchFixture(t) + for _, test := range []struct { + name string + edit func(*lintPatchInput) + }{ + {"missing event", func(input *lintPatchInput) { input.eventPath = "" }}, + {"missing file", func(input *lintPatchInput) { input.eventPath = filepath.Join(f.temp, "missing") }}, + {"unsafe temporary path", func(input *lintPatchInput) { input.runnerTemp += ";unsafe" }}, + } { + t.Run(test.name, func(t *testing.T) { + input := f.input() + test.edit(&input) + if _, err := generateLintPatch(input); err == nil { + t.Fatal("accepted invalid input") + } + }) + } + for _, test := range []struct{ name, base, head string }{ + {"missing base", "", f.head}, + {"malicious base", "--upload-pack=unsafe", f.head}, + {"abbreviated head", f.base, f.head[:12]}, + {"mismatched head", f.base, f.ancestor}, + {"missing object", strings.Repeat("a", 40), f.head}, + } { + t.Run(test.name, func(t *testing.T) { + event := filepath.Join(t.TempDir(), "event.json") + writeLintEvent(t, event, test.base, test.head) + input := f.input() + input.eventPath = event + if _, err := generateLintPatch(input); err == nil { + t.Fatal("accepted invalid event") + } + }) + } + for _, checkout := range []struct{ name, commit string }{ + {"synthetic merge checkout", f.merge}, + {"base checkout", f.base}, + } { + t.Run(checkout.name, func(t *testing.T) { + gitFixture(t, f.repo, "checkout", "--detach", checkout.commit) + if _, err := generateLintPatch(f.input()); err == nil { + t.Fatal("accepted checkout other than pull request head") + } + }) + } +} + +func TestLintPatchRejectsEmptyDiff(t *testing.T) { + f := newLintPatchFixture(t) + if _, _, err := writeLintPatch(f.repo, f.temp, f.ancestor, f.ancestor); err == nil { + t.Fatal("accepted empty patch") + } +} + +func TestLintPatchRejectsMalformedEvent(t *testing.T) { + for _, body := range []string{"{", "{}", "{} {}", setupNullDoc, "[]"} { + path := filepath.Join(t.TempDir(), "event.json") + writeFixture(t, path, body) + if _, _, err := readLintPatchEvent(path); err == nil { + t.Fatalf("accepted malformed event %q", body) + } + } + path := filepath.Join(t.TempDir(), "event.json") + writeLintEvent(t, path, strings.Repeat("a", 40), strings.Repeat("b", 40)) + data, err := os.ReadFile(path) + if err != nil { + t.Fatal(err) + } + writeFixture(t, path, string(data)+" {}") + if _, _, err := readLintPatchEvent(path); err == nil { + t.Fatal("accepted trailing event data") + } +} + +func TestLintPatchRejectsGitErrorsAndAmbiguousBases(t *testing.T) { + f := newLintPatchFixture(t) + if _, _, err := writeLintPatch(f.repo, f.temp, strings.Repeat("a", 40), f.head); err == nil { + t.Fatal("accepted git diff error") + } + tree := gitFixture(t, f.repo, "rev-parse", f.ancestor+"^{tree}") + a := f.treeCommit(t, tree, "branch a", f.ancestor) + b := f.treeCommit(t, tree, "branch b", f.ancestor) + left := f.treeCommit(t, tree, "left merge", a, b) + right := f.treeCommit(t, tree, "right merge", b, a) + if _, err := uniqueLintMergeBase(f.repo, left, right); err == nil { + t.Fatal("accepted multiple merge bases") + } + unrelated := f.treeCommit(t, tree, "unrelated") + if _, err := uniqueLintMergeBase(f.repo, left, unrelated); err == nil { + t.Fatal("accepted unrelated histories") + } +} + +func (f lintPatchFixture) treeCommit(t *testing.T, tree, message string, parents ...string) string { + t.Helper() + args := []string{"commit-tree", tree} + for _, parent := range parents { + args = append(args, "-p", parent) + } + cmd := exec.Command("git", args...) + cmd.Dir = f.repo + cmd.Env = append( + os.Environ(), + "GIT_CONFIG_NOSYSTEM=1", + "GIT_CONFIG_GLOBAL=/dev/null", + "GIT_TERMINAL_PROMPT=0", + ) + cmd.Stdin = strings.NewReader(message + "\n") + output, err := cmd.CombinedOutput() + if err != nil { + t.Fatalf("git commit-tree: %v\n%s", err, output) + } + return strings.TrimSpace(string(output)) +} diff --git a/hack/ci/main.go b/hack/ci/main.go index 6e7ae3c7c6..68b6460e8f 100644 --- a/hack/ci/main.go +++ b/hack/ci/main.go @@ -23,7 +23,7 @@ func main() { func runCLI(args []string, stdout, stderr io.Writer) int { if len(args) == 0 { - _, _ = fmt.Fprintln(stderr, "usage: ci [args]") + _, _ = fmt.Fprintln(stderr, "usage: ci [args]") return 2 } switch args[0] { @@ -37,6 +37,8 @@ func runCLI(args []string, stdout, stderr io.Writer) int { return setupCLI(args[1:], stdout, stderr) case "success": return successCLI(args[1:], stdout, stderr) + case "lint-patch": + return lintPatchCLI(args[1:], stdout, stderr) default: _, _ = fmt.Fprintf(stderr, "unknown command: %s\n", args[0]) return 2 From 773a3a74c00d7f2d725cb6cfb685cce6a1a4fdce Mon Sep 17 00:00:00 2001 From: Samuel K Date: Wed, 18 Nov 2026 20:41:33 -0600 Subject: [PATCH 2/2] fix(ci): preserve scoped lint findings --- .github/workflows/pr-ci.yml | 1 + hack/ci/lint_patch.go | 77 +++++++++++++++++++++++- hack/ci/lint_patch_test.go | 117 +++++++++++++++++++++++++++++++++++- 3 files changed, 192 insertions(+), 3 deletions(-) diff --git a/.github/workflows/pr-ci.yml b/.github/workflows/pr-ci.yml index c62bad2675..d661ef55c9 100644 --- a/.github/workflows/pr-ci.yml +++ b/.github/workflows/pr-ci.yml @@ -181,6 +181,7 @@ jobs: with: ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.sha || '' }} fetch-depth: ${{ github.event_name == 'pull_request' && '0' || '1' }} + persist-credentials: ${{ github.event_name != 'pull_request' }} - uses: ./.github/actions/relocate-tmp diff --git a/hack/ci/lint_patch.go b/hack/ci/lint_patch.go index 293c7f9a1a..9de0ccbf1b 100644 --- a/hack/ci/lint_patch.go +++ b/hack/ci/lint_patch.go @@ -1,6 +1,7 @@ package main import ( + "bufio" "crypto/sha256" "encoding/json" "errors" @@ -11,6 +12,8 @@ import ( "path/filepath" "regexp" "strings" + "unicode" + "unicode/utf8" ) var ( @@ -48,6 +51,12 @@ func lintPatchCLI(args []string, stdout, stderr io.Writer) int { _, _ = fmt.Fprintln(stderr, "lint-patch: unexpected arguments") return 2 } + temp := os.Getenv("RUNNER_TEMP") + if !filepath.IsAbs(temp) || filepath.Clean(temp) != temp || + !lintSafePath.MatchString(filepath.ToSlash(temp)) { + _, _ = fmt.Fprintln(stderr, "lint-patch: RUNNER_TEMP must be a safe absolute directory") + return 1 + } repo, err := os.Getwd() if err != nil { _, _ = fmt.Fprintln(stderr, "lint-patch: cannot determine checkout directory") @@ -55,7 +64,7 @@ func lintPatchCLI(args []string, stdout, stderr io.Writer) int { } result, err := generateLintPatch(lintPatchInput{ repo: repo, eventPath: os.Getenv("GITHUB_EVENT_PATH"), - runnerTemp: os.Getenv("RUNNER_TEMP"), + runnerTemp: temp, }) if err != nil { _, _ = fmt.Fprintf(stderr, "lint-patch: %v\n", err) @@ -170,6 +179,9 @@ func lintGitOutput(repo string, args ...string) (string, error) { } func writeLintPatch(repo, temp, mergeBase, head string) (string, string, error) { + if err := validateLintPatchPaths(repo, mergeBase, head); err != nil { + return "", "", err + } file, path, err := openLintPatch(temp) if err != nil { return "", "", err @@ -178,6 +190,8 @@ func writeLintPatch(repo, temp, mergeBase, head string) (string, string, error) cmd := exec.Command( "git", "--no-pager", + "-c", + "core.quotePath=false", "diff", "--no-ext-diff", "--no-textconv", @@ -208,7 +222,7 @@ func writeLintPatch(repo, temp, mergeBase, head string) (string, string, error) func openLintPatch(temp string) (*os.File, string, error) { if !filepath.IsAbs(temp) || filepath.Clean(temp) != temp || - !lintSafePath.MatchString(filepath.ToSlash(temp)) { + strings.HasPrefix(filepath.ToSlash(temp), "//") { return nil, "", errors.New("RUNNER_TEMP must be a safe absolute directory") } dir, err := os.MkdirTemp(temp, "devsy-lint-patch-") @@ -222,3 +236,62 @@ func openLintPatch(temp string) (*os.File, string, error) { } return file, filepath.ToSlash(path), nil } + +func validateLintPatchPaths(repo, mergeBase, head string) error { + cmd := exec.Command( + "git", "--no-pager", "-c", "core.quotePath=false", "diff", + "--no-ext-diff", "--no-textconv", "--no-color", "--no-relative", + "--find-renames", "-l0", "--name-only", "-z", "--diff-filter=d", + mergeBase, head, "--", + ) + cmd.Dir = repo + pipe, err := cmd.StdoutPipe() + if err != nil { + return errors.New("cannot open pull request path list") + } + if err := cmd.Start(); err != nil { + return errors.New("cannot start pull request path list") + } + pathErr := checkLintPatchPaths(pipe) + if pathErr != nil { + _ = pipe.Close() + _ = cmd.Process.Kill() + } + waitErr := cmd.Wait() + if pathErr != nil { + return pathErr + } + if waitErr != nil { + return errors.New("cannot validate pull request Go paths for lint") + } + return nil +} + +func checkLintPatchPaths(src io.Reader) error { + reader := bufio.NewReader(src) + for { + path, err := reader.ReadString(0) + if errors.Is(err, io.EOF) && path == "" { + return nil + } + if err != nil || !strings.HasSuffix(path, "\x00") { + return errors.New("invalid pull request path list") + } + path = strings.TrimSuffix(path, "\x00") + if strings.HasSuffix(path, ".go") && !lintRepresentableGoPath(path) { + return errors.New("pull request Go path cannot be represented in lint patch") + } + } +} + +func lintRepresentableGoPath(path string) bool { + if !utf8.ValidString(path) || strings.ContainsAny(path, " \"\\") { + return false + } + for _, char := range path { + if unicode.IsControl(char) { + return false + } + } + return true +} diff --git a/hack/ci/lint_patch_test.go b/hack/ci/lint_patch_test.go index 5089716eee..39b778ec67 100644 --- a/hack/ci/lint_patch_test.go +++ b/hack/ci/lint_patch_test.go @@ -1,6 +1,7 @@ package main import ( + "bytes" "crypto/sha256" "encoding/json" "fmt" @@ -225,6 +226,118 @@ func TestLintPatchSafePaths(t *testing.T) { } } +func TestLintPatchCLISafeArguments(t *testing.T) { + temp := filepath.Join(t.TempDir(), "temp with spaces") + if err := os.Mkdir(temp, 0o700); err != nil { + t.Fatal(err) + } + t.Setenv("RUNNER_TEMP", temp) + var stdout, stderr bytes.Buffer + if code := lintPatchCLI(nil, &stdout, &stderr); code != 1 || stdout.Len() != 0 { + t.Fatalf("unsafe temporary path: exit %d, output %q", code, stdout.String()) + } + entries, err := os.ReadDir(temp) + if err != nil || len(entries) != 0 { + t.Fatalf("unsafe temporary path was used: %v %v", entries, err) + } + stdout.Reset() + stderr.Reset() + if code := lintPatchCLI( + []string{"unexpected"}, + &stdout, + &stderr, + ); code != 2 || + stdout.Len() != 0 { + t.Fatalf("unexpected arguments: exit %d, output %q", code, stdout.String()) + } +} + +func TestLintPatchGeneratorAllowsSpacesInTemp(t *testing.T) { + f := newLintPatchFixture(t) + temp := filepath.Join(f.temp, "temp with spaces") + if err := os.Mkdir(temp, 0o700); err != nil { + t.Fatal(err) + } + input := f.input() + input.runnerTemp = temp + result, err := generateLintPatch(input) + if err != nil { + t.Fatal(err) + } + data, err := os.ReadFile(result.path) + if err != nil { + t.Fatal(err) + } + assertLintPatchContents(t, data, result, f) +} + +func TestLintPatchPathRepresentability(t *testing.T) { + for _, test := range []struct { + path string + want bool + }{ + {"pkg/caf\u00e9.go\x00", true}, + {"pkg/space name.go\x00", false}, + {"pkg/quoted\"name.go\x00", false}, + {"pkg/back\\slash.go\x00", false}, + {"pkg/new\nline.go\x00", false}, + {"pkg/tab\tname.go\x00", false}, + {"pkg/\xff.go\x00", false}, + {"docs/space name.txt\x00", true}, + {"pkg/truncated.go", false}, + } { + err := checkLintPatchPaths(strings.NewReader(test.path)) + if (err == nil) != test.want { + t.Errorf("path list %q: got error %v", test.path, err) + } + } +} + +func TestLintPatchUnicodeAndUnsupportedGoPaths(t *testing.T) { + f := newLintPatchFixture(t) + unicodeName := "caf\u00e9.go" + writeFixture(t, filepath.Join(f.repo, unicodeName), "package sample\n") + writeFixture(t, filepath.Join(f.repo, "space notes.txt"), "notes\n") + gitFixture(t, f.repo, "add", unicodeName, "space notes.txt") + gitFixture(t, f.repo, "commit", "-m", "unicode paths") + head := gitFixture(t, f.repo, "rev-parse", "HEAD") + path, _, err := writeLintPatch(f.repo, f.temp, f.ancestor, head) + if err != nil { + t.Fatal(err) + } + patch, err := os.ReadFile(path) + if err != nil { + t.Fatal(err) + } + f.assertRawPatch(t, head, patch) + if !bytes.Contains(patch, []byte("+++ b/"+unicodeName+"\n")) || + !bytes.Contains(patch, []byte("+++ b/space notes.txt\t\n")) { + t.Fatal("patch did not preserve raw Git postimage headers") + } + writeFixture(t, filepath.Join(f.repo, "space fixture.go"), "package sample\n") + gitFixture(t, f.repo, "add", "space fixture.go") + gitFixture(t, f.repo, "commit", "-m", "unsupported path") + head = gitFixture(t, f.repo, "rev-parse", "HEAD") + if _, _, err := writeLintPatch(f.repo, f.temp, f.ancestor, head); err == nil { + t.Fatal("accepted unsupported Go postimage path") + } +} + +func (f lintPatchFixture) assertRawPatch(t *testing.T, head string, patch []byte) { + t.Helper() + cmd := exec.Command( + "git", "--no-pager", "-c", "core.quotePath=false", "diff", + "--no-ext-diff", "--no-textconv", "--no-color", "--no-relative", + "--src-prefix=a/", "--dst-prefix=b/", "--find-renames", "-l0", "--unified=3", + f.ancestor, head, "--", + ) + cmd.Dir = f.repo + raw, err := cmd.Output() + if err != nil || !bytes.Equal(raw, patch) { + t.Fatalf("patch differs from raw Git diff: %v", err) + } +} + func TestLintPatchRejectsInvalidEventsAndCheckouts(t *testing.T) { f := newLintPatchFixture(t) for _, test := range []struct { @@ -233,7 +346,9 @@ func TestLintPatchRejectsInvalidEventsAndCheckouts(t *testing.T) { }{ {"missing event", func(input *lintPatchInput) { input.eventPath = "" }}, {"missing file", func(input *lintPatchInput) { input.eventPath = filepath.Join(f.temp, "missing") }}, - {"unsafe temporary path", func(input *lintPatchInput) { input.runnerTemp += ";unsafe" }}, + {"noncanonical temporary path", func(input *lintPatchInput) { + input.runnerTemp += string(os.PathSeparator) + ".." + }}, } { t.Run(test.name, func(t *testing.T) { input := f.input()