Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d6002e282f |
No files matched your search
@@ -54,11 +54,7 @@ func TestBlacklistLeavesANetblockWithAnActiveBanAsItIs(t *testing.T) {
|
||||
now := midnight()
|
||||
scores := map[netip.Prefix]int64{netblock: 100}
|
||||
|
||||
limitBan, _ := ledger.Ban(
|
||||
netblock,
|
||||
now,
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
)
|
||||
limitBan, _ := ledger.BanForLimit(netblock, now, bans.Notes{})
|
||||
|
||||
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)
|
||||
@@ -123,7 +119,7 @@ func TestEndedBlacklistBansAreForgottenAtTheNextFetch(t *testing.T) {
|
||||
expires := now.Add(30 * day)
|
||||
|
||||
ledger.BanForAbuseIPDB(now, expires, map[netip.Prefix]int64{listed: 100})
|
||||
ledger.Ban(limited, now, bans.Grounds{Cause: bans.CauseLimit})
|
||||
ledger.BanForLimit(limited, now, bans.Notes{})
|
||||
|
||||
// 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 {
|
||||
@@ -148,7 +144,7 @@ func TestBlacklistBansDoNotCountTowardMaxBans(t *testing.T) {
|
||||
scores[netip.PrefixFrom(netip.AddrFrom4([4]byte{198, 51, 100, byte(i)}), 32)] = 100
|
||||
}
|
||||
|
||||
ledger.Ban(limited, now, bans.Grounds{Cause: bans.CauseLimit})
|
||||
ledger.BanForLimit(limited, now, bans.Notes{})
|
||||
|
||||
if made := ledger.BanForAbuseIPDB(now, now.Add(30*day), scores); made != 3 {
|
||||
t.Errorf("made %d bans for 3 netblocks, want 3", made)
|
||||
@@ -158,7 +154,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.Ban(attacker, now, bans.Grounds{Cause: bans.CauseAttack})
|
||||
ledger.BanForAttack(attacker, now, bans.Notes{})
|
||||
|
||||
want := map[netip.Prefix]int{limited: 0, attacker: 1}
|
||||
for netblock := range scores {
|
||||
|
||||
+23
-64
@@ -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.Ban(second, midnight(), bans.Grounds{Cause: bans.CauseLimit})
|
||||
ledger.BanForLimit(second, midnight(), bans.Notes{})
|
||||
wantBans(
|
||||
t,
|
||||
ledger,
|
||||
@@ -77,7 +77,7 @@ func TestAdminsBansAreNeverDroppedAndDoNotCountTowardMaxBans(t *testing.T) {
|
||||
}
|
||||
|
||||
// And the next drops that one.
|
||||
ledger.Ban(third, midnight(), bans.Grounds{Cause: bans.CauseLimit})
|
||||
ledger.BanForLimit(third, midnight(), bans.Notes{})
|
||||
wantBans(
|
||||
t,
|
||||
ledger,
|
||||
@@ -98,28 +98,20 @@ 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.Ban(
|
||||
first, _ := ledger.BanForLimit(
|
||||
netblock,
|
||||
midnight().Add(time.Minute),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
)
|
||||
second, _ := ledger.Ban(
|
||||
netblock,
|
||||
first.Expires,
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
bans.Notes{},
|
||||
)
|
||||
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.Ban(other, second.Start, bans.Grounds{Cause: bans.CauseLimit})
|
||||
ledger.BanForLimit(other, second.Start, bans.Notes{})
|
||||
wantBans(t, ledger, map[netip.Prefix]int{netblock: 2, other: 1})
|
||||
|
||||
// The netblock's next ban still counts it.
|
||||
third, _ := ledger.Ban(
|
||||
netblock,
|
||||
second.Expires,
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
)
|
||||
third, _ := ledger.BanForLimit(netblock, second.Expires, bans.Notes{})
|
||||
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)
|
||||
@@ -135,27 +127,15 @@ 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.Ban(
|
||||
netblock,
|
||||
midnight(),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
)
|
||||
second, _ := ledger.Ban(
|
||||
netblock,
|
||||
first.Expires,
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
)
|
||||
first, _ := ledger.BanForLimit(netblock, midnight(), bans.Notes{})
|
||||
second, _ := ledger.BanForLimit(netblock, first.Expires, bans.Notes{})
|
||||
|
||||
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.Ban(
|
||||
netblock,
|
||||
second.Expires,
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
)
|
||||
third, _ := ledger.BanForLimit(netblock, second.Expires, bans.Notes{})
|
||||
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)
|
||||
@@ -167,37 +147,20 @@ func TestReasonOfTheBansSwwMakes(t *testing.T) {
|
||||
|
||||
ledger := bans.New(defaultParams())
|
||||
|
||||
limit, _ := ledger.Ban(
|
||||
limit, _ := ledger.BanForLimit(
|
||||
netip.MustParsePrefix("203.0.113.1/32"),
|
||||
midnight(),
|
||||
bans.Grounds{
|
||||
Cause: bans.CauseLimit,
|
||||
Notes: bans.Notes{
|
||||
Kind: "requests",
|
||||
Limit: 1000,
|
||||
Window: "minute",
|
||||
},
|
||||
},
|
||||
bans.Notes{Kind: "requests", Limit: 1000, Window: "minute"},
|
||||
)
|
||||
byteLimit, _ := ledger.Ban(
|
||||
byteLimit, _ := ledger.BanForLimit(
|
||||
netip.MustParsePrefix("203.0.113.3/32"),
|
||||
midnight(),
|
||||
bans.Grounds{
|
||||
Cause: bans.CauseLimit,
|
||||
Notes: bans.Notes{
|
||||
Kind: "bytes",
|
||||
Limit: 10 << 30,
|
||||
Window: "hour",
|
||||
},
|
||||
},
|
||||
bans.Notes{Kind: "bytes", Limit: 10 << 30, Window: "hour"},
|
||||
)
|
||||
attack, _ := ledger.Ban(
|
||||
attack, _ := ledger.BanForAttack(
|
||||
netip.MustParsePrefix("203.0.113.2/32"),
|
||||
midnight(),
|
||||
bans.Grounds{
|
||||
Cause: bans.CauseAttack,
|
||||
Notes: bans.Notes{RuleID: "git-dir", Target: "path"},
|
||||
},
|
||||
bans.Notes{RuleID: "git-dir", Target: "path"},
|
||||
)
|
||||
|
||||
for _, tc := range []struct{ got, want string }{
|
||||
@@ -237,7 +200,7 @@ func TestLiftedBanForALimitRefusesNothingAndMakesNoBanLonger(t *testing.T) {
|
||||
t.Error("the lifted ban refuses")
|
||||
}
|
||||
|
||||
ban, _ := ledger.Ban(netblock, now, bans.Grounds{Cause: bans.CauseLimit})
|
||||
ban, _ := ledger.BanForLimit(netblock, now, bans.Notes{})
|
||||
if ban.Expires.Sub(ban.Start) != time.Hour ||
|
||||
ban.Notes.EarlierBans != (bans.EarlierBans{Limit: 1}) {
|
||||
t.Errorf(
|
||||
@@ -288,11 +251,7 @@ func TestLiftedBanForAnAttackRefusesNothingAndMakesNoBanLonger(
|
||||
}
|
||||
|
||||
// The next clear sign of attack bans for seven days, as a first does.
|
||||
ban, _ := ledger.Ban(
|
||||
netblock,
|
||||
now,
|
||||
bans.Grounds{Cause: bans.CauseAttack},
|
||||
)
|
||||
ban, _ := ledger.BanForAttack(netblock, now, bans.Notes{})
|
||||
if ban.Expires.Sub(ban.Start) != 7*day {
|
||||
t.Errorf(
|
||||
"the next ban for an attack ends at %s, want seven days on",
|
||||
@@ -305,10 +264,10 @@ func TestLoadEditCountsTheBansAnAdminMade(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ledger := bans.New(defaultParams())
|
||||
made, _ := ledger.Ban(
|
||||
made, _ := ledger.BanForLimit(
|
||||
netip.MustParsePrefix("203.0.113.1/32"),
|
||||
midnight(),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
bans.Notes{},
|
||||
)
|
||||
atStart := bans.Ban{
|
||||
Netblock: netip.MustParsePrefix("203.0.113.2/32"),
|
||||
@@ -352,7 +311,7 @@ func TestAdminsBanIsMadeWhileAnotherLasts(t *testing.T) {
|
||||
ledger := bans.New(defaultParams())
|
||||
|
||||
// An hour's ban for a broken limit.
|
||||
ledger.Ban(netblock, midnight(), bans.Grounds{Cause: bans.CauseLimit})
|
||||
ledger.BanForLimit(netblock, midnight(), bans.Notes{})
|
||||
wantChanged(t, ledger, true)
|
||||
|
||||
// A minute later an admin bans the netblock for good, named by an
|
||||
@@ -472,10 +431,10 @@ func TestPermanentBanOnAWiderNetblockIsReturnedOverATemporaryBan(
|
||||
ledger := bans.New(defaultParams())
|
||||
|
||||
// The temporary ban is made first, so that its netblock is looked at first.
|
||||
ledger.Ban(
|
||||
ledger.BanForLimit(
|
||||
netip.MustParsePrefix("203.0.113.9/32"),
|
||||
midnight(),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
bans.Notes{},
|
||||
)
|
||||
ledger.BanForAdmin(
|
||||
netip.MustParsePrefix("203.0.113.0/24"),
|
||||
|
||||
+82
-54
@@ -300,50 +300,81 @@ func activeBan(bans []Ban, now time.Time) *Ban {
|
||||
return newest
|
||||
}
|
||||
|
||||
// Grounds are why a request makes a ban: its cause, CauseLimit or
|
||||
// CauseAttack, and its notes.
|
||||
type Grounds struct {
|
||||
Cause string
|
||||
Notes Notes
|
||||
// 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
|
||||
// "<Kind> per <Window> over the limit of <Limit>", 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,
|
||||
)
|
||||
}
|
||||
|
||||
// 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 "<Kind> per <Window> over the limit of <Limit>", 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 <RuleID>", or "asked for the trap
|
||||
// path <TrapPath>".
|
||||
func (l *Ledger) Ban(
|
||||
netblock netip.Prefix, now time.Time, grounds Grounds,
|
||||
// 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, bool) {
|
||||
return l.ban(netblock, now, grounds, true)
|
||||
return l.ban(
|
||||
netblock,
|
||||
now,
|
||||
CauseLimit,
|
||||
limitReason(notes),
|
||||
notes,
|
||||
false,
|
||||
)
|
||||
}
|
||||
|
||||
// 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,
|
||||
// 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 <RuleID>", or "asked for the trap path <TrapPath>".
|
||||
func (l *Ledger) BanForAttack(
|
||||
netblock netip.Prefix, now time.Time, notes Notes,
|
||||
) (Ban, bool) {
|
||||
return l.ban(netblock, now, grounds, false)
|
||||
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,
|
||||
)
|
||||
}
|
||||
|
||||
// WouldBePermanent reports whether a ban on netblock for cause, CauseLimit
|
||||
// or CauseAttack, made at now would be permanent, as Ban would make it. It
|
||||
// works out nothing else of the ban.
|
||||
// or CauseAttack, made at now would be permanent, as BanForLimit or
|
||||
// BanForAttack would make it. It works out nothing else of the ban.
|
||||
func (l *Ledger) WouldBePermanent(
|
||||
netblock netip.Prefix, now time.Time, cause string,
|
||||
) bool {
|
||||
@@ -380,9 +411,10 @@ 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 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.
|
||||
// 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.
|
||||
func (l *Ledger) BanForAdmin(
|
||||
netblock netip.Prefix, now, expires time.Time, reason string,
|
||||
) Ban {
|
||||
@@ -727,13 +759,15 @@ func (l *Ledger) forgetEndedAbuseIPDBBans(now time.Time) bool {
|
||||
return forgot
|
||||
}
|
||||
|
||||
// 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.
|
||||
// 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.
|
||||
func (l *Ledger) ban(
|
||||
netblock netip.Prefix,
|
||||
now time.Time,
|
||||
grounds Grounds,
|
||||
cause, reason string,
|
||||
notes Notes,
|
||||
keep bool,
|
||||
) (Ban, bool) {
|
||||
l.mu.Lock()
|
||||
@@ -742,8 +776,6 @@ 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)
|
||||
@@ -759,19 +791,15 @@ func (l *Ledger) ban(
|
||||
ban := Ban{
|
||||
Netblock: netblock,
|
||||
Start: now,
|
||||
Cause: grounds.Cause,
|
||||
Cause: cause,
|
||||
Reason: reason,
|
||||
Notes: notes,
|
||||
}
|
||||
|
||||
switch grounds.Cause {
|
||||
case CauseLimit:
|
||||
ban.Reason = limitReason(notes)
|
||||
ban.Expires = l.limitExpiry(held, now)
|
||||
case CauseAttack:
|
||||
ban.Reason = attackReason(notes)
|
||||
if cause == CauseAttack {
|
||||
ban.Expires = l.attackExpiry(held, now)
|
||||
default:
|
||||
panic("bans: a request's ban has the cause " + grounds.Cause)
|
||||
} else {
|
||||
ban.Expires = l.limitExpiry(held, now)
|
||||
}
|
||||
|
||||
if !keep {
|
||||
@@ -779,7 +807,7 @@ func (l *Ledger) ban(
|
||||
}
|
||||
|
||||
l.add(ban)
|
||||
l.made[grounds.Cause]++
|
||||
l.made[cause]++
|
||||
l.markChanged()
|
||||
|
||||
return ban, true
|
||||
|
||||
+51
-121
@@ -22,11 +22,7 @@ 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.Ban(
|
||||
netblock,
|
||||
now,
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
)
|
||||
ban, _ := ledger.BanForLimit(netblock, now, bans.Notes{})
|
||||
|
||||
length := time.Duration(hours) * time.Hour
|
||||
if !ban.Expires.Equal(now.Add(length)) ||
|
||||
@@ -46,7 +42,7 @@ func TestRepeatsTripleUntilPermanent(t *testing.T) {
|
||||
|
||||
// The sixth would last 243 hours, more than seven days: it is
|
||||
// permanent, and never ends.
|
||||
ban, _ := ledger.Ban(netblock, now, bans.Grounds{Cause: bans.CauseLimit})
|
||||
ban, _ := ledger.BanForLimit(netblock, now, bans.Notes{})
|
||||
if !ban.Permanent() {
|
||||
t.Fatalf("sixth ban ends at %s, want a permanent one", ban.Expires)
|
||||
}
|
||||
@@ -75,15 +71,15 @@ func TestRepeatWindowRunsOut(t *testing.T) {
|
||||
ledger := bans.New(defaultParams())
|
||||
netblock := netip.MustParsePrefix("203.0.113.9/32")
|
||||
|
||||
first, _ := ledger.Ban(
|
||||
first, _ := ledger.BanForLimit(
|
||||
netblock,
|
||||
midnight(),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
bans.Notes{},
|
||||
)
|
||||
second, _ := ledger.Ban(
|
||||
second, _ := ledger.BanForLimit(
|
||||
netblock,
|
||||
first.Expires.Add(tc.gap),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
bans.Notes{},
|
||||
)
|
||||
|
||||
if second.Expires.Sub(second.Start) != tc.want ||
|
||||
@@ -108,10 +104,10 @@ func TestFirstBanLongerThanLimitBanPermanentAboveIsPermanent(t *testing.T) {
|
||||
params.LimitBanDuration = params.LimitBanPermanentAbove + time.Hour
|
||||
ledger := bans.New(params)
|
||||
|
||||
ban, _ := ledger.Ban(
|
||||
ban, _ := ledger.BanForLimit(
|
||||
netip.MustParsePrefix("203.0.113.9/32"),
|
||||
midnight(),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
bans.Notes{},
|
||||
)
|
||||
if !ban.Permanent() {
|
||||
t.Errorf("first ban ends at %s, want a permanent one", ban.Expires)
|
||||
@@ -131,11 +127,7 @@ func TestLimitBanPermanentAboveSetFarOffDoesNotOverflow(t *testing.T) {
|
||||
now := midnight()
|
||||
|
||||
for i := range 14 {
|
||||
ban, _ := ledger.Ban(
|
||||
netblock,
|
||||
now,
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
)
|
||||
ban, _ := ledger.BanForLimit(netblock, now, bans.Notes{})
|
||||
if !ban.Expires.After(ban.Start) {
|
||||
t.Fatalf(
|
||||
"ban %d starts at %s and ends at %s",
|
||||
@@ -148,7 +140,7 @@ func TestLimitBanPermanentAboveSetFarOffDoesNotOverflow(t *testing.T) {
|
||||
now = ban.Expires
|
||||
}
|
||||
|
||||
ban, _ := ledger.Ban(netblock, now, bans.Grounds{Cause: bans.CauseLimit})
|
||||
ban, _ := ledger.BanForLimit(netblock, now, bans.Notes{})
|
||||
if !ban.Permanent() {
|
||||
t.Errorf("15th ban ends at %s, want a permanent one", ban.Expires)
|
||||
}
|
||||
@@ -160,19 +152,15 @@ func TestBrokenLimitDuringABanMakesNoOther(t *testing.T) {
|
||||
ledger := bans.New(defaultParams())
|
||||
netblock := netip.MustParsePrefix("203.0.113.9/32")
|
||||
|
||||
first, made := ledger.Ban(
|
||||
netblock,
|
||||
midnight(),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
)
|
||||
first, made := ledger.BanForLimit(netblock, midnight(), bans.Notes{})
|
||||
if !made {
|
||||
t.Error("the first ban was not made")
|
||||
}
|
||||
|
||||
again, made := ledger.Ban(
|
||||
again, made := ledger.BanForLimit(
|
||||
netblock,
|
||||
midnight().Add(time.Minute),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
bans.Notes{},
|
||||
)
|
||||
|
||||
if made || !reflect.DeepEqual(again, first) ||
|
||||
@@ -187,10 +175,10 @@ func TestBrokenLimitDuringABanMakesNoOther(t *testing.T) {
|
||||
)
|
||||
}
|
||||
|
||||
again, made = ledger.Ban(
|
||||
again, made = ledger.BanForAttack(
|
||||
netblock,
|
||||
midnight().Add(time.Minute),
|
||||
bans.Grounds{Cause: bans.CauseAttack},
|
||||
bans.Notes{},
|
||||
)
|
||||
if made || !reflect.DeepEqual(again, first) {
|
||||
t.Errorf(
|
||||
@@ -207,10 +195,10 @@ func TestCheckRefusesWhileTheBanLastsAndCountsTheRefusals(t *testing.T) {
|
||||
|
||||
ledger := bans.New(defaultParams())
|
||||
netblock := netip.MustParsePrefix("203.0.113.9/32")
|
||||
ban, _ := ledger.Ban(
|
||||
ban, _ := ledger.BanForLimit(
|
||||
netblock,
|
||||
midnight(),
|
||||
bans.Grounds{Cause: bans.CauseLimit, Notes: bans.Notes{Requests: 5}},
|
||||
bans.Notes{Requests: 5},
|
||||
)
|
||||
|
||||
for range 3 {
|
||||
@@ -249,10 +237,10 @@ func TestFindCountsNothing(t *testing.T) {
|
||||
|
||||
ledger := bans.New(defaultParams())
|
||||
netblock := netip.MustParsePrefix("203.0.113.9/32")
|
||||
ban, _ := ledger.Ban(
|
||||
ban, _ := ledger.BanForLimit(
|
||||
netblock,
|
||||
midnight(),
|
||||
bans.Grounds{Cause: bans.CauseLimit, Notes: bans.Notes{Requests: 5}},
|
||||
bans.Notes{Requests: 5},
|
||||
)
|
||||
|
||||
got, banned, _ := ledger.Find(
|
||||
@@ -299,24 +287,24 @@ func TestMaxBansDropsTheEarliestBanOfTheNetblockSeenLongestAgo(
|
||||
d := netip.MustParsePrefix("2001:db8::/64")
|
||||
now := midnight()
|
||||
|
||||
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})
|
||||
first, _ := ledger.BanForLimit(a, now, bans.Notes{})
|
||||
ledger.BanForLimit(b, now, bans.Notes{})
|
||||
ledger.BanForLimit(c, now, bans.Notes{})
|
||||
|
||||
// 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.Ban(d, now, bans.Grounds{Cause: bans.CauseLimit})
|
||||
ledger.BanForLimit(d, now, bans.Notes{})
|
||||
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.Ban(a, first.Expires, bans.Grounds{Cause: bans.CauseLimit})
|
||||
ledger.BanForLimit(a, first.Expires, bans.Notes{})
|
||||
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.Ban(b, first.Expires, bans.Grounds{Cause: bans.CauseLimit})
|
||||
ledger.BanForLimit(b, first.Expires, bans.Notes{})
|
||||
wantBans(t, ledger, map[netip.Prefix]int{a: 1, b: 1, d: 1})
|
||||
|
||||
if !ledger.Bans(a)[0].Start.Equal(first.Expires) {
|
||||
@@ -337,16 +325,8 @@ func TestFullLedgerDropsTheEarlierBanOfTheNetblockBannedAgain(t *testing.T) {
|
||||
ledger := bans.New(params)
|
||||
netblock := netip.MustParsePrefix("203.0.113.9/32")
|
||||
|
||||
first, _ := ledger.Ban(
|
||||
netblock,
|
||||
midnight(),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
)
|
||||
second, _ := ledger.Ban(
|
||||
netblock,
|
||||
first.Expires,
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
)
|
||||
first, _ := ledger.BanForLimit(netblock, midnight(), bans.Notes{})
|
||||
second, _ := ledger.BanForLimit(netblock, first.Expires, bans.Notes{})
|
||||
|
||||
held := ledger.Bans(netblock)
|
||||
if len(held) != 1 || !reflect.DeepEqual(held[0], second) ||
|
||||
@@ -363,11 +343,7 @@ func TestRequestDuringAnAttackBanMakesItPermanent(t *testing.T) {
|
||||
netblock := netip.MustParsePrefix("203.0.113.9/32")
|
||||
notes := bans.Notes{RuleID: "env-file", Target: "path"}
|
||||
|
||||
ban, _ := ledger.Ban(
|
||||
netblock,
|
||||
midnight(),
|
||||
bans.Grounds{Cause: bans.CauseAttack, Notes: notes},
|
||||
)
|
||||
ban, _ := ledger.BanForAttack(netblock, midnight(), notes)
|
||||
if !ban.Expires.Equal(midnight().Add(7*day)) ||
|
||||
ban.Cause != bans.CauseAttack ||
|
||||
ban.Notes.RuleID != "env-file" ||
|
||||
@@ -442,16 +418,8 @@ 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.Ban(
|
||||
netblock,
|
||||
midnight(),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
)
|
||||
second, _ := ledger.Ban(
|
||||
netblock,
|
||||
first.Expires,
|
||||
bans.Grounds{Cause: bans.CauseAttack},
|
||||
)
|
||||
first, _ := ledger.BanForLimit(netblock, midnight(), bans.Notes{})
|
||||
second, _ := ledger.BanForAttack(netblock, first.Expires, bans.Notes{})
|
||||
|
||||
if second.Expires.Sub(second.Start) != 7*day {
|
||||
t.Fatalf("the first ban for an attack lasts %s, want 7 days",
|
||||
@@ -467,10 +435,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.Ban(
|
||||
third, _ := ledger.BanForAttack(
|
||||
netblock,
|
||||
second.Expires.Add(30*day),
|
||||
bans.Grounds{Cause: bans.CauseAttack},
|
||||
bans.Notes{},
|
||||
)
|
||||
if !third.Permanent() ||
|
||||
third.Notes.EarlierBans != (bans.EarlierBans{Limit: 1, Attack: 1}) {
|
||||
@@ -484,18 +452,14 @@ func TestWouldBanGivesTheBanWithoutMakingIt(t *testing.T) {
|
||||
|
||||
ledger := bans.New(defaultParams())
|
||||
netblock := netip.MustParsePrefix("203.0.113.9/32")
|
||||
first, _ := ledger.Ban(
|
||||
netblock,
|
||||
midnight(),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
)
|
||||
first, _ := ledger.BanForLimit(netblock, midnight(), bans.Notes{})
|
||||
wantChanged(t, ledger, true)
|
||||
|
||||
// While the first ban lasts, none would be made.
|
||||
during, would := ledger.WouldBan(
|
||||
during, would := ledger.WouldBanForAttack(
|
||||
netblock,
|
||||
midnight(),
|
||||
bans.Grounds{Cause: bans.CauseAttack},
|
||||
bans.Notes{},
|
||||
)
|
||||
if would || !reflect.DeepEqual(during, first) {
|
||||
t.Errorf(
|
||||
@@ -508,19 +472,13 @@ 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.
|
||||
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(
|
||||
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(
|
||||
netblock,
|
||||
first.Expires,
|
||||
limitGrounds,
|
||||
limitNotes,
|
||||
)
|
||||
|
||||
if !wouldAttack || !attack.Expires.Equal(first.Expires.Add(7*day)) ||
|
||||
@@ -547,7 +505,7 @@ func TestWouldBanGivesTheBanWithoutMakingIt(t *testing.T) {
|
||||
wantChanged(t, ledger, false)
|
||||
|
||||
// The ban made is the one that would have been.
|
||||
made, _ := ledger.Ban(netblock, first.Expires, limitGrounds)
|
||||
made, _ := ledger.BanForLimit(netblock, first.Expires, limitNotes)
|
||||
if !reflect.DeepEqual(made, limit) {
|
||||
t.Errorf("the ban made is %+v, want %+v", made, limit)
|
||||
}
|
||||
@@ -568,11 +526,7 @@ func TestWouldBePermanentAnswersAsTheBanWouldBeMade(t *testing.T) {
|
||||
t.Fatalf("ban %d for a limit would be permanent", i+1)
|
||||
}
|
||||
|
||||
ban, _ := ledger.Ban(
|
||||
netblock,
|
||||
now,
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
)
|
||||
ban, _ := ledger.BanForLimit(netblock, now, bans.Notes{})
|
||||
now = ban.Expires
|
||||
}
|
||||
|
||||
@@ -585,11 +539,7 @@ func TestWouldBePermanentAnswersAsTheBanWouldBeMade(t *testing.T) {
|
||||
}
|
||||
|
||||
// Once a first ban for an attack has ended, the next would be permanent.
|
||||
attack, _ := ledger.Ban(
|
||||
netblock,
|
||||
now,
|
||||
bans.Grounds{Cause: bans.CauseAttack},
|
||||
)
|
||||
attack, _ := ledger.BanForAttack(netblock, now, bans.Notes{})
|
||||
if !ledger.WouldBePermanent(netblock, attack.Expires, bans.CauseAttack) {
|
||||
t.Error("a second ban for an attack would not be permanent")
|
||||
}
|
||||
@@ -603,16 +553,8 @@ 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.Ban(
|
||||
netblock,
|
||||
midnight(),
|
||||
bans.Grounds{Cause: bans.CauseAttack},
|
||||
)
|
||||
limit, _ := ledger.Ban(
|
||||
netblock,
|
||||
attack.Expires,
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
)
|
||||
attack, _ := ledger.BanForAttack(netblock, midnight(), bans.Notes{})
|
||||
limit, _ := ledger.BanForLimit(netblock, attack.Expires, bans.Notes{})
|
||||
|
||||
if limit.Expires.Sub(limit.Start) != time.Hour ||
|
||||
limit.Cause != bans.CauseLimit {
|
||||
@@ -636,13 +578,10 @@ func TestRequestTextsAreCutTo256Bytes(t *testing.T) {
|
||||
Time: midnight(), Method: long, Host: long, Path: long, Status: 403, UserAgent: long,
|
||||
}
|
||||
|
||||
ban, _ := ledger.Ban(
|
||||
ban, _ := ledger.BanForLimit(
|
||||
netblock,
|
||||
midnight(),
|
||||
bans.Grounds{
|
||||
Cause: bans.CauseLimit,
|
||||
Notes: bans.Notes{Request: request},
|
||||
},
|
||||
bans.Notes{Request: request},
|
||||
)
|
||||
|
||||
cut := long[:256]
|
||||
@@ -668,20 +607,11 @@ 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.Ban(netblock, midnight(), bans.Grounds{
|
||||
Cause: bans.CauseLimit,
|
||||
Notes: bans.Notes{
|
||||
ASN: "AS64497",
|
||||
ASName: "Other Net",
|
||||
Country: "FR",
|
||||
},
|
||||
ledger.BanForLimit(netblock, midnight(), bans.Notes{
|
||||
ASN: "AS64497", ASName: "Other Net", Country: "FR",
|
||||
})
|
||||
ledger.Ban(
|
||||
netblock,
|
||||
midnight().Add(time.Hour),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
)
|
||||
ledger.Ban(other, midnight(), bans.Grounds{Cause: bans.CauseLimit})
|
||||
ledger.BanForLimit(netblock, midnight().Add(time.Hour), bans.Notes{})
|
||||
ledger.BanForLimit(other, midnight(), bans.Notes{})
|
||||
|
||||
ledger.AddLookup(netblock, "AS64496", "Example Net", "DE")
|
||||
|
||||
|
||||
@@ -19,29 +19,25 @@ func TestChangedAfterABanIsMade(t *testing.T) {
|
||||
|
||||
wantChanged(t, ledger, false)
|
||||
|
||||
ledger.Ban(netblock, midnight(), bans.Grounds{Cause: bans.CauseLimit})
|
||||
ledger.BanForLimit(netblock, midnight(), bans.Notes{})
|
||||
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.Ban(
|
||||
netblock,
|
||||
midnight().Add(time.Minute),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
)
|
||||
ledger.BanForLimit(netblock, midnight().Add(time.Minute), bans.Notes{})
|
||||
ledger.Check(netblock.Addr(), midnight().Add(time.Minute))
|
||||
wantChanged(t, ledger, false)
|
||||
|
||||
// Two bans before the value is read leave one.
|
||||
ledger.Ban(
|
||||
ledger.BanForLimit(
|
||||
netip.MustParsePrefix("203.0.113.10/32"),
|
||||
midnight(),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
bans.Notes{},
|
||||
)
|
||||
ledger.Ban(
|
||||
ledger.BanForLimit(
|
||||
netip.MustParsePrefix("203.0.113.11/32"),
|
||||
midnight(),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
bans.Notes{},
|
||||
)
|
||||
wantChanged(t, ledger, true)
|
||||
wantChanged(t, ledger, false)
|
||||
@@ -55,14 +51,10 @@ func TestSnapshotListsEveryBanByNetblock(t *testing.T) {
|
||||
high := netip.MustParsePrefix("203.0.113.10/32")
|
||||
low := netip.MustParsePrefix("203.0.113.9/32")
|
||||
|
||||
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})
|
||||
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{})
|
||||
|
||||
snapshot := ledger.Snapshot()
|
||||
|
||||
@@ -88,10 +80,7 @@ func TestLoadedBansCarryOn(t *testing.T) {
|
||||
|
||||
before := bans.New(defaultParams())
|
||||
netblock := netip.MustParsePrefix("203.0.113.9/32")
|
||||
ban, _ := before.Ban(netblock, midnight(), bans.Grounds{
|
||||
Cause: bans.CauseLimit,
|
||||
Notes: bans.Notes{Limit: 1},
|
||||
})
|
||||
ban, _ := before.BanForLimit(netblock, midnight(), 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
|
||||
@@ -107,11 +96,7 @@ func TestLoadedBansCarryOn(t *testing.T) {
|
||||
t.Error("the loaded ban does not refuse")
|
||||
}
|
||||
|
||||
again, _ := after.Ban(
|
||||
netblock,
|
||||
ban.Expires,
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
)
|
||||
again, _ := after.BanForLimit(netblock, ban.Expires, bans.Notes{})
|
||||
if again.Expires.Sub(again.Start) != 3*time.Hour ||
|
||||
again.Notes.EarlierBans != (bans.EarlierBans{Limit: 1}) {
|
||||
t.Errorf(
|
||||
@@ -139,10 +124,10 @@ func TestLoadedBanRefusesEveryClientInItsNetblock(t *testing.T) {
|
||||
Start: midnight(),
|
||||
},
|
||||
})
|
||||
ledger.Ban(
|
||||
ledger.BanForLimit(
|
||||
netip.MustParsePrefix("198.51.100.7/32"),
|
||||
midnight(),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
bans.Notes{},
|
||||
)
|
||||
|
||||
for client, want := range map[string]bool{
|
||||
@@ -215,7 +200,7 @@ func TestPermanentBanStartedBeforeAnEndedOneRefuses(t *testing.T) {
|
||||
}
|
||||
|
||||
// A limit broken now makes no shorter ban over the permanent one.
|
||||
ban, _ = ledger.Ban(netblock, now, bans.Grounds{Cause: bans.CauseLimit})
|
||||
ban, _ = ledger.BanForLimit(netblock, now, bans.Notes{})
|
||||
if !ban.Permanent() || len(ledger.Bans(netblock)) != 2 {
|
||||
t.Errorf(
|
||||
"a broken limit returned %+v and left the netblock %d bans, "+
|
||||
@@ -251,10 +236,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.Ban(
|
||||
ban, _ := ledger.BanForLimit(
|
||||
netblock,
|
||||
nineHours.Expires.Add(time.Hour),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
bans.Notes{},
|
||||
)
|
||||
if ban.Expires.Sub(ban.Start) != 27*time.Hour ||
|
||||
ban.Notes.EarlierBans != (bans.EarlierBans{Limit: 3, Admin: 1}) {
|
||||
@@ -328,15 +313,15 @@ func TestLoadReplacesTheBansHeld(t *testing.T) {
|
||||
}
|
||||
|
||||
// The ledger holds one ban, so it makes two more without dropping any.
|
||||
first, _ := ledger.Ban(
|
||||
first, _ := ledger.BanForLimit(
|
||||
netip.MustParsePrefix("198.51.100.7/32"),
|
||||
midnight(),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
bans.Notes{},
|
||||
)
|
||||
second, _ := ledger.Ban(
|
||||
second, _ := ledger.BanForLimit(
|
||||
netip.MustParsePrefix("198.51.100.8/32"),
|
||||
midnight(),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
bans.Notes{},
|
||||
)
|
||||
|
||||
want := []bans.Ban{first, second, kept}
|
||||
|
||||
@@ -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.Ban(
|
||||
first, _ := params.Ledger.BanForLimit(
|
||||
netip.MustParsePrefix("203.0.113.9/32"),
|
||||
midnight(),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
bans.Notes{},
|
||||
)
|
||||
|
||||
time.Sleep(5 * time.Second)
|
||||
|
||||
second, _ := params.Ledger.Ban(
|
||||
second, _ := params.Ledger.BanForLimit(
|
||||
netip.MustParsePrefix("203.0.113.10/32"),
|
||||
midnight(),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
bans.Notes{},
|
||||
)
|
||||
|
||||
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.Ban(netip.MustParsePrefix("203.0.113.9/32"),
|
||||
midnight(), bans.Grounds{Cause: bans.CauseLimit})
|
||||
params.Ledger.BanForLimit(netip.MustParsePrefix("203.0.113.9/32"),
|
||||
midnight(), bans.Notes{})
|
||||
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.Ban(netip.MustParsePrefix("203.0.113.10/32"),
|
||||
midnight(), bans.Grounds{Cause: bans.CauseLimit})
|
||||
params.Ledger.BanForLimit(netip.MustParsePrefix("203.0.113.10/32"),
|
||||
midnight(), bans.Notes{})
|
||||
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.Ban(
|
||||
params.Ledger.BanForLimit(
|
||||
netip.MustParsePrefix("203.0.113.9/32"),
|
||||
midnight(),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
bans.Notes{},
|
||||
)
|
||||
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.Ban(
|
||||
params.Ledger.BanForLimit(
|
||||
netip.MustParsePrefix("203.0.113.9/32"),
|
||||
midnight(),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
bans.Notes{},
|
||||
)
|
||||
|
||||
err = files.WriteAll()
|
||||
@@ -2135,11 +2135,7 @@ func TestBanLiftedByAnEditWhileRunning(t *testing.T) {
|
||||
|
||||
// The ban that liftedBansJSON lifts, before it is lifted.
|
||||
netblock := netip.MustParsePrefix(liftedClient + "/32")
|
||||
params.Ledger.Ban(
|
||||
netblock,
|
||||
midnight(),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
)
|
||||
params.Ledger.BanForLimit(netblock, midnight(), bans.Notes{})
|
||||
|
||||
_, banned, _ := params.Ledger.Find(netblock.Addr(), afterLifting())
|
||||
if !banned {
|
||||
@@ -2514,15 +2510,11 @@ func fill(params state.Params) {
|
||||
client := netip.MustParsePrefix("203.0.113.9/32")
|
||||
|
||||
params.Ledger.Load([]bans.Ban{permanentBan()})
|
||||
params.Ledger.Ban(client, now, bans.Grounds{
|
||||
Cause: bans.CauseLimit,
|
||||
Notes: bans.Notes{ASN: asn, ASName: asName, Country: "DE", Limit: 1},
|
||||
params.Ledger.BanForLimit(client, now, bans.Notes{
|
||||
ASN: asn, ASName: asName, Country: "DE", Limit: 1,
|
||||
})
|
||||
params.Ledger.Ban(netip.MustParsePrefix("192.0.2.1/32"), now,
|
||||
bans.Grounds{
|
||||
Cause: bans.CauseAttack,
|
||||
Notes: bans.Notes{RuleID: "env-file", Target: "path"},
|
||||
})
|
||||
params.Ledger.BanForAttack(netip.MustParsePrefix("192.0.2.1/32"), now,
|
||||
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)
|
||||
@@ -2708,11 +2700,7 @@ func wantLiftedBanKept(
|
||||
}
|
||||
|
||||
// Were the lifted ban counted, the next would last three hours.
|
||||
ban, _ := ledger.Ban(
|
||||
netblock,
|
||||
afterLifting(),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
)
|
||||
ban, _ := ledger.BanForLimit(netblock, afterLifting(), bans.Notes{})
|
||||
if ban.Expires.Sub(ban.Start) != time.Hour {
|
||||
t.Errorf(
|
||||
"the next ban lasts %s, want 1h",
|
||||
|
||||
@@ -37,10 +37,10 @@ func TestAdminEndpointsAreOffWhileTheTokenIsUnset(t *testing.T) {
|
||||
"",
|
||||
map[string]string{metricsToken: token},
|
||||
)
|
||||
server.Ledger.Ban(
|
||||
server.Ledger.BanForLimit(
|
||||
netip.MustParsePrefix(otherClient+"/32"),
|
||||
clk.Now(),
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
bans.Notes{},
|
||||
)
|
||||
before := server.Ledger.Snapshot()
|
||||
|
||||
|
||||
@@ -130,9 +130,11 @@ 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.Ban(group, start, bans.Grounds{
|
||||
Cause: bans.CauseAttack, Notes: bans.Notes{RuleID: "probe"},
|
||||
})
|
||||
attackBan, _ := server.Ledger.BanForAttack(
|
||||
group,
|
||||
start,
|
||||
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.
|
||||
|
||||
@@ -686,10 +686,10 @@ func askDirectly(
|
||||
|
||||
// banClient bans client at now.
|
||||
func banClient(_ *testing.T, server *waf.Server, now time.Time) {
|
||||
server.Ledger.Ban(
|
||||
server.Ledger.BanForLimit(
|
||||
netip.MustParsePrefix(client+"/32"),
|
||||
now,
|
||||
bans.Grounds{Cause: bans.CauseLimit},
|
||||
bans.Notes{},
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
+53
-72
@@ -103,8 +103,9 @@ 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, banClient says what
|
||||
// happens instead.
|
||||
// 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.
|
||||
func (rq *request) banForLimit(
|
||||
now time.Time,
|
||||
hit ratelimit.Hit,
|
||||
@@ -122,11 +123,23 @@ 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{
|
||||
Kind: hit.Kind,
|
||||
Limit: hit.Limit,
|
||||
Window: hit.Window,
|
||||
Count: hit.Count,
|
||||
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),
|
||||
}
|
||||
|
||||
switch hit.Kind {
|
||||
@@ -136,54 +149,47 @@ func (rq *request) banForLimit(
|
||||
notes.LimitPercent, notes.LimitPercentSetting = rq.bytesLimitPercent.logged()
|
||||
}
|
||||
|
||||
rq.banClient(
|
||||
now,
|
||||
status,
|
||||
bans.Grounds{Cause: bans.CauseLimit, Notes: notes},
|
||||
)
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
// 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 returns the action for that ban. In observe mode, banClient
|
||||
// says what happens instead.
|
||||
// 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.
|
||||
func (rq *request) banForAttack(now time.Time, notes bans.Notes) string {
|
||||
return rq.banClient(
|
||||
now,
|
||||
rq.h.config.BanResponse,
|
||||
bans.Grounds{Cause: bans.CauseAttack, Notes: notes},
|
||||
)
|
||||
}
|
||||
|
||||
// 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 &&
|
||||
!rq.wouldAlertBan(netblock, now, bans.CauseAttack) {
|
||||
return banAction(
|
||||
rq.h.ledger.WouldBePermanent(netblock, now, bans.CauseAttack),
|
||||
)
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
if rq.h.config.Observe {
|
||||
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)
|
||||
ban, wouldBan := rq.h.ledger.WouldBanForAttack(netblock, now, notes)
|
||||
if wouldBan {
|
||||
rq.alertBan(ban)
|
||||
}
|
||||
@@ -191,13 +197,7 @@ func (rq *request) banClient(
|
||||
return banAction(ban.Permanent())
|
||||
}
|
||||
|
||||
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))
|
||||
}
|
||||
|
||||
ban, made := rq.h.ledger.BanForAttack(netblock, now, notes)
|
||||
rq.line.BanExpires = banExpires(ban)
|
||||
|
||||
if made {
|
||||
@@ -207,25 +207,6 @@ func (rq *request) banClient(
|
||||
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
|
||||
|
||||
@@ -104,12 +104,6 @@ func onReport() []heuristic {
|
||||
}
|
||||
}
|
||||
|
||||
// runAfterAnswer runs the heuristics of afterAnswer on the request, at the time
|
||||
// it is called.
|
||||
func (rq *request) runAfterAnswer(ctx context.Context) {
|
||||
rq.runBanningHeuristics(ctx, afterAnswer())
|
||||
}
|
||||
|
||||
// runHeuristics runs list, one of those checkClient runs, on the request at now,
|
||||
// and returns the first verdict, or nil when none gives one. The log line's
|
||||
// heuristic and heuristic_figures name the heuristic that gave it, and give its
|
||||
|
||||
@@ -345,6 +345,16 @@ func timing(start, end time.Time) *float64 {
|
||||
return new(requestlog.Milliseconds(end.Sub(start)))
|
||||
}
|
||||
|
||||
// banAndCount runs list, afterAnswer or onReport, whose heuristics only ban.
|
||||
// It then counts the request for the anomaly thresholds and adds it to its
|
||||
// client's history. list runs first, so that a refusal limit it finds broken
|
||||
// is in that history. ctx is the request's own context.
|
||||
func (rq *request) banAndCount(ctx context.Context, list []heuristic) {
|
||||
rq.runBanningHeuristics(ctx, list)
|
||||
rq.countAnomalies()
|
||||
rq.addToHistory()
|
||||
}
|
||||
|
||||
// addToHistory adds the request, which has ended, to its client's
|
||||
// history, and counts its offences in the metrics, and then the lookup's
|
||||
// answer about the client, as answerAtTheEnd gives it, to that history and
|
||||
|
||||
+21
-28
@@ -177,11 +177,13 @@ func (h *handler) answerQuestion(w http.ResponseWriter, r *http.Request) {
|
||||
peer.Unmap(),
|
||||
true,
|
||||
)
|
||||
// The last deferred runs first, so that what is held has the finished log
|
||||
// line. Go's server sends the answer only once this function has
|
||||
// returned, so the report cannot come before it is held.
|
||||
defer rq.holdForReport()
|
||||
defer rq.finish()
|
||||
// These run even when a check panics. finish runs first, so that what is
|
||||
// held has the finished log line. Go's server sends the answer only once
|
||||
// this function has returned, so the report cannot come before it is held.
|
||||
defer func() {
|
||||
rq.finish()
|
||||
rq.holdForReport()
|
||||
}()
|
||||
|
||||
rq.site = r.Host
|
||||
rq.line.WAFLine = requestlog.WAFLineDecision
|
||||
@@ -190,35 +192,28 @@ func (h *handler) answerQuestion(w http.ResponseWriter, r *http.Request) {
|
||||
underOwnPrefix := strings.HasPrefix(r.URL.Path, ownEndpointPrefix)
|
||||
|
||||
if refused == nil && !underOwnPrefix {
|
||||
// In observe mode, what enforce mode would have refused counts here.
|
||||
defer rq.runAfterAnswer(r.Context())
|
||||
|
||||
rq.allow()
|
||||
// In observe mode, what enforce mode would have refused counts here.
|
||||
rq.runBanningHeuristics(r.Context(), afterAnswer())
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// The last deferred runs first: runAfterAnswer before addToHistory, so that
|
||||
// a broken refusal limit is in the client's history.
|
||||
defer rq.addToHistory()
|
||||
defer rq.countAnomalies()
|
||||
defer rq.runAfterAnswer(r.Context())
|
||||
|
||||
if refused != nil {
|
||||
rq.deny(*refused)
|
||||
|
||||
return
|
||||
} else {
|
||||
// The app never sees this prefix. waf's own endpoints are at its
|
||||
// own address, so this answers as a path with no endpoint does.
|
||||
rq.line.Action = requestlog.ActionOwnEndpoint
|
||||
rq.out.Header().Set(DispositionHeader, DispositionDeny)
|
||||
http.Error(
|
||||
rq.out,
|
||||
http.StatusText(http.StatusNotFound),
|
||||
http.StatusNotFound,
|
||||
)
|
||||
}
|
||||
|
||||
// The app never sees this prefix. waf's own endpoints are at its
|
||||
// own address, so this answers as a path with no endpoint does.
|
||||
rq.line.Action = requestlog.ActionOwnEndpoint
|
||||
rq.out.Header().Set(DispositionHeader, DispositionDeny)
|
||||
http.Error(
|
||||
rq.out,
|
||||
http.StatusText(http.StatusNotFound),
|
||||
http.StatusNotFound,
|
||||
)
|
||||
rq.banAndCount(r.Context(), afterAnswer())
|
||||
}
|
||||
|
||||
// readQuestion returns the visitor's address and the body a question carries,
|
||||
@@ -491,9 +486,7 @@ func (h *handler) finishWith(
|
||||
if rq.allowed {
|
||||
rq.line.DurationUpstreamTotalMS = &report.DurationUpstreamTotalMS
|
||||
|
||||
rq.runBanningHeuristics(ctx, onReport())
|
||||
rq.countAnomalies()
|
||||
rq.addToHistory()
|
||||
rq.banAndCount(ctx, onReport())
|
||||
|
||||
rq.h.metrics.RequestReported(
|
||||
&rq.line,
|
||||
|
||||
Reference in new issue
Block a user