Compare commits

..
Author SHA1 Message Date
clawbot 334a082a42 sww: lift waf's check time limit and rproxy's wait in tests (closes #418)
check / check (push) Successful in 5m33s
Under heavy host load the sww tests got 503 where they expected 200 or
403: waf refused requests whose trap paths, rule files and Core Rule Set
took longer than 500 ms, and rproxy gave up on waf after 2 s.

waf.Params and sww.Params gain BesideLookupTimeLimit; zero keeps the
500 ms limit, so the binary is unchanged. The sww tests' run gives waf
an hour, and startRProxy sets SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS to
60000, as the rproxy package's tests do. waf's own tests set the limit
through the new field instead of SetBesideLookupTimeLimit, which is gone.

Model: opus-5-5
2026-11-19 08:48:15 +00:00
7 changed files with 33 additions and 22 deletions

No files matched your search

+4 -1
View File
@@ -51,7 +51,9 @@ func runRProxy(ctx context.Context, env map[string]string, out *output) int {
// startRProxy runs rproxy with waf at backend and the app at appURL, and // startRProxy runs rproxy with waf at backend and the app at appURL, and
// returns its URL and what it writes. It believes X-Forwarded-For from the // returns its URL and what it writes. It believes X-Forwarded-For from the
// tests. It stops as the test ends, and that it stopped in order is checked. // tests. It waits a minute for waf's answer, so that a slow answer on a busy
// host is not taken for no answer. It stops as the test ends, and that it
// stopped in order is checked.
func startRProxy(t *testing.T, backend, appURL string) (string, *output) { func startRProxy(t *testing.T, backend, appURL string) (string, *output) {
t.Helper() t.Helper()
@@ -65,6 +67,7 @@ func startRProxy(t *testing.T, backend, appURL string) (string, *output) {
"SWW_RPROXY_UPSTREAM_URL": appURL, "SWW_RPROXY_UPSTREAM_URL": appURL,
"SWW_RPROXY_TRUSTED_PROXIES": localhost + "/32", "SWW_RPROXY_TRUSTED_PROXIES": localhost + "/32",
wafBackend: backend, wafBackend: backend,
wafBackendTimeout: "60000",
}, out) }, out)
}() }()
+10 -4
View File
@@ -67,6 +67,9 @@ type Params struct {
Viper *viper.Viper Viper *viper.Viper
// Stdout receives the request log and the process's own messages. // Stdout receives the request log and the process's own messages.
Stdout io.Writer Stdout io.Writer
// BesideLookupTimeLimit is the waf.Params.BesideLookupTimeLimit RunWAF
// gives waf. Main leaves it zero, waf's own limit; tests lengthen it.
BesideLookupTimeLimit time.Duration
} }
// Main runs the command os.Args names, and returns the process's exit status. // Main runs the command os.Args names, and returns the process's exit status.
@@ -146,7 +149,7 @@ func RunWAF(ctx context.Context, params Params) int {
return runWAFWithSettings( return runWAFWithSettings(
ctx, ctx,
params.Version, params,
cfg, cfg,
stdout, stdout,
remote, remote,
@@ -159,7 +162,7 @@ func RunWAF(ctx context.Context, params Params) int {
// SWW_WAF_LOG_REMOTE_URL is set. // SWW_WAF_LOG_REMOTE_URL is set.
func runWAFWithSettings( func runWAFWithSettings(
ctx context.Context, ctx context.Context,
version string, params Params,
cfg *config.WAF, cfg *config.WAF,
stdout io.Writer, stdout io.Writer,
remote *remotelog.Sender, remote *remotelog.Sender,
@@ -193,6 +196,7 @@ func runWAFWithSettings(
ruleFiles, ruleFiles,
alertQueue, alertQueue,
crs, crs,
params.BesideLookupTimeLimit,
) )
if err != nil { if err != nil {
return cannotStart(processLog, "cannot use the lookup database", err) return cannotStart(processLog, "cannot use the lookup database", err)
@@ -221,7 +225,7 @@ func runWAFWithSettings(
} }
processLog.Info("starting", processLog.Info("starting",
"version", version, "version", params.Version,
"address", listener.Addr().String(), "address", listener.Addr().String(),
"settings", cfg) "settings", cfg)
@@ -249,7 +253,7 @@ func cannotStart(processLog *slog.Logger, message string, err error) int {
func newServer( func newServer(
cfg *config.WAF, stdout io.Writer, processLog *slog.Logger, cfg *config.WAF, stdout io.Writer, processLog *slog.Logger,
now func() time.Time, ruleFiles *rules.Files, alertQueue *alerts.Queue, now func() time.Time, ruleFiles *rules.Files, alertQueue *alerts.Queue,
crs *coreruleset.CoreRuleSet, crs *coreruleset.CoreRuleSet, besideLookupTimeLimit time.Duration,
) (*waf.Server, error) { ) (*waf.Server, error) {
var lookupFile *lookup.File var lookupFile *lookup.File
@@ -278,6 +282,8 @@ func newServer(
Now: now, Now: now,
Rules: ruleFiles, Rules: ruleFiles,
Alerts: alertQueue, Alerts: alertQueue,
BesideLookupTimeLimit: besideLookupTimeLimit,
}) })
server.Metrics.AddAlerts(alertQueue) server.Metrics.AddAlerts(alertQueue)
+3
View File
@@ -173,6 +173,9 @@ func run(ctx context.Context, env map[string]string, out *output) int {
Version: testVersion, Version: testVersion,
Viper: v, Viper: v,
Stdout: out, Stdout: out,
// The race detector and a busy host can slow the Core Rule Set's
// inspections past waf's own limit.
BesideLookupTimeLimit: time.Hour,
}) })
} }
+1 -1
View File
@@ -73,10 +73,10 @@ func newWAFHoldingTheCoreRuleSet(
server, out, queue := newWAFWith(t, func(params waf.Params) *waf.Server { server, out, queue := newWAFWith(t, func(params waf.Params) *waf.Server {
params.CoreRuleSet = crs params.CoreRuleSet = crs
params.BesideLookupTimeLimit = waf.BesideLookupTimeLimit
return waf.New(params) return waf.New(params)
}, silentGeoJSURL, time.Now, settings) }, silentGeoJSURL, time.Now, settings)
server.SetBesideLookupTimeLimit(waf.BesideLookupTimeLimit)
return server, out, queue return server, out, queue
} }
-9
View File
@@ -1,13 +1,4 @@
package waf package waf
import "time"
// BesideLookupTimeLimit is besideLookupTimeLimit. // BesideLookupTimeLimit is besideLookupTimeLimit.
const BesideLookupTimeLimit = besideLookupTimeLimit const BesideLookupTimeLimit = besideLookupTimeLimit
// SetBesideLookupTimeLimit gives the heuristics of besideLookup limit on s in
// place of besideLookupTimeLimit.
func (s *Server) SetBesideLookupTimeLimit(limit time.Duration) {
h := s.Handler.(*handler) //nolint:forcetypeassert // New makes it one
h.besideLookupLimit = limit
}
+12 -2
View File
@@ -4,6 +4,7 @@
package waf package waf
import ( import (
"cmp"
"fmt" "fmt"
"io" "io"
"log/slog" "log/slog"
@@ -83,6 +84,11 @@ type Params struct {
// failing, a query to a DNSBL zone or a check with AbuseIPDB failing, // failing, a query to a DNSBL zone or a check with AbuseIPDB failing,
// or the day's AbuseIPDB checks used up. // or the day's AbuseIPDB checks used up.
Alerts *alerts.Queue Alerts *alerts.Queue
// BesideLookupTimeLimit is how long the heuristics of besideLookup have
// for a request. Zero means besideLookupTimeLimit. Tests that run the
// Core Rule Set lengthen it, since the race detector and a busy host can
// slow an inspection past besideLookupTimeLimit.
BesideLookupTimeLimit time.Duration
} }
// CoreRuleSetInspector inspects a request with the Core Rule Set, as // CoreRuleSetInspector inspects a request with the Core Rule Set, as
@@ -168,7 +174,10 @@ func newHandler(params Params) *handler {
coreRuleSet: params.CoreRuleSet, coreRuleSet: params.CoreRuleSet,
alerts: params.Alerts, alerts: params.Alerts,
besideLookupLimit: besideLookupTimeLimit, besideLookupLimit: cmp.Or(
params.BesideLookupTimeLimit,
besideLookupTimeLimit,
),
} }
if params.Config.RouteWatchURL != "" { if params.Config.RouteWatchURL != "" {
h.routeWatch = routewatch.New(routewatch.Params{ h.routeWatch = routewatch.New(routewatch.Params{
@@ -292,7 +301,8 @@ type handler struct {
// waitForGeoJS is true when a setting needs each request's answer from // waitForGeoJS is true when a setting needs each request's answer from
// GeoJS before the request goes on. // GeoJS before the request goes on.
waitForGeoJS bool waitForGeoJS bool
// besideLookupLimit is besideLookupTimeLimit. Only tests change it. // besideLookupLimit is how long the heuristics of besideLookup have for a
// request, as Params.BesideLookupTimeLimit says.
besideLookupLimit time.Duration besideLookupLimit time.Duration
// awaitingReport holds the decision on each answered question by its id // awaitingReport holds the decision on each answered question by its id
// until its report comes, and nil for a question still being handled. // until its report comes, and nil for a question still being handled.
+3 -5
View File
@@ -510,13 +510,11 @@ func newWAFWith(
Now: now, Now: now,
Rules: ruleFiles, Rules: ruleFiles,
Alerts: alertQueue, Alerts: alertQueue,
// The tests of the limit set it back, and hold the Core Rule Set
// with a stand-in.
BesideLookupTimeLimit: time.Hour,
}) })
// The race detector and a busy host can slow the Core Rule Set's
// inspections past the time limit. The tests of the limit set it back, and
// hold the Core Rule Set with a stand-in.
server.SetBesideLookupTimeLimit(time.Hour)
return server, out, alertQueue return server, out, alertQueue
} }