package waf // The heuristic refusal_limit. It reads SWW_WAF_REFUSALS_LIMIT_PER_MINUTE. import ( "context" "time" "sneak.berlin/go/smallwebwaf/internal/ratelimit" ) // 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. // 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 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 } r.banMaker.banForLimit(rq, now, hit, rq.refusalStatus) return &verdict{figures: limitFigures(hit)} }