check / check (push) Successful in 5m7s
The command is now `sww waf`, and the package is `internal/waf`. Types, functions, files, tests, comments, docs, the Dockerfile, the runit script, `script/`, compose examples, the log field `waf_line` and the `X-From-Waf` test header follow. No behaviour changes, and no setting changes name: rproxy already finds the waf through `SWRP_WAF_BACKEND`. Done with one `sed -i` (Discriminator and DISCRIMINATOR to WAF, discriminator to waf) over the files `git grep -i discriminator` listed, as sneak allowed for this rename, then `git mv` for the package and files. Model: opus-5-5
55 lines
1.5 KiB
Go
55 lines
1.5 KiB
Go
package waf
|
|
|
|
// The heuristic refusal_limit. It reads SWW_ERROR_BURST_THRESHOLD and
|
|
// SWW_BAN_RESPONSE.
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"time"
|
|
|
|
"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_ERROR_BURST_THRESHOLD such refusals of the client within a minute break
|
|
// a limit, which bans the client. A client in SWW_ALLOW_NETS, which the
|
|
// heuristics skip, is not counted, and nothing is while the threshold is off.
|
|
// Its figures are the window, the count and the limit.
|
|
type refusalLimit struct{}
|
|
|
|
func (refusalLimit) name() string { return "refusal_limit" }
|
|
|
|
func (refusalLimit) check(_ context.Context, rq *request, now time.Time) *verdict {
|
|
cfg := rq.h.config
|
|
if cfg.ErrorBurstThreshold == 0 {
|
|
return nil
|
|
}
|
|
|
|
if !rq.attack && !rq.blockedByBlockRule && !rq.blockedByCoreRuleSet {
|
|
return nil
|
|
}
|
|
|
|
hit, over := rq.h.limiter.CountRefusal(rq.h.clientGroup(rq.client), now,
|
|
cfg.ErrorBurstThreshold)
|
|
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 = cfg.BanResponse
|
|
}
|
|
|
|
rq.banForLimit(now, hit, status)
|
|
|
|
return &verdict{figures: limitFigures(hit)}
|
|
}
|