Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3497c41185 | ||
|
|
9cb8f362bc | ||
|
|
48edab5dad |
No files matched your search
@@ -324,7 +324,8 @@ func greet(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// startRProxy starts rproxy with waf at backend and the app at appURL, and
|
||||
// the settings in env on top of the defaults. It returns where rproxy listens
|
||||
// and what it writes.
|
||||
// and what it writes. SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS is a minute unless env
|
||||
// sets it, so that a busy host cannot make rproxy drop a question or a report.
|
||||
func startRProxy(
|
||||
t *testing.T, backend, appURL string, env map[string]string,
|
||||
) (string, *output) {
|
||||
@@ -357,6 +358,7 @@ func startServerLogging(
|
||||
|
||||
settings := map[string]string{
|
||||
"SWW_RPROXY_WAF_BACKEND": backend, "SWW_RPROXY_UPSTREAM_URL": appURL,
|
||||
wafBackendTimeout: "60000",
|
||||
}
|
||||
maps.Copy(settings, env)
|
||||
|
||||
|
||||
@@ -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
|
||||
// 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) {
|
||||
t.Helper()
|
||||
|
||||
@@ -65,6 +67,7 @@ func startRProxy(t *testing.T, backend, appURL string) (string, *output) {
|
||||
"SWW_RPROXY_UPSTREAM_URL": appURL,
|
||||
"SWW_RPROXY_TRUSTED_PROXIES": localhost + "/32",
|
||||
wafBackend: backend,
|
||||
wafBackendTimeout: "60000",
|
||||
}, out)
|
||||
}()
|
||||
|
||||
@@ -549,7 +552,9 @@ func TestAStatusWAFDoesNotTakeIsReportedAs599(t *testing.T) {
|
||||
// startRProxyProcess runs `sww rproxy` in a process of its own, with the
|
||||
// settings in env, each as NAME=value. It returns where rproxy listens and a
|
||||
// function that sends rproxy SIGTERM and fails the test unless it exits with
|
||||
// status 0 within waitLimit.
|
||||
// status 0 within waitLimit. It waits a minute for waf's answers unless env
|
||||
// sets SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS, so that a busy host cannot make it
|
||||
// drop a question.
|
||||
func startRProxyProcess(t *testing.T, env ...string) (string, func()) {
|
||||
t.Helper()
|
||||
|
||||
@@ -565,7 +570,8 @@ func startRProxyProcess(t *testing.T, env ...string) (string, func()) {
|
||||
|
||||
cmd.Env = append(os.Environ(),
|
||||
commandForMain+"=rproxy",
|
||||
"SWW_RPROXY_LISTEN_ADDR="+localhost+":0")
|
||||
"SWW_RPROXY_LISTEN_ADDR="+localhost+":0",
|
||||
wafBackendTimeout+"=60000")
|
||||
cmd.Env = append(cmd.Env, env...)
|
||||
|
||||
exited := startProcess(t, cmd)
|
||||
|
||||
+10
-4
@@ -67,6 +67,9 @@ type Params struct {
|
||||
Viper *viper.Viper
|
||||
// Stdout receives the request log and the process's own messages.
|
||||
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.
|
||||
@@ -146,7 +149,7 @@ func RunWAF(ctx context.Context, params Params) int {
|
||||
|
||||
return runWAFWithSettings(
|
||||
ctx,
|
||||
params.Version,
|
||||
params,
|
||||
cfg,
|
||||
stdout,
|
||||
remote,
|
||||
@@ -159,7 +162,7 @@ func RunWAF(ctx context.Context, params Params) int {
|
||||
// SWW_WAF_LOG_REMOTE_URL is set.
|
||||
func runWAFWithSettings(
|
||||
ctx context.Context,
|
||||
version string,
|
||||
params Params,
|
||||
cfg *config.WAF,
|
||||
stdout io.Writer,
|
||||
remote *remotelog.Sender,
|
||||
@@ -193,6 +196,7 @@ func runWAFWithSettings(
|
||||
ruleFiles,
|
||||
alertQueue,
|
||||
crs,
|
||||
params.BesideLookupTimeLimit,
|
||||
)
|
||||
if err != nil {
|
||||
return cannotStart(processLog, "cannot use the lookup database", err)
|
||||
@@ -221,7 +225,7 @@ func runWAFWithSettings(
|
||||
}
|
||||
|
||||
processLog.Info("starting",
|
||||
"version", version,
|
||||
"version", params.Version,
|
||||
"address", listener.Addr().String(),
|
||||
"settings", cfg)
|
||||
|
||||
@@ -249,7 +253,7 @@ func cannotStart(processLog *slog.Logger, message string, err error) int {
|
||||
func newServer(
|
||||
cfg *config.WAF, stdout io.Writer, processLog *slog.Logger,
|
||||
now func() time.Time, ruleFiles *rules.Files, alertQueue *alerts.Queue,
|
||||
crs *coreruleset.CoreRuleSet,
|
||||
crs *coreruleset.CoreRuleSet, besideLookupTimeLimit time.Duration,
|
||||
) (*waf.Server, error) {
|
||||
var lookupFile *lookup.File
|
||||
|
||||
@@ -278,6 +282,8 @@ func newServer(
|
||||
Now: now,
|
||||
Rules: ruleFiles,
|
||||
Alerts: alertQueue,
|
||||
|
||||
BesideLookupTimeLimit: besideLookupTimeLimit,
|
||||
})
|
||||
server.Metrics.AddAlerts(alertQueue)
|
||||
|
||||
|
||||
@@ -173,6 +173,9 @@ func run(ctx context.Context, env map[string]string, out *output) int {
|
||||
Version: testVersion,
|
||||
Viper: v,
|
||||
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,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -90,6 +90,8 @@ func TestMainRunsWAF(t *testing.T) {
|
||||
cmd := exec.CommandContext(t.Context(), binary)
|
||||
cmd.Stdout = out
|
||||
|
||||
// The Core Rule Set is off. Main keeps waf's own limit for its checks,
|
||||
// and on a busy host the Core Rule Set's inspection can pass it.
|
||||
cmd.Env = append(os.Environ(),
|
||||
"TZ="+zone,
|
||||
commandForMain+"="+wafCommand,
|
||||
@@ -97,7 +99,8 @@ func TestMainRunsWAF(t *testing.T) {
|
||||
stateDir+"="+dir,
|
||||
cacheDir+"="+dir,
|
||||
ruleFilesDir+"="+t.TempDir(),
|
||||
lookupSource+"=off")
|
||||
lookupSource+"=off",
|
||||
"SWW_WAF_CORE_RULE_SET_MODE=off")
|
||||
|
||||
exited := startProcess(t, cmd)
|
||||
|
||||
|
||||
@@ -73,10 +73,10 @@ func newWAFHoldingTheCoreRuleSet(
|
||||
|
||||
server, out, queue := newWAFWith(t, func(params waf.Params) *waf.Server {
|
||||
params.CoreRuleSet = crs
|
||||
params.BesideLookupTimeLimit = waf.BesideLookupTimeLimit
|
||||
|
||||
return waf.New(params)
|
||||
}, silentGeoJSURL, time.Now, settings)
|
||||
server.SetBesideLookupTimeLimit(waf.BesideLookupTimeLimit)
|
||||
|
||||
return server, out, queue
|
||||
}
|
||||
|
||||
@@ -1,13 +1,4 @@
|
||||
package waf
|
||||
|
||||
import "time"
|
||||
|
||||
// BesideLookupTimeLimit is 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
@@ -4,6 +4,7 @@
|
||||
package waf
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
@@ -83,6 +84,11 @@ type Params struct {
|
||||
// failing, a query to a DNSBL zone or a check with AbuseIPDB failing,
|
||||
// or the day's AbuseIPDB checks used up.
|
||||
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
|
||||
@@ -168,7 +174,10 @@ func newHandler(params Params) *handler {
|
||||
coreRuleSet: params.CoreRuleSet,
|
||||
alerts: params.Alerts,
|
||||
|
||||
besideLookupLimit: besideLookupTimeLimit,
|
||||
besideLookupLimit: cmp.Or(
|
||||
params.BesideLookupTimeLimit,
|
||||
besideLookupTimeLimit,
|
||||
),
|
||||
}
|
||||
if params.Config.RouteWatchURL != "" {
|
||||
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
|
||||
// GeoJS before the request goes on.
|
||||
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
|
||||
// awaitingReport holds the decision on each answered question by its id
|
||||
// until its report comes, and nil for a question still being handled.
|
||||
|
||||
@@ -510,13 +510,11 @@ func newWAFWith(
|
||||
Now: now,
|
||||
Rules: ruleFiles,
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user