coreruleset: time worst-case inspections against reference work (closes #400)
check / check (push) Failing after 3m52s
check / check (push) Failing after 3m52s
The worst-case inspection test compared each inspection with a fixed 1 second, so a busy host failed it at random. It now times, on the same thread and in processor time, a fixed regular-expression job just before and after each inspection. An inspection fails when it takes more than 13 times that job, about 1 second on an idle host. Model: opus-5-5
This commit is contained in:
1 parent
3c6b0d519f
commit
0e8472fc96
2 files changed
+84
-19
No files matched your search
@@ -12,6 +12,7 @@ require (
|
|||||||
github.com/oschwald/maxminddb-golang/v2 v2.7.0
|
github.com/oschwald/maxminddb-golang/v2 v2.7.0
|
||||||
github.com/prometheus/client_golang v1.24.1
|
github.com/prometheus/client_golang v1.24.1
|
||||||
github.com/spf13/viper v1.21.0
|
github.com/spf13/viper v1.21.0
|
||||||
|
golang.org/x/sys v0.48.0
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
@@ -47,7 +48,6 @@ require (
|
|||||||
go4.org/netipx v0.0.0-20231129151722-fdeea329fbba // indirect
|
go4.org/netipx v0.0.0-20231129151722-fdeea329fbba // indirect
|
||||||
golang.org/x/net v0.60.0 // indirect
|
golang.org/x/net v0.60.0 // indirect
|
||||||
golang.org/x/sync v0.23.0 // indirect
|
golang.org/x/sync v0.23.0 // indirect
|
||||||
golang.org/x/sys v0.48.0 // indirect
|
|
||||||
golang.org/x/text v0.42.0 // indirect
|
golang.org/x/text v0.42.0 // indirect
|
||||||
google.golang.org/protobuf v1.36.11 // indirect
|
google.golang.org/protobuf v1.36.11 // indirect
|
||||||
rsc.io/binaryregexp v0.2.0 // indirect
|
rsc.io/binaryregexp v0.2.0 // indirect
|
||||||
|
|||||||
@@ -5,12 +5,16 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
|
"regexp"
|
||||||
|
"runtime"
|
||||||
"slices"
|
"slices"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/coreruleset"
|
"sneak.berlin/go/smallwebwaf/internal/coreruleset"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -31,12 +35,45 @@ const (
|
|||||||
mostJSONRead = 1 << 10
|
mostJSONRead = 1 << 10
|
||||||
)
|
)
|
||||||
|
|
||||||
// inspectionTimeLimit is the most time the Core Rule Set may take on one
|
// The Core Rule Set has 1 second for one request. That is the default of
|
||||||
// request. It is the default of rproxy's SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS, 2
|
// rproxy's SWW_RPROXY_WAF_BACKEND_TIMEOUT_MS, 2 seconds, less the default of
|
||||||
// seconds, less the default of SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS, 800
|
// SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS, 800 milliseconds, and less 200
|
||||||
// milliseconds, which a question can also wait for GeoJS, and less 200
|
|
||||||
// milliseconds for waf's other checks.
|
// milliseconds for waf's other checks.
|
||||||
const inspectionTimeLimit = time.Second
|
//
|
||||||
|
// A busy host slows all work, so the test does not compare an inspection with
|
||||||
|
// 1 second. It compares it with referenceTime, timed on the same thread just
|
||||||
|
// before and after. inspectionTimeLimitMultiple is how many times
|
||||||
|
// referenceTime an inspection may take.
|
||||||
|
//
|
||||||
|
// On the host that runs our checks, referenceTime took at least 78
|
||||||
|
// milliseconds, so 13 times it is about 1 second. With that host fully busy,
|
||||||
|
// no try of the slowest request took more than 9.5 times referenceTime.
|
||||||
|
const inspectionTimeLimitMultiple = 13
|
||||||
|
|
||||||
|
// referencePattern is what referenceTime matches. Like the Core Rule Set's
|
||||||
|
// rules, it is a regular expression with alternatives and repeats.
|
||||||
|
var referencePattern = regexp.MustCompile(
|
||||||
|
`(?i)(union|select|insert|delete)\s+\w+\s+(from|into)\s+\w+[0-9]+;`,
|
||||||
|
)
|
||||||
|
|
||||||
|
// referenceTime returns the processor time the calling thread takes to do a
|
||||||
|
// fixed amount of work unrelated to the Core Rule Set, so that a change to the
|
||||||
|
// rules does not change it. referencePattern matches nowhere in the text, so
|
||||||
|
// each match reads all of it.
|
||||||
|
func referenceTime(t *testing.T) time.Duration {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
referenceText := text(4 << 10)
|
||||||
|
began := threadCPUTime(t)
|
||||||
|
|
||||||
|
for range 200 {
|
||||||
|
if referencePattern.MatchString(referenceText) {
|
||||||
|
t.Fatal("the reference pattern matched")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return threadCPUTime(t) - began
|
||||||
|
}
|
||||||
|
|
||||||
// text returns n bytes of letters, digits and spaces, which the Core Rule Set
|
// text returns n bytes of letters, digits and spaces, which the Core Rule Set
|
||||||
// takes longer over than one letter repeated.
|
// takes longer over than one letter repeated.
|
||||||
@@ -302,11 +339,30 @@ func worstCaseBodies() []visitorRequest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// inspectionTime returns how long crs takes to inspect tc, and fails t unless
|
// threadCPUTime returns the processor time the calling thread has used.
|
||||||
// it reads all of tc's body and Coraza parses it.
|
func threadCPUTime(t *testing.T) time.Duration {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
var now unix.Timespec
|
||||||
|
|
||||||
|
err := unix.ClockGettime(unix.CLOCK_THREAD_CPUTIME_ID, &now)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("read the thread's processor time: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return time.Duration(now.Nano())
|
||||||
|
}
|
||||||
|
|
||||||
|
// inspectionTime returns the processor time crs takes to inspect tc, and the
|
||||||
|
// mean of referenceTime just before and after. It fails t unless crs reads all
|
||||||
|
// of tc's body and Coraza parses it.
|
||||||
|
//
|
||||||
|
// All of it runs on this goroutine, kept on one thread, so the thread's
|
||||||
|
// processor time measures it. Time other work holds the processor does not
|
||||||
|
// count.
|
||||||
func inspectionTime(
|
func inspectionTime(
|
||||||
t *testing.T, crs *coreruleset.CoreRuleSet, tc visitorRequest,
|
t *testing.T, crs *coreruleset.CoreRuleSet, tc visitorRequest,
|
||||||
) time.Duration {
|
) (time.Duration, time.Duration) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
req := httptest.NewRequestWithContext(
|
req := httptest.NewRequestWithContext(
|
||||||
@@ -318,11 +374,19 @@ func inspectionTime(
|
|||||||
req.Header.Set("Content-Type", tc.contentType)
|
req.Header.Set("Content-Type", tc.contentType)
|
||||||
}
|
}
|
||||||
|
|
||||||
began := time.Now()
|
runtime.LockOSThread()
|
||||||
|
|
||||||
|
defer runtime.UnlockOSThread()
|
||||||
|
|
||||||
|
before := referenceTime(t)
|
||||||
|
|
||||||
|
began := threadCPUTime(t)
|
||||||
result, read, _ := crs.Inspect(
|
result, read, _ := crs.Inspect(
|
||||||
req, netip.MustParseAddr("192.0.2.1"), strings.NewReader(tc.body),
|
req, netip.MustParseAddr("192.0.2.1"), strings.NewReader(tc.body),
|
||||||
)
|
)
|
||||||
took := time.Since(began)
|
took := threadCPUTime(t) - began
|
||||||
|
|
||||||
|
reference := (before + referenceTime(t)) / 2
|
||||||
|
|
||||||
if string(read) != tc.body {
|
if string(read) != tc.body {
|
||||||
t.Errorf("read %d bytes of %d", len(read), len(tc.body))
|
t.Errorf("read %d bytes of %d", len(read), len(tc.body))
|
||||||
@@ -333,13 +397,13 @@ func inspectionTime(
|
|||||||
t.Errorf("Coraza could not parse the body")
|
t.Errorf("Coraza could not parse the body")
|
||||||
}
|
}
|
||||||
|
|
||||||
return took
|
return took, reference
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestTheCoreRuleSetInspectsEachWorstCaseRequestInTime runs one at a time, so
|
// TestTheCoreRuleSetInspectsEachWorstCaseRequestInTime runs one at a time, so
|
||||||
// that the tests running in parallel do not slow it. Other work on the host
|
// that the tests running in parallel do not slow it. Other work on the host
|
||||||
// can still slow one try, so a request fails only when three tries in a row
|
// can still slow one try more than the reference work around it, so a request
|
||||||
// take too long.
|
// fails only when three tries in a row take too long.
|
||||||
//
|
//
|
||||||
//nolint:paralleltest // one at a time, as above
|
//nolint:paralleltest // one at a time, as above
|
||||||
func TestTheCoreRuleSetInspectsEachWorstCaseRequestInTime(t *testing.T) {
|
func TestTheCoreRuleSetInspectsEachWorstCaseRequestInTime(t *testing.T) {
|
||||||
@@ -353,18 +417,19 @@ func TestTheCoreRuleSetInspectsEachWorstCaseRequestInTime(t *testing.T) {
|
|||||||
|
|
||||||
for _, tc := range worstCaseRequests() {
|
for _, tc := range worstCaseRequests() {
|
||||||
t.Run(tc.name, func(t *testing.T) {
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
var took time.Duration
|
var took, reference time.Duration
|
||||||
|
|
||||||
for range tries {
|
for range tries {
|
||||||
took = inspectionTime(t, crs, tc)
|
took, reference = inspectionTime(t, crs, tc)
|
||||||
if took <= inspectionTimeLimit {
|
if took <= inspectionTimeLimitMultiple*reference {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Errorf(
|
t.Errorf(
|
||||||
"took %s on the last of %d tries, over %s",
|
"took %s on the last of %d tries, over %d times the %s "+
|
||||||
took, tries, inspectionTimeLimit,
|
"of the reference work",
|
||||||
|
took, tries, inspectionTimeLimitMultiple, reference,
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user