check / check (push) Waiting to run
The tags, what observe mode would have done, the broken limit and the three reputation flags are fields of decision, each naming what sets it. The log line is written from them and from the lookup answer, and no heuristic, ban note or alert reads it back. A ban's notes take the request's method, Host and User-Agent from decision, since the report's steps have no request to read them from. 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.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)}
|
|
}
|