waf: run the trap paths, rule files and Core Rule Set during the GeoJS wait (closes #405)
check / check (push) Failing after 1m51s

While the lookup waits for GeoJS, the trap paths, the rule files and the
Core Rule Set run at the same time as the checks after the lookup. The
first side to refuse answers the request; what the other side finds leaves
no trace. Those three checks have 500 ms together: past it, the request is
refused with 503 as checks_timed_out, with no ban and no offence.

Tests hold GeoJS and the Core Rule Set with stand-ins under synctest. The
test helper lifts the 500 ms limit for tests that run the real Core Rule
Set, which the race detector slows past it.

Model: opus-5-5
This commit was merged in pull request #408.
This commit is contained in:
clawbot committed 2026-11-17 06:11:52 +01:00
1 parent caa3079847
commit 8c9f20d346
19 files changed
+902 -84

No files matched your search

+66 -22
View File
@@ -21,36 +21,80 @@ const (
countryHeader = "X-Client-Country"
)
// lookUp looks up the client's AS number and country, in the lookup
// database or through GeoJS, and notes them for the log line and as the
// request's asn= and country= tags, unless
// SWW_WAF_LOOKUP_SOURCE is off or the client is on a private, loopback or
// link-local address, which no lookup can place. The lookup database
// answers at once. With GeoJS, while a setting needs the answer, such as a
// country list or a biased threshold, a new client's request waits for it up
// to SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS, unless refusedWhateverTheAnswer
// reports true. ctx is the request's own context.
func (rq *request) lookUp(ctx context.Context, now time.Time) {
// lookupResult is what the lookup gave for a request: whether its client was
// looked up, the answer, and whether the request skipped the wait for GeoJS.
type lookupResult struct {
lookedUp, skippedWait bool
answer geojs.Answer
}
// startLookUp starts looking up the client's AS number and country, in the
// lookup database or through GeoJS, unless SWW_WAF_LOOKUP_SOURCE is off or the
// client is on a private, loopback or link-local address, which no lookup can
// place. It returns the channel the result comes on. A result that needs no
// wait is on it when startLookUp returns: the lookup database's, an answer
// GeoJS gave earlier, or none. With GeoJS, while a setting needs the answer,
// such as a country list or a biased threshold, a new client's request waits
// for it up to SWW_WAF_ASN_AND_COUNTRY_LOOKUP_TIMEOUT_MS, unless
// refusedWhateverTheAnswer reports true. The wait goes on to its end even once
// the request has been answered, so that it ends as it would have for a
// request that waited. startLookUp changes nothing of the request. ctx is the
// request's own context.
func (rq *request) startLookUp(
ctx context.Context, now time.Time,
) <-chan lookupResult {
result := make(chan lookupResult, 1)
if rq.h.config.LookupSource == "off" || !canBePlaced(rq.client) {
return
result <- lookupResult{}
return result
}
client := rq.h.clientGroup(rq.client)
switch {
case rq.h.config.LookupSource == "file":
rq.lookupAnswer = rq.h.lookupFile.LookUp(client)
case rq.refusedWhateverTheAnswer(now):
rq.lookupAnswer = rq.h.geojs.LookUpWithoutWaiting(ctx, client)
rq.skippedLookupWait = rq.lookupAnswer.Answered.IsZero()
case !rq.h.waitForGeoJS:
rq.lookupAnswer = rq.h.geojs.LookUpWithoutWaiting(ctx, client)
default:
rq.lookupAnswer = rq.h.geojs.LookUp(ctx, client,
rq.h.config.ASNAndCountryLookupTimeout)
if rq.h.config.LookupSource == "file" {
result <- lookupResult{
lookedUp: true, answer: rq.h.lookupFile.LookUp(client),
}
return result
}
answer := rq.h.geojs.LookUpWithoutWaiting(ctx, client)
refused := rq.refusedWhateverTheAnswer(now)
if !answer.Answered.IsZero() || refused || !rq.h.waitForGeoJS {
result <- lookupResult{
lookedUp: true,
skippedWait: refused && answer.Answered.IsZero(),
answer: answer,
}
return result
}
go func() {
result <- lookupResult{
lookedUp: true,
answer: rq.h.geojs.LookUp(context.WithoutCancel(ctx), client,
rq.h.config.ASNAndCountryLookupTimeout),
}
}()
return result
}
// noteLookup notes result for the log line and as the request's asn= and
// country= tags.
func (rq *request) noteLookup(result lookupResult) {
if !result.lookedUp {
return
}
rq.lookedUp = true
rq.lookupAnswer = result.answer
rq.skippedLookupWait = result.skippedWait
rq.line.ASN = rq.lookupAnswer.ASN
rq.line.ASName = rq.lookupAnswer.ASName
rq.line.Country = rq.lookupAnswer.Country