package e2e import ( "os" "path/filepath" "strings" "testing" ) // The instance's code scan end to end: enable, queue, a real runner runs // the configured steps and uploads their SARIF, and the findings land in // an issue. A results directory committed to the repository is cleared // before the steps run, so it is never uploaded as the scan's (#340). func TestSecurityScan(t *testing.T) { t.Parallel() inst := startInstanceWith(t, `[security] image = "localhost/scan:1" steps = ['mkdir -p "$GITBAY_RESULTS"', 'cp scan.sarif "$GITBAY_RESULTS/scan.sarif"'] `) inst.runner = buildRunner(t) aliceKey := inst.newKey(t, "alice") inst.admin(t, "admin", "user", "create", "alice", "--key", aliceKey+".pub") runnerKey := inst.newKey(t, "ci") inst.admin(t, "admin", "user", "create", "ci", "--key", runnerKey+".pub", "--admin") if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/app"); code != 0 { t.Fatalf("repo create: %s", errOut) } work := t.TempDir() env := inst.gitEnv(aliceKey) mustGit(t, work, env, "clone", "-q", inst.sshURL("alice/app"), "w") dir := filepath.Join(work, "w") os.MkdirAll(filepath.Join(dir, ".gitbay-results"), 0o755) os.WriteFile(filepath.Join(dir, "scan.sarif"), []byte(`{"version":"2.1.0","runs":[{"tool":{"driver":{"name":"opengrep"}}, "results":[{"ruleId":"python.exec-used","level":"error","message":{"text":"exec on input"}, "locations":[{"physicalLocation":{"artifactLocation":{"uri":"app.py"},"region":{"startLine":7}}}]}]}]}`), 0o644) os.WriteFile(filepath.Join(dir, ".gitbay-results", "planted.sarif"), []byte(`{"runs":[{"tool":{"driver":{"name":"planted"}}, "results":[{"ruleId":"planted.rule","message":{"text":"planted"}}]}]}`), 0o644) mustGit(t, dir, env, "checkout", "-q", "-b", "main") mustGit(t, dir, env, "add", ".") mustGit(t, dir, env, "commit", "-q", "-m", "base") mustGit(t, dir, env, "push", "-q", "origin", "main") if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "security", "enable", "alice/app"); code != 0 { t.Fatalf("security enable: %s", errOut) } if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "security", "run", "alice/app"); code != 0 { t.Fatalf("security run: %s", errOut) } if out := inst.runnerOnce(t, runnerKey); !strings.Contains(out, "success") { t.Fatalf("runner:\n%s", out) } out, errOut, code := inst.ssh(t, aliceKey, "", "repo", "security", "status", "alice/app", "--json") if code != 0 || !strings.Contains(out, `"rule":"python.exec-used"`) || strings.Contains(out, "planted") { t.Fatalf("status: %d %s\n%s", code, errOut, out) } out, _, _ = inst.ssh(t, aliceKey, "", "issue", "list", "alice/app") if !strings.Contains(out, "Code scanning findings") { t.Fatalf("no findings issue:\n%s", out) } // The scan sets no commit status. sha := strings.TrimSpace(mustGit(t, dir, env, "rev-parse", "HEAD")) if out, _, _ := inst.ssh(t, aliceKey, "", "status", "list", "alice/app", sha); strings.Contains(out, "gitbay-security") { t.Fatalf("scan set a status:\n%s", out) } }