servewaf: each decision heuristic is its own module (closes #195)
check / check (push) Successful in 3m12s

Each check servewaf runs on a request is a type in its own file behind
one interface, heuristic, and is registered in one of four ordered lists
in heuristics.go. Order and behaviour are unchanged.

Every refusal now logs heuristic, the name of the heuristic that made
it, and heuristic_figures, the figures it acted on. A ban by
refusal_limit or byte_limits after the answer logs report_heuristic and
report_heuristic_figures instead, so a line can name both. All four are
in docs/request-log.md, and SPEC.md names each heuristic at its step.

Model: opus-5-5
This commit is contained in:
2026-11-06 19:53:13 +00:00
parent 21cac70b50
commit 14e8ee7413
36 changed files with 1108 additions and 397 deletions
+42
View File
@@ -1,6 +1,7 @@
package servewaf_test
import (
"encoding/json"
"io"
"math"
"net/http"
@@ -272,6 +273,47 @@ func wantTimings(t *testing.T, line logLine, want ...string) {
}
}
// wantHeuristic checks that line names heuristic as the one that refused the
// request, with figures, a JSON object. A heuristic of "" with figures "null" is
// a line that names none.
func wantHeuristic(t *testing.T, line logLine, heuristic, figures string) {
t.Helper()
if line.Heuristic != heuristic ||
!reflect.DeepEqual(line.HeuristicFigures, decodeFigures(t, figures)) {
t.Errorf("log line has heuristic %q with heuristic_figures %v, want %q with %s",
line.Heuristic, line.HeuristicFigures, heuristic, figures)
}
}
// wantReportHeuristic checks report_heuristic and report_heuristic_figures as
// wantHeuristic checks heuristic and heuristic_figures.
func wantReportHeuristic(t *testing.T, line logLine, heuristic, figures string) {
t.Helper()
if line.ReportHeuristic != heuristic ||
!reflect.DeepEqual(line.ReportHeuristicFigures, decodeFigures(t, figures)) {
t.Errorf("log line has report_heuristic %q with report_heuristic_figures %v, "+
"want %q with %s",
line.ReportHeuristic, line.ReportHeuristicFigures, heuristic, figures)
}
}
// decodeFigures decodes figures, a JSON object of a heuristic's figures, as
// the log line's figures are decoded.
func decodeFigures(t *testing.T, figures string) map[string]any {
t.Helper()
var decoded map[string]any
err := json.Unmarshal([]byte(figures), &decoded)
if err != nil {
t.Fatalf("decode %s: %v", figures, err)
}
return decoded
}
// microseconds is a timing in whole microseconds.
func microseconds(milliseconds float64) int64 {
return int64(math.Round(milliseconds * 1000))