Files
smallwebwaf/internal/waf/countries.go
sneak a46770ed58
check / check (push) Waiting to run
waf: each heuristic holds its own settings and parts (closes #441)
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
2026-11-19 19:41:05 +00:00

44 lines
1.0 KiB
Go

package waf
// The heuristic countries. It reads SWW_WAF_EXCLUSIVELY_ALLOWED_COUNTRIES.
import (
"context"
"slices"
"time"
"sneak.berlin/go/smallwebwaf/internal/config"
"sneak.berlin/go/smallwebwaf/internal/requestlog"
)
// countries refuses a request when SWW_WAF_EXCLUSIVELY_ALLOWED_COUNTRIES is set and
// does not hold the client's country as it was looked up. A client without a
// country, or whose country cannot be found, is refused. Its figures are the
// country and the setting that refused it.
type countries struct {
allowed []string // SWW_WAF_EXCLUSIVELY_ALLOWED_COUNTRIES
}
func (countries) name() string { return "countries" }
func (c countries) check(
_ context.Context,
rq *request,
_ time.Time,
) *verdict {
allowed := c.allowed
country := rq.line.Country
if len(allowed) == 0 || slices.Contains(allowed, country) {
return nil
}
return &verdict{
action: requestlog.ActionCountryDenied,
figures: map[string]any{
"country": country,
"setting": config.ExclusivelyAllowedCountriesSetting,
},
}
}