internal/control/security_test.go
115 lines · 4490 bytes
1package control
2
3import (
4 "encoding/json"
5 "strings"
6 "testing"
7
8 "gitbay.org/gitbay/internal/ci"
9 "gitbay.org/gitbay/internal/codescan"
10 "gitbay.org/gitbay/internal/config"
11 "gitbay.org/gitbay/internal/protocol"
12)
13
14const testSARIF = `{"version":"2.1.0","runs":[{"tool":{"driver":{"name":"opengrep"}},"results":[
15 {"ruleId":"go.lang.security.audit.sqli","level":"error","message":{"text":"SQL built from input"},
16 "locations":[{"physicalLocation":{"artifactLocation":{"uri":"store/q.go"},"region":{"startLine":12}}}]}]}]}`
17
18// The whole round trip without a runner process: enable, queue, claim,
19// upload, and the issue that results in.
20func TestSecurityScanRoundTrip(t *testing.T) {
21 st, repo, u, root := browseFixture(t)
22 p := repo.Path()
23 sec := config.Security{Image: "localhost/gitbay-security:1", Schedule: "0 4 * * *", Steps: config.DefaultSecuritySteps}
24
25 c, out := runnerCtx(st, u.ID, root)
26 if code := runSecurityEnable(c, []string{p}); code != protocol.ExitFailure || !strings.Contains(out.String(), "no code scanning image") {
27 t.Fatalf("enable without an image: exit %d %s", code, out)
28 }
29 run := func(f func(*Ctx, []string) int, stdin string, args ...string) (int, string) {
30 t.Helper()
31 c, out := runnerCtx(st, u.ID, root)
32 c.Cfg.Security = sec
33 c.Cfg.Mail.SMTPHost = "localhost:587"
34 c.Stdin = strings.NewReader(stdin)
35 c.JSON = true
36 return f(c, args), out.String()
37 }
38 if code, out := run(runSecurityEnable, "", p); code != protocol.ExitOK {
39 t.Fatalf("enable: %d %s", code, out)
40 }
41 if code, out := run(runSecurityRun, "", p); code != protocol.ExitOK {
42 t.Fatalf("run: %d %s", code, out)
43 }
44 if code, out := run(runSecurityRun, "", p); code == protocol.ExitOK || !strings.Contains(out, "already pending") {
45 t.Fatalf("second run: %d %s", code, out)
46 }
47
48 st.SetBuildSecret(repo.ID, "DEPLOY_TOKEN", "s3cret")
49 code, claim := run(runRunnerNext, "")
50 if code != protocol.ExitOK || !strings.Contains(claim, `"job":"`+ci.SecurityJob+`"`) || !strings.Contains(claim, `"results":true`) {
51 t.Fatalf("claim: %d %s", code, claim)
52 }
53 if strings.Contains(claim, "s3cret") {
54 t.Fatalf("the scan was handed the repository's secrets: %s", claim)
55 }
56 var claimed struct {
57 Data struct {
58 ID int64 `json:"id"`
59 SHA string `json:"sha"`
60 } `json:"data"`
61 }
62 json.Unmarshal([]byte(claim), &claimed)
63 id := claimed.Data.ID
64
65 if code, out := run(runRunnerResults, testSARIF, itoa(id)); code != protocol.ExitOK {
66 t.Fatalf("results: %d %s", code, out)
67 }
68 scan, _ := st.SecurityScanFor(repo.ID)
69 if scan.IssueNumber == 0 || scan.LastResults == "" {
70 t.Fatalf("scan = %+v", scan)
71 }
72 issue, _ := st.IssueByNumber(repo.ID, scan.IssueNumber)
73 if issue.Title != codescan.IssueTitle || !strings.Contains(issue.Body, "`store/q.go:12` go.lang.security.audit.sqli (error)") {
74 t.Errorf("issue %q:\n%s", issue.Title, issue.Body)
75 }
76 if code, out := run(runRunnerDone, "", itoa(id), "success"); code != protocol.ExitOK {
77 t.Fatalf("done: %d %s", code, out)
78 }
79 if statuses, _ := st.CommitStatusesFor(repo.ID, []string{claimed.Data.SHA}); len(statuses[claimed.Data.SHA]) != 0 {
80 t.Errorf("the scan set a commit status: %+v", statuses)
81 }
82
83 var status struct {
84 Data SecurityOut `json:"data"`
85 }
86 json.Unmarshal([]byte(dispatchIn(t, st, u, root, "", "repo", "security", "status", p, "--json")), &status)
87 if !status.Data.Enabled || len(status.Data.Findings) != 1 || status.Data.Findings[0].Line != 12 {
88 t.Errorf("status = %+v", status.Data)
89 }
90 screen := atTerminalIn(t, st, u, root, "repo", "security", "status", p)
91 if !strings.Contains(screen, "Findings (1)") || !strings.Contains(screen, "store/q.go:12") {
92 t.Errorf("screen:\n%s", screen)
93 }
94 checkLegend(t, screen)
95
96 // A clean scan closes the issue.
97 run(runSecurityRun, "", p)
98 _, claim = run(runRunnerNext, "")
99 json.Unmarshal([]byte(claim), &claimed)
100 if code, out := run(runRunnerResults, `{"version":"2.1.0","runs":[]}`, itoa(claimed.Data.ID)); code != protocol.ExitOK {
101 t.Fatalf("clean results: %d %s", code, out)
102 }
103 if closed, _ := st.IssueByNumber(repo.ID, scan.IssueNumber); closed.State != "closed" {
104 t.Errorf("issue state after a clean scan = %q", closed.State)
105 }
106
107 // A job of the repository's own may not upload.
108 other, err := st.CreateBuild(repo.ID, "unit", claimed.Data.SHA, "main", "[]", "", "", true)
109 if err != nil {
110 t.Fatal(err)
111 }
112 if code, out := run(runRunnerResults, testSARIF, itoa(other)); code != protocol.ExitDenied {
113 t.Errorf("results from another job: %d %s", code, out)
114 }
115}