check / check (push) Waiting to run
Every heuristic is a struct built once in newHandler with the settings and collaborators it reads; no check reads the handler. The six lists are handler fields built there. Code several heuristics share takes what it needs as arguments or lives on a struct they hold: banMaker for bans and their alerts, reputationHitNoter for reputation hits, biasedThresholds for limit percentages. The handler keeps blocklists, rateLimits, trapPaths, ruleFiles and coreRuleSet as fields too, since refusedWhateverTheAnswer and findBesideLookup use them. Model: opus-5-5
70 lines
1.8 KiB
Go
70 lines
1.8 KiB
Go
package waf
|
|
|
|
// The heuristic refusal_limit. It reads SWW_WAF_REFUSALS_LIMIT_PER_MINUTE and
|
|
// SWW_WAF_BAN_RESPONSE.
|
|
|
|
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
|
|
// refused it after a rule file match, a trap path or a Core Rule Set match, and
|
|
// in observe mode if enforce mode would have: more than
|
|
// 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.
|
|
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
|
|
}
|
|
|
|
func (refusalLimit) name() string { return "refusal_limit" }
|
|
|
|
func (r refusalLimit) check(
|
|
_ context.Context,
|
|
rq *request,
|
|
now time.Time,
|
|
) *verdict {
|
|
if r.perMinute == 0 {
|
|
return nil
|
|
}
|
|
|
|
if !rq.attack && !rq.blockedByBlockRule && !rq.blockedByCoreRuleSet {
|
|
return nil
|
|
}
|
|
|
|
hit, over := r.limiter.CountRefusal(
|
|
clientGroup(r.ipv6GroupPrefix, rq.client),
|
|
now,
|
|
r.perMinute,
|
|
)
|
|
if !over {
|
|
return nil
|
|
}
|
|
|
|
// What the client was sent, or in observe mode would have been.
|
|
status := rq.out.status
|
|
|
|
switch rq.line.WouldAction {
|
|
case requestlog.ActionBlockedByBlockRule,
|
|
requestlog.ActionBlockedByCoreRuleSet:
|
|
status = http.StatusForbidden
|
|
case requestlog.ActionTemporarilyBanned,
|
|
requestlog.ActionPermanentlyBanned:
|
|
status = r.banResponse
|
|
}
|
|
|
|
r.banMaker.banForLimit(rq, now, hit, status)
|
|
|
|
return &verdict{figures: limitFigures(hit)}
|
|
}
|