check / check (push) Failing after 1m14s
Every setting of discriminator and rproxy is read through viper, in place of the os.LookupEnv function Main passed down. NewViper turns AutomaticEnv and AllowEmptyEnv on, so a setting that is set but empty reads as set. A setting may also be given as a file, the _FILE form, which rproxy's settings now have too; rproxy still writes nothing to disk. The settings are renamed. Discriminator's take the prefix SWW_WAF_, rproxy's take SWW_RPROXY_, and the shared SWW_COMMAND keeps SWW_. The Core Rule Set's settings take SWW_WAF_CORE_RULE_SET_ rather than SWW_WAF_WAF_. Model: deepseek-v4-pro (first version); opus-5-5 (rework)
70 lines
2.3 KiB
Go
70 lines
2.3 KiB
Go
package discriminator
|
|
|
|
// 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"
|
|
"time"
|
|
|
|
"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, 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{}
|
|
|
|
func (rateLimits) name() string { return "rate_limits" }
|
|
|
|
func (rateLimits) check(_ context.Context, rq *request, now time.Time) *verdict {
|
|
rq.counted = rq.countedByRateLimits()
|
|
if !rq.counted {
|
|
return nil
|
|
}
|
|
|
|
rq.rateLimitPercent, rq.bytesLimitPercent = rq.limitPercentages()
|
|
rq.line.RateLimitPercent, rq.line.RateLimitPercentSetting =
|
|
rq.rateLimitPercent.logged()
|
|
rq.line.BytesLimitPercent, rq.line.BytesLimitPercentSetting =
|
|
rq.bytesLimitPercent.logged()
|
|
|
|
counts, hit, over := rq.h.limiter.Count(rq.h.clientGroup(rq.client), now,
|
|
rq.rateLimitPercent.percent)
|
|
rq.line.Counts = counts
|
|
|
|
if !over {
|
|
return nil
|
|
}
|
|
|
|
rq.banForLimit(now, hit, rq.h.config.BanResponse)
|
|
|
|
return &verdict{
|
|
action: requestlog.ActionClientIsRateLimited, figures: limitFigures(hit),
|
|
}
|
|
}
|
|
|
|
// overAWholeRateLimit reports whether the heuristic rate_limits will refuse the
|
|
// request at now, whatever its client's limit percentages: the rate limits
|
|
// count it, and one more request from its client takes it over a whole rate
|
|
// limit. It counts nothing.
|
|
func (rq *request) overAWholeRateLimit(now time.Time) bool {
|
|
return rq.countedByRateLimits() &&
|
|
rq.h.limiter.OverARateLimit(rq.h.clientGroup(rq.client), now)
|
|
}
|
|
|
|
// countedByRateLimits reports whether the rate limits count the request: 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 (rq *request) countedByRateLimits() bool {
|
|
cfg := rq.h.config
|
|
|
|
return !isInside(rq.client, cfg.RateLimitExemptNets) &&
|
|
!pathExempt(rq.in, cfg.RateLimitExemptPaths)
|
|
}
|