package reputation_test import ( "context" "fmt" "io" "net/http" "net/netip" "slices" "strings" "sync" "testing" "testing/synctest" "time" "sneak.berlin/go/smallwebwaf/internal/alerts" "sneak.berlin/go/smallwebwaf/internal/reputation" ) // The tests of the blacklist run in synctest bubbles, as those of the // checks do, with AbuseIPDB a stand-in reached without the network. // blacklistURL is where the tests fetch the blacklist from. const blacklistURL = "https://abuseipdb.example/api/v2/blacklist" func TestBlacklistIsFetchedAtOnceAndEveryRefresh(t *testing.T) { t.Parallel() synctest.Test(t, func(t *testing.T) { abuseIPDB := &blacklistStandIn{body: `{"meta":{},"data":[` + `{"ipAddress":"203.0.113.9","abuseConfidenceScore":100},` + `{"ipAddress":"::ffff:198.51.100.7","abuseConfidenceScore":95},` + `{"ipAddress":"2001:db8::9","abuseConfidenceScore":90}]}`} got := runBlacklist(t, abuseIPDB, key, newQueue()) want := []reputation.Listed{ {Addr: netip.MustParseAddr("203.0.113.9"), Score: 100}, {Addr: netip.MustParseAddr("198.51.100.7"), Score: 95}, {Addr: netip.MustParseAddr("2001:db8::9"), Score: 90}, } synctest.Wait() got.want(t, want) time.Sleep(time.Hour - time.Nanosecond) synctest.Wait() got.want(t, want) time.Sleep(time.Nanosecond) synctest.Wait() got.want(t, want, want) // Each fetch asks for the clients scored 90 or more, with the key // in the header Key alone. abuseIPDB.mu.Lock() defer abuseIPDB.mu.Unlock() for _, req := range abuseIPDB.asked { if req.URL.String() != blacklistURL+"?confidenceMinimum=90" || req.Header.Get("Key") != key { t.Errorf( "asked %s with the key %q, want %s?confidenceMinimum=90 "+ "with %q", req.URL, req.Header.Get("Key"), blacklistURL, key, ) } } }) } func TestFailedBlacklistFetchIsAlertedAndTriedAgainAtTheNextRefresh( t *testing.T, ) { t.Parallel() for _, tc := range []struct{ name, body, error string }{ { "a refusal", "", "the server answered 429 Too Many Requests", }, { "an answer without the list", `{"errors":[]}`, "the answer gives no data", }, { "an entry that is not an address", `{"data":[{"ipAddress":"203.0.113.9"},{"ipAddress":"example"}]}`, "entry 2's ipAddress is not an address", }, } { t.Run(tc.name, func(t *testing.T) { t.Parallel() synctest.Test(t, func(t *testing.T) { abuseIPDB := &blacklistStandIn{body: tc.body} if tc.body == "" { abuseIPDB.status = http.StatusTooManyRequests } queue := newQueue() got := runBlacklist(t, abuseIPDB, key, queue) synctest.Wait() got.want(t) alerted := waiting(queue) if len(alerted) != 1 || alerted[0].Event != alerts.EventSourceFailure || alerted[0].Reason != "fetching AbuseIPDB's blacklist failed" || alerted[0].Detail["source"] != reputation.AbuseIPDBSource || alerted[0].Detail["error"] != tc.error { t.Errorf( "alerts %+v, want the failure %q", alerted, tc.error, ) } abuseIPDB.answer(`{"data":[]}`) time.Sleep(time.Hour) synctest.Wait() got.want(t, []reputation.Listed{}) }) }) } } func TestBlacklistIsNotFetchedWithoutAKey(t *testing.T) { t.Parallel() synctest.Test(t, func(t *testing.T) { abuseIPDB := &blacklistStandIn{body: `{"data":[]}`} got := runBlacklist(t, abuseIPDB, "", newQueue()) time.Sleep(time.Hour) synctest.Wait() got.want(t) if len(abuseIPDB.asked) != 0 { t.Errorf("fetched the blacklist %d times, want none", len(abuseIPDB.asked)) } }) } // blacklistStandIn is a stand-in for AbuseIPDB that answers each fetch of // the blacklist with status, 200 when it is zero, and body, and keeps the // requests. type blacklistStandIn struct { mu sync.Mutex status int body string asked []*http.Request } // RoundTrip has the stand-in answer req, in place of the network. func (s *blacklistStandIn) RoundTrip( req *http.Request, ) (*http.Response, error) { s.mu.Lock() s.asked = append(s.asked, req) status, body := s.status, s.body s.mu.Unlock() if status == 0 { status = http.StatusOK } return &http.Response{ StatusCode: status, Status: fmt.Sprintf("%d %s", status, http.StatusText(status)), Header: http.Header{}, Body: io.NopCloser(strings.NewReader(body)), Request: req, }, nil } // answer has the stand-in answer each fetch with 200 and body. func (s *blacklistStandIn) answer(body string) { s.mu.Lock() defer s.mu.Unlock() s.status, s.body = 0, body } // blacklisted holds the clients on each blacklist given to Blacklisted. type blacklisted struct { mu sync.Mutex fetched [][]reputation.Listed } // want checks the clients on each blacklist given so far. func (b *blacklisted) want(t *testing.T, want ...[]reputation.Listed) { t.Helper() b.mu.Lock() defer b.mu.Unlock() if !slices.EqualFunc(b.fetched, want, slices.Equal) { t.Errorf("blacklists %v, want %v", b.fetched, want) } } // runBlacklist runs RunBlacklist, until the test ends, with the key // accountKey, the threshold 90 and a refresh of an hour, fetching from // abuseIPDB and raising alerts into queue, and returns what it gives // Blacklisted. func runBlacklist( t *testing.T, abuseIPDB *blacklistStandIn, accountKey string, queue *alerts.Queue, ) *blacklisted { t.Helper() got := &blacklisted{} p := abuseIPDBParams() p.Key = accountKey p.BlacklistURL = blacklistURL p.Threshold = 90 p.BlacklistRefresh = time.Hour p.Alerts = queue p.Blacklisted = func(listed []reputation.Listed) { got.mu.Lock() defer got.mu.Unlock() got.fetched = append(got.fetched, listed) } checker := reputation.NewAbuseIPDB(p) checker.SetTransport(abuseIPDB) ctx, cancel := context.WithCancel(t.Context()) done := make(chan struct{}) go func() { checker.RunBlacklist(ctx) close(done) }() t.Cleanup(func() { cancel() <-done }) return got }