Clamp negative protocol ban durations

This commit is contained in:
Brandon McGinty (chatgpt)
2026-08-09 14:55:39 -04:00
committed by Brandon McGinty
parent 9f90bd5c66
commit 1679a37783
2 changed files with 27 additions and 0 deletions
+6
View File
@@ -16,6 +16,9 @@ type BanList []*Ban
// Add creates a new ban list entry with the given parameters.
func (b *BanList) Add(address net.IP, mask net.IPMask, reason string, duration time.Duration) *Ban {
if duration < 0 {
duration = 0
}
ban := &Ban{
Address: address,
Mask: mask,
@@ -66,6 +69,9 @@ func (b *Ban) SetReason(reason string) {
// SetDuration changes the duration of the ban.
func (b *Ban) SetDuration(duration time.Duration) {
if duration < 0 {
duration = 0
}
b.Duration = duration
}
+21
View File
@@ -0,0 +1,21 @@
package gumble
import (
"net"
"testing"
"time"
)
// Regression: negative durations were converted to uint32 seconds for the
// protocol, creating an unexpectedly huge ban rather than a safe duration.
func TestBanDurationsNeverRemainNegative(t *testing.T) {
var bans BanList
ban := bans.Add(net.ParseIP("192.0.2.1"), net.CIDRMask(32, 32), "test", -time.Minute)
if ban.Duration != 0 {
t.Fatalf("Add duration = %v", ban.Duration)
}
ban.SetDuration(-time.Second)
if ban.Duration != 0 {
t.Fatalf("SetDuration = %v", ban.Duration)
}
}