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
61 lines
1.8 KiB
Go
61 lines
1.8 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},
|
|
}
|
|
}
|
|
|
|
// refusesWhateverTheLookupAnswers reports whether blocklists refuses rq, as
|
|
// refusedByBlocklists says. The lookup's answer plays no part in it.
|
|
func (b blocklists) refusesWhateverTheLookupAnswers(
|
|
rq *request,
|
|
_ time.Time,
|
|
) bool {
|
|
return b.refusedByBlocklists(b.lists.ListedBy(rq.client))
|
|
}
|
|
|
|
// 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
|
|
}
|