package waf // The heuristic blocklists. It reads SWW_WAF_BLOCKLIST_ACTION and // SWW_WAF_BAN_RESPONSE, 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 with SWW_WAF_BAN_RESPONSE 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 banResponse int // SWW_WAF_BAN_RESPONSE 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, status: b.banResponse, 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 }