check / check (push) Waiting to run
refusedWhateverTheAnswer asks each heuristic of afterLookup that implements the new refusesWithoutLookup interface, instead of naming blocklists and rate_limits. The comment on adding a heuristic names the places outside its file that may change with it. Model: opus-5-5
106 lines
3.6 KiB
Go
106 lines
3.6 KiB
Go
package waf
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
)
|
|
|
|
// To add a heuristic, write one file with a struct that implements heuristic
|
|
// and holds the settings and parts it reads. Add one line for it to one of the
|
|
// lists that newHandler builds, which hands it those settings and parts. Its
|
|
// settings go in config.WAF, and the comment at the top of its file names them.
|
|
// A heuristic of afterLookup that can refuse a request whatever the lookup
|
|
// answers also implements refusesWithoutLookup.
|
|
//
|
|
// Three places outside its file may change with it:
|
|
// - waitForGeoJS in newHandler lists the settings that need the lookup's
|
|
// answer;
|
|
// - requestCanGetTag in internal/config/waf.go holds which setting can give a
|
|
// request which tag;
|
|
// - findBesideLookup does the work of each heuristic of besideLookup, by
|
|
// name.
|
|
|
|
// heuristic is one of the checks that decide what waf does with a
|
|
// request.
|
|
type heuristic interface {
|
|
// name is the heuristic's name, which the log line gives.
|
|
name() string
|
|
// check looks at the request at now, and returns nil to let it through.
|
|
// ctx is the request's own context.
|
|
check(ctx context.Context, rq *request, now time.Time) *verdict
|
|
}
|
|
|
|
// refusesWithoutLookup is a heuristic of afterLookup that can tell, before the
|
|
// lookup answers, that it will refuse a request whatever the answer.
|
|
type refusesWithoutLookup interface {
|
|
// refusesWhateverTheLookupAnswers reports whether the heuristic will refuse
|
|
// rq at now, whatever the lookup answers about its client. It changes
|
|
// nothing.
|
|
refusesWhateverTheLookupAnswers(rq *request, now time.Time) bool
|
|
}
|
|
|
|
// verdict is a heuristic refusing a request, or banning its client: the action
|
|
// the log line names, one of requestlog's Action constants, and the figures the
|
|
// heuristic acted on, by name, such as the count and the limit.
|
|
type verdict struct {
|
|
action string
|
|
figures map[string]any
|
|
}
|
|
|
|
// The names of the figures that more than one heuristic gives.
|
|
const (
|
|
figureWindow = "window"
|
|
figureCount = "count"
|
|
figureLimit = "limit"
|
|
figureScore = "score"
|
|
figureNetblock = "netblock"
|
|
)
|
|
|
|
// runHeuristics runs list, one of those checkClient runs, on the request at now,
|
|
// and returns the first verdict, or nil when none gives one. The log line's
|
|
// heuristic and heuristic_figures name the heuristic that gave it, and give its
|
|
// figures. ctx is the request's own context.
|
|
func (rq *request) runHeuristics(
|
|
ctx context.Context, list []heuristic, now time.Time,
|
|
) *verdict {
|
|
name, v := rq.firstVerdict(ctx, list, now)
|
|
if v != nil {
|
|
rq.line.Heuristic = name
|
|
rq.line.HeuristicFigures = v.figures
|
|
}
|
|
|
|
return v
|
|
}
|
|
|
|
// runBanningHeuristics runs list, afterAnswer or onReport, on the request at
|
|
// the time it is called. The log line's after_answer_heuristic and
|
|
// after_answer_heuristic_figures name the heuristic that gave a verdict, and
|
|
// give its figures, so that heuristic and heuristic_figures still name the one
|
|
// that refused the request. ctx is the request's own context.
|
|
func (rq *request) runBanningHeuristics(
|
|
ctx context.Context,
|
|
list []heuristic,
|
|
) {
|
|
name, v := rq.firstVerdict(ctx, list, rq.h.now())
|
|
if v != nil {
|
|
rq.line.AfterAnswerHeuristic = name
|
|
rq.line.AfterAnswerHeuristicFigures = v.figures
|
|
}
|
|
}
|
|
|
|
// firstVerdict runs list on the request at now, in order, until a heuristic
|
|
// gives a verdict, and returns that heuristic's name and its verdict, or "" and
|
|
// nil when none gives one. ctx is the request's own context.
|
|
func (rq *request) firstVerdict(
|
|
ctx context.Context, list []heuristic, now time.Time,
|
|
) (string, *verdict) {
|
|
for _, each := range list {
|
|
v := each.check(ctx, rq, now)
|
|
if v != nil {
|
|
return each.name(), v
|
|
}
|
|
}
|
|
|
|
return "", nil
|
|
}
|