check / check (push) Waiting to run
Every heuristic is a struct built once in newHandler with the settings and collaborators it reads; no check reads the handler. The six lists are handler fields built there. Code several heuristics share takes what it needs as arguments or lives on a struct they hold: banMaker for bans and their alerts, reputationHitNoter for reputation hits, biasedThresholds for limit percentages. The handler keeps blocklists, rateLimits, trapPaths, ruleFiles and coreRuleSet as fields too, since refusedWhateverTheAnswer and findBesideLookup use them. Model: opus-5-5
52 lines
1.5 KiB
Go
52 lines
1.5 KiB
Go
package waf
|
|
|
|
// The heuristic blocklists. It reads SWW_WAF_BLOCKLIST_ACTION, and the blocklists
|
|
// through the reputation lists.
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"sneak.berlin/go/smallwebwaf/internal/config"
|
|
"sneak.berlin/go/smallwebwaf/internal/reputation"
|
|
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
|
)
|
|
|
|
// blocklists notes the blocklists that list the client, as noteListed does,
|
|
// and refuses the request when SWW_WAF_BLOCKLIST_ACTION is deny. Being limit, it
|
|
// lowers the client's limits instead (see limitPercentages), and being log, it
|
|
// does nothing more. Its figure is the blocklists that list the client.
|
|
type blocklists struct {
|
|
lists *reputation.Lists
|
|
action config.ReputationAction // SWW_WAF_BLOCKLIST_ACTION
|
|
hitNoter reputationHitNoter
|
|
}
|
|
|
|
func (blocklists) name() string { return "blocklists" }
|
|
|
|
func (b blocklists) check(
|
|
_ context.Context,
|
|
rq *request,
|
|
_ time.Time,
|
|
) *verdict {
|
|
listedBy := b.lists.ListedBy(rq.client)
|
|
rq.blocklisted = len(listedBy) > 0
|
|
b.hitNoter.noteListed(rq, listedBy, "listed by a blocklist")
|
|
|
|
if !b.refusedByBlocklists(listedBy) {
|
|
return nil
|
|
}
|
|
|
|
return &verdict{
|
|
action: requestlog.ActionClientDenied,
|
|
figures: map[string]any{"lists": listedBy},
|
|
}
|
|
}
|
|
|
|
// refusedByBlocklists reports whether the heuristic blocklists refuses a
|
|
// client, given listedBy, the blocklists that list it: it does when one lists
|
|
// it and SWW_WAF_BLOCKLIST_ACTION is deny.
|
|
func (b blocklists) refusedByBlocklists(listedBy []string) bool {
|
|
return len(listedBy) > 0 && b.action == config.ReputationActionDeny
|
|
}
|