The list that runs tag_deny the second time was named last(). The bare word does not say what it holds or when it runs, a local variable in rulefiles.go shadows it, and its doc read "the heuristics of last run". It is now afterBesideLookup, beside the other lists named for when they run. Model: opus-5-5
173 lines
5.2 KiB
Go
173 lines
5.2 KiB
Go
package waf
|
|
|
|
// The heuristics of besideLookup, and how they run beside the lookup.
|
|
|
|
import (
|
|
"context"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"sneak.berlin/go/smallwebwaf/internal/config"
|
|
"sneak.berlin/go/smallwebwaf/internal/coreruleset"
|
|
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
|
"sneak.berlin/go/smallwebwaf/internal/rules"
|
|
)
|
|
|
|
// besideLookupTimeLimit is how long the heuristics of besideLookup have for a
|
|
// request, from when they start. They only read the request, and no request
|
|
// should take them that long. A request they have not finished by then is
|
|
// refused with 503, as ActionChecksTimedOut.
|
|
const besideLookupTimeLimit = 500 * time.Millisecond
|
|
|
|
// besideLookupFindings is what the heuristics of besideLookup found of a
|
|
// request, before any of it is applied to the request.
|
|
type besideLookupFindings struct {
|
|
// trapPath is the request's path when it is in SWW_WAF_TRAP_PATHS, and ""
|
|
// otherwise.
|
|
trapPath string
|
|
// rules are the rules of the rule files the request matched.
|
|
rules []rules.Rule
|
|
// inspected is true when the Core Rule Set inspected the request.
|
|
// inspection, read and inspectionTime are then what it found, what it read
|
|
// of the body, and how long it took.
|
|
inspected bool
|
|
inspection coreruleset.Result
|
|
read []byte
|
|
inspectionTime time.Duration
|
|
// refuses is true when one of the heuristics refuses the request, or would
|
|
// in observe mode. Those after it did no work.
|
|
refuses bool
|
|
}
|
|
|
|
// runBesideLookup runs the heuristics of besideLookup while the lookup waits
|
|
// for GeoJS, and returns the first verdict, or nil when none gives one.
|
|
// lookedUp is the channel the lookup's result comes on, or nil once afterLookup
|
|
// has let the request through.
|
|
//
|
|
// The first of the two to refuse the request ends the checks: waf answers at
|
|
// once, and what the other found is dropped. besideLookup has
|
|
// besideLookupTimeLimit; past it, the request is refused as
|
|
// ActionChecksTimedOut. Once both have let the request through, the heuristics
|
|
// of afterBesideLookup run. ctx is the request's own context.
|
|
func (rq *request) runBesideLookup(
|
|
ctx context.Context, lookedUp <-chan lookupResult,
|
|
) *verdict {
|
|
var running atomic.Pointer[string]
|
|
|
|
running.Store(new(trapPaths{}.name()))
|
|
|
|
found := make(chan *besideLookupFindings, 1)
|
|
|
|
go func() { found <- rq.findBesideLookup(&running) }()
|
|
|
|
timeUp := time.NewTimer(rq.h.besideLookupLimit)
|
|
defer timeUp.Stop()
|
|
|
|
var findings *besideLookupFindings
|
|
|
|
for findings == nil || lookedUp != nil {
|
|
select {
|
|
case result := <-lookedUp:
|
|
lookedUp = nil
|
|
|
|
v := rq.runAfterLookup(ctx, result)
|
|
if v != nil {
|
|
return v
|
|
}
|
|
case findings = <-found:
|
|
found = nil
|
|
|
|
timeUp.Stop()
|
|
|
|
if findings.refuses {
|
|
return rq.applyFindings(ctx, findings)
|
|
}
|
|
case <-timeUp.C:
|
|
return rq.timedOut(*running.Load())
|
|
}
|
|
}
|
|
|
|
v := rq.applyFindings(ctx, findings)
|
|
if v != nil {
|
|
return v
|
|
}
|
|
|
|
return rq.runHeuristics(ctx, afterBesideLookup(), rq.h.now())
|
|
}
|
|
|
|
// findBesideLookup does the work of the heuristics of besideLookup on the
|
|
// request, in their order, until one refuses it, and returns what they found.
|
|
// It changes nothing of the request or of what waf keeps, so that it can run
|
|
// while the lookup and afterLookup do. running holds the name of the heuristic
|
|
// at work, the first one's when findBesideLookup is called.
|
|
func (rq *request) findBesideLookup(
|
|
running *atomic.Pointer[string],
|
|
) *besideLookupFindings {
|
|
findings := &besideLookupFindings{}
|
|
|
|
findings.trapPath = rq.trapPath()
|
|
if findings.trapPath != "" {
|
|
findings.refuses = true
|
|
|
|
return findings
|
|
}
|
|
|
|
running.Store(new(ruleFiles{}.name()))
|
|
|
|
findings.rules = rq.h.rules.Match(rq.in)
|
|
if refusedByRule(findings.rules) {
|
|
findings.refuses = true
|
|
|
|
return findings
|
|
}
|
|
|
|
running.Store(new(coreRuleSet{}.name()))
|
|
|
|
cfg := rq.h.config
|
|
if cfg.CoreRuleSetMode == config.CoreRuleSetModeOff ||
|
|
pathExempt(rq.in, cfg.CoreRuleSetExemptPaths) {
|
|
return findings
|
|
}
|
|
|
|
start := time.Now()
|
|
// A question's body has been read into memory, so that reading it here
|
|
// cannot fail.
|
|
findings.inspection, findings.read, _ = rq.h.coreRuleSet.Inspect(
|
|
rq.in, rq.client, rq.in.Body,
|
|
)
|
|
findings.inspectionTime = time.Since(start)
|
|
findings.inspected = true
|
|
findings.refuses = rq.refusedByCoreRuleSet(findings.inspection)
|
|
|
|
return findings
|
|
}
|
|
|
|
// applyFindings applies findings, what findBesideLookup found, to the
|
|
// request: it runs the heuristics of besideLookup, which read them, at the
|
|
// time it is called, and returns the first verdict, or nil when none gives
|
|
// one.
|
|
func (rq *request) applyFindings(
|
|
ctx context.Context, findings *besideLookupFindings,
|
|
) *verdict {
|
|
rq.findings = findings
|
|
|
|
return rq.runHeuristics(ctx, besideLookup(), rq.h.now())
|
|
}
|
|
|
|
// timedOut refuses the request for the heuristics of besideLookup not having
|
|
// finished with it within besideLookupTimeLimit. running is the name of the
|
|
// one at work, which the log line's heuristic gives. The refusal makes no ban
|
|
// and is no offence.
|
|
func (rq *request) timedOut(running string) *verdict {
|
|
v := &verdict{
|
|
action: requestlog.ActionChecksTimedOut,
|
|
figures: map[string]any{
|
|
"time_limit_ms": rq.h.besideLookupLimit.Milliseconds(),
|
|
},
|
|
}
|
|
rq.line.Heuristic = running
|
|
rq.line.HeuristicFigures = v.figures
|
|
|
|
return v
|
|
}
|