Skip to content

Commit

Permalink
validate commit sha to avoid common problem (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakecoffman authored Aug 16, 2023
1 parent 438a698 commit 44181f8
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
17 changes: 17 additions & 0 deletions internal/infra/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"net/http"
"os"
"os/signal"
"regexp"
"strings"
"syscall"
"time"
Expand Down Expand Up @@ -62,7 +63,23 @@ type RunParams struct {
InputRaw []byte
}

var gitShaRegex = regexp.MustCompile(`^[0-9a-f]{40}$`)

func (p *RunParams) Validate() error {
if p.Job == nil {
return fmt.Errorf("job is required")
}
if p.Job.Source.Commit != nil && *p.Job.Source.Commit != "" && !gitShaRegex.MatchString(*p.Job.Source.Commit) {
return fmt.Errorf("commit must be a SHA, or not provided")
}
return nil
}

func Run(params RunParams) error {
if err := params.Validate(); err != nil {
return err
}

var ctx context.Context
var cancel func()
if params.Timeout > 0 {
Expand Down
10 changes: 10 additions & 0 deletions testdata/invalid-commit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
job:
package-manager: go_modules
allowed-updates:
- dependency-type: direct
update-type: all
source:
provider: github
repo: rsc/quote
directory: /
commit: unknown
10 changes: 10 additions & 0 deletions testdata/valid-commit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
job:
package-manager: go_modules
allowed-updates:
- dependency-type: direct
update-type: all
source:
provider: github
repo: rsc/quote
directory: /
commit: 5d9f230bcfbae514bb6c2215694c2ce7273fc604
49 changes: 49 additions & 0 deletions tests/integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package tests

import (
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"strings"
"testing"
)

func TestIntegration(t *testing.T) {
// build the binary for the rest of the tests
_, filename, _, _ := runtime.Caller(0)
testPath := filepath.Dir(filename)
cliMain := path.Join(testPath, "../cmd/dependabot/dependabot.go")

if data, err := exec.Command("go", "build", cliMain).CombinedOutput(); err != nil {
t.Fatal("Failed to build the binary: ", string(data))
}
defer func() {
_ = os.Remove("dependabot")
}()

// Helper to run dependabot in the right directory
dependabot := func(args ...string) (string, error) {
cmd := exec.Command("./dependabot", args...)
cmd.Dir = testPath
output, err := cmd.CombinedOutput()
return string(output), err
}

t.Run("works with valid commits", func(t *testing.T) {
if output, err := dependabot("update", "-f", "../testdata/valid-commit.yml"); err != nil {
t.Fatal("Expected no error, but got: ", output)
}
})

t.Run("rejects invalid commits", func(t *testing.T) {
output, err := dependabot("update", "-f", "../testdata/invalid-commit.yml")
if err == nil {
t.Fatal("Expected an error, but got none")
}
if !strings.Contains(output, "commit must be a SHA, or not provided") {
t.Fatalf("Expected error message to mention bad commit, but got: \n%s", output)
}
})
}

0 comments on commit 44181f8

Please sign in to comment.