config: refuse a negative max_snippets_per_user !384

merged merged by cmc on 2026-09-12 02:31 UTC · krz/gitbay:fix-214-negative-snippet-limit into main

2 files changed, +14 −4

Layout: unified · split

internal/config/config.go +4 −4
@@ -180,8 +180,8 @@ type Limits struct {
180 // MaxSnippetsPerUser caps snippets an account may own. 0 means 180 // MaxSnippetsPerUser caps snippets an account may own. 0 means
181 // unlimited, like MaxReposPerUser. 181 // unlimited, like MaxReposPerUser.
182 MaxSnippetsPerUser int `toml:"max_snippets_per_user"` 182 MaxSnippetsPerUser int `toml:"max_snippets_per_user"`
183 CloneTimeoutSec int `toml:"clone_timeout"` 183 CloneTimeoutSec int `toml:"clone_timeout"`
184 SSHAuthRate int `toml:"ssh_auth_rate"` 184 SSHAuthRate int `toml:"ssh_auth_rate"`
185 // APIRate is sustained JSON-API requests per minute per caller; writes 185 // APIRate is sustained JSON-API requests per minute per caller; writes
186 // draw on a tenth of it. 0 uses the default. 186 // draw on a tenth of it. 0 uses the default.
187 APIRate int `toml:"api_rate"` 187 APIRate int `toml:"api_rate"`
@@ -277,8 +277,8 @@ func (c Config) Validate() error {
277 errs = append(errs, fmt.Errorf("registration.pending_expiry %q must be a positive duration such as 168h", c.Registration.PendingExpiry)) 277 errs = append(errs, fmt.Errorf("registration.pending_expiry %q must be a positive duration such as 168h", c.Registration.PendingExpiry))
278 } 278 }
279 } 279 }
280 if c.Limits.MaxReposPerUser < 0 || c.Limits.MaxBytesPerUser < 0 { 280 if c.Limits.MaxReposPerUser < 0 || c.Limits.MaxBytesPerUser < 0 || c.Limits.MaxSnippetsPerUser < 0 {
281 errs = append(errs, errors.New("limits.max_repos_per_user and max_bytes_per_user must not be negative")) 281 errs = append(errs, errors.New("limits.max_repos_per_user, max_bytes_per_user and max_snippets_per_user must not be negative"))
282 } 282 }
283 if c.SSH.Port < 1 || c.SSH.Port > 65535 { 283 if c.SSH.Port < 1 || c.SSH.Port > 65535 {
284 errs = append(errs, fmt.Errorf("ssh.port %d out of range", c.SSH.Port)) 284 errs = append(errs, fmt.Errorf("ssh.port %d out of range", c.SSH.Port))
internal/config/config_test.go +10
@@ -80,6 +80,16 @@ func TestContradictions(t *testing.T) {
80 "[server]\nroot = \"/var/lib/gitbay\"\n", 80 "[server]\nroot = \"/var/lib/gitbay\"\n",
81 "site_url", 81 "site_url",
82 }, 82 },
83 {
84 "negative repo limit",
85 minimal + "\n[limits]\nmax_repos_per_user = -1\n",
86 "must not be negative",
87 },
88 {
89 "negative snippet limit",
90 minimal + "\n[limits]\nmax_snippets_per_user = -1\n",
91 "max_snippets_per_user",
92 },
83 } 93 }
84 for _, tc := range cases { 94 for _, tc := range cases {
85 t.Run(tc.name, func(t *testing.T) { 95 t.Run(tc.name, func(t *testing.T) {