package bans_test import ( "net/netip" "testing" "time" "sneak.berlin/go/smallwebwaf/internal/bans" ) func TestBlacklistedNetblockIsBannedUntilTheGivenEnd(t *testing.T) { t.Parallel() ledger := bans.New(defaultParams()) netblock := netip.MustParsePrefix("203.0.113.9/32") now := midnight() expires := now.Add(30 * day) made := ledger.BanForAbuseIPDB( now, expires, map[netip.Prefix]int64{netblock: 95}, ) if made != 1 || ledger.Made(bans.CauseAbuseIPDB) != 1 { t.Fatalf("made %d bans, %d counted, want 1 and 1", made, ledger.Made(bans.CauseAbuseIPDB)) } ban, banned, madePermanent := ledger.Check(netblock.Addr(), now) if !banned || madePermanent || !ban.Expires.Equal(expires) || ban.Cause != bans.CauseAbuseIPDB || ban.Reason != "on AbuseIPDB's blacklist with a score of 95" || len(ban.Notes.Reputation) != 1 || ban.Notes.Reputation[0].Source != "abuseipdb" || *ban.Notes.Reputation[0].Score != 95 { t.Errorf( "ban %+v, banned %t, made permanent %t, want the blacklist's "+ "ban until %s", ban, banned, madePermanent, expires, ) } if _, banned, _ := ledger.Check(netblock.Addr(), expires); banned { t.Error("the ban refuses a request at its end") } } func TestBlacklistLeavesANetblockWithAnActiveBanAsItIs(t *testing.T) { t.Parallel() ledger := bans.New(defaultParams()) netblock := netip.MustParsePrefix("203.0.113.9/32") now := midnight() scores := map[netip.Prefix]int64{netblock: 100} 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) } // Once that ban has ended the netblock is banned, and the blacklist // fetched again makes no second ban while the first lasts. later := limitBan.Expires if made := ledger.BanForAbuseIPDB(later, later.Add(30*day), scores); made != 1 { t.Errorf("made %d bans after the ban for a limit, want 1", made) } later = later.Add(time.Hour) if made := ledger.BanForAbuseIPDB(later, later.Add(30*day), scores); made != 0 { t.Errorf("made %d bans while the blacklist's lasts, want none", made) } wantBans(t, ledger, map[netip.Prefix]int{netblock: 2}) if got := ledger.Bans(netblock)[1].Notes.EarlierBans; got != (bans.EarlierBans{ Limit: 1, }) { t.Errorf("earlier bans %+v, want 1 for a limit", got) } } func TestLiftedBlacklistBanHoldsUntilItsEnd(t *testing.T) { t.Parallel() ledger := bans.New(defaultParams()) netblock := netip.MustParsePrefix("203.0.113.9/32") now := midnight() expires := now.Add(30 * day) scores := map[netip.Prefix]int64{netblock: 100} ledger.BanForAbuseIPDB(now, expires, scores) ledger.Lift(netblock.Addr(), now) // The admin lifted it: the blacklist fetched again until its end bans // nothing. later := expires.Add(-time.Hour) if made := ledger.BanForAbuseIPDB(later, later.Add(30*day), scores); made != 0 { t.Errorf("made %d bans before the lifted ban's end, want none", made) } // From its end on, the blacklist bans the netblock again, and the // lifted ban, ended, is forgotten. if made := ledger.BanForAbuseIPDB(expires, expires.Add(30*day), scores); made != 1 { t.Errorf("made %d bans at the lifted ban's end, want 1", made) } wantBans(t, ledger, map[netip.Prefix]int{netblock: 1}) } func TestEndedBlacklistBansAreForgottenAtTheNextFetch(t *testing.T) { t.Parallel() ledger := bans.New(defaultParams()) listed := netip.MustParsePrefix("203.0.113.9/32") limited := netip.MustParsePrefix("203.0.113.10/32") now := midnight() expires := now.Add(30 * day) ledger.BanForAbuseIPDB(now, expires, map[netip.Prefix]int64{listed: 100}) 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 { t.Errorf("made %d bans for an empty blacklist, want none", made) } wantBans(t, ledger, map[netip.Prefix]int{listed: 0, limited: 1}) } func TestBlacklistBansDoNotCountTowardMaxBans(t *testing.T) { t.Parallel() params := defaultParams() params.MaxBans = 1 ledger := bans.New(params) limited := netip.MustParsePrefix("203.0.113.1/32") attacker := netip.MustParsePrefix("203.0.113.2/32") now := midnight() scores := map[netip.Prefix]int64{} for i := range 3 { scores[netip.PrefixFrom(netip.AddrFrom4([4]byte{198, 51, 100, byte(i)}), 32)] = 100 } 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) } wantBans(t, ledger, map[netip.Prefix]int{limited: 1}) // 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}) want := map[netip.Prefix]int{limited: 0, attacker: 1} for netblock := range scores { want[netblock] = 1 } wantBans(t, ledger, want) }