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
48 lines
1.1 KiB
Go
48 lines
1.1 KiB
Go
package waf
|
|
|
|
import (
|
|
"net/netip"
|
|
"testing"
|
|
"time"
|
|
|
|
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
|
)
|
|
|
|
func TestDenyNetsRefusesOnlyAClientInsideItsNetblocks(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
heuristic := denyNets{
|
|
netblocks: []netip.Prefix{netip.MustParsePrefix("198.51.100.0/24")},
|
|
}
|
|
|
|
// The request has no handler: the heuristic reads only what it holds.
|
|
inside := &request{
|
|
decision: decision{client: netip.MustParseAddr("198.51.100.7")},
|
|
}
|
|
|
|
v := heuristic.check(t.Context(), inside, time.Time{})
|
|
if v == nil {
|
|
t.Fatal("a client inside the netblock was let through")
|
|
}
|
|
|
|
if v.action != requestlog.ActionClientDenied {
|
|
t.Errorf(
|
|
"action = %q, want %q",
|
|
v.action,
|
|
requestlog.ActionClientDenied,
|
|
)
|
|
}
|
|
|
|
if got := v.figures[figureNetblock]; got != "198.51.100.0/24" {
|
|
t.Errorf("netblock figure = %v, want 198.51.100.0/24", got)
|
|
}
|
|
|
|
outside := &request{
|
|
decision: decision{client: netip.MustParseAddr("203.0.113.7")},
|
|
}
|
|
|
|
if v := heuristic.check(t.Context(), outside, time.Time{}); v != nil {
|
|
t.Errorf("a client outside the netblock was refused: %+v", v)
|
|
}
|
|
}
|