From c61f054e50225b732fa0f9315d64d99b1d173879 Mon Sep 17 00:00:00 2001 From: sneak Date: Fri, 20 Nov 2026 00:36:19 +0000 Subject: [PATCH 1/2] waf: a verdict carries the status the visitor gets (closes #443) Each heuristic that refuses a request sets the status on its verdict. check copies it into the refusal, or in observe mode notes it, and no longer works the status out from the action. refusalLimit takes the status from that verdict instead of from a switch over the action. Model: opus-5-5 --- internal/waf/abuseipdb.go | 14 +++++++++----- internal/waf/bans.go | 24 +++++++++++------------ internal/waf/besidelookup.go | 2 ++ internal/waf/blocklists.go | 19 ++++++++++-------- internal/waf/coreruleset.go | 2 ++ internal/waf/countries.go | 12 ++++++++---- internal/waf/denynets.go | 10 ++++++---- internal/waf/dnsbl.go | 24 +++++++++++++---------- internal/waf/headerlimits.go | 3 +++ internal/waf/heuristics.go | 7 +++++-- internal/waf/proxy.go | 29 +++++++++++++++++----------- internal/waf/ratelimits.go | 10 ++++++---- internal/waf/refusallimit.go | 31 ++++-------------------------- internal/waf/request.go | 37 ++++++++++++------------------------ internal/waf/rulefiles.go | 11 ++++++++--- internal/waf/tagdeny.go | 10 ++++++---- internal/waf/trappaths.go | 12 +++++++----- 17 files changed, 132 insertions(+), 125 deletions(-) diff --git a/internal/waf/abuseipdb.go b/internal/waf/abuseipdb.go index d3c4964..b13790c 100644 --- a/internal/waf/abuseipdb.go +++ b/internal/waf/abuseipdb.go @@ -1,7 +1,8 @@ package waf // The heuristic abuseipdb. It reads SWW_WAF_ABUSEIPDB_KEY, -// SWW_WAF_ABUSEIPDB_MIN_SCORE and SWW_WAF_DNSBL_ABUSEIPDB_ACTION. +// SWW_WAF_ABUSEIPDB_MIN_SCORE, SWW_WAF_DNSBL_ABUSEIPDB_ACTION and +// SWW_WAF_BAN_RESPONSE. import ( "context" @@ -16,10 +17,11 @@ import ( // abuseIPDB adds the tag abuseipdb and notes AbuseIPDB, as noteHit does, with // the score, when its score of the client is a hit, and refuses the request -// when SWW_WAF_DNSBL_ABUSEIPDB_ACTION is deny, as dnsbl does for a zone. While -// SWW_WAF_ABUSEIPDB_KEY is unset it does nothing. A client without a score is -// checked in the background, by the request's address, if its history counts -// an offence, and the request does not wait for the answer. The score is then +// with SWW_WAF_BAN_RESPONSE when SWW_WAF_DNSBL_ABUSEIPDB_ACTION is deny, as +// dnsbl does for a zone. While SWW_WAF_ABUSEIPDB_KEY is unset it does nothing. +// A client without a score is checked in the background, by the request's +// address, if its history counts an offence, and the request does not wait for +// the answer. The score is then // used for each address of the client. Its figures are the score and // SWW_WAF_ABUSEIPDB_MIN_SCORE. type abuseIPDB struct { @@ -30,6 +32,7 @@ type abuseIPDB struct { keySet bool // SWW_WAF_ABUSEIPDB_KEY is set minScore int64 // SWW_WAF_ABUSEIPDB_MIN_SCORE action config.ReputationAction // SWW_WAF_DNSBL_ABUSEIPDB_ACTION + banResponse int // SWW_WAF_BAN_RESPONSE ipv6GroupPrefix int // SWW_WAF_IPV6_GROUP_PREFIX hitNoter reputationHitNoter } @@ -71,6 +74,7 @@ func (a abuseIPDB) check( return &verdict{ action: requestlog.ActionClientDenied, + status: a.banResponse, figures: map[string]any{ figureScore: score, "min_score": a.minScore, }, diff --git a/internal/waf/bans.go b/internal/waf/bans.go index 931b06f..f201b99 100644 --- a/internal/waf/bans.go +++ b/internal/waf/bans.go @@ -1,7 +1,8 @@ package waf // The heuristic active_ban, and banMaker, which makes the bans of several -// heuristics. active_ban reads SWW_WAF_MODE; the bans are the ban ledger's. +// heuristics. active_ban reads SWW_WAF_MODE and SWW_WAF_BAN_RESPONSE; the bans +// are the ban ledger's. import ( "context" @@ -16,16 +17,11 @@ import ( "sneak.berlin/go/smallwebwaf/internal/requestlog" ) -// banResponse is a refusal answered with SWW_WAF_BAN_RESPONSE, and logged -// with action. -func (rq *request) banResponse(action requestlog.Action) *refusal { - return &refusal{status: rq.h.config.BanResponse, action: action} -} - -// activeBan refuses a client a ban covers: a ban on a netblock the client is -// in, in force at now. It notes for the log line when that ban ends. A request -// that makes the ban permanent, or in observe mode would have, raises the alert -// for it. Its figures are the ban's netblock and when it ends. +// activeBan refuses a client a ban covers, with SWW_WAF_BAN_RESPONSE: a ban on +// a netblock the client is in, in force at now. It notes for the log line when +// that ban ends. A request that makes the ban permanent, or in observe mode +// would have, raises the alert for it. Its figures are the ban's netblock and +// when it ends. type activeBan struct { banMaker banMaker } @@ -63,6 +59,7 @@ func (a activeBan) check( return &verdict{ action: banAction(ban.Permanent()), + status: a.banMaker.banResponse, figures: map[string]any{ figureNetblock: ban.Netblock.String(), "ban_expires": expires, }, @@ -118,8 +115,9 @@ type banMaker struct { alerts *alerts.Queue // observe is true in observe mode (SWW_WAF_MODE). observe bool - // banResponse is SWW_WAF_BAN_RESPONSE, the status the notes of a ban for a - // clear sign of attack give. + // banResponse is SWW_WAF_BAN_RESPONSE: the status of a request refused + // under a ban or for a clear sign of attack, which the notes of a ban for + // a clear sign of attack give. banResponse int // banScopeV4Prefix is SWW_WAF_BAN_SCOPE_V4_PREFIX and ipv6GroupPrefix // SWW_WAF_IPV6_GROUP_PREFIX, which set the netblock a ban covers. diff --git a/internal/waf/besidelookup.go b/internal/waf/besidelookup.go index ffdc026..3fec64b 100644 --- a/internal/waf/besidelookup.go +++ b/internal/waf/besidelookup.go @@ -4,6 +4,7 @@ package waf import ( "context" + "net/http" "sync/atomic" "time" @@ -161,6 +162,7 @@ func (rq *request) applyFindings( func (rq *request) timedOut(running string) *verdict { v := &verdict{ action: requestlog.ActionChecksTimedOut, + status: http.StatusServiceUnavailable, figures: map[string]any{ "time_limit_ms": rq.h.besideLookupLimit.Milliseconds(), }, diff --git a/internal/waf/blocklists.go b/internal/waf/blocklists.go index e27b935..7b78f0a 100644 --- a/internal/waf/blocklists.go +++ b/internal/waf/blocklists.go @@ -1,7 +1,7 @@ package waf -// The heuristic blocklists. It reads SWW_WAF_BLOCKLIST_ACTION, and the blocklists -// through the reputation lists. +// The heuristic blocklists. It reads SWW_WAF_BLOCKLIST_ACTION and +// SWW_WAF_BAN_RESPONSE, and the blocklists through the reputation lists. import ( "context" @@ -13,13 +13,15 @@ import ( ) // blocklists notes the blocklists that list the client, as noteListed does, -// and refuses the request when SWW_WAF_BLOCKLIST_ACTION is deny. Being limit, it -// lowers the client's limits instead (see limitPercentages), and being log, it -// does nothing more. Its figure is the blocklists that list the client. +// and refuses the request with SWW_WAF_BAN_RESPONSE when +// SWW_WAF_BLOCKLIST_ACTION is deny. Being limit, it lowers the client's limits +// instead (see limitPercentages), and being log, it does nothing more. Its +// figure is the blocklists that list the client. type blocklists struct { - lists *reputation.Lists - action config.ReputationAction // SWW_WAF_BLOCKLIST_ACTION - hitNoter reputationHitNoter + lists *reputation.Lists + action config.ReputationAction // SWW_WAF_BLOCKLIST_ACTION + banResponse int // SWW_WAF_BAN_RESPONSE + hitNoter reputationHitNoter } func (blocklists) name() string { return "blocklists" } @@ -39,6 +41,7 @@ func (b blocklists) check( return &verdict{ action: requestlog.ActionClientDenied, + status: b.banResponse, figures: map[string]any{"lists": listedBy}, } } diff --git a/internal/waf/coreruleset.go b/internal/waf/coreruleset.go index fcb0864..2908508 100644 --- a/internal/waf/coreruleset.go +++ b/internal/waf/coreruleset.go @@ -5,6 +5,7 @@ package waf import ( "context" + "net/http" "time" "sneak.berlin/go/smallwebwaf/internal/alerts" @@ -77,6 +78,7 @@ func (c coreRuleSet) check( return &verdict{ action: requestlog.ActionBlockedByCoreRuleSet, + status: http.StatusForbidden, figures: map[string]any{ figureScore: result.Score, "threshold": threshold, diff --git a/internal/waf/countries.go b/internal/waf/countries.go index 96a4bba..c304a9e 100644 --- a/internal/waf/countries.go +++ b/internal/waf/countries.go @@ -1,6 +1,7 @@ package waf -// The heuristic countries. It reads SWW_WAF_EXCLUSIVELY_ALLOWED_COUNTRIES. +// The heuristic countries. It reads SWW_WAF_EXCLUSIVELY_ALLOWED_COUNTRIES and +// SWW_WAF_BAN_RESPONSE. import ( "context" @@ -13,10 +14,12 @@ import ( // countries refuses a request when SWW_WAF_EXCLUSIVELY_ALLOWED_COUNTRIES is set and // does not hold the client's country as it was looked up. A client without a -// country, or whose country cannot be found, is refused. Its figures are the -// country and the setting that refused it. +// country, or whose country cannot be found, is refused. It refuses with +// SWW_WAF_BAN_RESPONSE. Its figures are the country and the setting that +// refused it. type countries struct { - allowed []string // SWW_WAF_EXCLUSIVELY_ALLOWED_COUNTRIES + allowed []string // SWW_WAF_EXCLUSIVELY_ALLOWED_COUNTRIES + banResponse int // SWW_WAF_BAN_RESPONSE } func (countries) name() string { return "countries" } @@ -35,6 +38,7 @@ func (c countries) check( return &verdict{ action: requestlog.ActionCountryDenied, + status: c.banResponse, figures: map[string]any{ "country": country, "setting": config.ExclusivelyAllowedCountriesSetting, diff --git a/internal/waf/denynets.go b/internal/waf/denynets.go index a33a843..7f446b8 100644 --- a/internal/waf/denynets.go +++ b/internal/waf/denynets.go @@ -1,6 +1,6 @@ package waf -// The heuristic deny_nets. It reads SWW_WAF_DENY_NETS. +// The heuristic deny_nets. It reads SWW_WAF_DENY_NETS and SWW_WAF_BAN_RESPONSE. import ( "context" @@ -10,10 +10,11 @@ import ( "sneak.berlin/go/smallwebwaf/internal/requestlog" ) -// denyNets refuses a client in SWW_WAF_DENY_NETS. Its figure is the netblock of -// the setting the client is in. +// denyNets refuses a client in SWW_WAF_DENY_NETS, with SWW_WAF_BAN_RESPONSE. +// Its figure is the netblock of the setting the client is in. type denyNets struct { - netblocks []netip.Prefix // SWW_WAF_DENY_NETS + netblocks []netip.Prefix // SWW_WAF_DENY_NETS + banResponse int // SWW_WAF_BAN_RESPONSE } func (denyNets) name() string { return "deny_nets" } @@ -27,6 +28,7 @@ func (d denyNets) check( if netblock.Contains(rq.client) { return &verdict{ action: requestlog.ActionClientDenied, + status: d.banResponse, figures: map[string]any{figureNetblock: netblock.String()}, } } diff --git a/internal/waf/dnsbl.go b/internal/waf/dnsbl.go index 79f6b4b..f58bc79 100644 --- a/internal/waf/dnsbl.go +++ b/internal/waf/dnsbl.go @@ -1,7 +1,8 @@ package waf -// The heuristic dnsbl. It reads SWW_WAF_DNSBL_ABUSEIPDB_ACTION, and the DNSBL zones' -// verdicts through the DNSBL checker. +// The heuristic dnsbl. It reads SWW_WAF_DNSBL_ABUSEIPDB_ACTION and +// SWW_WAF_BAN_RESPONSE, and the DNSBL zones' verdicts through the DNSBL +// checker. import ( "context" @@ -15,15 +16,17 @@ import ( // dnsbl notes the DNSBL zones whose verdict lists the client, as noteListed // does, adds the tag dnsbl= for each, the zone's key masked, and refuses -// the request when SWW_WAF_DNSBL_ABUSEIPDB_ACTION is deny. Being -// limit, it lowers the client's limits instead (see limitPercentages), and -// being log, it does nothing more. A zone without a verdict on the client is -// asked about it in the background, and the request does not wait for the -// answer. Its figure is the zones that list the client, their keys masked. +// the request with SWW_WAF_BAN_RESPONSE when SWW_WAF_DNSBL_ABUSEIPDB_ACTION is +// deny. Being limit, it lowers the client's limits instead (see +// limitPercentages), and being log, it does nothing more. A zone without a +// verdict on the client is asked about it in the background, and the request +// does not wait for the answer. Its figure is the zones that list the client, +// their keys masked. type dnsbl struct { - dnsbl *reputation.DNSBL - action config.ReputationAction // SWW_WAF_DNSBL_ABUSEIPDB_ACTION - hitNoter reputationHitNoter + dnsbl *reputation.DNSBL + action config.ReputationAction // SWW_WAF_DNSBL_ABUSEIPDB_ACTION + banResponse int // SWW_WAF_BAN_RESPONSE + hitNoter reputationHitNoter } func (dnsbl) name() string { return "dnsbl" } @@ -47,6 +50,7 @@ func (d dnsbl) check( return &verdict{ action: requestlog.ActionClientDenied, + status: d.banResponse, figures: map[string]any{"zones": listedBy}, } } diff --git a/internal/waf/headerlimits.go b/internal/waf/headerlimits.go index 19c9b09..8a0367b 100644 --- a/internal/waf/headerlimits.go +++ b/internal/waf/headerlimits.go @@ -4,6 +4,7 @@ package waf import ( "context" + "net/http" "strings" "time" @@ -39,6 +40,7 @@ func (headerLimits) check( if requestLine > maxLineBytes { return &verdict{ action: requestlog.ActionRequestLineTooLong, + status: http.StatusRequestURITooLong, figures: map[string]any{ "request_line_bytes": requestLine, "request_line_bytes_limit": maxLineBytes, @@ -56,6 +58,7 @@ func (headerLimits) check( return &verdict{ action: requestlog.ActionRequestHeaderTooLarge, + status: http.StatusRequestHeaderFieldsTooLarge, figures: map[string]any{ "longest_header_line_bytes": longest, "header_line_bytes_limit": maxLineBytes, diff --git a/internal/waf/heuristics.go b/internal/waf/heuristics.go index 44d6b80..10316df 100644 --- a/internal/waf/heuristics.go +++ b/internal/waf/heuristics.go @@ -42,10 +42,13 @@ type refusesWithoutLookup interface { } // verdict is a heuristic refusing a request, or banning its client: the action -// the log line names, and the figures the heuristic acted on, by name, such as -// the count and the limit. +// the log line names, the status the visitor is answered with, and the figures +// the heuristic acted on, by name, such as the count and the limit. A status of +// 0 closes the visitor's connection without an answer. A verdict of afterAnswer +// or onReport bans and refuses nothing, and has neither action nor status. type verdict struct { action requestlog.Action + status int figures map[string]any } diff --git a/internal/waf/proxy.go b/internal/waf/proxy.go index da437b2..89f4d30 100644 --- a/internal/waf/proxy.go +++ b/internal/waf/proxy.go @@ -189,20 +189,25 @@ func newHandler(params Params) *handler { routeWatch: params.RouteWatch, ipv6GroupPrefix: cfg.IPv6GroupPrefix, }, - denyNets{netblocks: cfg.DenyNets}, + denyNets{netblocks: cfg.DenyNets, banResponse: cfg.BanResponse}, activeBan{banMaker: sharedBanMaker}, } h.afterLookup = []heuristic{ - countries{allowed: cfg.ExclusivelyAllowedCountries}, + countries{ + allowed: cfg.ExclusivelyAllowedCountries, + banResponse: cfg.BanResponse, + }, blocklists{ - lists: params.Lists, - action: cfg.BlocklistAction, - hitNoter: hitNoter, + lists: params.Lists, + action: cfg.BlocklistAction, + banResponse: cfg.BanResponse, + hitNoter: hitNoter, }, dnsbl{ - dnsbl: params.DNSBL, - action: cfg.DNSBLAbuseIPDBAction, - hitNoter: hitNoter, + dnsbl: params.DNSBL, + action: cfg.DNSBLAbuseIPDBAction, + banResponse: cfg.BanResponse, + hitNoter: hitNoter, }, abuseIPDB{ abuseIPDB: params.AbuseIPDB, @@ -210,10 +215,11 @@ func newHandler(params Params) *handler { keySet: cfg.AbuseIPDBKey != "", minScore: cfg.AbuseIPDBMinScore, action: cfg.DNSBLAbuseIPDBAction, + banResponse: cfg.BanResponse, ipv6GroupPrefix: cfg.IPv6GroupPrefix, hitNoter: hitNoter, }, - tagDeny{tags: cfg.TagDeny}, + tagDeny{tags: cfg.TagDeny, banResponse: cfg.BanResponse}, rateLimits{ limiter: params.Limiter, exemptNets: cfg.RateLimitExemptNets, @@ -235,12 +241,13 @@ func newHandler(params Params) *handler { }, } h.besideLookup = []heuristic{h.trapPaths, h.ruleFiles, h.coreRuleSet} - h.afterBesideLookup = []heuristic{tagDeny{tags: cfg.TagDeny}} + h.afterBesideLookup = []heuristic{ + tagDeny{tags: cfg.TagDeny, banResponse: cfg.BanResponse}, + } h.afterAnswer = []heuristic{ refusalLimit{ limiter: params.Limiter, perMinute: cfg.RefusalsLimitPerMinute, - banResponse: cfg.BanResponse, ipv6GroupPrefix: cfg.IPv6GroupPrefix, banMaker: sharedBanMaker, }, diff --git a/internal/waf/ratelimits.go b/internal/waf/ratelimits.go index 437c7ff..26119d7 100644 --- a/internal/waf/ratelimits.go +++ b/internal/waf/ratelimits.go @@ -17,9 +17,9 @@ import ( // countedByRateLimits exempts it, by the client's limit percentages, which it // notes for the log line with the client's counts. A request that takes the // client over a rate limit, as its limit percentage lowers it, breaks it: it is -// refused, and bans the client. A request exempt from the rate limits is exempt -// from the byte limits too. Its figures are the window, the count and the -// limit. +// refused with SWW_WAF_BAN_RESPONSE, and bans the client. A request exempt +// from the rate limits is exempt from the byte limits too. Its figures are the +// window, the count and the limit. type rateLimits struct { limiter *ratelimit.Limiter exemptNets []netip.Prefix // SWW_WAF_RATE_LIMIT_EXEMPT_NETS @@ -63,7 +63,9 @@ func (r rateLimits) check( r.banMaker.banForLimit(rq, now, hit, r.banResponse) return &verdict{ - action: requestlog.ActionClientIsRateLimited, figures: limitFigures(hit), + action: requestlog.ActionClientIsRateLimited, + status: r.banResponse, + figures: limitFigures(hit), } } diff --git a/internal/waf/refusallimit.go b/internal/waf/refusallimit.go index f13dcbe..bdadf5b 100644 --- a/internal/waf/refusallimit.go +++ b/internal/waf/refusallimit.go @@ -1,15 +1,12 @@ package waf -// The heuristic refusal_limit. It reads SWW_WAF_REFUSALS_LIMIT_PER_MINUTE and -// SWW_WAF_BAN_RESPONSE. +// The heuristic refusal_limit. It reads SWW_WAF_REFUSALS_LIMIT_PER_MINUTE. import ( "context" - "net/http" "time" "sneak.berlin/go/smallwebwaf/internal/ratelimit" - "sneak.berlin/go/smallwebwaf/internal/requestlog" ) // refusalLimit counts the request once it has been answered, if waf @@ -18,11 +15,11 @@ import ( // SWW_WAF_REFUSALS_LIMIT_PER_MINUTE such refusals of the client within a minute break // a limit, which bans the client. A client in SWW_WAF_ALLOW_NETS, which the // heuristics skip, is not counted, and nothing is while the limit is off. -// Its figures are the window, the count and the limit. +// The ban's notes give the status the client was sent, or in observe mode +// would have been. Its figures are the window, the count and the limit. type refusalLimit struct { limiter *ratelimit.Limiter perMinute int64 // SWW_WAF_REFUSALS_LIMIT_PER_MINUTE - banResponse int // SWW_WAF_BAN_RESPONSE ipv6GroupPrefix int // SWW_WAF_IPV6_GROUP_PREFIX banMaker banMaker } @@ -51,27 +48,7 @@ func (r refusalLimit) check( return nil } - // What the client was sent, or in observe mode would have been. - status := rq.out.status - - switch rq.wouldAction { - case requestlog.ActionBlockedByBlockRule, - requestlog.ActionBlockedByCoreRuleSet: - status = http.StatusForbidden - case requestlog.ActionTemporarilyBanned, - requestlog.ActionPermanentlyBanned: - status = r.banResponse - case requestlog.ActionForward, - requestlog.ActionRequestLineTooLong, - requestlog.ActionRequestHeaderTooLarge, - requestlog.ActionClientIsRateLimited, - requestlog.ActionClientDenied, - requestlog.ActionCountryDenied, - requestlog.ActionChecksTimedOut, - requestlog.ActionOwnEndpoint: - } - - r.banMaker.banForLimit(rq, now, hit, status) + r.banMaker.banForLimit(rq, now, hit, rq.refusalStatus) return &verdict{figures: limitFigures(hit)} } diff --git a/internal/waf/request.go b/internal/waf/request.go index 8a302bf..1555704 100644 --- a/internal/waf/request.go +++ b/internal/waf/request.go @@ -69,6 +69,10 @@ type decision struct { // its client's history counts. trapPaths and ruleFiles set attack, // ruleFiles blockedByBlockRule, and coreRuleSet blockedByCoreRuleSet. attack, blockedByBlockRule, blockedByCoreRuleSet bool + // refusalStatus is the status of the verdict that refused the request, or + // in observe mode would have: what the visitor was answered with, or would + // have been. check sets it. + refusalStatus int // wouldAction is, in observe mode, the action enforce mode would have // refused the request with, one of requestlog's Action constants, and "" // otherwise. check sets it. @@ -203,9 +207,8 @@ func requestHeaders(r *http.Request, names []string) map[string]string { // check is the one place where a request can be refused. It returns nil to let // the request through. The heuristics of checkClient decide: a request they -// refuse is answered with SWW_WAF_BAN_RESPONSE, 403 for a block rule or the Core -// Rule Set, 503 for heuristics that ran out of time, or 414 or 431 for the -// header limits. In observe mode nothing is refused, and the log line names +// refuse is answered with the status and logged with the action of the verdict +// that refused it. In observe mode nothing is refused, and the log line names // what enforce mode would have done. ctx is the request's own context. func (rq *request) check(ctx context.Context) *refusal { v := rq.checkClient(ctx) @@ -213,32 +216,16 @@ func (rq *request) check(ctx context.Context) *refusal { return nil } - action := v.action + rq.refusalStatus = v.status - switch { - case rq.h.config.Observe: - rq.wouldAction = action - rq.line.WouldAction = action + if rq.h.config.Observe { + rq.wouldAction = v.action + rq.line.WouldAction = v.action return nil - case action == requestlog.ActionBlockedByBlockRule || - action == requestlog.ActionBlockedByCoreRuleSet: - return &refusal{status: http.StatusForbidden, action: action} - case action == requestlog.ActionChecksTimedOut: - return &refusal{ - status: http.StatusServiceUnavailable, - action: action, - } - case action == requestlog.ActionRequestLineTooLong: - return &refusal{status: http.StatusRequestURITooLong, action: action} - case action == requestlog.ActionRequestHeaderTooLarge: - return &refusal{ - status: http.StatusRequestHeaderFieldsTooLarge, - action: action, - } - default: - return rq.banResponse(action) } + + return &refusal{status: v.status, action: v.action} } // checkClient runs the heuristics on the request, and returns the first diff --git a/internal/waf/rulefiles.go b/internal/waf/rulefiles.go index 748a0f3..6740cd6 100644 --- a/internal/waf/rulefiles.go +++ b/internal/waf/rulefiles.go @@ -1,10 +1,12 @@ package waf // The heuristic rule_files. It reads the rules of the rule files, which -// SWW_WAF_RULE_FILES_DIR and SWW_WAF_RULE_FILES_ENABLED name. +// SWW_WAF_RULE_FILES_DIR and SWW_WAF_RULE_FILES_ENABLED name, and +// SWW_WAF_BAN_RESPONSE. import ( "context" + "net/http" "time" "sneak.berlin/go/smallwebwaf/internal/bans" @@ -17,8 +19,9 @@ import ( // ruleFiles takes the rules of the rule files the request matched, which // findBesideLookup found, notes their ids in the log line, adds the tag // exploitsignature when it matches any, and refuses the request -// when the last rule it matches refuses it: with ActionBlockedByBlockRule for a -// block rule, and for a ban rule with the action for the ban it makes. A ban +// when the last rule it matches refuses it: with 403 and +// ActionBlockedByBlockRule for a block rule, and for a ban rule with +// SWW_WAF_BAN_RESPONSE and the action for the ban it makes. A ban // rule bans the client's netblock for a clear sign of attack, or in observe // mode raises the alert for the ban // it would have made. Either rule's match is noted as an offence, for the @@ -59,6 +62,7 @@ func (r ruleFiles) check( return &verdict{ action: requestlog.ActionBlockedByBlockRule, + status: http.StatusForbidden, figures: figures, } case rules.ActionBan: @@ -70,6 +74,7 @@ func (r ruleFiles) check( now, bans.Notes{RuleID: last.ID, Target: last.Target}, ), + status: r.banMaker.banResponse, figures: figures, } case rules.ActionLog: diff --git a/internal/waf/tagdeny.go b/internal/waf/tagdeny.go index f49f0ce..ff388d7 100644 --- a/internal/waf/tagdeny.go +++ b/internal/waf/tagdeny.go @@ -1,6 +1,6 @@ package waf -// The heuristic tag_deny. It reads SWW_WAF_TAG_DENY. +// The heuristic tag_deny. It reads SWW_WAF_TAG_DENY and SWW_WAF_BAN_RESPONSE. import ( "context" @@ -10,10 +10,11 @@ import ( "sneak.berlin/go/smallwebwaf/internal/requestlog" ) -// tagDeny refuses a request that carries a tag in SWW_WAF_TAG_DENY. Its figure is -// the first such tag, in sorted order. +// tagDeny refuses a request that carries a tag in SWW_WAF_TAG_DENY, with +// SWW_WAF_BAN_RESPONSE. Its figure is the first such tag, in sorted order. type tagDeny struct { - tags []string // SWW_WAF_TAG_DENY + tags []string // SWW_WAF_TAG_DENY + banResponse int // SWW_WAF_BAN_RESPONSE } func (tagDeny) name() string { return "tag_deny" } @@ -27,6 +28,7 @@ func (t tagDeny) check( if slices.Contains(t.tags, tag) { return &verdict{ action: requestlog.ActionClientDenied, + status: t.banResponse, figures: map[string]any{"tag": tag}, } } diff --git a/internal/waf/trappaths.go b/internal/waf/trappaths.go index 39ad33b..87a0feb 100644 --- a/internal/waf/trappaths.go +++ b/internal/waf/trappaths.go @@ -1,6 +1,7 @@ package waf -// The heuristic trap_paths. It reads SWW_WAF_TRAP_PATHS. +// The heuristic trap_paths. It reads SWW_WAF_TRAP_PATHS and +// SWW_WAF_BAN_RESPONSE. import ( "context" @@ -12,10 +13,10 @@ import ( ) // trapPaths refuses a request for a path in SWW_WAF_TRAP_PATHS, as trapPath -// finds it. Such a request is a clear sign of attack, as a ban rule's match is: -// it bans the client's netblock, or in observe mode raises the alert for the -// ban it would have made. The ban's notes give the path as sent, and so does -// its figure. +// finds it, with SWW_WAF_BAN_RESPONSE. Such a request is a clear sign of +// attack, as a ban rule's match is: it bans the client's netblock, or in +// observe mode raises the alert for the ban it would have made. The ban's +// notes give the path as sent, and so does its figure. type trapPaths struct { paths []string // SWW_WAF_TRAP_PATHS banMaker banMaker @@ -41,6 +42,7 @@ func (t trapPaths) check( now, bans.Notes{TrapPath: path}, ), + status: t.banMaker.banResponse, figures: map[string]any{"path": path}, } } -- 2.54.0 From 55216c34c63787ab6cbd5044e1a0e318efe184e8 Mon Sep 17 00:00:00 2001 From: sneak Date: Fri, 20 Nov 2026 00:54:42 +0000 Subject: [PATCH 2/2] waf: the verdict comment no longer reads as banning nothing The doc comment of verdict said a verdict of afterAnswer or onReport "bans and refuses nothing", which reads as banning nothing, while refusalLimit and byteLimits give one only when they ban. It now says that such a verdict refuses nothing. Model: opus-5-5 --- internal/waf/heuristics.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/waf/heuristics.go b/internal/waf/heuristics.go index 10316df..157d92a 100644 --- a/internal/waf/heuristics.go +++ b/internal/waf/heuristics.go @@ -45,7 +45,7 @@ type refusesWithoutLookup interface { // the log line names, the status the visitor is answered with, and the figures // the heuristic acted on, by name, such as the count and the limit. A status of // 0 closes the visitor's connection without an answer. A verdict of afterAnswer -// or onReport bans and refuses nothing, and has neither action nor status. +// or onReport refuses nothing, and has neither action nor status. type verdict struct { action requestlog.Action status int -- 2.54.0