From 7336df8a0e0a9720a7d5f615c27afb7a5fd43fd2 Mon Sep 17 00:00:00 2001 From: unkin-agent Date: Sun, 25 Oct 2026 19:15:19 +1100 Subject: [PATCH] Cap sync_interval at 30 days --- internal/api/v2/remotes_ttl_test.go | 4 +++- pkg/models/remote.go | 11 +++++++---- pkg/models/remote_test.go | 2 ++ 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/internal/api/v2/remotes_ttl_test.go b/internal/api/v2/remotes_ttl_test.go index 0c55b32..233be5d 100644 --- a/internal/api/v2/remotes_ttl_test.go +++ b/internal/api/v2/remotes_ttl_test.go @@ -57,7 +57,7 @@ func TestMutableTTLOmittedVsZero(t *testing.T) { } // sync_interval round-trips through the API and DB on github_* remotes and is -// rejected when negative or on a type without a background syncer. +// rejected when negative, over the maximum, or on a type without a background syncer. func TestSyncIntervalAPI(t *testing.T) { if testDSN == "" { t.Skip("Docker unavailable") @@ -93,6 +93,8 @@ func TestSyncIntervalAPI(t *testing.T) { for _, body := range []string{ `{"name":"si-neg",` + gh + `,"sync_interval":-1}`, + `{"name":"si-max",` + gh + `,"sync_interval":2592001}`, + `{"name":"si-int32",` + gh + `,"sync_interval":2147483648}`, `{"name":"si-rpm","package_type":"rpm","base_url":"https://example.com/repo","sync_interval":60}`, } { if c := do(t, h, "POST", "/", body); c != 400 { diff --git a/pkg/models/remote.go b/pkg/models/remote.go index a230fd0..6596499 100644 --- a/pkg/models/remote.go +++ b/pkg/models/remote.go @@ -185,11 +185,14 @@ func syncPhase(name string, period time.Duration) time.Duration { return time.Duration(h.Sum64() % uint64(period)) } -// ValidateSyncInterval rejects a negative sync_interval, or one set on a type -// without a background syncer. +// MaxSyncInterval caps sync_interval at 30 days (seconds). +const MaxSyncInterval = 30 * 24 * 60 * 60 + +// ValidateSyncInterval rejects a sync_interval outside 0..MaxSyncInterval, or +// one set on a type without a background syncer. func (r *Remote) ValidateSyncInterval() error { - if r.SyncInterval < 0 { - return fmt.Errorf("sync_interval must be >= 0, got %d", r.SyncInterval) + if r.SyncInterval < 0 || r.SyncInterval > MaxSyncInterval { + return fmt.Errorf("sync_interval must be between 0 and %d, got %d", MaxSyncInterval, r.SyncInterval) } switch r.PackageType { case PackageGitHubRPM, PackageGitHubDeb, PackageGitHubAlpine: diff --git a/pkg/models/remote_test.go b/pkg/models/remote_test.go index 25dcad5..5dab0bf 100644 --- a/pkg/models/remote_test.go +++ b/pkg/models/remote_test.go @@ -153,6 +153,8 @@ func TestValidateSyncInterval(t *testing.T) { {PackageGitHubDeb, 3600, true}, {PackageGitHubAlpine, 3600, true}, {PackageGitHubRPM, -1, false}, + {PackageGitHubRPM, MaxSyncInterval, true}, + {PackageGitHubRPM, MaxSyncInterval + 1, false}, {PackageRPM, 0, true}, {PackageRPM, 60, false}, } {