From da6667957f066dafbecdeca7daa87a30e5e73554 Mon Sep 17 00:00:00 2001 From: clawbot <35+clawbot@noreply.example.org> Date: Thu, 19 Nov 2026 08:47:51 +0000 Subject: [PATCH] waf: test the blacklist through the server's own wiring No test fetched the blacklist through the server: the waf tests handed clients to the ban code directly, so a wrong key, threshold or URL in newReputation, or no Blacklisted at all, left every test green. Model: opus-5-5 --- internal/waf/abuseipdbblacklist_test.go | 31 +++++++++++++++++++++++ internal/waf/proxy_test.go | 5 +++- internal/waf/reputation_test.go | 33 +++++++++++++++++++------ 3 files changed, 60 insertions(+), 9 deletions(-) diff --git a/internal/waf/abuseipdbblacklist_test.go b/internal/waf/abuseipdbblacklist_test.go index 738a756..33b78ab 100644 --- a/internal/waf/abuseipdbblacklist_test.go +++ b/internal/waf/abuseipdbblacklist_test.go @@ -1,6 +1,7 @@ package waf_test import ( + "context" "net/http" "net/netip" "testing" @@ -15,6 +16,36 @@ import ( // 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() diff --git a/internal/waf/proxy_test.go b/internal/waf/proxy_test.go index 054b46b..b38902f 100644 --- a/internal/waf/proxy_test.go +++ b/internal/waf/proxy_test.go @@ -424,7 +424,8 @@ func startRProxy( // With no geojsURL, there is no stand-in for GeoJS to look clients up at, and // SWW_WAF_LOOKUP_SOURCE is off unless env sets it. While it is file, the lookup // database SWW_WAF_LOOKUP_DB_PATH names is read. Clients are checked with AbuseIPDB -// at abuseIPDBURL while env sets SWW_WAF_ABUSEIPDB_KEY. +// at abuseIPDBURL while env sets SWW_WAF_ABUSEIPDB_KEY, and its blacklist is +// fetched from abuseIPDBBlacklistURL. // SWW_WAF_CORE_RULE_SET_MODE is off unless env sets it, so that only the tests of the // Core Rule Set have their requests inspected by it. func newWAF( @@ -510,6 +511,8 @@ func newWAFWith( Now: now, Rules: ruleFiles, Alerts: alertQueue, + + AbuseIPDBBlacklistURL: abuseIPDBBlacklistURL, }) // The race detector and a busy host can slow the Core Rule Set's diff --git a/internal/waf/reputation_test.go b/internal/waf/reputation_test.go index 7bb9335..ab4af72 100644 --- a/internal/waf/reputation_test.go +++ b/internal/waf/reputation_test.go @@ -822,22 +822,39 @@ const abuseipdb = reputation.AbuseIPDBSource // through which AbuseIPDB is asked. const abuseIPDBURL = "abuseipdb://stand-in/api/v2/check" +// abuseIPDBBlacklistURL is where newWAF has AbuseIPDB's blacklist fetched +// from: at abuseIPDBStandIn too. +const abuseIPDBBlacklistURL = "abuseipdb://stand-in/api/v2/blacklist" + // abuseIPDBStandIn is a stand-in for AbuseIPDB that gives every client the -// score 100, at once and without the network. +// score 100, at once and without the network. Its blacklist lists client +// alone, with the score 100, to a fetch with accountKey that asks for the +// clients scored 90 or more, the default threshold; it refuses any other +// fetch of it with 400. type abuseIPDBStandIn struct{} -// RoundTrip answers req with the score 100. +// RoundTrip answers req with the score 100, or with the blacklist. func (abuseIPDBStandIn) RoundTrip( req *http.Request, ) (*http.Response, error) { + status, body := http.StatusOK, `{"data":{"abuseConfidenceScore":100}}` + + if req.URL.Path == "/api/v2/blacklist" { + body = `{"data":[{"ipAddress":"` + client + + `","abuseConfidenceScore":100}]}` + + if req.Header.Get("Key") != accountKey || + req.URL.RawQuery != "confidenceMinimum=90" { + status, body = http.StatusBadRequest, `{"errors":[]}` + } + } + return &http.Response{ - StatusCode: http.StatusOK, - Status: "200 OK", + StatusCode: status, + Status: fmt.Sprintf("%d %s", status, http.StatusText(status)), Header: http.Header{}, - Body: io.NopCloser( - strings.NewReader(`{"data":{"abuseConfidenceScore":100}}`), - ), - Request: req, + Body: io.NopCloser(strings.NewReader(body)), + Request: req, }, nil }