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) } }