check / check (push) Failing after 1m42s
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
94 lines
2.9 KiB
Go
94 lines
2.9 KiB
Go
package waf
|
|
|
|
// The heuristic rate_limits. It reads SWW_WAF_RATE_LIMIT_EXEMPT_NETS,
|
|
// SWW_WAF_RATE_LIMIT_EXEMPT_PATHS and SWW_WAF_BAN_RESPONSE, and the rate limits and
|
|
// biased thresholds through the limiter and limitPercentages.
|
|
|
|
import (
|
|
"context"
|
|
"net/netip"
|
|
"time"
|
|
|
|
"sneak.berlin/go/smallwebwaf/internal/ratelimit"
|
|
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
|
)
|
|
|
|
// rateLimits counts the request for the rate limits, unless
|
|
// 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 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
|
|
exemptPaths []string // SWW_WAF_RATE_LIMIT_EXEMPT_PATHS
|
|
banResponse int // SWW_WAF_BAN_RESPONSE
|
|
ipv6GroupPrefix int // SWW_WAF_IPV6_GROUP_PREFIX
|
|
biasedThresholds biasedThresholds
|
|
banMaker banMaker
|
|
}
|
|
|
|
func (rateLimits) name() string { return "rate_limits" }
|
|
|
|
func (r rateLimits) check(
|
|
_ context.Context,
|
|
rq *request,
|
|
now time.Time,
|
|
) *verdict {
|
|
rq.counted = r.countedByRateLimits(rq)
|
|
if !rq.counted {
|
|
return nil
|
|
}
|
|
|
|
rq.rateLimitPercent, rq.bytesLimitPercent =
|
|
r.biasedThresholds.limitPercentages(rq)
|
|
rq.line.RateLimitPercent, rq.line.RateLimitPercentSetting =
|
|
rq.rateLimitPercent.logged()
|
|
rq.line.BytesLimitPercent, rq.line.BytesLimitPercentSetting =
|
|
rq.bytesLimitPercent.logged()
|
|
|
|
counts, hit, over := r.limiter.Count(
|
|
clientGroup(r.ipv6GroupPrefix, rq.client),
|
|
now,
|
|
rq.rateLimitPercent.percent,
|
|
)
|
|
rq.line.Counts = counts
|
|
|
|
if !over {
|
|
return nil
|
|
}
|
|
|
|
r.banMaker.banForLimit(rq, now, hit, r.banResponse)
|
|
|
|
return &verdict{
|
|
action: requestlog.ActionClientIsRateLimited,
|
|
status: r.banResponse,
|
|
figures: limitFigures(hit),
|
|
}
|
|
}
|
|
|
|
// refusesWhateverTheLookupAnswers reports whether the heuristic rate_limits
|
|
// will refuse rq at now, whatever its client's limit percentages, which the
|
|
// lookup's answer can lower: the rate limits count it, and one more request
|
|
// from its client takes it over a whole rate limit. It counts nothing.
|
|
func (r rateLimits) refusesWhateverTheLookupAnswers(
|
|
rq *request,
|
|
now time.Time,
|
|
) bool {
|
|
return r.countedByRateLimits(rq) &&
|
|
r.limiter.OverARateLimit(
|
|
clientGroup(r.ipv6GroupPrefix, rq.client),
|
|
now,
|
|
)
|
|
}
|
|
|
|
// countedByRateLimits reports whether the rate limits count rq: its client is
|
|
// not in SWW_WAF_RATE_LIMIT_EXEMPT_NETS, and its path is not exempt under
|
|
// SWW_WAF_RATE_LIMIT_EXEMPT_PATHS.
|
|
func (r rateLimits) countedByRateLimits(rq *request) bool {
|
|
return !isInside(rq.client, r.exemptNets) &&
|
|
!pathExempt(rq.in, r.exemptPaths)
|
|
}
|