Commit eb2ad4cd3e

eb2ad4cd3e4564084c29300be23307a648e950ca

parent: 7f5e83a69e

Verified · cmc ci/build: success ci/quality: success ci/test: success

cmc <hello@cleberg.net> · 2026-11-05 10:27 UTC

Code scanning frequency per repository, within an instance range

[security] takes most_often, least_often, default (daily, weekly,
monthly) and hour (UTC) in place of the schedule cron, which is now
refused at load. Weekly scans run on Sunday, monthly on the 1st.

repo settings security-frequency sets a repository's frequency within
the range and moves its next scan; a choice outside a narrowed range
scans at the nearest allowed. repo security status reports the
frequency, and the settings page has a dropdown.

Next runs are computed in UTC; the cron used the caller's zone.

Closes #346

Layout: unified · split

.gitbay/wiki/Admin.org +10 −2
@@ -462,8 +462,16 @@ The instance's code scan, which a repository admin turns on with
462 runner host that serves a scanned repository. Build it from 462 runner host that serves a scanned repository. Build it from
463 =deploy/Containerfile.security= (Opengrep and its rules, both pinned 463 =deploy/Containerfile.security= (Opengrep and its rules, both pinned
464 by sha256); the file has the commands. 464 by sha256); the file has the commands.
465- =schedule= ("0 4 * * *") — when each enabled repository's default 465- =most_often= ("daily") and =least_often= ("monthly") — the range of
466 branch is scanned. =repo security run= queues one now. 466 frequencies a repository may choose with =repo settings
467 security-frequency=, from =daily=, =weekly= (Sunday) and =monthly=
468 (the 1st). A repository whose choice falls outside a narrowed range
469 scans at the nearest frequency in it, from its next scan on.
470- =default= ("daily") — the frequency of a repository that has not
471 chosen one; within the range.
472- =hour= (4) — the hour, UTC, every scan is due. =repo security run=
473 queues one now.
474- =schedule= is gone; a config that sets it is refused at load.
467- =steps= — shell steps that write SARIF into =$GITBAY_RESULTS=. The 475- =steps= — shell steps that write SARIF into =$GITBAY_RESULTS=. The
468 default runs Opengrep with the rules in the image at 476 default runs Opengrep with the rules in the image at
469 =/opt/opengrep-rules=. 477 =/opt/opengrep-rules=.
.gitbay/wiki/Parity.org +1
@@ -251,6 +251,7 @@ there shows the app's rendering rather than the one the web page shows.
251| code scan now | yes | no | yes | 251| code scan now | yes | no | yes |
252| code scan dismiss, reopen | yes | yes | no | 252| code scan dismiss, reopen | yes | yes | no |
253| code scan ignores, rules | yes | yes | no | 253| code scan ignores, rules | yes | yes | no |
254| code scan frequency | yes | yes | no |
254| dependency checks on/off | yes | yes | yes | 255| dependency checks on/off | yes | yes | yes |
255| dependency status | yes | yes | yes | 256| dependency status | yes | yes | yes |
256| advisory dismiss, reopen | yes | yes | no | 257| advisory dismiss, reopen | yes | yes | no |
.gitbay/wiki/Users.org +1
@@ -250,6 +250,7 @@ gitbay repo security dismiss you/project 3f9a0c1d2e --reason false-positive # o
250gitbay repo security reopen you/project 3f9a0c1d2e 250gitbay repo security reopen you/project 3f9a0c1d2e
251gitbay repo settings security-ignore you/project 'testdata/**' # paths set aside from the scan 251gitbay repo settings security-ignore you/project 'testdata/**' # paths set aside from the scan
252gitbay repo settings security-exclude-rule you/project swift.lang.crypto.insecure-random 252gitbay repo settings security-exclude-rule you/project swift.lang.crypto.insecure-random
253gitbay repo settings security-frequency you/project weekly # daily, weekly or monthly, as the instance allows
253gitbay repo settings git-daemon you/project on # expose over git:// 254gitbay repo settings git-daemon you/project on # expose over git://
254gitbay repo topics add you/project cli forge # free-form tags, shown on the web 255gitbay repo topics add you/project cli forge # free-form tags, shown on the web
255gitbay repo search forge # find repos by name/description/topic 256gitbay repo search forge # find repos by name/description/topic
cmd/gitbay/main.go +1
@@ -651,6 +651,7 @@ func repoCmd() *cobra.Command {
651 pass("secret-scan-allow", passOpts{server: []string{"repo", "settings", "secret-scan-allow"}, needsRepo: true}), 651 pass("secret-scan-allow", passOpts{server: []string{"repo", "settings", "secret-scan-allow"}, needsRepo: true}),
652 pass("security-ignore", passOpts{server: []string{"repo", "settings", "security-ignore"}, needsRepo: true}), 652 pass("security-ignore", passOpts{server: []string{"repo", "settings", "security-ignore"}, needsRepo: true}),
653 pass("security-exclude-rule", passOpts{server: []string{"repo", "settings", "security-exclude-rule"}, needsRepo: true}), 653 pass("security-exclude-rule", passOpts{server: []string{"repo", "settings", "security-exclude-rule"}, needsRepo: true}),
654 pass("security-frequency", passOpts{server: []string{"repo", "settings", "security-frequency"}, needsRepo: true}),
654 pass("require-mr", passOpts{server: []string{"repo", "settings", "require-mr"}, needsRepo: true}), 655 pass("require-mr", passOpts{server: []string{"repo", "settings", "require-mr"}, needsRepo: true}),
655 pass("description", passOpts{server: []string{"repo", "settings", "description"}, needsRepo: true}), 656 pass("description", passOpts{server: []string{"repo", "settings", "description"}, needsRepo: true}),
656 pass("website", passOpts{server: []string{"repo", "settings", "website"}, needsRepo: true}), 657 pass("website", passOpts{server: []string{"repo", "settings", "website"}, needsRepo: true}),
cmd/gitbay/summaries_gen.go +1
@@ -220,6 +220,7 @@ var summaries = map[string]string{
220 "repo settings secret-scan": "scan pushes for credentials: off, warn the pusher, or block the push", 220 "repo settings secret-scan": "scan pushes for credentials: off, warn the pusher, or block the push",
221 "repo settings secret-scan-allow": "paths whose secret scan findings are ignored", 221 "repo settings secret-scan-allow": "paths whose secret scan findings are ignored",
222 "repo settings security-exclude-rule": "code scanning rules, or families of them, whose findings are set aside", 222 "repo settings security-exclude-rule": "code scanning rules, or families of them, whose findings are set aside",
223 "repo settings security-frequency": "how often the code scan runs, within what the instance allows",
223 "repo settings security-ignore": "paths whose code scanning findings are set aside", 224 "repo settings security-ignore": "paths whose code scanning findings are set aside",
224 "repo settings show": "show settings", 225 "repo settings show": "show settings",
225 "repo settings unprotect": "unprotect a branch", 226 "repo settings unprotect": "unprotect a branch",
internal/ci/sched.go +8 −1
@@ -31,6 +31,13 @@ func NextRun(expr string, after time.Time) string {
31 return isoNow(n) 31 return isoNow(n)
32} 32}
33 33
34// SecurityNextRun is when a repository that chose freq is next scanned:
35// at the configured hour, daily, on Sunday, or on the 1st.
36func SecurityNextRun(sec config.Security, freq string, after time.Time) string {
37 field := map[string]string{"daily": "* * *", "weekly": "* * 0", "monthly": "1 * *"}[sec.Frequency(freq)]
38 return NextRun(fmt.Sprintf("0 %d %s", sec.Hour, field), after.UTC())
39}
40
34// Scheduler fires scheduled builds. repoDir maps a repo to its bare path. 41// Scheduler fires scheduled builds. repoDir maps a repo to its bare path.
35// Security is the instance's code scanning job; with no image it never 42// Security is the instance's code scanning job; with no image it never
36// queues. 43// queues.
@@ -139,7 +146,7 @@ func (s *Scheduler) runSecurityDue(now time.Time) {
139 return 146 return
140 } 147 }
141 for _, repo := range due { 148 for _, repo := range due {
142 s.St.SetSecurityNext(repo.ID, NextRun(s.Security.Schedule, now)) 149 s.St.SetSecurityNext(repo.ID, SecurityNextRun(s.Security, repo.Settings.SecurityFrequency, now))
143 if _, err := QueueSecurityScan(s.St, s.Security, s.RepoDir(repo.OwnerName, repo.Name), repo); err != nil { 150 if _, err := QueueSecurityScan(s.St, s.Security, s.RepoDir(repo.OwnerName, repo.Name), repo); err != nil {
144 slog.Warn("scheduler: queueing security scan", "repo", repo.Path(), "err", err) 151 slog.Warn("scheduler: queueing security scan", "repo", repo.Path(), "err", err)
145 } 152 }
internal/ci/sched_test.go +23 −1
@@ -177,7 +177,7 @@ func TestSchedulerQueuesSecurityScan(t *testing.T) {
177 t.Fatalf("queued with no image: %+v", builds) 177 t.Fatalf("queued with no image: %+v", builds)
178 } 178 }
179 179
180 s.Security = config.Security{Image: "localhost/gitbay-security:1", Schedule: "0 4 * * *", Steps: []string{"true"}} 180 s.Security = config.Security{Image: "localhost/gitbay-security:1", Hour: 4, MostOften: "daily", LeastOften: "monthly", Default: "daily", Steps: []string{"true"}}
181 st.SetSecurityNext(repoID, now.Add(-time.Minute).UTC().Format("2006-01-02T15:04:05Z")) 181 st.SetSecurityNext(repoID, now.Add(-time.Minute).UTC().Format("2006-01-02T15:04:05Z"))
182 s.RunDue(now) 182 s.RunDue(now)
183 builds, _ := st.ListBuilds(repoID, store.BuildFilter{}, 10) 183 builds, _ := st.ListBuilds(repoID, store.BuildFilter{}, 10)
@@ -204,3 +204,25 @@ func TestSecurityJobNameIsReserved(t *testing.T) {
204 t.Fatalf("Parse = %v", err) 204 t.Fatalf("Parse = %v", err)
205 } 205 }
206} 206}
207
208func TestSecurityNextRun(t *testing.T) {
209 sec := config.Default().Security
210 sec.Hour = 6
211 // Wednesday 2026-11-04 10:00 UTC.
212 after := time.Date(2026, 11, 4, 10, 0, 0, 0, time.UTC).In(time.FixedZone("UTC-6", -6*3600))
213 for chosen, want := range map[string]string{
214 "": "2026-11-05T06:00:00Z",
215 "daily": "2026-11-05T06:00:00Z",
216 "weekly": "2026-11-08T06:00:00Z",
217 "monthly": "2026-12-01T06:00:00Z",
218 } {
219 if got := SecurityNextRun(sec, chosen, after); got != want {
220 t.Errorf("%q: %s, want %s", chosen, got, want)
221 }
222 }
223 // A choice the instance no longer allows scans at the nearest allowed.
224 sec.LeastOften, sec.Default = "weekly", "weekly"
225 if got := SecurityNextRun(sec, "monthly", after); got != "2026-11-08T06:00:00Z" {
226 t.Errorf("monthly outside the range: %s", got)
227 }
228}
internal/config/config.go +58 −8
@@ -12,6 +12,7 @@ import (
12 "net" 12 "net"
13 "os" 13 "os"
14 "path/filepath" 14 "path/filepath"
15 "slices"
15 "strconv" 16 "strconv"
16 "strings" 17 "strings"
17 "time" 18 "time"
@@ -231,11 +232,46 @@ type Deps struct {
231// enable` schedules. It is unavailable until an operator names Image, an 232// enable` schedules. It is unavailable until an operator names Image, an
232// image built from deploy/Containerfile.security and provisioned on the 233// image built from deploy/Containerfile.security and provisioned on the
233// runner hosts. Steps write SARIF into $GITBAY_RESULTS; the runner uploads 234// runner hosts. Steps write SARIF into $GITBAY_RESULTS; the runner uploads
234// it when they succeed. 235// it when they succeed. Repositories scan at a frequency between MostOften
236// and LeastOften, Default unless they choose one, at Hour UTC.
235type Security struct { 237type Security struct {
236 Image string `toml:"image"` 238 Image string `toml:"image"`
237 Schedule string `toml:"schedule"` 239 Hour int `toml:"hour"`
238 Steps []string `toml:"steps"` 240 MostOften string `toml:"most_often"`
241 LeastOften string `toml:"least_often"`
242 Default string `toml:"default"`
243 Steps []string `toml:"steps"`
244}
245
246// ScanFrequencies are the code scan frequencies, most often first. Weekly
247// scans run on Sunday, monthly ones on the 1st.
248var ScanFrequencies = []string{"daily", "weekly", "monthly"}
249
250// Frequencies returns the frequencies a repository may choose, most often
251// first.
252func (s Security) Frequencies() []string {
253 lo, hi := s.bounds()
254 return ScanFrequencies[lo : hi+1]
255}
256
257// Frequency is the frequency a repository scans at: its choice, or the
258// default, moved to the nearest one the instance allows.
259func (s Security) Frequency(chosen string) string {
260 if chosen == "" {
261 chosen = s.Default
262 }
263 lo, hi := s.bounds()
264 return ScanFrequencies[max(lo, min(slices.Index(ScanFrequencies, chosen), hi))]
265}
266
267// bounds are the indexes of MostOften and LeastOften in ScanFrequencies,
268// or the whole list when they are unset or out of order.
269func (s Security) bounds() (int, int) {
270 lo, hi := slices.Index(ScanFrequencies, s.MostOften), slices.Index(ScanFrequencies, s.LeastOften)
271 if lo < 0 || hi < lo {
272 return 0, len(ScanFrequencies) - 1
273 }
274 return lo, hi
239} 275}
240 276
241// DefaultSecuritySteps run Opengrep with the rules the image carries. 277// DefaultSecuritySteps run Opengrep with the rules the image carries.
@@ -591,7 +627,8 @@ func Default() Config {
591 GitDaemon: GitDaemon{Port: 9418}, 627 GitDaemon: GitDaemon{Port: 9418},
592 Mirrors: Mirrors{PullIntervalMinutes: 15}, 628 Mirrors: Mirrors{PullIntervalMinutes: 15},
593 Deps: Deps{CheckIntervalHours: 24}, 629 Deps: Deps{CheckIntervalHours: 24},
594 Security: Security{Schedule: "0 4 * * *", Steps: DefaultSecuritySteps}, 630 Security: Security{Hour: 4, MostOften: "daily", LeastOften: "monthly", Default: "daily",
631 Steps: DefaultSecuritySteps},
595 Limits: Limits{ 632 Limits: Limits{
596 MaxPackBytes: 2 << 30, // 2 GiB 633 MaxPackBytes: 2 << 30, // 2 GiB
597 MaxBlobBytes: 100 << 20, 634 MaxBlobBytes: 100 << 20,
@@ -612,6 +649,9 @@ func Load(path string) (Config, error) {
612 if err != nil { 649 if err != nil {
613 return cfg, err 650 return cfg, err
614 } 651 }
652 if md.IsDefined("security", "schedule") {
653 return cfg, errors.New("security.schedule is replaced by security.hour, most_often, least_often and default")
654 }
615 if u := md.Undecoded(); len(u) > 0 { 655 if u := md.Undecoded(); len(u) > 0 {
616 return cfg, fmt.Errorf("unknown config key %q", u[0].String()) 656 return cfg, fmt.Errorf("unknown config key %q", u[0].String())
617 } 657 }
@@ -675,10 +715,20 @@ func (c Config) Validate() error {
675 if c.Server.Root == "" { 715 if c.Server.Root == "" {
676 errs = append(errs, errors.New("server.root is required")) 716 errs = append(errs, errors.New("server.root is required"))
677 } 717 }
718 sec := c.Security
719 if sec.Hour < 0 || sec.Hour > 23 {
720 errs = append(errs, fmt.Errorf("security.hour must be 0 to 23, got %d", sec.Hour))
721 }
722 if err := oneOf("security.most_often", sec.MostOften, ScanFrequencies...); err != nil {
723 errs = append(errs, err)
724 } else if err := oneOf("security.least_often", sec.LeastOften, ScanFrequencies...); err != nil {
725 errs = append(errs, err)
726 } else if slices.Index(ScanFrequencies, sec.MostOften) > slices.Index(ScanFrequencies, sec.LeastOften) {
727 errs = append(errs, fmt.Errorf("security.most_often %q is less often than security.least_often %q", sec.MostOften, sec.LeastOften))
728 } else if !slices.Contains(sec.Frequencies(), sec.Default) {
729 errs = append(errs, fmt.Errorf("security.default must be one of %v, got %q", sec.Frequencies(), sec.Default))
730 }
678 if c.Security.Image != "" { 731 if c.Security.Image != "" {
679 if len(strings.Fields(c.Security.Schedule)) != 5 {
680 errs = append(errs, errors.New("security.schedule must be a five-field cron expression"))
681 }
682 if len(c.Security.Steps) == 0 { 732 if len(c.Security.Steps) == 0 {
683 errs = append(errs, errors.New("security.steps must name at least one step when security.image is set")) 733 errs = append(errs, errors.New("security.steps must name at least one step when security.image is set"))
684 } 734 }
internal/config/config_test.go +35
@@ -519,3 +519,38 @@ func TestMailInboundPassword(t *testing.T) {
519 t.Fatal("two lines accepted") 519 t.Fatal("two lines accepted")
520 } 520 }
521} 521}
522
523func TestSecurityFrequencies(t *testing.T) {
524 cfg, err := Load(writeConfig(t, minimal))
525 if err != nil {
526 t.Fatal(err)
527 }
528 if s := cfg.Security; s.Hour != 4 || s.Default != "daily" || strings.Join(s.Frequencies(), ",") != "daily,weekly,monthly" {
529 t.Fatalf("defaults: %+v %v", s, s.Frequencies())
530 }
531 cfg, err = Load(writeConfig(t, minimal+"\n[security]\nhour = 0\nmost_often = \"weekly\"\ndefault = \"weekly\"\n"))
532 if err != nil {
533 t.Fatal(err)
534 }
535 s := cfg.Security
536 if s.Hour != 0 || strings.Join(s.Frequencies(), ",") != "weekly,monthly" {
537 t.Fatalf("narrowed: %+v %v", s, s.Frequencies())
538 }
539 for chosen, want := range map[string]string{"": "weekly", "daily": "weekly", "weekly": "weekly", "monthly": "monthly"} {
540 if got := s.Frequency(chosen); got != want {
541 t.Errorf("Frequency(%q) = %q, want %q", chosen, got, want)
542 }
543 }
544 for body, want := range map[string]string{
545 "schedule = \"0 4 * * *\"\n": "security.schedule",
546 "hour = 24\n": "security.hour",
547 "most_often = \"hourly\"\n": "security.most_often",
548 "least_often = \"yearly\"\n": "security.least_often",
549 "most_often = \"monthly\"\nleast_often = \"weekly\"\n": "security.most_often",
550 "least_often = \"weekly\"\ndefault = \"monthly\"\n": "security.default",
551 } {
552 if _, err := Load(writeConfig(t, minimal+"\n[security]\n"+body)); err == nil || !strings.Contains(err.Error(), want) {
553 t.Errorf("want %q, got %v\n%s", want, err, body)
554 }
555 }
556}
internal/control/security.go +45 −2
@@ -15,6 +15,7 @@ import (
15 "gitbay.org/gitbay/internal/botissue" 15 "gitbay.org/gitbay/internal/botissue"
16 "gitbay.org/gitbay/internal/ci" 16 "gitbay.org/gitbay/internal/ci"
17 "gitbay.org/gitbay/internal/codescan" 17 "gitbay.org/gitbay/internal/codescan"
18 "gitbay.org/gitbay/internal/config"
18 "gitbay.org/gitbay/internal/deps" 19 "gitbay.org/gitbay/internal/deps"
19 "gitbay.org/gitbay/internal/gitutil" 20 "gitbay.org/gitbay/internal/gitutil"
20 "gitbay.org/gitbay/internal/policy" 21 "gitbay.org/gitbay/internal/policy"
@@ -70,6 +71,11 @@ func init() {
70 Usage: "repo settings security-exclude-rule <owner/name> [<rule>...] (none clears the list)", 71 Usage: "repo settings security-exclude-rule <owner/name> [<rule>...] (none clears the list)",
71 Examples: []string{"repo settings security-exclude-rule krz/gitbay swift.lang.crypto.insecure-random"}, 72 Examples: []string{"repo settings security-exclude-rule krz/gitbay swift.lang.crypto.insecure-random"},
72 Run: runSecurityExcludeRule}) 73 Run: runSecurityExcludeRule})
74 register(Command{Path: []string{"repo", "settings", "security-frequency"},
75 Summary: "how often the code scan runs, within what the instance allows",
76 Usage: "repo settings security-frequency <owner/name> daily|weekly|monthly",
77 Examples: []string{"repo settings security-frequency krz/gitbay weekly"},
78 Run: runSecurityFrequency})
73 register(Command{Path: []string{"runner", "results"}, 79 register(Command{Path: []string{"runner", "results"},
74 Summary: "upload a code scan's SARIF from stdin", 80 Summary: "upload a code scan's SARIF from stdin",
75 Usage: "runner results <build-id>", 81 Usage: "runner results <build-id>",
@@ -81,6 +87,7 @@ func init() {
81// findings, the ones dismissed, and how many it set aside. 87// findings, the ones dismissed, and how many it set aside.
82type SecurityOut struct { 88type SecurityOut struct {
83 Enabled bool `json:"enabled"` 89 Enabled bool `json:"enabled"`
90 Frequency string `json:"frequency,omitempty"`
84 NextRun string `json:"next_run,omitempty"` 91 NextRun string `json:"next_run,omitempty"`
85 LastResults string `json:"last_results,omitempty"` 92 LastResults string `json:"last_results,omitempty"`
86 IssueNumber int64 `json:"issue_number,omitempty"` 93 IssueNumber int64 `json:"issue_number,omitempty"`
@@ -116,7 +123,7 @@ func runSecurityEnable(c *Ctx, args []string) int {
116 if c.Cfg.Security.Image == "" { 123 if c.Cfg.Security.Image == "" {
117 return c.fail(protocol.ExitFailure, "this instance has no code scanning image; an operator sets [security] image") 124 return c.fail(protocol.ExitFailure, "this instance has no code scanning image; an operator sets [security] image")
118 } 125 }
119 next := ci.NextRun(c.Cfg.Security.Schedule, time.Now()) 126 next := ci.SecurityNextRun(c.Cfg.Security, repo.Settings.SecurityFrequency, time.Now())
120 if err := c.Store.EnableSecurityScan(repo.ID, next); err != nil { 127 if err := c.Store.EnableSecurityScan(repo.ID, next); err != nil {
121 return c.fail(protocol.ExitFailure, "%v", err) 128 return c.fail(protocol.ExitFailure, "%v", err)
122 } 129 }
@@ -196,7 +203,8 @@ func runSecurityStatus(c *Ctx, args []string) int {
196 if err != nil { 203 if err != nil {
197 return c.fail(protocol.ExitFailure, "%v", err) 204 return c.fail(protocol.ExitFailure, "%v", err)
198 } 205 }
199 out := SecurityOut{Enabled: true, NextRun: scan.NextRun, LastResults: scan.LastResults, 206 freq := c.Cfg.Security.Frequency(repo.Settings.SecurityFrequency)
207 out := SecurityOut{Enabled: true, Frequency: freq, NextRun: scan.NextRun, LastResults: scan.LastResults,
200 IssueNumber: scan.IssueNumber, Findings: []SecurityFinding{}, Dismissed: []SecurityFinding{}} 208 IssueNumber: scan.IssueNumber, Findings: []SecurityFinding{}, Dismissed: []SecurityFinding{}}
201 json.Unmarshal([]byte(scan.Aside), &out.Aside) 209 json.Unmarshal([]byte(scan.Aside), &out.Aside)
202 for _, f := range codescan.ByPlace(reported) { 210 for _, f := range codescan.ByPlace(reported) {
@@ -217,6 +225,7 @@ func runSecurityStatus(c *Ctx, args []string) int {
217 v := c.view(w) 225 v := c.view(w)
218 v.fields( 226 v.fields(
219 "scanning", "on", 227 "scanning", "on",
228 "frequency", freq,
220 "next scan", scan.NextRun, 229 "next scan", scan.NextRun,
221 "last results", c.when(scan.LastResults), 230 "last results", c.when(scan.LastResults),
222 "tracked in", tracked, 231 "tracked in", tracked,
@@ -241,6 +250,7 @@ func runSecurityStatus(c *Ctx, args []string) int {
241 }, func() screen { 250 }, func() screen {
242 s := screen{fields: []field{ 251 s := screen{fields: []field{
243 {"Scanning", []cell{cGlyph("ok"), cText("on")}}, 252 {"Scanning", []cell{cGlyph("ok"), cText("on")}},
253 {"Frequency", []cell{cText(freq)}},
244 {"Next scan", []cell{cText(scan.NextRun)}}, 254 {"Next scan", []cell{cText(scan.NextRun)}},
245 {"Last results", []cell{cAge(scan.LastResults)}}, 255 {"Last results", []cell{cAge(scan.LastResults)}},
246 }} 256 }}
@@ -568,6 +578,39 @@ func resolveFingerprint(findings []store.SecurityFinding, given string) (string,
568 return match, "" 578 return match, ""
569} 579}
570 580
581// runSecurityFrequency sets how often the repository is scanned and, when
582// scanning is on, moves the next scan to match.
583func runSecurityFrequency(c *Ctx, args []string) int {
584 if len(args) != 2 || !slices.Contains(config.ScanFrequencies, args[1]) {
585 return c.usage()
586 }
587 freq := args[1]
588 repo, code := resolveRepo(c, args[0], policy.CanAdmin)
589 if code >= 0 {
590 return code
591 }
592 if allowed := c.Cfg.Security.Frequencies(); !slices.Contains(allowed, freq) {
593 return c.fail(protocol.ExitFailure, "this instance scans %s", strings.Join(allowed, ", "))
594 }
595 if _, err := c.Store.UpdateRepoSettings(repo.ID, func(s *store.RepoSettings) { s.SecurityFrequency = freq }); err != nil {
596 return c.fail(protocol.ExitFailure, "%v", err)
597 }
598 next := ""
599 if _, err := c.Store.SecurityScanFor(repo.ID); err == nil {
600 next = ci.SecurityNextRun(c.Cfg.Security, freq, time.Now())
601 if err := c.Store.SetSecurityNext(repo.ID, next); err != nil {
602 return c.fail(protocol.ExitFailure, "%v", err)
603 }
604 }
605 return c.emit(map[string]any{"frequency": freq, "next_run": next}, func(w io.Writer) {
606 if next == "" {
607 fmt.Fprintf(w, "%s scans %s once scanning is on\n", repo.Path(), freq)
608 return
609 }
610 fmt.Fprintf(w, "%s scans %s; next scan %s\n", repo.Path(), freq, next)
611 })
612}
613
571// maxSecurityList bounds the ignore and exclude-rule settings. 614// maxSecurityList bounds the ignore and exclude-rule settings.
572const maxSecurityList = 50 615const maxSecurityList = 50
573 616
internal/control/security_test.go +52 −2
@@ -7,6 +7,7 @@ import (
7 "path/filepath" 7 "path/filepath"
8 "strings" 8 "strings"
9 "testing" 9 "testing"
10 "time"
10 11
11 "gitbay.org/gitbay/internal/ci" 12 "gitbay.org/gitbay/internal/ci"
12 "gitbay.org/gitbay/internal/codescan" 13 "gitbay.org/gitbay/internal/codescan"
@@ -24,7 +25,8 @@ const testSARIF = `{"version":"2.1.0","runs":[{"tool":{"driver":{"name":"opengre
24func TestSecurityScanRoundTrip(t *testing.T) { 25func TestSecurityScanRoundTrip(t *testing.T) {
25 st, repo, u, root := browseFixture(t) 26 st, repo, u, root := browseFixture(t)
26 p := repo.Path() 27 p := repo.Path()
27 sec := config.Security{Image: "localhost/gitbay-security:1", Schedule: "0 4 * * *", Steps: config.DefaultSecuritySteps} 28 sec := config.Default().Security
29 sec.Image = "localhost/gitbay-security:1"
28 30
29 c, out := runnerCtx(st, u.ID, root) 31 c, out := runnerCtx(st, u.ID, root)
30 if code := runSecurityEnable(c, []string{p}); code != protocol.ExitFailure || !strings.Contains(out.String(), "no code scanning image") { 32 if code := runSecurityEnable(c, []string{p}); code != protocol.ExitFailure || !strings.Contains(out.String(), "no code scanning image") {
@@ -202,7 +204,8 @@ const twoFindings = `{"version":"2.1.0","runs":[{"tool":{"driver":{"name":"openg
202func TestSecurityDismiss(t *testing.T) { 204func TestSecurityDismiss(t *testing.T) {
203 st, repo, u, root := browseFixture(t) 205 st, repo, u, root := browseFixture(t)
204 p := repo.Path() 206 p := repo.Path()
205 sec := config.Security{Image: "localhost/gitbay-security:1", Schedule: "0 4 * * *", Steps: config.DefaultSecuritySteps} 207 sec := config.Default().Security
208 sec.Image = "localhost/gitbay-security:1"
206 run := func(f func(*Ctx, []string) int, stdin string, args ...string) (int, string) { 209 run := func(f func(*Ctx, []string) int, stdin string, args ...string) (int, string) {
207 t.Helper() 210 t.Helper()
208 c, out := runnerCtx(st, u.ID, root) 211 c, out := runnerCtx(st, u.ID, root)
@@ -310,3 +313,50 @@ func TestDefaultBranchRefusesOptionShape(t *testing.T) {
310 t.Errorf("a normal branch: exit %d: %s", code, out) 313 t.Errorf("a normal branch: exit %d: %s", code, out)
311 } 314 }
312} 315}
316
317func TestSecurityFrequency(t *testing.T) {
318 st, repo, u, root := browseFixture(t)
319 p := repo.Path()
320 sec := config.Default().Security
321 sec.Image = "localhost/gitbay-security:1"
322 run := func(f func(*Ctx, []string) int, args ...string) (int, string) {
323 t.Helper()
324 c, out := runnerCtx(st, u.ID, root)
325 c.Cfg.Security = sec
326 c.JSON = true
327 return f(c, args), out.String()
328 }
329 // Before scanning is on the choice is kept and nothing is scheduled.
330 if code, out := run(runSecurityFrequency, p, "monthly"); code != protocol.ExitOK || !strings.Contains(out, `"next_run":""`) {
331 t.Fatalf("monthly while off: %d %s", code, out)
332 }
333 run(runSecurityEnable, p)
334 if sc, _ := st.SecurityScanFor(repo.ID); !strings.Contains(sc.NextRun, "-01T04:00:00Z") {
335 t.Fatalf("enable ignored the monthly choice: %q", sc.NextRun)
336 }
337 code, out := run(runSecurityFrequency, p, "weekly")
338 if code != protocol.ExitOK {
339 t.Fatalf("weekly: %d %s", code, out)
340 }
341 sc, _ := st.SecurityScanFor(repo.ID)
342 next, err := time.Parse("2006-01-02T15:04:05Z", sc.NextRun)
343 if err != nil || next.Weekday() != time.Sunday || next.Hour() != 4 || !strings.Contains(out, sc.NextRun) {
344 t.Fatalf("next run %q (%v), out %s", sc.NextRun, err, out)
345 }
346 if _, out := run(runSecurityStatus, p); !strings.Contains(out, `"frequency":"weekly"`) {
347 t.Fatalf("status: %s", out)
348 }
349 if code, _ := run(runSecurityFrequency, p, "hourly"); code != protocol.ExitUsage {
350 t.Fatalf("hourly: exit %d", code)
351 }
352 // Narrowed: daily is refused, and the stored monthly scans weekly.
353 sec.LeastOften, sec.Default, sec.MostOften = "weekly", "weekly", "weekly"
354 if code, out := run(runSecurityFrequency, p, "daily"); code != protocol.ExitFailure || !strings.Contains(out, "scans weekly") {
355 t.Fatalf("daily outside the range: %d %s", code, out)
356 }
357 sec.MostOften, sec.LeastOften = "daily", "daily"
358 sec.Default = "daily"
359 if _, out := run(runSecurityStatus, p); !strings.Contains(out, `"frequency":"daily"`) {
360 t.Fatalf("weekly outside the range: %s", out)
361 }
362}
internal/httpd/depspage_test.go +25
@@ -75,6 +75,31 @@ func TestSettingsPageRendersSecurityTriage(t *testing.T) {
75 } 75 }
76} 76}
77 77
78// The frequency dropdown offers what the instance allows, with the
79// repository's frequency selected.
80func TestSettingsPageRendersScanFrequency(t *testing.T) {
81 var sb strings.Builder
82 err := web.Render(&sb, "settings.html", settingsPage{
83 repoPage: testRepoPage(), ScanAvailable: true,
84 ScanFrequencies: []string{"weekly", "monthly"}, ScanFrequency: "weekly", ScanHour: 4,
85 })
86 if err != nil {
87 t.Fatal(err)
88 }
89 page := sb.String()
90 for _, want := range []string{
91 `value="security-frequency"`, `<option value="weekly" selected>weekly</option>`,
92 `<option value="monthly">monthly</option>`, "04:00 UTC",
93 } {
94 if !strings.Contains(page, want) {
95 t.Errorf("settings page does not show %q", want)
96 }
97 }
98 if strings.Contains(page, `value="daily"`) {
99 t.Error("offers a frequency the instance does not allow")
100 }
101}
102
78// The advisories offer dismissal, the dismissed ones reopening, and the 103// The advisories offer dismissal, the dismissed ones reopening, and the
79// ones nothing imports are named. 104// ones nothing imports are named.
80func TestSettingsPageRendersAdvisoryTriage(t *testing.T) { 105func TestSettingsPageRendersAdvisoryTriage(t *testing.T) {
internal/httpd/settings.go +20 −10
@@ -26,14 +26,19 @@ type settingsPage struct {
26 // ScanAvailable is whether the instance has a code scanning image; 26 // ScanAvailable is whether the instance has a code scanning image;
27 // without one the toggle would only ever be refused. 27 // without one the toggle would only ever be refused.
28 ScanAvailable bool 28 ScanAvailable bool
29 Runners []store.RepoRunner 29 // ScanFrequencies are the frequencies the instance allows, ScanFrequency
30 Access []accessRow 30 // the one this repository scans at, and ScanHour the hour UTC.
31 Hooks []hookRow 31 ScanFrequencies []string
32 Deliveries []deliveryRow 32 ScanFrequency string
33 Notice string 33 ScanHour int
34 Saved bool 34 Runners []store.RepoRunner
35 Reauth bool // Notice is the stale-session refusal: link to sign in 35 Access []accessRow
36 Submitted map[string]string 36 Hooks []hookRow
37 Deliveries []deliveryRow
38 Notice string
39 Saved bool
40 Reauth bool // Notice is the stale-session refusal: link to sign in
41 Submitted map[string]string
37} 42}
38 43
39type accessRow struct { 44type accessRow struct {
@@ -113,8 +118,11 @@ func (s *Server) settingsFormWith(w http.ResponseWriter, r *http.Request, u stor
113 repoPage: p, Topics: topics, Branches: branches, 118 repoPage: p, Topics: topics, Branches: branches,
114 DepsEnabled: deps.Enabled, Deps: deps, 119 DepsEnabled: deps.Enabled, Deps: deps,
115 Security: security, ScanAvailable: s.cfg.Security.Image != "", 120 Security: security, ScanAvailable: s.cfg.Security.Image != "",
116 Runners: runners, 121 ScanFrequencies: s.cfg.Security.Frequencies(),
117 Access: access, Hooks: hooks, Deliveries: deliveries, 122 ScanFrequency: s.cfg.Security.Frequency(repo.Settings.SecurityFrequency),
123 ScanHour: s.cfg.Security.Hour,
124 Runners: runners,
125 Access: access, Hooks: hooks, Deliveries: deliveries,
118 Notice: notice, 126 Notice: notice,
119 Saved: strings.HasPrefix(notice, "Saved "), 127 Saved: strings.HasPrefix(notice, "Saved "),
120 Reauth: s.reauthNotice(w, notice, r.URL.Path), 128 Reauth: s.reauthNotice(w, notice, r.URL.Path),
@@ -169,6 +177,8 @@ func (s *Server) settingsSubmit(w http.ResponseWriter, r *http.Request, u store.
169 verb = "enable" 177 verb = "enable"
170 } 178 }
171 argv = []string{"repo", "security", verb, repo} 179 argv = []string{"repo", "security", verb, repo}
180 case "security-frequency":
181 argv = []string{"repo", "settings", "security-frequency", repo, v("security-frequency")}
172 case "deps-dismiss": 182 case "deps-dismiss":
173 argv = []string{"repo", "deps", "dismiss", repo, v("advisory"), "--reason", v("reason")} 183 argv = []string{"repo", "deps", "dismiss", repo, v("advisory"), "--reason", v("reason")}
174 case "deps-reopen": 184 case "deps-reopen":
internal/store/repos.go +3
@@ -46,6 +46,9 @@ type RepoSettings struct {
46 // or families, whose code scanning findings are set aside. 46 // or families, whose code scanning findings are set aside.
47 SecurityIgnore []string `json:"security_ignore,omitempty"` 47 SecurityIgnore []string `json:"security_ignore,omitempty"`
48 SecurityExcludeRules []string `json:"security_exclude_rules,omitempty"` 48 SecurityExcludeRules []string `json:"security_exclude_rules,omitempty"`
49 // SecurityFrequency is the code scan frequency chosen, empty for the
50 // instance default.
51 SecurityFrequency string `json:"security_frequency,omitempty"`
49} 52}
50 53
51// Path returns the canonical owner/name form. 54// Path returns the canonical owner/name form.
internal/web/templates/settings.html +6
@@ -180,6 +180,12 @@
180 <div class="check"><input type="checkbox" id="security-enabled" name="security" value="on"{{if .Security.Enabled}} checked{{end}}></div> 180 <div class="check"><input type="checkbox" id="security-enabled" name="security" value="on"{{if .Security.Enabled}} checked{{end}}></div>
181 <div><button type="submit" class="btn">Save</button></div> 181 <div><button type="submit" class="btn">Save</button></div>
182</form> 182</form>
183{{if .ScanAvailable}}<form method="post" action="{{$base}}" class="setform">
184 <input type="hidden" name="field" value="security-frequency">
185 <div><label for="security-frequency">Frequency</label><p class="hint">Daily, weekly on Sunday, or monthly on the 1st, at {{printf "%02d:00" .ScanHour}} UTC.</p></div>
186 <div><select id="security-frequency" name="security-frequency">{{$freq := .ScanFrequency}}{{range .ScanFrequencies}}<option value="{{.}}"{{if eq . $freq}} selected{{end}}>{{.}}</option>{{end}}</select></div>
187 <div><button type="submit" class="btn">Save</button></div>
188</form>{{end}}
183{{if .Security.Enabled}} 189{{if .Security.Enabled}}
184<p class="meta">Next scan {{.Security.NextRun}} · last results {{if .Security.LastResults}}{{when .Security.LastResults}}{{else}}never{{end}}{{if .Security.IssueNumber}} · <a href="/{{.Repo.OwnerName}}/{{.Repo.Name}}/issues/{{.Security.IssueNumber}}">#{{.Security.IssueNumber}}</a>{{end}}</p> 190<p class="meta">Next scan {{.Security.NextRun}} · last results {{if .Security.LastResults}}{{when .Security.LastResults}}{{else}}never{{end}}{{if .Security.IssueNumber}} · <a href="/{{.Repo.OwnerName}}/{{.Repo.Name}}/issues/{{.Security.IssueNumber}}">#{{.Security.IssueNumber}}</a>{{end}}</p>
185{{with .Security.Aside}}{{if or .Ignored .Unused}}<p class="meta">Set aside by the last scan:{{if .Ignored}} {{.Ignored}} under the paths or rules below, or marked vendored or generated in <code>.gitattributes</code>.{{end}}{{range $lib, $n := .Unused}} {{$n}} from {{$lib}} rules, which no manifest names.{{end}}</p>{{end}}{{end}} 191{{with .Security.Aside}}{{if or .Ignored .Unused}}<p class="meta">Set aside by the last scan:{{if .Ignored}} {{.Ignored}} under the paths or rules below, or marked vendored or generated in <code>.gitattributes</code>.{{end}}{{range $lib, $n := .Unused}} {{$n}} from {{$lib}} rules, which no manifest names.{{end}}</p>{{end}}{{end}}