From 433d5cc0a973a1bce7cc3d2af8bc9188eb18cf05 Mon Sep 17 00:00:00 2001 From: clawbot <35+clawbot@noreply.example.org> Date: Thu, 19 Nov 2026 20:29:53 +0100 Subject: [PATCH] bans: one ledger method makes a request's ban, one place decides observe mode (closes #444) The ledger's BanForLimit, WouldBanForLimit, BanForAttack and WouldBanForAttack become Ban and WouldBan, which take the cause and the notes in one bans.Grounds. The waf's banForLimit and banForAttack fill in what differs and call banClient, the one function that tests observe mode on the way to a ban. Behaviour is unchanged; tests change only where they called the four methods. Model: opus-5-5 --- internal/bans/abuseipdb_test.go | 12 ++- internal/bans/admin_test.go | 87 +++++++++++----- internal/bans/bans.go | 136 ++++++++++--------------- internal/bans/bans_test.go | 172 ++++++++++++++++++++++---------- internal/bans/snapshot_test.go | 57 +++++++---- internal/state/state_test.go | 48 +++++---- internal/waf/admin_test.go | 4 +- internal/waf/alerts_test.go | 8 +- internal/waf/answertime_test.go | 4 +- internal/waf/bans.go | 125 +++++++++++++---------- 10 files changed, 392 insertions(+), 261 deletions(-) diff --git a/internal/bans/abuseipdb_test.go b/internal/bans/abuseipdb_test.go index 71c670a..c16a352 100644 --- a/internal/bans/abuseipdb_test.go +++ b/internal/bans/abuseipdb_test.go @@ -54,7 +54,11 @@ func TestBlacklistLeavesANetblockWithAnActiveBanAsItIs(t *testing.T) { now := midnight() scores := map[netip.Prefix]int64{netblock: 100} - limitBan, _ := ledger.BanForLimit(netblock, now, bans.Notes{}) + limitBan, _ := ledger.Ban( + netblock, + now, + bans.Grounds{Cause: bans.CauseLimit}, + ) if made := ledger.BanForAbuseIPDB(now, now.Add(30*day), scores); made != 0 { t.Errorf("made %d bans over a ban for a limit, want none", made) @@ -119,7 +123,7 @@ func TestEndedBlacklistBansAreForgottenAtTheNextFetch(t *testing.T) { expires := now.Add(30 * day) ledger.BanForAbuseIPDB(now, expires, map[netip.Prefix]int64{listed: 100}) - ledger.BanForLimit(limited, now, bans.Notes{}) + ledger.Ban(limited, now, bans.Grounds{Cause: bans.CauseLimit}) // listed is off the blacklist when it is fetched after the ban's end. if made := ledger.BanForAbuseIPDB(expires, expires.Add(30*day), nil); made != 0 { @@ -144,7 +148,7 @@ func TestBlacklistBansDoNotCountTowardMaxBans(t *testing.T) { scores[netip.PrefixFrom(netip.AddrFrom4([4]byte{198, 51, 100, byte(i)}), 32)] = 100 } - ledger.BanForLimit(limited, now, bans.Notes{}) + ledger.Ban(limited, now, bans.Grounds{Cause: bans.CauseLimit}) if made := ledger.BanForAbuseIPDB(now, now.Add(30*day), scores); made != 3 { t.Errorf("made %d bans for 3 netblocks, want 3", made) @@ -154,7 +158,7 @@ func TestBlacklistBansDoNotCountTowardMaxBans(t *testing.T) { // The ban for an attack takes the place of the one for a limit, and no // blacklist ban goes. - ledger.BanForAttack(attacker, now, bans.Notes{}) + ledger.Ban(attacker, now, bans.Grounds{Cause: bans.CauseAttack}) want := map[netip.Prefix]int{limited: 0, attacker: 1} for netblock := range scores { diff --git a/internal/bans/admin_test.go b/internal/bans/admin_test.go index dd2c55d..800bdba 100644 --- a/internal/bans/admin_test.go +++ b/internal/bans/admin_test.go @@ -61,7 +61,7 @@ func TestAdminsBansAreNeverDroppedAndDoNotCountTowardMaxBans(t *testing.T) { wantBans(t, ledger, map[netip.Prefix]int{adminsOnly: 2, both: 2}) // A new ban drops the ban sww made, and only that one. - ledger.BanForLimit(second, midnight(), bans.Notes{}) + ledger.Ban(second, midnight(), bans.Grounds{Cause: bans.CauseLimit}) wantBans( t, ledger, @@ -77,7 +77,7 @@ func TestAdminsBansAreNeverDroppedAndDoNotCountTowardMaxBans(t *testing.T) { } // And the next drops that one. - ledger.BanForLimit(third, midnight(), bans.Notes{}) + ledger.Ban(third, midnight(), bans.Grounds{Cause: bans.CauseLimit}) wantBans( t, ledger, @@ -98,20 +98,28 @@ func TestEarlierBansCountABanDroppedAfterAnAdminsBan(t *testing.T) { // twice, each time after its last ban has ended. ledger.BanForAdmin(netblock, midnight(), midnight().Add(time.Minute), "probes for logins") - first, _ := ledger.BanForLimit( + first, _ := ledger.Ban( netblock, midnight().Add(time.Minute), - bans.Notes{}, + bans.Grounds{Cause: bans.CauseLimit}, + ) + second, _ := ledger.Ban( + netblock, + first.Expires, + bans.Grounds{Cause: bans.CauseLimit}, ) - second, _ := ledger.BanForLimit(netblock, first.Expires, bans.Notes{}) // A ban on another netblock fills the ledger, and the netblock's first // ban for a limit, its earliest that is not an admin's, goes. - ledger.BanForLimit(other, second.Start, bans.Notes{}) + ledger.Ban(other, second.Start, bans.Grounds{Cause: bans.CauseLimit}) wantBans(t, ledger, map[netip.Prefix]int{netblock: 2, other: 1}) // The netblock's next ban still counts it. - third, _ := ledger.BanForLimit(netblock, second.Expires, bans.Notes{}) + third, _ := ledger.Ban( + netblock, + second.Expires, + bans.Grounds{Cause: bans.CauseLimit}, + ) if third.Notes.EarlierBans != (bans.EarlierBans{Limit: 2, Admin: 1}) { t.Errorf("the third ban for a limit has earlier bans %+v, "+ "want 2 for a limit and 1 an admin's", third.Notes.EarlierBans) @@ -127,15 +135,27 @@ func TestEarlierBansCountABanAnAdminKeepsOnce(t *testing.T) { // The netblock breaks a limit twice, the second time after its first // ban has ended, and the second ban's notes count the first. An admin // then keeps the first ban. - first, _ := ledger.BanForLimit(netblock, midnight(), bans.Notes{}) - second, _ := ledger.BanForLimit(netblock, first.Expires, bans.Notes{}) + first, _ := ledger.Ban( + netblock, + midnight(), + bans.Grounds{Cause: bans.CauseLimit}, + ) + second, _ := ledger.Ban( + netblock, + first.Expires, + bans.Grounds{Cause: bans.CauseLimit}, + ) kept := first kept.Cause = bans.CauseAdmin ledger.LoadEdit([]bans.Ban{kept, second}) // The netblock's next ban counts the kept ban once, as an admin's. - third, _ := ledger.BanForLimit(netblock, second.Expires, bans.Notes{}) + third, _ := ledger.Ban( + netblock, + second.Expires, + bans.Grounds{Cause: bans.CauseLimit}, + ) if third.Notes.EarlierBans != (bans.EarlierBans{Limit: 1, Admin: 1}) { t.Errorf("the third ban for a limit has earlier bans %+v, "+ "want 1 for a limit and 1 an admin's", third.Notes.EarlierBans) @@ -147,20 +167,37 @@ func TestReasonOfTheBansSwwMakes(t *testing.T) { ledger := bans.New(defaultParams()) - limit, _ := ledger.BanForLimit( + limit, _ := ledger.Ban( netip.MustParsePrefix("203.0.113.1/32"), midnight(), - bans.Notes{Kind: "requests", Limit: 1000, Window: "minute"}, + bans.Grounds{ + Cause: bans.CauseLimit, + Notes: bans.Notes{ + Kind: "requests", + Limit: 1000, + Window: "minute", + }, + }, ) - byteLimit, _ := ledger.BanForLimit( + byteLimit, _ := ledger.Ban( netip.MustParsePrefix("203.0.113.3/32"), midnight(), - bans.Notes{Kind: "bytes", Limit: 10 << 30, Window: "hour"}, + bans.Grounds{ + Cause: bans.CauseLimit, + Notes: bans.Notes{ + Kind: "bytes", + Limit: 10 << 30, + Window: "hour", + }, + }, ) - attack, _ := ledger.BanForAttack( + attack, _ := ledger.Ban( netip.MustParsePrefix("203.0.113.2/32"), midnight(), - bans.Notes{RuleID: "git-dir", Target: "path"}, + bans.Grounds{ + Cause: bans.CauseAttack, + Notes: bans.Notes{RuleID: "git-dir", Target: "path"}, + }, ) for _, tc := range []struct{ got, want string }{ @@ -200,7 +237,7 @@ func TestLiftedBanForALimitRefusesNothingAndMakesNoBanLonger(t *testing.T) { t.Error("the lifted ban refuses") } - ban, _ := ledger.BanForLimit(netblock, now, bans.Notes{}) + ban, _ := ledger.Ban(netblock, now, bans.Grounds{Cause: bans.CauseLimit}) if ban.Expires.Sub(ban.Start) != time.Hour || ban.Notes.EarlierBans != (bans.EarlierBans{Limit: 1}) { t.Errorf( @@ -251,7 +288,11 @@ func TestLiftedBanForAnAttackRefusesNothingAndMakesNoBanLonger( } // The next clear sign of attack bans for seven days, as a first does. - ban, _ := ledger.BanForAttack(netblock, now, bans.Notes{}) + ban, _ := ledger.Ban( + netblock, + now, + bans.Grounds{Cause: bans.CauseAttack}, + ) if ban.Expires.Sub(ban.Start) != 7*day { t.Errorf( "the next ban for an attack ends at %s, want seven days on", @@ -264,10 +305,10 @@ func TestLoadEditCountsTheBansAnAdminMade(t *testing.T) { t.Parallel() ledger := bans.New(defaultParams()) - made, _ := ledger.BanForLimit( + made, _ := ledger.Ban( netip.MustParsePrefix("203.0.113.1/32"), midnight(), - bans.Notes{}, + bans.Grounds{Cause: bans.CauseLimit}, ) atStart := bans.Ban{ Netblock: netip.MustParsePrefix("203.0.113.2/32"), @@ -311,7 +352,7 @@ func TestAdminsBanIsMadeWhileAnotherLasts(t *testing.T) { ledger := bans.New(defaultParams()) // An hour's ban for a broken limit. - ledger.BanForLimit(netblock, midnight(), bans.Notes{}) + ledger.Ban(netblock, midnight(), bans.Grounds{Cause: bans.CauseLimit}) wantChanged(t, ledger, true) // A minute later an admin bans the netblock for good, named by an @@ -431,10 +472,10 @@ func TestPermanentBanOnAWiderNetblockIsReturnedOverATemporaryBan( ledger := bans.New(defaultParams()) // The temporary ban is made first, so that its netblock is looked at first. - ledger.BanForLimit( + ledger.Ban( netip.MustParsePrefix("203.0.113.9/32"), midnight(), - bans.Notes{}, + bans.Grounds{Cause: bans.CauseLimit}, ) ledger.BanForAdmin( netip.MustParsePrefix("203.0.113.0/24"), diff --git a/internal/bans/bans.go b/internal/bans/bans.go index a558c82..3c1cec3 100644 --- a/internal/bans/bans.go +++ b/internal/bans/bans.go @@ -300,81 +300,50 @@ func activeBan(bans []Ban, now time.Time) *Ban { return newest } -// BanForLimit bans netblock at now for a broken limit, with notes, and -// returns the ban, and true. A first ban lasts LimitBanDuration. A ban -// made within LimitBanRepeatWindow after the netblock's ban that ended -// last, other than one for a clear sign of attack or a lifted one, lasts -// repeatFactor times as long as that one. A ban that would be longer -// than LimitBanPermanentAbove is permanent instead. If a ban on netblock is still -// active, as when two of its requests break a limit at once, that ban is -// returned with false, and no other is made. The ledger fills in the -// notes' Refused and EarlierBans itself, and gives the ban the reason -// " per over the limit of ", from the notes, such -// as "requests per minute over the limit of 1000". -func (l *Ledger) BanForLimit( - netblock netip.Prefix, now time.Time, notes Notes, -) (Ban, bool) { - return l.ban( - netblock, - now, - CauseLimit, - limitReason(notes), - notes, - true, - ) +// Grounds are why a request makes a ban: its cause, CauseLimit or +// CauseAttack, and its notes. +type Grounds struct { + Cause string + Notes Notes } -// WouldBanForLimit returns what BanForLimit would, without making the ban: -// what observe mode would have done. -func (l *Ledger) WouldBanForLimit( - netblock netip.Prefix, now time.Time, notes Notes, +// Ban bans netblock at now on grounds, and returns the ban, and true. If +// a ban on netblock is still active, as when two of its requests break a +// limit at once, that ban is returned with false, and no other is made. +// The ledger fills in the notes' Refused and EarlierBans itself. +// +// A ban for a broken limit: +// - lasts LimitBanDuration the first time; +// - lasts repeatFactor times as long as the netblock's ban that ended +// last, when made within LimitBanRepeatWindow after it, unless that +// ban was for a clear sign of attack or was lifted; +// - is permanent instead when it would be longer than +// LimitBanPermanentAbove; +// - has the reason " per over the limit of ", from +// the notes, such as "requests per minute over the limit of 1000". +// +// A ban for a clear sign of attack: +// - lasts AttackBanDuration the first time; +// - is permanent once the netblock has had one that was not lifted; +// - has the reason "matched the rule ", or "asked for the trap +// path ". +func (l *Ledger) Ban( + netblock netip.Prefix, now time.Time, grounds Grounds, ) (Ban, bool) { - return l.ban( - netblock, - now, - CauseLimit, - limitReason(notes), - notes, - false, - ) + return l.ban(netblock, now, grounds, true) } -// BanForAttack bans netblock at now for a clear sign of attack, with -// notes, and returns the ban, and whether it made it, as BanForLimit -// does. A first ban lasts AttackBanDuration; once the netblock has had -// one that was not lifted, the next is permanent. Its reason is "matched -// the rule ", or "asked for the trap path ". -func (l *Ledger) BanForAttack( - netblock netip.Prefix, now time.Time, notes Notes, +// WouldBan returns what Ban would, without making the ban: what observe +// mode would have done. +func (l *Ledger) WouldBan( + netblock netip.Prefix, now time.Time, grounds Grounds, ) (Ban, bool) { - return l.ban( - netblock, - now, - CauseAttack, - attackReason(notes), - notes, - true, - ) -} - -// WouldBanForAttack returns what BanForAttack would, without making the -// ban: what observe mode would have done. -func (l *Ledger) WouldBanForAttack( - netblock netip.Prefix, now time.Time, notes Notes, -) (Ban, bool) { - return l.ban( - netblock, - now, - CauseAttack, - attackReason(notes), - notes, - false, - ) + return l.ban(netblock, now, grounds, false) } // WouldBePermanent reports whether a ban on netblock for cause, CauseLimit -// or CauseAttack, made at now would be permanent, as BanForLimit or -// BanForAttack would make it. It works out nothing else of the ban. +// or CauseAttack, made at now would be permanent, as Ban would make it. It +// works out nothing else of the ban. func (l *Ledger) WouldBePermanent( netblock netip.Prefix, now time.Time, cause string, ) bool { @@ -411,10 +380,9 @@ func attackReason(notes Notes) string { // BanForAdmin bans netblock at now for an admin, with reason, until // expires, or for good when expires is zero, and returns the ban, whose -// cause is CauseAdmin. Unlike BanForLimit and BanForAttack, it makes the -// ban even while another on netblock is active, since the admin asked -// for this one. The ledger fills in the notes' EarlierBans, and counts -// the ban among those made. +// cause is CauseAdmin. Unlike Ban, it makes the ban even while another +// on netblock is active, since the admin asked for this one. The ledger +// fills in the notes' EarlierBans, and counts the ban among those made. func (l *Ledger) BanForAdmin( netblock netip.Prefix, now, expires time.Time, reason string, ) Ban { @@ -759,15 +727,13 @@ func (l *Ledger) forgetEndedAbuseIPDBBans(now time.Time) bool { return forgot } -// ban bans netblock at now for cause, CauseLimit or CauseAttack, with -// reason and notes, as BanForLimit and BanForAttack describe, and returns -// the ban, and whether it made it. Unless keep is true, the ban is not -// made, only returned: it is the ban that would have been made. +// ban bans netblock at now on grounds, as Ban describes, and returns the +// ban, and whether it made it. Unless keep is true, the ban is not made, +// only returned: it is the ban that would have been made. func (l *Ledger) ban( netblock netip.Prefix, now time.Time, - cause, reason string, - notes Notes, + grounds Grounds, keep bool, ) (Ban, bool) { l.mu.Lock() @@ -776,6 +742,8 @@ func (l *Ledger) ban( // held are the netblock's bans, none of them active. var held []Ban + notes := grounds.Notes + bans, found := l.netblocks.Get(netblock) if found { active := activeBan(*bans, now) @@ -791,15 +759,19 @@ func (l *Ledger) ban( ban := Ban{ Netblock: netblock, Start: now, - Cause: cause, - Reason: reason, + Cause: grounds.Cause, Notes: notes, } - if cause == CauseAttack { - ban.Expires = l.attackExpiry(held, now) - } else { + switch grounds.Cause { + case CauseLimit: + ban.Reason = limitReason(notes) ban.Expires = l.limitExpiry(held, now) + case CauseAttack: + ban.Reason = attackReason(notes) + ban.Expires = l.attackExpiry(held, now) + default: + panic("bans: a request's ban has the cause " + grounds.Cause) } if !keep { @@ -807,7 +779,7 @@ func (l *Ledger) ban( } l.add(ban) - l.made[cause]++ + l.made[grounds.Cause]++ l.markChanged() return ban, true diff --git a/internal/bans/bans_test.go b/internal/bans/bans_test.go index e3f3b21..29f34eb 100644 --- a/internal/bans/bans_test.go +++ b/internal/bans/bans_test.go @@ -22,7 +22,11 @@ func TestRepeatsTripleUntilPermanent(t *testing.T) { // Each ban is followed by another as soon as it ends: 1, 3, 9, 27 and // 81 hours. for i, hours := range []int{1, 3, 9, 27, 81} { - ban, _ := ledger.BanForLimit(netblock, now, bans.Notes{}) + ban, _ := ledger.Ban( + netblock, + now, + bans.Grounds{Cause: bans.CauseLimit}, + ) length := time.Duration(hours) * time.Hour if !ban.Expires.Equal(now.Add(length)) || @@ -42,7 +46,7 @@ func TestRepeatsTripleUntilPermanent(t *testing.T) { // The sixth would last 243 hours, more than seven days: it is // permanent, and never ends. - ban, _ := ledger.BanForLimit(netblock, now, bans.Notes{}) + ban, _ := ledger.Ban(netblock, now, bans.Grounds{Cause: bans.CauseLimit}) if !ban.Permanent() { t.Fatalf("sixth ban ends at %s, want a permanent one", ban.Expires) } @@ -71,15 +75,15 @@ func TestRepeatWindowRunsOut(t *testing.T) { ledger := bans.New(defaultParams()) netblock := netip.MustParsePrefix("203.0.113.9/32") - first, _ := ledger.BanForLimit( + first, _ := ledger.Ban( netblock, midnight(), - bans.Notes{}, + bans.Grounds{Cause: bans.CauseLimit}, ) - second, _ := ledger.BanForLimit( + second, _ := ledger.Ban( netblock, first.Expires.Add(tc.gap), - bans.Notes{}, + bans.Grounds{Cause: bans.CauseLimit}, ) if second.Expires.Sub(second.Start) != tc.want || @@ -104,10 +108,10 @@ func TestFirstBanLongerThanLimitBanPermanentAboveIsPermanent(t *testing.T) { params.LimitBanDuration = params.LimitBanPermanentAbove + time.Hour ledger := bans.New(params) - ban, _ := ledger.BanForLimit( + ban, _ := ledger.Ban( netip.MustParsePrefix("203.0.113.9/32"), midnight(), - bans.Notes{}, + bans.Grounds{Cause: bans.CauseLimit}, ) if !ban.Permanent() { t.Errorf("first ban ends at %s, want a permanent one", ban.Expires) @@ -127,7 +131,11 @@ func TestLimitBanPermanentAboveSetFarOffDoesNotOverflow(t *testing.T) { now := midnight() for i := range 14 { - ban, _ := ledger.BanForLimit(netblock, now, bans.Notes{}) + ban, _ := ledger.Ban( + netblock, + now, + bans.Grounds{Cause: bans.CauseLimit}, + ) if !ban.Expires.After(ban.Start) { t.Fatalf( "ban %d starts at %s and ends at %s", @@ -140,7 +148,7 @@ func TestLimitBanPermanentAboveSetFarOffDoesNotOverflow(t *testing.T) { now = ban.Expires } - ban, _ := ledger.BanForLimit(netblock, now, bans.Notes{}) + ban, _ := ledger.Ban(netblock, now, bans.Grounds{Cause: bans.CauseLimit}) if !ban.Permanent() { t.Errorf("15th ban ends at %s, want a permanent one", ban.Expires) } @@ -152,15 +160,19 @@ func TestBrokenLimitDuringABanMakesNoOther(t *testing.T) { ledger := bans.New(defaultParams()) netblock := netip.MustParsePrefix("203.0.113.9/32") - first, made := ledger.BanForLimit(netblock, midnight(), bans.Notes{}) + first, made := ledger.Ban( + netblock, + midnight(), + bans.Grounds{Cause: bans.CauseLimit}, + ) if !made { t.Error("the first ban was not made") } - again, made := ledger.BanForLimit( + again, made := ledger.Ban( netblock, midnight().Add(time.Minute), - bans.Notes{}, + bans.Grounds{Cause: bans.CauseLimit}, ) if made || !reflect.DeepEqual(again, first) || @@ -175,10 +187,10 @@ func TestBrokenLimitDuringABanMakesNoOther(t *testing.T) { ) } - again, made = ledger.BanForAttack( + again, made = ledger.Ban( netblock, midnight().Add(time.Minute), - bans.Notes{}, + bans.Grounds{Cause: bans.CauseAttack}, ) if made || !reflect.DeepEqual(again, first) { t.Errorf( @@ -195,10 +207,10 @@ func TestCheckRefusesWhileTheBanLastsAndCountsTheRefusals(t *testing.T) { ledger := bans.New(defaultParams()) netblock := netip.MustParsePrefix("203.0.113.9/32") - ban, _ := ledger.BanForLimit( + ban, _ := ledger.Ban( netblock, midnight(), - bans.Notes{Requests: 5}, + bans.Grounds{Cause: bans.CauseLimit, Notes: bans.Notes{Requests: 5}}, ) for range 3 { @@ -237,10 +249,10 @@ func TestFindCountsNothing(t *testing.T) { ledger := bans.New(defaultParams()) netblock := netip.MustParsePrefix("203.0.113.9/32") - ban, _ := ledger.BanForLimit( + ban, _ := ledger.Ban( netblock, midnight(), - bans.Notes{Requests: 5}, + bans.Grounds{Cause: bans.CauseLimit, Notes: bans.Notes{Requests: 5}}, ) got, banned, _ := ledger.Find( @@ -287,24 +299,24 @@ func TestMaxBansDropsTheEarliestBanOfTheNetblockSeenLongestAgo( d := netip.MustParsePrefix("2001:db8::/64") now := midnight() - first, _ := ledger.BanForLimit(a, now, bans.Notes{}) - ledger.BanForLimit(b, now, bans.Notes{}) - ledger.BanForLimit(c, now, bans.Notes{}) + first, _ := ledger.Ban(a, now, bans.Grounds{Cause: bans.CauseLimit}) + ledger.Ban(b, now, bans.Grounds{Cause: bans.CauseLimit}) + ledger.Ban(c, now, bans.Grounds{Cause: bans.CauseLimit}) // A request from a makes b the netblock seen longest ago, and its ban // goes to make room for d's. ledger.Check(a.Addr(), now) - ledger.BanForLimit(d, now, bans.Notes{}) + ledger.Ban(d, now, bans.Grounds{Cause: bans.CauseLimit}) wantBans(t, ledger, map[netip.Prefix]int{a: 1, b: 0, c: 1, d: 1}) // a is banned again once its ban has ended; c, seen longest ago, goes. - ledger.BanForLimit(a, first.Expires, bans.Notes{}) + ledger.Ban(a, first.Expires, bans.Grounds{Cause: bans.CauseLimit}) wantBans(t, ledger, map[netip.Prefix]int{a: 2, c: 0, d: 1}) // With d seen since, a is seen longest ago, and its earlier ban goes // first. ledger.Check(d.Addr(), first.Expires) - ledger.BanForLimit(b, first.Expires, bans.Notes{}) + ledger.Ban(b, first.Expires, bans.Grounds{Cause: bans.CauseLimit}) wantBans(t, ledger, map[netip.Prefix]int{a: 1, b: 1, d: 1}) if !ledger.Bans(a)[0].Start.Equal(first.Expires) { @@ -325,8 +337,16 @@ func TestFullLedgerDropsTheEarlierBanOfTheNetblockBannedAgain(t *testing.T) { ledger := bans.New(params) netblock := netip.MustParsePrefix("203.0.113.9/32") - first, _ := ledger.BanForLimit(netblock, midnight(), bans.Notes{}) - second, _ := ledger.BanForLimit(netblock, first.Expires, bans.Notes{}) + first, _ := ledger.Ban( + netblock, + midnight(), + bans.Grounds{Cause: bans.CauseLimit}, + ) + second, _ := ledger.Ban( + netblock, + first.Expires, + bans.Grounds{Cause: bans.CauseLimit}, + ) held := ledger.Bans(netblock) if len(held) != 1 || !reflect.DeepEqual(held[0], second) || @@ -343,7 +363,11 @@ func TestRequestDuringAnAttackBanMakesItPermanent(t *testing.T) { netblock := netip.MustParsePrefix("203.0.113.9/32") notes := bans.Notes{RuleID: "env-file", Target: "path"} - ban, _ := ledger.BanForAttack(netblock, midnight(), notes) + ban, _ := ledger.Ban( + netblock, + midnight(), + bans.Grounds{Cause: bans.CauseAttack, Notes: notes}, + ) if !ban.Expires.Equal(midnight().Add(7*day)) || ban.Cause != bans.CauseAttack || ban.Notes.RuleID != "env-file" || @@ -418,8 +442,16 @@ func TestAttackAfterAnAttackBanHasEndedBansPermanently(t *testing.T) { netblock := netip.MustParsePrefix("203.0.113.9/32") // A ban for a broken limit before does not count. - first, _ := ledger.BanForLimit(netblock, midnight(), bans.Notes{}) - second, _ := ledger.BanForAttack(netblock, first.Expires, bans.Notes{}) + first, _ := ledger.Ban( + netblock, + midnight(), + bans.Grounds{Cause: bans.CauseLimit}, + ) + second, _ := ledger.Ban( + netblock, + first.Expires, + bans.Grounds{Cause: bans.CauseAttack}, + ) if second.Expires.Sub(second.Start) != 7*day { t.Fatalf("the first ban for an attack lasts %s, want 7 days", @@ -435,10 +467,10 @@ func TestAttackAfterAnAttackBanHasEndedBansPermanently(t *testing.T) { // Its notes show the earlier ban for an attack that makes it permanent, // beside the one for a limit. - third, _ := ledger.BanForAttack( + third, _ := ledger.Ban( netblock, second.Expires.Add(30*day), - bans.Notes{}, + bans.Grounds{Cause: bans.CauseAttack}, ) if !third.Permanent() || third.Notes.EarlierBans != (bans.EarlierBans{Limit: 1, Attack: 1}) { @@ -452,14 +484,18 @@ func TestWouldBanGivesTheBanWithoutMakingIt(t *testing.T) { ledger := bans.New(defaultParams()) netblock := netip.MustParsePrefix("203.0.113.9/32") - first, _ := ledger.BanForLimit(netblock, midnight(), bans.Notes{}) + first, _ := ledger.Ban( + netblock, + midnight(), + bans.Grounds{Cause: bans.CauseLimit}, + ) wantChanged(t, ledger, true) // While the first ban lasts, none would be made. - during, would := ledger.WouldBanForAttack( + during, would := ledger.WouldBan( netblock, midnight(), - bans.Notes{}, + bans.Grounds{Cause: bans.CauseAttack}, ) if would || !reflect.DeepEqual(during, first) { t.Errorf( @@ -472,13 +508,19 @@ func TestWouldBanGivesTheBanWithoutMakingIt(t *testing.T) { // As it ends, a clear sign of attack would ban for seven days, and a // limit broken again for three hours, but neither is made. - limitNotes := bans.Notes{Kind: "requests", Limit: 1, Window: "minute"} - attack, wouldAttack := ledger.WouldBanForAttack(netblock, first.Expires, - bans.Notes{RuleID: "git-dir"}) - limit, wouldLimit := ledger.WouldBanForLimit( + limitGrounds := bans.Grounds{ + Cause: bans.CauseLimit, + Notes: bans.Notes{Kind: "requests", Limit: 1, Window: "minute"}, + } + attack, wouldAttack := ledger.WouldBan(netblock, first.Expires, + bans.Grounds{ + Cause: bans.CauseAttack, + Notes: bans.Notes{RuleID: "git-dir"}, + }) + limit, wouldLimit := ledger.WouldBan( netblock, first.Expires, - limitNotes, + limitGrounds, ) if !wouldAttack || !attack.Expires.Equal(first.Expires.Add(7*day)) || @@ -505,7 +547,7 @@ func TestWouldBanGivesTheBanWithoutMakingIt(t *testing.T) { wantChanged(t, ledger, false) // The ban made is the one that would have been. - made, _ := ledger.BanForLimit(netblock, first.Expires, limitNotes) + made, _ := ledger.Ban(netblock, first.Expires, limitGrounds) if !reflect.DeepEqual(made, limit) { t.Errorf("the ban made is %+v, want %+v", made, limit) } @@ -526,7 +568,11 @@ func TestWouldBePermanentAnswersAsTheBanWouldBeMade(t *testing.T) { t.Fatalf("ban %d for a limit would be permanent", i+1) } - ban, _ := ledger.BanForLimit(netblock, now, bans.Notes{}) + ban, _ := ledger.Ban( + netblock, + now, + bans.Grounds{Cause: bans.CauseLimit}, + ) now = ban.Expires } @@ -539,7 +585,11 @@ func TestWouldBePermanentAnswersAsTheBanWouldBeMade(t *testing.T) { } // Once a first ban for an attack has ended, the next would be permanent. - attack, _ := ledger.BanForAttack(netblock, now, bans.Notes{}) + attack, _ := ledger.Ban( + netblock, + now, + bans.Grounds{Cause: bans.CauseAttack}, + ) if !ledger.WouldBePermanent(netblock, attack.Expires, bans.CauseAttack) { t.Error("a second ban for an attack would not be permanent") } @@ -553,8 +603,16 @@ func TestAttackBanDoesNotLengthenTheNextBanForALimit(t *testing.T) { // Three times the seven days would be permanent; a limit broken as the // ban for an attack ends bans for an hour, as a first broken limit does. - attack, _ := ledger.BanForAttack(netblock, midnight(), bans.Notes{}) - limit, _ := ledger.BanForLimit(netblock, attack.Expires, bans.Notes{}) + attack, _ := ledger.Ban( + netblock, + midnight(), + bans.Grounds{Cause: bans.CauseAttack}, + ) + limit, _ := ledger.Ban( + netblock, + attack.Expires, + bans.Grounds{Cause: bans.CauseLimit}, + ) if limit.Expires.Sub(limit.Start) != time.Hour || limit.Cause != bans.CauseLimit { @@ -578,10 +636,13 @@ func TestRequestTextsAreCutTo256Bytes(t *testing.T) { Time: midnight(), Method: long, Host: long, Path: long, Status: 403, UserAgent: long, } - ban, _ := ledger.BanForLimit( + ban, _ := ledger.Ban( netblock, midnight(), - bans.Notes{Request: request}, + bans.Grounds{ + Cause: bans.CauseLimit, + Notes: bans.Notes{Request: request}, + }, ) cut := long[:256] @@ -607,11 +668,20 @@ func TestLookupFillsTheNotesOfTheNetblocksBansWithoutOne(t *testing.T) { // A ban made with the client's lookup, one made before it came, after // the first ended, and one on another netblock. - ledger.BanForLimit(netblock, midnight(), bans.Notes{ - ASN: "AS64497", ASName: "Other Net", Country: "FR", + ledger.Ban(netblock, midnight(), bans.Grounds{ + Cause: bans.CauseLimit, + Notes: bans.Notes{ + ASN: "AS64497", + ASName: "Other Net", + Country: "FR", + }, }) - ledger.BanForLimit(netblock, midnight().Add(time.Hour), bans.Notes{}) - ledger.BanForLimit(other, midnight(), bans.Notes{}) + ledger.Ban( + netblock, + midnight().Add(time.Hour), + bans.Grounds{Cause: bans.CauseLimit}, + ) + ledger.Ban(other, midnight(), bans.Grounds{Cause: bans.CauseLimit}) ledger.AddLookup(netblock, "AS64496", "Example Net", "DE") diff --git a/internal/bans/snapshot_test.go b/internal/bans/snapshot_test.go index 9a21e7a..548c0be 100644 --- a/internal/bans/snapshot_test.go +++ b/internal/bans/snapshot_test.go @@ -19,25 +19,29 @@ func TestChangedAfterABanIsMade(t *testing.T) { wantChanged(t, ledger, false) - ledger.BanForLimit(netblock, midnight(), bans.Notes{}) + ledger.Ban(netblock, midnight(), bans.Grounds{Cause: bans.CauseLimit}) wantChanged(t, ledger, true) // A limit broken during the ban makes no other, and a refusal changes // only the counts in the notes, which wait for the interval's write. - ledger.BanForLimit(netblock, midnight().Add(time.Minute), bans.Notes{}) + ledger.Ban( + netblock, + midnight().Add(time.Minute), + bans.Grounds{Cause: bans.CauseLimit}, + ) ledger.Check(netblock.Addr(), midnight().Add(time.Minute)) wantChanged(t, ledger, false) // Two bans before the value is read leave one. - ledger.BanForLimit( + ledger.Ban( netip.MustParsePrefix("203.0.113.10/32"), midnight(), - bans.Notes{}, + bans.Grounds{Cause: bans.CauseLimit}, ) - ledger.BanForLimit( + ledger.Ban( netip.MustParsePrefix("203.0.113.11/32"), midnight(), - bans.Notes{}, + bans.Grounds{Cause: bans.CauseLimit}, ) wantChanged(t, ledger, true) wantChanged(t, ledger, false) @@ -51,10 +55,14 @@ func TestSnapshotListsEveryBanByNetblock(t *testing.T) { high := netip.MustParsePrefix("203.0.113.10/32") low := netip.MustParsePrefix("203.0.113.9/32") - first, _ := ledger.BanForLimit(v6, midnight(), bans.Notes{}) - ledger.BanForLimit(high, midnight(), bans.Notes{}) - ledger.BanForLimit(low, midnight(), bans.Notes{}) - ledger.BanForLimit(v6, first.Expires, bans.Notes{}) + first, _ := ledger.Ban( + v6, + midnight(), + bans.Grounds{Cause: bans.CauseLimit}, + ) + ledger.Ban(high, midnight(), bans.Grounds{Cause: bans.CauseLimit}) + ledger.Ban(low, midnight(), bans.Grounds{Cause: bans.CauseLimit}) + ledger.Ban(v6, first.Expires, bans.Grounds{Cause: bans.CauseLimit}) snapshot := ledger.Snapshot() @@ -80,7 +88,10 @@ func TestLoadedBansCarryOn(t *testing.T) { before := bans.New(defaultParams()) netblock := netip.MustParsePrefix("203.0.113.9/32") - ban, _ := before.BanForLimit(netblock, midnight(), bans.Notes{Limit: 1}) + ban, _ := before.Ban(netblock, midnight(), bans.Grounds{ + Cause: bans.CauseLimit, + Notes: bans.Notes{Limit: 1}, + }) // Loaded into a new ledger, as across a restart, the ban still refuses // while it lasts, and once it has ended a broken limit bans for three @@ -96,7 +107,11 @@ func TestLoadedBansCarryOn(t *testing.T) { t.Error("the loaded ban does not refuse") } - again, _ := after.BanForLimit(netblock, ban.Expires, bans.Notes{}) + again, _ := after.Ban( + netblock, + ban.Expires, + bans.Grounds{Cause: bans.CauseLimit}, + ) if again.Expires.Sub(again.Start) != 3*time.Hour || again.Notes.EarlierBans != (bans.EarlierBans{Limit: 1}) { t.Errorf( @@ -124,10 +139,10 @@ func TestLoadedBanRefusesEveryClientInItsNetblock(t *testing.T) { Start: midnight(), }, }) - ledger.BanForLimit( + ledger.Ban( netip.MustParsePrefix("198.51.100.7/32"), midnight(), - bans.Notes{}, + bans.Grounds{Cause: bans.CauseLimit}, ) for client, want := range map[string]bool{ @@ -200,7 +215,7 @@ func TestPermanentBanStartedBeforeAnEndedOneRefuses(t *testing.T) { } // A limit broken now makes no shorter ban over the permanent one. - ban, _ = ledger.BanForLimit(netblock, now, bans.Notes{}) + ban, _ = ledger.Ban(netblock, now, bans.Grounds{Cause: bans.CauseLimit}) if !ban.Permanent() || len(ledger.Bans(netblock)) != 2 { t.Errorf( "a broken limit returned %+v and left the netblock %d bans, "+ @@ -236,10 +251,10 @@ func TestNextBanWorkedOutFromTheBanThatEndedLast(t *testing.T) { // Once both have ended, a limit broken within the repeat window bans // for three times the 9 hours, and the notes count the two bans // before the 9-hour one and it, for a limit, and the admin's. - ban, _ := ledger.BanForLimit( + ban, _ := ledger.Ban( netblock, nineHours.Expires.Add(time.Hour), - bans.Notes{}, + bans.Grounds{Cause: bans.CauseLimit}, ) if ban.Expires.Sub(ban.Start) != 27*time.Hour || ban.Notes.EarlierBans != (bans.EarlierBans{Limit: 3, Admin: 1}) { @@ -313,15 +328,15 @@ func TestLoadReplacesTheBansHeld(t *testing.T) { } // The ledger holds one ban, so it makes two more without dropping any. - first, _ := ledger.BanForLimit( + first, _ := ledger.Ban( netip.MustParsePrefix("198.51.100.7/32"), midnight(), - bans.Notes{}, + bans.Grounds{Cause: bans.CauseLimit}, ) - second, _ := ledger.BanForLimit( + second, _ := ledger.Ban( netip.MustParsePrefix("198.51.100.8/32"), midnight(), - bans.Notes{}, + bans.Grounds{Cause: bans.CauseLimit}, ) want := []bans.Ban{first, second, kept} diff --git a/internal/state/state_test.go b/internal/state/state_test.go index d1861b9..43772e1 100644 --- a/internal/state/state_test.go +++ b/internal/state/state_test.go @@ -1284,18 +1284,18 @@ func TestBansWrittenOnceBansWriteDelayAfterABan(t *testing.T) { // A second ban, made while the first waits to be written, puts the // write off no further, and is written with it. - first, _ := params.Ledger.BanForLimit( + first, _ := params.Ledger.Ban( netip.MustParsePrefix("203.0.113.9/32"), midnight(), - bans.Notes{}, + bans.Grounds{Cause: bans.CauseLimit}, ) time.Sleep(5 * time.Second) - second, _ := params.Ledger.BanForLimit( + second, _ := params.Ledger.Ban( netip.MustParsePrefix("203.0.113.10/32"), midnight(), - bans.Notes{}, + bans.Grounds{Cause: bans.CauseLimit}, ) time.Sleep(5*time.Second - time.Nanosecond) @@ -1358,16 +1358,16 @@ func TestEditJustBeforeAScheduledWriteSurvivesIt(t *testing.T) { run(t, load(t, params).Run) // A ban, and bans.json written with it. - params.Ledger.BanForLimit(netip.MustParsePrefix("203.0.113.9/32"), - midnight(), bans.Notes{}) + params.Ledger.Ban(netip.MustParsePrefix("203.0.113.9/32"), + midnight(), bans.Grounds{Cause: bans.CauseLimit}) time.Sleep(params.BansWriteDelay) synctest.Wait() // A second ban is to be written BansWriteDelay later. Just before // then, an admin saves bans.json with the first ban lifted and // another added. - params.Ledger.BanForLimit(netip.MustParsePrefix("203.0.113.10/32"), - midnight(), bans.Notes{}) + params.Ledger.Ban(netip.MustParsePrefix("203.0.113.10/32"), + midnight(), bans.Grounds{Cause: bans.CauseLimit}) time.Sleep(params.BansWriteDelay - time.Nanosecond) synctest.Wait() edit(t, dir, bansJSON, permanentBansJSON) @@ -1483,10 +1483,10 @@ func TestFailedWriteLeavesTheFileAsItWas(t *testing.T) { t.Fatalf("mkdir: %v", err) } - params.Ledger.BanForLimit( + params.Ledger.Ban( netip.MustParsePrefix("203.0.113.9/32"), midnight(), - bans.Notes{}, + bans.Grounds{Cause: bans.CauseLimit}, ) params.Limiter.Count( netip.MustParsePrefix("203.0.113.9/32"), @@ -1556,10 +1556,10 @@ func TestWritesAreCountedInTheMetrics(t *testing.T) { t.Fatalf("mkdir: %v", err) } - params.Ledger.BanForLimit( + params.Ledger.Ban( netip.MustParsePrefix("203.0.113.9/32"), midnight(), - bans.Notes{}, + bans.Grounds{Cause: bans.CauseLimit}, ) err = files.WriteAll() @@ -2135,7 +2135,11 @@ func TestBanLiftedByAnEditWhileRunning(t *testing.T) { // The ban that liftedBansJSON lifts, before it is lifted. netblock := netip.MustParsePrefix(liftedClient + "/32") - params.Ledger.BanForLimit(netblock, midnight(), bans.Notes{}) + params.Ledger.Ban( + netblock, + midnight(), + bans.Grounds{Cause: bans.CauseLimit}, + ) _, banned, _ := params.Ledger.Find(netblock.Addr(), afterLifting()) if !banned { @@ -2510,11 +2514,15 @@ func fill(params state.Params) { client := netip.MustParsePrefix("203.0.113.9/32") params.Ledger.Load([]bans.Ban{permanentBan()}) - params.Ledger.BanForLimit(client, now, bans.Notes{ - ASN: asn, ASName: asName, Country: "DE", Limit: 1, + params.Ledger.Ban(client, now, bans.Grounds{ + Cause: bans.CauseLimit, + Notes: bans.Notes{ASN: asn, ASName: asName, Country: "DE", Limit: 1}, }) - params.Ledger.BanForAttack(netip.MustParsePrefix("192.0.2.1/32"), now, - bans.Notes{RuleID: "env-file", Target: "path"}) + params.Ledger.Ban(netip.MustParsePrefix("192.0.2.1/32"), now, + bans.Grounds{ + Cause: bans.CauseAttack, + Notes: bans.Notes{RuleID: "env-file", Target: "path"}, + }) for _, c := range []string{"2001:db8::/64", "203.0.113.9/32", "192.0.2.1/32"} { params.Limiter.Count(netip.MustParsePrefix(c), now, whole) @@ -2700,7 +2708,11 @@ func wantLiftedBanKept( } // Were the lifted ban counted, the next would last three hours. - ban, _ := ledger.BanForLimit(netblock, afterLifting(), bans.Notes{}) + ban, _ := ledger.Ban( + netblock, + afterLifting(), + bans.Grounds{Cause: bans.CauseLimit}, + ) if ban.Expires.Sub(ban.Start) != time.Hour { t.Errorf( "the next ban lasts %s, want 1h", diff --git a/internal/waf/admin_test.go b/internal/waf/admin_test.go index be342b1..8ece3a4 100644 --- a/internal/waf/admin_test.go +++ b/internal/waf/admin_test.go @@ -37,10 +37,10 @@ func TestAdminEndpointsAreOffWhileTheTokenIsUnset(t *testing.T) { "", map[string]string{metricsToken: token}, ) - server.Ledger.BanForLimit( + server.Ledger.Ban( netip.MustParsePrefix(otherClient+"/32"), clk.Now(), - bans.Notes{}, + bans.Grounds{Cause: bans.CauseLimit}, ) before := server.Ledger.Snapshot() diff --git a/internal/waf/alerts_test.go b/internal/waf/alerts_test.go index 42f1487..6e02984 100644 --- a/internal/waf/alerts_test.go +++ b/internal/waf/alerts_test.go @@ -130,11 +130,9 @@ func TestObserveModeRaisesTheBanAlertsItWouldHave(t *testing.T) { // A ban for a clear sign of attack, which a request under it would make // permanent. group := netip.MustParsePrefix(ipv6Group) - attackBan, _ := server.Ledger.BanForAttack( - group, - start, - bans.Notes{RuleID: "probe"}, - ) + attackBan, _ := server.Ledger.Ban(group, start, bans.Grounds{ + Cause: bans.CauseAttack, Notes: bans.Notes{RuleID: "probe"}, + }) // The third request breaks the limit, and so does the fourth, within the // cooldown, which raises nothing. The probe is a clear sign of attack. diff --git a/internal/waf/answertime_test.go b/internal/waf/answertime_test.go index c3e16b5..f0ac54c 100644 --- a/internal/waf/answertime_test.go +++ b/internal/waf/answertime_test.go @@ -686,10 +686,10 @@ func askDirectly( // banClient bans client at now. func banClient(_ *testing.T, server *waf.Server, now time.Time) { - server.Ledger.BanForLimit( + server.Ledger.Ban( netip.MustParsePrefix(client+"/32"), now, - bans.Notes{}, + bans.Grounds{Cause: bans.CauseLimit}, ) } diff --git a/internal/waf/bans.go b/internal/waf/bans.go index f6ca9a8..c9d557d 100644 --- a/internal/waf/bans.go +++ b/internal/waf/bans.go @@ -103,9 +103,8 @@ func (rq *request) countedBytes() int64 { // one whose bytes broke a byte limit, the refusal for one that broke the // refusal limit. The ban's notes give the client's limit percentage for a // rate limit or a byte limit; the refusal limit is not lowered. The ban sets -// the client's counters back to zero. In observe mode it makes no ban and -// sets nothing back, and raises the alert for the ban it would have made, -// if that alert would be sent. +// the client's counters back to zero. In observe mode, banClient says what +// happens instead. func (rq *request) banForLimit( now time.Time, hit ratelimit.Hit, @@ -123,23 +122,11 @@ func (rq *request) banForLimit( rq.line.Offence = requestlog.OffenceLimit rq.h.metrics.LimitHit(hit) - netblock := banNetblock(rq.h.config, rq.client) - if rq.h.config.Observe && - !rq.wouldAlertBan(netblock, now, bans.CauseLimit) { - return - } - notes := bans.Notes{ - ASN: rq.line.ASN, - ASName: rq.line.ASName, - Country: rq.line.Country, - Kind: hit.Kind, - Limit: hit.Limit, - Window: hit.Window, - Count: hit.Count, - Reputation: rq.reputation, - Request: rq.noted(now, status), - Requests: rq.netblockRequests(netblock), + Kind: hit.Kind, + Limit: hit.Limit, + Window: hit.Window, + Count: hit.Count, } switch hit.Kind { @@ -149,47 +136,54 @@ func (rq *request) banForLimit( notes.LimitPercent, notes.LimitPercentSetting = rq.bytesLimitPercent.logged() } - if rq.h.config.Observe { - ban, wouldBan := rq.h.ledger.WouldBanForLimit(netblock, now, notes) - if wouldBan { - rq.alertBan(ban) - } - - return - } - - ban, made := rq.h.ledger.BanForLimit(netblock, now, notes) - rq.h.limiter.Reset(clientGroup(rq.h.config, rq.client)) - rq.line.BanExpires = banExpires(ban) - - if made { - rq.alertBan(ban) - } + rq.banClient( + now, + status, + bans.Grounds{Cause: bans.CauseLimit, Notes: notes}, + ) } // banForAttack bans the client's netblock at now for a clear sign of // attack, which notes name: the ban rule that matched, or the trap path -// asked for. It fills in the rest of the notes. In observe mode it makes -// no ban, and raises the alert for the ban it would have made, if that -// alert would be sent. It returns the action for that ban. +// asked for. It returns the action for that ban. In observe mode, banClient +// says what happens instead. func (rq *request) banForAttack(now time.Time, notes bans.Notes) string { - netblock := banNetblock(rq.h.config, rq.client) - if rq.h.config.Observe && - !rq.wouldAlertBan(netblock, now, bans.CauseAttack) { - return banAction( - rq.h.ledger.WouldBePermanent(netblock, now, bans.CauseAttack), - ) - } + return rq.banClient( + now, + rq.h.config.BanResponse, + bans.Grounds{Cause: bans.CauseAttack, Notes: notes}, + ) +} - notes.ASN = rq.line.ASN - notes.ASName = rq.line.ASName - notes.Country = rq.line.Country - notes.Reputation = rq.reputation - notes.Request = rq.noted(now, rq.h.config.BanResponse) - notes.Requests = rq.netblockRequests(netblock) +// banClient bans the client's netblock at now on grounds, and returns the +// action for that ban. It adds to the notes what the request tells of the +// client, with status, what the client was sent. A ban for a broken limit +// sets the client's counters back to zero. +// +// In observe mode it makes no ban and sets nothing back. It raises the +// alert for the ban it would have made, if that alert would be sent. +func (rq *request) banClient( + now time.Time, + status int, + grounds bans.Grounds, +) string { + netblock := banNetblock(rq.h.config, rq.client) if rq.h.config.Observe { - ban, wouldBan := rq.h.ledger.WouldBanForAttack(netblock, now, notes) + if !rq.wouldAlertBan(netblock, now, grounds.Cause) { + return banAction( + rq.h.ledger.WouldBePermanent(netblock, now, grounds.Cause), + ) + } + + grounds.Notes = rq.withClientNotes( + grounds.Notes, + now, + status, + netblock, + ) + + ban, wouldBan := rq.h.ledger.WouldBan(netblock, now, grounds) if wouldBan { rq.alertBan(ban) } @@ -197,7 +191,13 @@ func (rq *request) banForAttack(now time.Time, notes bans.Notes) string { return banAction(ban.Permanent()) } - ban, made := rq.h.ledger.BanForAttack(netblock, now, notes) + grounds.Notes = rq.withClientNotes(grounds.Notes, now, status, netblock) + + ban, made := rq.h.ledger.Ban(netblock, now, grounds) + if grounds.Cause == bans.CauseLimit { + rq.h.limiter.Reset(clientGroup(rq.h.config, rq.client)) + } + rq.line.BanExpires = banExpires(ban) if made { @@ -207,6 +207,25 @@ func (rq *request) banForAttack(now time.Time, notes bans.Notes) string { return banAction(ban.Permanent()) } +// withClientNotes is notes with what the request tells of the client +// added: its AS, country and reputation, the request at now with status, +// and how many requests netblock, the client's, has sent. +func (rq *request) withClientNotes( + notes bans.Notes, + now time.Time, + status int, + netblock netip.Prefix, +) bans.Notes { + notes.ASN = rq.line.ASN + notes.ASName = rq.line.ASName + notes.Country = rq.line.Country + notes.Reputation = rq.reputation + notes.Request = rq.noted(now, status) + notes.Requests = rq.netblockRequests(netblock) + + return notes +} + // wouldAlertBan reports whether the alert for a ban on netblock for cause // made at now would be sent. In observe mode the ban the request would // have made is worked out only then, at most once per