check / check (push) Failing after 1m37s
waf.Server held its parts as public fields only for the tests to read. The tests now keep the Params the server was built from in a test-only wafAndParts, which embeds both, so every assertion reads as before. newWAFWith takes a change to the Params instead of a build function, so the Params kept are the ones the server was built from. Model: opus-5-5
101 lines
2.6 KiB
Go
101 lines
2.6 KiB
Go
package waf_test
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/netip"
|
|
"testing"
|
|
"time"
|
|
|
|
"sneak.berlin/go/smallwebwaf/internal/bans"
|
|
"sneak.berlin/go/smallwebwaf/internal/reputation"
|
|
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
|
)
|
|
|
|
// abuseIPDBBanDays is the setting of how long a ban for AbuseIPDB's
|
|
// blacklist lasts, in days.
|
|
const abuseIPDBBanDays = "SWW_WAF_ABUSEIPDB_BAN_DAYS"
|
|
|
|
func TestClientOnTheFetchedAbuseIPDBBlacklistIsRefused(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
s, clk, server := startWithClock(t, "", map[string]string{
|
|
abuseIPDBKey: accountKey,
|
|
})
|
|
|
|
ctx, cancel := context.WithCancel(t.Context())
|
|
stopped := make(chan struct{})
|
|
|
|
go func() {
|
|
server.AbuseIPDB.RunBlacklist(ctx)
|
|
close(stopped)
|
|
}()
|
|
|
|
t.Cleanup(func() {
|
|
cancel()
|
|
<-stopped
|
|
})
|
|
|
|
waitUntil(func() bool {
|
|
active, _ := server.Ledger.Count(clk.Now())
|
|
|
|
return active == 1
|
|
})
|
|
|
|
s.get(client, http.StatusForbidden, requestlog.ActionTemporarilyBanned)
|
|
s.get(otherClient, http.StatusOK, requestlog.ActionForward)
|
|
}
|
|
|
|
func TestClientsOnTheAbuseIPDBBlacklistAreBannedForTheBanDays(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
s, clk, server := startWithClock(t, "", map[string]string{
|
|
abuseIPDBBanDays: "2", banScopeV4Prefix: "24",
|
|
})
|
|
|
|
// Two clients in one netblock make one ban, with the higher score.
|
|
server.banAbuseIPDBBlacklist([]reputation.BlacklistEntry{
|
|
{Addr: netip.MustParseAddr(client), Score: 95},
|
|
{Addr: netip.MustParseAddr(otherClient), Score: 100},
|
|
{Addr: netip.MustParseAddr("2001:db8:5::1"), Score: 90},
|
|
})
|
|
|
|
held := server.Ledger.Bans(netip.MustParsePrefix(clientsNet))
|
|
if len(held) != 1 || held[0].Cause != bans.CauseAbuseIPDB ||
|
|
*held[0].Notes.Reputation[0].Score != 100 {
|
|
t.Errorf("bans on %s %+v, want one for the blacklist, with 100",
|
|
clientsNet, held)
|
|
}
|
|
|
|
// Every client of a banned netblock is refused, the IPv6 address's /64
|
|
// included.
|
|
for _, from := range []string{"203.0.113.200", "2001:db8:5::ffff:1"} {
|
|
s.get(from, http.StatusForbidden, requestlog.ActionTemporarilyBanned)
|
|
}
|
|
|
|
s.get("2001:db8:5:1::1", http.StatusOK, requestlog.ActionForward)
|
|
|
|
clk.advance(2 * 24 * time.Hour)
|
|
s.get(client, http.StatusOK, requestlog.ActionForward)
|
|
}
|
|
|
|
func TestAbuseIPDBBlacklistMakesNoBanInObserveMode(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
s, clk, server := startWithClock(
|
|
t,
|
|
"",
|
|
map[string]string{mode: "observe"},
|
|
)
|
|
|
|
server.banAbuseIPDBBlacklist([]reputation.BlacklistEntry{
|
|
{Addr: netip.MustParseAddr(client), Score: 100},
|
|
})
|
|
|
|
if active, _ := server.Ledger.Count(clk.Now()); active != 0 {
|
|
t.Errorf("%d bans active, want none", active)
|
|
}
|
|
|
|
s.get(client, http.StatusOK, requestlog.ActionForward)
|
|
}
|