From 9d3f212b0d63884d3a2c4458eaa9e2341d936a45 Mon Sep 17 00:00:00 2001 From: sneak Date: Thu, 19 Nov 2026 21:59:05 +0000 Subject: [PATCH] waf: a heuristic says itself that it refuses whatever the lookup answers (closes #446) refusedWhateverTheAnswer asks each heuristic of afterLookup that implements the new refusesWithoutLookup interface, instead of naming blocklists and rate_limits. The comment on adding a heuristic names the places outside its file that may change with it. Model: opus-5-5 --- internal/waf/blocklists.go | 9 ++++++ internal/waf/heuristics.go | 19 +++++++++++++ internal/waf/proxy.go | 56 +++++++++++++++++--------------------- internal/waf/ratelimits.go | 13 +++++---- internal/waf/waf.go | 16 +++++++---- 5 files changed, 71 insertions(+), 42 deletions(-) diff --git a/internal/waf/blocklists.go b/internal/waf/blocklists.go index 0e35966..e27b935 100644 --- a/internal/waf/blocklists.go +++ b/internal/waf/blocklists.go @@ -43,6 +43,15 @@ func (b blocklists) check( } } +// refusesWhateverTheLookupAnswers reports whether blocklists refuses rq, as +// refusedByBlocklists says. The lookup's answer plays no part in it. +func (b blocklists) refusesWhateverTheLookupAnswers( + rq *request, + _ time.Time, +) bool { + return b.refusedByBlocklists(b.lists.ListedBy(rq.client)) +} + // refusedByBlocklists reports whether the heuristic blocklists refuses a // client, given listedBy, the blocklists that list it: it does when one lists // it and SWW_WAF_BLOCKLIST_ACTION is deny. diff --git a/internal/waf/heuristics.go b/internal/waf/heuristics.go index e9e188b..f818042 100644 --- a/internal/waf/heuristics.go +++ b/internal/waf/heuristics.go @@ -9,6 +9,16 @@ import ( // and holds the settings and parts it reads. Add one line for it to one of the // lists that newHandler builds, which hands it those settings and parts. Its // settings go in config.WAF, and the comment at the top of its file names them. +// A heuristic of afterLookup that can refuse a request whatever the lookup +// answers also implements refusesWithoutLookup. +// +// Three places outside its file may change with it: +// - waitForGeoJS in newHandler lists the settings that need the lookup's +// answer; +// - requestCanGetTag in internal/config/waf.go holds which setting can give a +// request which tag; +// - findBesideLookup does the work of each heuristic of besideLookup, by +// name. // heuristic is one of the checks that decide what waf does with a // request. @@ -20,6 +30,15 @@ type heuristic interface { check(ctx context.Context, rq *request, now time.Time) *verdict } +// refusesWithoutLookup is a heuristic of afterLookup that can tell, before the +// lookup answers, that it will refuse a request whatever the answer. +type refusesWithoutLookup interface { + // refusesWhateverTheLookupAnswers reports whether the heuristic will refuse + // rq at now, whatever the lookup answers about its client. It changes + // nothing. + refusesWhateverTheLookupAnswers(rq *request, now time.Time) bool +} + // verdict is a heuristic refusing a request, or banning its client: the action // the log line names, one of requestlog's Action constants, and the figures the // heuristic acted on, by name, such as the count and the limit. diff --git a/internal/waf/proxy.go b/internal/waf/proxy.go index 5a507e9..72424d8 100644 --- a/internal/waf/proxy.go +++ b/internal/waf/proxy.go @@ -177,30 +177,6 @@ func newHandler(params Params) *handler { ipv6GroupPrefix: cfg.IPv6GroupPrefix, } - h.blocklists = blocklists{ - lists: params.Lists, - action: cfg.BlocklistAction, - hitNoter: hitNoter, - } - h.rateLimits = rateLimits{ - limiter: params.Limiter, - exemptNets: cfg.RateLimitExemptNets, - exemptPaths: cfg.RateLimitExemptPaths, - banResponse: cfg.BanResponse, - ipv6GroupPrefix: cfg.IPv6GroupPrefix, - biasedThresholds: biasedThresholds{ - lists: params.Lists, - tagLimitPercent: cfg.TagLimitPercent, - asnBytesLimitPercent: cfg.ASNBytesLimitPercent, - countryBytesLimitPercent: cfg.CountryBytesLimitPercent, - unknownCountryLimitPercent: cfg.UnknownCountryLimitPercent, - blocklistAction: cfg.BlocklistAction, - blocklistLimitPercent: cfg.BlocklistLimitPercent, - dnsblAbuseIPDBAction: cfg.DNSBLAbuseIPDBAction, - dnsblAbuseIPDBLimitPercent: cfg.DNSBLAbuseIPDBLimitPercent, - }, - banMaker: sharedBanMaker, - } h.trapPaths = trapPaths{paths: cfg.TrapPaths, banMaker: sharedBanMaker} h.ruleFiles = ruleFiles{ rules: params.Rules, @@ -230,7 +206,11 @@ func newHandler(params Params) *handler { } h.afterLookup = []heuristic{ countries{allowed: cfg.ExclusivelyAllowedCountries}, - h.blocklists, + blocklists{ + lists: params.Lists, + action: cfg.BlocklistAction, + hitNoter: hitNoter, + }, dnsbl{ dnsbl: params.DNSBL, action: cfg.DNSBLAbuseIPDBAction, @@ -246,7 +226,25 @@ func newHandler(params Params) *handler { hitNoter: hitNoter, }, tagDeny{tags: cfg.TagDeny}, - h.rateLimits, + rateLimits{ + limiter: params.Limiter, + exemptNets: cfg.RateLimitExemptNets, + exemptPaths: cfg.RateLimitExemptPaths, + banResponse: cfg.BanResponse, + ipv6GroupPrefix: cfg.IPv6GroupPrefix, + biasedThresholds: biasedThresholds{ + lists: params.Lists, + tagLimitPercent: cfg.TagLimitPercent, + asnBytesLimitPercent: cfg.ASNBytesLimitPercent, + countryBytesLimitPercent: cfg.CountryBytesLimitPercent, + unknownCountryLimitPercent: cfg.UnknownCountryLimitPercent, + blocklistAction: cfg.BlocklistAction, + blocklistLimitPercent: cfg.BlocklistLimitPercent, + dnsblAbuseIPDBAction: cfg.DNSBLAbuseIPDBAction, + dnsblAbuseIPDBLimitPercent: cfg.DNSBLAbuseIPDBLimitPercent, + }, + banMaker: sharedBanMaker, + }, } h.besideLookup = []heuristic{h.trapPaths, h.ruleFiles, h.coreRuleSet} h.afterBesideLookup = []heuristic{tagDeny{tags: cfg.TagDeny}} @@ -323,11 +321,7 @@ type handler struct { // afterAnswer, they only ban. onReport []heuristic - // The heuristics of the lists that other steps of a request use too: - // refusedWhateverTheAnswer asks blocklists and rateLimits, and - // findBesideLookup does the work of trapPaths, ruleFiles and coreRuleSet. - blocklists blocklists - rateLimits rateLimits + // The heuristics of besideLookup, whose work findBesideLookup does. trapPaths trapPaths ruleFiles ruleFiles coreRuleSet coreRuleSet diff --git a/internal/waf/ratelimits.go b/internal/waf/ratelimits.go index 7d92f5b..437c7ff 100644 --- a/internal/waf/ratelimits.go +++ b/internal/waf/ratelimits.go @@ -67,11 +67,14 @@ func (r rateLimits) check( } } -// overAWholeRateLimit reports whether the heuristic rate_limits will refuse rq -// at now, whatever its client's limit percentages: the rate limits count it, -// and one more request from its client takes it over a whole rate limit. It -// counts nothing. -func (r rateLimits) overAWholeRateLimit(rq *request, now time.Time) bool { +// refusesWhateverTheLookupAnswers reports whether the heuristic rate_limits +// will refuse rq at now, whatever its client's limit percentages, which the +// lookup's answer can lower: the rate limits count it, and one more request +// from its client takes it over a whole rate limit. It counts nothing. +func (r rateLimits) refusesWhateverTheLookupAnswers( + rq *request, + now time.Time, +) bool { return r.countedByRateLimits(rq) && r.limiter.OverARateLimit( clientGroup(r.ipv6GroupPrefix, rq.client), diff --git a/internal/waf/waf.go b/internal/waf/waf.go index eea7054..4061fbf 100644 --- a/internal/waf/waf.go +++ b/internal/waf/waf.go @@ -72,18 +72,22 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { // refusedWhateverTheAnswer reports whether the request is one that does not wait // for GeoJS, since the checks will refuse it at now whatever GeoJS answers about -// its client: the heuristic blocklists or rate_limits will refuse it, as a -// function in that heuristic's file says. In observe mode, which refuses -// nothing, none is. rproxy lets a request through when waf answers too late. +// its client: a heuristic of afterLookup that implements refusesWithoutLookup +// says it will refuse it. In observe mode, which refuses nothing, none is. +// rproxy lets a request through when waf answers too late. func (rq *request) refusedWhateverTheAnswer(now time.Time) bool { if rq.h.config.Observe { return false } - listedBy := rq.h.blocklists.lists.ListedBy(rq.client) + for _, each := range rq.h.afterLookup { + knows, ok := each.(refusesWithoutLookup) + if ok && knows.refusesWhateverTheLookupAnswers(rq, now) { + return true + } + } - return rq.h.blocklists.refusedByBlocklists(listedBy) || - rq.h.rateLimits.overAWholeRateLimit(rq, now) + return false } // answerQuestion runs the checks on the visitor's request a question -- 2.54.0