waf.Server held its parts as public fields only for the tests to read. The tests now keep the Params the server was built from in a test-only wafAndParts, which embeds both, so every assertion reads as before. newWAFWith takes a change to the Params instead of a build function, so the Params kept are the ones the server was built from. Model: opus-5-5
This commit is contained in:
1 parent
809d2664d7
commit
ac46289c62
16 files changed
+81
-98
No files matched your search
@@ -54,7 +54,7 @@ func TestClientsOnTheAbuseIPDBBlacklistAreBannedForTheBanDays(t *testing.T) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// Two clients in one netblock make one ban, with the higher score.
|
// Two clients in one netblock make one ban, with the higher score.
|
||||||
server.BanAbuseIPDBBlacklist([]reputation.BlacklistEntry{
|
server.banAbuseIPDBBlacklist([]reputation.BlacklistEntry{
|
||||||
{Addr: netip.MustParseAddr(client), Score: 95},
|
{Addr: netip.MustParseAddr(client), Score: 95},
|
||||||
{Addr: netip.MustParseAddr(otherClient), Score: 100},
|
{Addr: netip.MustParseAddr(otherClient), Score: 100},
|
||||||
{Addr: netip.MustParseAddr("2001:db8:5::1"), Score: 90},
|
{Addr: netip.MustParseAddr("2001:db8:5::1"), Score: 90},
|
||||||
@@ -88,7 +88,7 @@ func TestAbuseIPDBBlacklistMakesNoBanInObserveMode(t *testing.T) {
|
|||||||
map[string]string{mode: "observe"},
|
map[string]string{mode: "observe"},
|
||||||
)
|
)
|
||||||
|
|
||||||
server.BanAbuseIPDBBlacklist([]reputation.BlacklistEntry{
|
server.banAbuseIPDBBlacklist([]reputation.BlacklistEntry{
|
||||||
{Addr: netip.MustParseAddr(client), Score: 100},
|
{Addr: netip.MustParseAddr(client), Score: 100},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ import (
|
|||||||
"sneak.berlin/go/smallwebwaf/internal/alerts"
|
"sneak.berlin/go/smallwebwaf/internal/alerts"
|
||||||
"sneak.berlin/go/smallwebwaf/internal/bans"
|
"sneak.berlin/go/smallwebwaf/internal/bans"
|
||||||
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
||||||
"sneak.berlin/go/smallwebwaf/internal/waf"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -252,7 +251,7 @@ func TestObserveModeWorksOutABanOnlyWhenItsAlertWouldBeSent(t *testing.T) {
|
|||||||
// never sent them, and returns the queue they wait in as well.
|
// never sent them, and returns the queue they wait in as well.
|
||||||
func startWithAlerts(
|
func startWithAlerts(
|
||||||
t *testing.T, env map[string]string,
|
t *testing.T, env map[string]string,
|
||||||
) (*sender, *clock, *waf.Server, *alerts.Queue) {
|
) (*sender, *clock, *wafAndParts, *alerts.Queue) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
return startAppWithAlerts(
|
return startAppWithAlerts(
|
||||||
@@ -265,7 +264,7 @@ func startWithAlerts(
|
|||||||
// startAppWithAlerts is startWithAlerts in front of the app handler.
|
// startAppWithAlerts is startWithAlerts in front of the app handler.
|
||||||
func startAppWithAlerts(
|
func startAppWithAlerts(
|
||||||
t *testing.T, handler http.HandlerFunc, env map[string]string,
|
t *testing.T, handler http.HandlerFunc, env map[string]string,
|
||||||
) (*sender, *clock, *waf.Server, *alerts.Queue) {
|
) (*sender, *clock, *wafAndParts, *alerts.Queue) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
app := startApp(t, handler)
|
app := startApp(t, handler)
|
||||||
|
|||||||
@@ -15,7 +15,6 @@ import (
|
|||||||
"sneak.berlin/go/smallwebwaf/internal/anomaly"
|
"sneak.berlin/go/smallwebwaf/internal/anomaly"
|
||||||
"sneak.berlin/go/smallwebwaf/internal/ratelimit"
|
"sneak.berlin/go/smallwebwaf/internal/ratelimit"
|
||||||
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
||||||
"sneak.berlin/go/smallwebwaf/internal/waf"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// The anomaly thresholds: the prefix of a scope followed by the end of a
|
// The anomaly thresholds: the prefix of a scope followed by the end of a
|
||||||
@@ -332,7 +331,7 @@ func TestRequestCountsForTheASNumberGeoJSGivesBeforeItEnds(t *testing.T) {
|
|||||||
// kept.
|
// kept.
|
||||||
geojsURL, _, release := startHeldGeoJS(t)
|
geojsURL, _, release := startHeldGeoJS(t)
|
||||||
|
|
||||||
var server atomic.Pointer[waf.Server]
|
var server atomic.Pointer[wafAndParts]
|
||||||
|
|
||||||
app := startApp(t, func(http.ResponseWriter, *http.Request) {
|
app := startApp(t, func(http.ResponseWriter, *http.Request) {
|
||||||
release()
|
release()
|
||||||
|
|||||||
@@ -411,7 +411,7 @@ func TestTheHeaderLimitsComeBeforeEveryCheckThatRefuses(t *testing.T) {
|
|||||||
env map[string]string
|
env map[string]string
|
||||||
path string
|
path string
|
||||||
// prepare gets the server ready, at now, before the question.
|
// prepare gets the server ready, at now, before the question.
|
||||||
prepare func(t *testing.T, server *waf.Server, now time.Time)
|
prepare func(t *testing.T, server *wafAndParts, now time.Time)
|
||||||
}{
|
}{
|
||||||
{"SWW_WAF_DENY_NETS", map[string]string{denyNets: client}, "/", leaveAsItIs},
|
{"SWW_WAF_DENY_NETS", map[string]string{denyNets: client}, "/", leaveAsItIs},
|
||||||
{"a ban", nil, "/", banClient},
|
{"a ban", nil, "/", banClient},
|
||||||
@@ -426,21 +426,21 @@ func TestTheHeaderLimitsComeBeforeEveryCheckThatRefuses(t *testing.T) {
|
|||||||
"a DNS blocklist", map[string]string{
|
"a DNS blocklist", map[string]string{
|
||||||
dnsblZones: dnsblZone, dnsblResolver: noResolver, dnsblAbuseIPDBAction: actionDeny,
|
dnsblZones: dnsblZone, dnsblResolver: noResolver, dnsblAbuseIPDBAction: actionDeny,
|
||||||
},
|
},
|
||||||
"/", func(_ *testing.T, server *waf.Server, _ time.Time) {
|
"/", func(_ *testing.T, server *wafAndParts, _ time.Time) {
|
||||||
loadVerdicts(server, map[string][]string{client: {dnsblZone}})
|
loadVerdicts(server, map[string][]string{client: {dnsblZone}})
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"AbuseIPDB",
|
"AbuseIPDB",
|
||||||
map[string]string{abuseIPDBKey: accountKey, dnsblAbuseIPDBAction: actionDeny},
|
map[string]string{abuseIPDBKey: accountKey, dnsblAbuseIPDBAction: actionDeny},
|
||||||
"/", func(_ *testing.T, server *waf.Server, _ time.Time) {
|
"/", func(_ *testing.T, server *wafAndParts, _ time.Time) {
|
||||||
// Over SWW_WAF_ABUSEIPDB_MIN_SCORE, 75 by default.
|
// Over SWW_WAF_ABUSEIPDB_MIN_SCORE, 75 by default.
|
||||||
loadScores(server, map[string]int64{client: 80})
|
loadScores(server, map[string]int64{client: 80})
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"SWW_WAF_TAG_DENY", map[string]string{tagLists: "vpn=" + vpnList, tagDeny: "vpn"},
|
"SWW_WAF_TAG_DENY", map[string]string{tagLists: "vpn=" + vpnList, tagDeny: "vpn"},
|
||||||
"/", func(t *testing.T, server *waf.Server, _ time.Time) {
|
"/", func(t *testing.T, server *wafAndParts, _ time.Time) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
loadLists(t, server, map[string][]string{vpnList: {client}})
|
loadLists(t, server, map[string][]string{vpnList: {client}})
|
||||||
},
|
},
|
||||||
@@ -647,7 +647,7 @@ func TestObserveModeAsksGeoJSNothingForARequestOverTheHeaderLimits(
|
|||||||
// for env. No request to it may use the network.
|
// for env. No request to it may use the network.
|
||||||
func newWAFInBubble(
|
func newWAFInBubble(
|
||||||
t *testing.T, env map[string]string,
|
t *testing.T, env map[string]string,
|
||||||
) (*waf.Server, *output) {
|
) (*wafAndParts, *output) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
settings := map[string]string{tagDeny: "country=KP"}
|
settings := map[string]string{tagDeny: "country=KP"}
|
||||||
@@ -661,7 +661,7 @@ func newWAFInBubble(
|
|||||||
// askDirectly hands a question about a request from client straight to server,
|
// askDirectly hands a question about a request from client straight to server,
|
||||||
// as change leaves it, and returns the answer's status and how long it took.
|
// as change leaves it, and returns the answer's status and how long it took.
|
||||||
func askDirectly(
|
func askDirectly(
|
||||||
t *testing.T, server *waf.Server, change func(*http.Request),
|
t *testing.T, server *wafAndParts, change func(*http.Request),
|
||||||
) (int, time.Duration) {
|
) (int, time.Duration) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
@@ -685,7 +685,7 @@ func askDirectly(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// banClient bans client at now.
|
// banClient bans client at now.
|
||||||
func banClient(_ *testing.T, server *waf.Server, now time.Time) {
|
func banClient(_ *testing.T, server *wafAndParts, now time.Time) {
|
||||||
server.Ledger.BanForLimit(
|
server.Ledger.BanForLimit(
|
||||||
netip.MustParsePrefix(client+"/32"),
|
netip.MustParsePrefix(client+"/32"),
|
||||||
now,
|
now,
|
||||||
@@ -694,7 +694,7 @@ func banClient(_ *testing.T, server *waf.Server, now time.Time) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// countClient counts one request from client at now for the rate limits.
|
// countClient counts one request from client at now for the rate limits.
|
||||||
func countClient(_ *testing.T, server *waf.Server, now time.Time) {
|
func countClient(_ *testing.T, server *wafAndParts, now time.Time) {
|
||||||
server.Limiter.Count(
|
server.Limiter.Count(
|
||||||
netip.MustParsePrefix(client+"/32"),
|
netip.MustParsePrefix(client+"/32"),
|
||||||
now,
|
now,
|
||||||
@@ -703,7 +703,7 @@ func countClient(_ *testing.T, server *waf.Server, now time.Time) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// listClient puts client on the blocklist at dropURL.
|
// listClient puts client on the blocklist at dropURL.
|
||||||
func listClient(t *testing.T, server *waf.Server, now time.Time) {
|
func listClient(t *testing.T, server *wafAndParts, now time.Time) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
err := server.Lists.Load([]reputation.List{{
|
err := server.Lists.Load([]reputation.List{{
|
||||||
@@ -715,7 +715,7 @@ func listClient(t *testing.T, server *waf.Server, now time.Time) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// leaveAsItIs prepares nothing.
|
// leaveAsItIs prepares nothing.
|
||||||
func leaveAsItIs(*testing.T, *waf.Server, time.Time) {}
|
func leaveAsItIs(*testing.T, *wafAndParts, time.Time) {}
|
||||||
|
|
||||||
func TestARefusedRequestDoesNotWaitForASlowGeoJS(t *testing.T) {
|
func TestARefusedRequestDoesNotWaitForASlowGeoJS(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
@@ -726,7 +726,7 @@ func TestARefusedRequestDoesNotWaitForASlowGeoJS(t *testing.T) {
|
|||||||
name string
|
name string
|
||||||
env map[string]string
|
env map[string]string
|
||||||
// prepare gets the server ready, at now, before the question.
|
// prepare gets the server ready, at now, before the question.
|
||||||
prepare func(t *testing.T, server *waf.Server, now time.Time)
|
prepare func(t *testing.T, server *wafAndParts, now time.Time)
|
||||||
// cookie is the request's Cookie header.
|
// cookie is the request's Cookie header.
|
||||||
cookie string
|
cookie string
|
||||||
status int
|
status int
|
||||||
@@ -811,7 +811,7 @@ func TestAQuestionWaitsForASlowGeoJSOnlyAsLongAsTheDefaultSays(
|
|||||||
name string
|
name string
|
||||||
env map[string]string
|
env map[string]string
|
||||||
// prepare gets the server ready, at now, before the question.
|
// prepare gets the server ready, at now, before the question.
|
||||||
prepare func(t *testing.T, server *waf.Server, now time.Time)
|
prepare func(t *testing.T, server *wafAndParts, now time.Time)
|
||||||
status int
|
status int
|
||||||
action string
|
action string
|
||||||
}{
|
}{
|
||||||
@@ -877,7 +877,7 @@ func TestWAFAnswersWithinTheTimeLimitWhileEveryOutsideSourceHangs(
|
|||||||
held, _, _ := startHeldGeoJS(t)
|
held, _, _ := startHeldGeoJS(t)
|
||||||
resolver, releaseResolver := startHeldResolver(t)
|
resolver, releaseResolver := startHeldResolver(t)
|
||||||
|
|
||||||
server, out, _ := newWAFWith(t, waf.New,
|
server, out, _ := newWAFWith(t, func(*waf.Params) {},
|
||||||
silentGeoJSURL, silentAbuseIPDBURL, time.Now, map[string]string{
|
silentGeoJSURL, silentAbuseIPDBURL, time.Now, map[string]string{
|
||||||
tagDeny: "country=KP", coreRuleSetMode: block,
|
tagDeny: "country=KP", coreRuleSetMode: block,
|
||||||
dnsblZones: dnsblZone, dnsblResolver: resolver,
|
dnsblZones: dnsblZone, dnsblResolver: resolver,
|
||||||
|
|||||||
@@ -16,7 +16,6 @@ import (
|
|||||||
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/bans"
|
"sneak.berlin/go/smallwebwaf/internal/bans"
|
||||||
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
||||||
"sneak.berlin/go/smallwebwaf/internal/waf"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -466,7 +465,7 @@ func (c *clock) advance(d time.Duration) {
|
|||||||
// window.
|
// window.
|
||||||
func startWithClock(
|
func startWithClock(
|
||||||
t *testing.T, geojsURL string, env map[string]string,
|
t *testing.T, geojsURL string, env map[string]string,
|
||||||
) (*sender, *clock, *waf.Server) {
|
) (*sender, *clock, *wafAndParts) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
app := startApp(t, func(http.ResponseWriter, *http.Request) {})
|
app := startApp(t, func(http.ResponseWriter, *http.Request) {})
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ func coreRuleSetMatch() coreruleset.Result {
|
|||||||
// at its default but for env. No request to it may use the network.
|
// at its default but for env. No request to it may use the network.
|
||||||
func newWAFHoldingTheCoreRuleSet(
|
func newWAFHoldingTheCoreRuleSet(
|
||||||
t *testing.T, crs *heldCoreRuleSet, env map[string]string,
|
t *testing.T, crs *heldCoreRuleSet, env map[string]string,
|
||||||
) (*waf.Server, *output, *alerts.Queue) {
|
) (*wafAndParts, *output, *alerts.Queue) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
settings := map[string]string{
|
settings := map[string]string{
|
||||||
@@ -71,11 +71,9 @@ func newWAFHoldingTheCoreRuleSet(
|
|||||||
}
|
}
|
||||||
maps.Copy(settings, env)
|
maps.Copy(settings, env)
|
||||||
|
|
||||||
server, out, queue := newWAFWith(t, func(params waf.Params) *waf.Server {
|
server, out, queue := newWAFWith(t, func(params *waf.Params) {
|
||||||
params.CoreRuleSet = crs
|
params.CoreRuleSet = crs
|
||||||
params.BesideLookupTimeLimit = waf.BesideLookupTimeLimit
|
params.BesideLookupTimeLimit = waf.BesideLookupTimeLimit
|
||||||
|
|
||||||
return waf.New(params)
|
|
||||||
}, silentGeoJSURL, abuseIPDBURL, time.Now, settings)
|
}, silentGeoJSURL, abuseIPDBURL, time.Now, settings)
|
||||||
|
|
||||||
return server, out, queue
|
return server, out, queue
|
||||||
@@ -105,7 +103,7 @@ func wantAnswer(
|
|||||||
// left no trace: line has none of its fields, and server has counted no match
|
// left no trace: line has none of its fields, and server has counted no match
|
||||||
// of it and raised no alert for one.
|
// of it and raised no alert for one.
|
||||||
func wantNoCoreRuleSetTrace(
|
func wantNoCoreRuleSetTrace(
|
||||||
t *testing.T, server *waf.Server, queue *alerts.Queue, line logLine,
|
t *testing.T, server *wafAndParts, queue *alerts.Queue, line logLine,
|
||||||
) {
|
) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
|
|||||||
@@ -529,7 +529,7 @@ func TestBanForALoweredLimitGivesThePercentageInItsNotesAndItsAlert(
|
|||||||
// clock.
|
// clock.
|
||||||
func startWithLookups(
|
func startWithLookups(
|
||||||
t *testing.T, env map[string]string,
|
t *testing.T, env map[string]string,
|
||||||
) (*sender, *waf.Server, *alerts.Queue) {
|
) (*sender, *wafAndParts, *alerts.Queue) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
s, _, server, queue := startWithLookupsAndClock(t, env)
|
s, _, server, queue := startWithLookupsAndClock(t, env)
|
||||||
@@ -544,7 +544,7 @@ func startWithLookups(
|
|||||||
// country, and no other address.
|
// country, and no other address.
|
||||||
func startWithLookupsAndClock(
|
func startWithLookupsAndClock(
|
||||||
t *testing.T, env map[string]string,
|
t *testing.T, env map[string]string,
|
||||||
) (*sender, *clock, *waf.Server, *alerts.Queue) {
|
) (*sender, *clock, *wafAndParts, *alerts.Queue) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
path := filepath.Join(t.TempDir(), "ipinfo_lite.mmdb")
|
path := filepath.Join(t.TempDir(), "ipinfo_lite.mmdb")
|
||||||
@@ -584,7 +584,7 @@ func (s *sender) uploadFrom(from string) logLine {
|
|||||||
// straight to server's handler, without the network, and returns once it is
|
// straight to server's handler, without the network, and returns once it is
|
||||||
// answered.
|
// answered.
|
||||||
func serveFromDE(
|
func serveFromDE(
|
||||||
t *testing.T, server *waf.Server, method string, body io.Reader,
|
t *testing.T, server *wafAndParts, method string, body io.Reader,
|
||||||
) {
|
) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,4 @@
|
|||||||
package waf
|
package waf
|
||||||
|
|
||||||
import "sneak.berlin/go/smallwebwaf/internal/reputation"
|
|
||||||
|
|
||||||
// BesideLookupTimeLimit is besideLookupTimeLimit.
|
// BesideLookupTimeLimit is besideLookupTimeLimit.
|
||||||
const BesideLookupTimeLimit = besideLookupTimeLimit
|
const BesideLookupTimeLimit = besideLookupTimeLimit
|
||||||
|
|
||||||
// BanAbuseIPDBBlacklist does on s what a fetch of AbuseIPDB's blacklist with
|
|
||||||
// the clients on listed does.
|
|
||||||
func (s *Server) BanAbuseIPDBBlacklist(listed []reputation.BlacklistEntry) {
|
|
||||||
h := s.Handler.(*handler) //nolint:forcetypeassert // New makes it one
|
|
||||||
BanAbuseIPDBBlacklist(h.config, h.ledger, h.processLog, h.now(), listed)
|
|
||||||
}
|
|
||||||
@@ -150,7 +150,7 @@ func TestPathUnderTheOwnPrefixIsNeitherForwardedNorRefused(t *testing.T) {
|
|||||||
|
|
||||||
// historyOf returns the history of the client at addr.
|
// historyOf returns the history of the client at addr.
|
||||||
func historyOf(
|
func historyOf(
|
||||||
t *testing.T, server *waf.Server, addr string,
|
t *testing.T, server *wafAndParts, addr string,
|
||||||
) ratelimit.History {
|
) ratelimit.History {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
|
|||||||
+1
-13
@@ -116,21 +116,9 @@ type CoreRuleSetInspector interface {
|
|||||||
) (coreruleset.Result, []byte, error)
|
) (coreruleset.Result, []byte, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Server is the server waf runs, with the parts of its Params. The tests
|
// Server is the server waf runs.
|
||||||
// read them.
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
*http.Server
|
*http.Server
|
||||||
|
|
||||||
Ledger *bans.Ledger
|
|
||||||
Limiter *ratelimit.Limiter
|
|
||||||
GeoJS *geojs.GeoJS
|
|
||||||
Anomalies *anomaly.Counters
|
|
||||||
LookupFile *lookup.File
|
|
||||||
Lists *reputation.Lists
|
|
||||||
DNSBL *reputation.DNSBL
|
|
||||||
AbuseIPDB *reputation.AbuseIPDB
|
|
||||||
RouteWatch *routewatch.Client
|
|
||||||
Metrics *metrics.Metrics
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// newHandler returns the handler with what every request shares, the parts
|
// newHandler returns the handler with what every request shares, the parts
|
||||||
|
|||||||
+36
-12
@@ -318,7 +318,7 @@ func startProxyWithGeoJS(
|
|||||||
func startProxyWithClock(
|
func startProxyWithClock(
|
||||||
t *testing.T, appURL, geojsURL string, now func() time.Time,
|
t *testing.T, appURL, geojsURL string, now func() time.Time,
|
||||||
env map[string]string,
|
env map[string]string,
|
||||||
) (string, *output, *waf.Server) {
|
) (string, *output, *wafAndParts) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
addr, out, server, _ := startProxyWithAlerts(
|
addr, out, server, _ := startProxyWithAlerts(
|
||||||
@@ -337,7 +337,7 @@ func startProxyWithClock(
|
|||||||
func startProxyWithAlerts(
|
func startProxyWithAlerts(
|
||||||
t *testing.T, appURL, geojsURL string, now func() time.Time,
|
t *testing.T, appURL, geojsURL string, now func() time.Time,
|
||||||
env map[string]string,
|
env map[string]string,
|
||||||
) (string, *output, *waf.Server, *alerts.Queue) {
|
) (string, *output, *wafAndParts, *alerts.Queue) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
server, out, alertQueue := newWAF(t, geojsURL, now, env)
|
server, out, alertQueue := newWAF(t, geojsURL, now, env)
|
||||||
@@ -348,7 +348,7 @@ func startProxyWithAlerts(
|
|||||||
|
|
||||||
// serve serves server on a port of its own until the test ends, and sets its
|
// serve serves server on a port of its own until the test ends, and sets its
|
||||||
// Addr to where it listens.
|
// Addr to where it listens.
|
||||||
func serve(t *testing.T, server *waf.Server) {
|
func serve(t *testing.T, server *wafAndParts) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
listener, err := (&net.ListenConfig{}).Listen(
|
listener, err := (&net.ListenConfig{}).Listen(
|
||||||
@@ -424,8 +424,29 @@ func startRProxy(
|
|||||||
return listener.Addr().String()
|
return listener.Addr().String()
|
||||||
}
|
}
|
||||||
|
|
||||||
// newWAF makes waf's server, without starting it, and
|
// wafAndParts is waf's server with the Params it was built from, so that a
|
||||||
// returns it, what it writes, and the queue of the alerts it raises, as the
|
// test can read the parts the server uses.
|
||||||
|
type wafAndParts struct {
|
||||||
|
*waf.Server
|
||||||
|
waf.Params
|
||||||
|
}
|
||||||
|
|
||||||
|
// banAbuseIPDBBlacklist does on w what a fetch of AbuseIPDB's blacklist with
|
||||||
|
// the clients on listed does.
|
||||||
|
func (w *wafAndParts) banAbuseIPDBBlacklist(
|
||||||
|
listed []reputation.BlacklistEntry,
|
||||||
|
) {
|
||||||
|
waf.BanAbuseIPDBBlacklist(
|
||||||
|
w.Config,
|
||||||
|
w.Ledger,
|
||||||
|
w.ProcessLog,
|
||||||
|
w.Now(),
|
||||||
|
listed,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// newWAF makes waf's server, without starting it, and returns it with its
|
||||||
|
// parts, what it writes, and the queue of the alerts it raises, as the
|
||||||
// settings in env make it. No alert is sent from the queue: they wait in it,
|
// settings in env make it. No alert is sent from the queue: they wait in it,
|
||||||
// for the test to look at.
|
// for the test to look at.
|
||||||
// With no geojsURL, there is no stand-in for GeoJS to look clients up at, and
|
// With no geojsURL, there is no stand-in for GeoJS to look clients up at, and
|
||||||
@@ -440,19 +461,21 @@ func newWAF(
|
|||||||
geojsURL string,
|
geojsURL string,
|
||||||
now func() time.Time,
|
now func() time.Time,
|
||||||
env map[string]string,
|
env map[string]string,
|
||||||
) (*waf.Server, *output, *alerts.Queue) {
|
) (*wafAndParts, *output, *alerts.Queue) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
return newWAFWith(t, waf.New, geojsURL, abuseIPDBURL, now, env)
|
return newWAFWith(
|
||||||
|
t, func(*waf.Params) {}, geojsURL, abuseIPDBURL, now, env,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// newWAFWith is newWAF for the server build makes, with clients checked with
|
// newWAFWith is newWAF with change applied to the Params before the server is
|
||||||
// AbuseIPDB at abuseIPDBCheckURL.
|
// built from them, and clients checked with AbuseIPDB at abuseIPDBCheckURL.
|
||||||
func newWAFWith(
|
func newWAFWith(
|
||||||
t *testing.T, build func(waf.Params) *waf.Server,
|
t *testing.T, change func(*waf.Params),
|
||||||
geojsURL, abuseIPDBCheckURL string, now func() time.Time,
|
geojsURL, abuseIPDBCheckURL string, now func() time.Time,
|
||||||
env map[string]string,
|
env map[string]string,
|
||||||
) (*waf.Server, *output, *alerts.Queue) {
|
) (*wafAndParts, *output, *alerts.Queue) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
settings := map[string]string{
|
settings := map[string]string{
|
||||||
@@ -524,8 +547,9 @@ func newWAFWith(
|
|||||||
}
|
}
|
||||||
addCounters(¶ms)
|
addCounters(¶ms)
|
||||||
addOutsideSources(¶ms, geojsURL, abuseIPDBCheckURL)
|
addOutsideSources(¶ms, geojsURL, abuseIPDBCheckURL)
|
||||||
|
change(¶ms)
|
||||||
|
|
||||||
server := build(params)
|
server := &wafAndParts{Server: waf.New(params), Params: params}
|
||||||
|
|
||||||
return server, out, alertQueue
|
return server, out, alertQueue
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ import (
|
|||||||
"sneak.berlin/go/smallwebwaf/internal/bans"
|
"sneak.berlin/go/smallwebwaf/internal/bans"
|
||||||
"sneak.berlin/go/smallwebwaf/internal/ratelimit"
|
"sneak.berlin/go/smallwebwaf/internal/ratelimit"
|
||||||
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
||||||
"sneak.berlin/go/smallwebwaf/internal/waf"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const refusalsLimitPerMinute = "SWW_WAF_REFUSALS_LIMIT_PER_MINUTE"
|
const refusalsLimitPerMinute = "SWW_WAF_REFUSALS_LIMIT_PER_MINUTE"
|
||||||
@@ -52,7 +51,7 @@ func (r refused) send(s *sender, from string) logLine {
|
|||||||
// SWW_WAF_REFUSALS_LIMIT_PER_MINUTE at limit, and the settings in env.
|
// SWW_WAF_REFUSALS_LIMIT_PER_MINUTE at limit, and the settings in env.
|
||||||
func startForRefusalLimit(
|
func startForRefusalLimit(
|
||||||
t *testing.T, limit string, env map[string]string,
|
t *testing.T, limit string, env map[string]string,
|
||||||
) (*sender, *clock, *waf.Server) {
|
) (*sender, *clock, *wafAndParts) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
settings := map[string]string{
|
settings := map[string]string{
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ import (
|
|||||||
"sneak.berlin/go/smallwebwaf/internal/ratelimit"
|
"sneak.berlin/go/smallwebwaf/internal/ratelimit"
|
||||||
"sneak.berlin/go/smallwebwaf/internal/reputation"
|
"sneak.berlin/go/smallwebwaf/internal/reputation"
|
||||||
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
||||||
"sneak.berlin/go/smallwebwaf/internal/waf"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// The reputation settings.
|
// The reputation settings.
|
||||||
@@ -139,13 +138,13 @@ func TestReputationRefusalNamesItsHeuristicAndWhatListedTheClient(
|
|||||||
for _, tc := range []struct {
|
for _, tc := range []struct {
|
||||||
heuristic string
|
heuristic string
|
||||||
env map[string]string
|
env map[string]string
|
||||||
load func(*testing.T, *waf.Server)
|
load func(*testing.T, *wafAndParts)
|
||||||
figures string
|
figures string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
"blocklists",
|
"blocklists",
|
||||||
map[string]string{blocklistURLs: dropURL, blocklistAction: actionDeny},
|
map[string]string{blocklistURLs: dropURL, blocklistAction: actionDeny},
|
||||||
func(t *testing.T, server *waf.Server) {
|
func(t *testing.T, server *wafAndParts) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
loadLists(t, server, map[string][]string{dropURL: {client}})
|
loadLists(t, server, map[string][]string{dropURL: {client}})
|
||||||
},
|
},
|
||||||
@@ -157,7 +156,7 @@ func TestReputationRefusalNamesItsHeuristicAndWhatListedTheClient(
|
|||||||
dnsblZones: dnsblZone, dnsblResolver: noResolver,
|
dnsblZones: dnsblZone, dnsblResolver: noResolver,
|
||||||
dnsblAbuseIPDBAction: actionDeny,
|
dnsblAbuseIPDBAction: actionDeny,
|
||||||
},
|
},
|
||||||
func(_ *testing.T, server *waf.Server) {
|
func(_ *testing.T, server *wafAndParts) {
|
||||||
loadVerdicts(server, map[string][]string{client: {dnsblZone}})
|
loadVerdicts(server, map[string][]string{client: {dnsblZone}})
|
||||||
},
|
},
|
||||||
`{"zones": ["` + dnsblZone + `"]}`,
|
`{"zones": ["` + dnsblZone + `"]}`,
|
||||||
@@ -165,7 +164,7 @@ func TestReputationRefusalNamesItsHeuristicAndWhatListedTheClient(
|
|||||||
{
|
{
|
||||||
"abuseipdb",
|
"abuseipdb",
|
||||||
map[string]string{abuseIPDBKey: accountKey, dnsblAbuseIPDBAction: actionDeny},
|
map[string]string{abuseIPDBKey: accountKey, dnsblAbuseIPDBAction: actionDeny},
|
||||||
func(_ *testing.T, server *waf.Server) {
|
func(_ *testing.T, server *wafAndParts) {
|
||||||
loadScores(server, map[string]int64{client: 80})
|
loadScores(server, map[string]int64{client: 80})
|
||||||
},
|
},
|
||||||
`{"score": 80, "min_score": 75}`,
|
`{"score": 80, "min_score": 75}`,
|
||||||
@@ -560,7 +559,7 @@ func TestReputationSourcesTagTheRequestWhateverTheirAction(t *testing.T) {
|
|||||||
for _, tc := range []struct {
|
for _, tc := range []struct {
|
||||||
name string
|
name string
|
||||||
env map[string]string
|
env map[string]string
|
||||||
load func(*waf.Server)
|
load func(*wafAndParts)
|
||||||
want []string
|
want []string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
@@ -568,7 +567,7 @@ func TestReputationSourcesTagTheRequestWhateverTheirAction(t *testing.T) {
|
|||||||
map[string]string{
|
map[string]string{
|
||||||
dnsblZones: dnsblZone, dnsblResolver: noResolver, dnsblAbuseIPDBAction: actionLog,
|
dnsblZones: dnsblZone, dnsblResolver: noResolver, dnsblAbuseIPDBAction: actionLog,
|
||||||
},
|
},
|
||||||
func(server *waf.Server) {
|
func(server *wafAndParts) {
|
||||||
loadVerdicts(server, map[string][]string{fromDE: {dnsblZone}})
|
loadVerdicts(server, map[string][]string{fromDE: {dnsblZone}})
|
||||||
},
|
},
|
||||||
[]string{"asn=64496", "country=DE", "dnsbl=" + dnsblZone},
|
[]string{"asn=64496", "country=DE", "dnsbl=" + dnsblZone},
|
||||||
@@ -576,7 +575,7 @@ func TestReputationSourcesTagTheRequestWhateverTheirAction(t *testing.T) {
|
|||||||
{
|
{
|
||||||
abuseipdb,
|
abuseipdb,
|
||||||
map[string]string{abuseIPDBKey: accountKey, dnsblAbuseIPDBAction: actionLog},
|
map[string]string{abuseIPDBKey: accountKey, dnsblAbuseIPDBAction: actionLog},
|
||||||
func(server *waf.Server) {
|
func(server *wafAndParts) {
|
||||||
loadScores(server, map[string]int64{fromDE: 80})
|
loadScores(server, map[string]int64{fromDE: 80})
|
||||||
},
|
},
|
||||||
[]string{abuseipdb, "asn=64496", "country=DE"},
|
[]string{abuseipdb, "asn=64496", "country=DE"},
|
||||||
@@ -1256,7 +1255,7 @@ func verdictsFetched() time.Time {
|
|||||||
// verdictsFetched, as dnsbl.json would at start: one that lists the
|
// verdictsFetched, as dnsbl.json would at start: one that lists the
|
||||||
// client from each zone listedBy gives for it, and one that does not from
|
// client from each zone listedBy gives for it, and one that does not from
|
||||||
// each other zone.
|
// each other zone.
|
||||||
func loadVerdicts(server *waf.Server, listedBy map[string][]string) {
|
func loadVerdicts(server *wafAndParts, listedBy map[string][]string) {
|
||||||
verdicts := make(
|
verdicts := make(
|
||||||
[]reputation.Verdict,
|
[]reputation.Verdict,
|
||||||
0,
|
0,
|
||||||
@@ -1281,7 +1280,7 @@ func loadVerdicts(server *waf.Server, listedBy map[string][]string) {
|
|||||||
// loadScores puts into server's AbuseIPDB the score scores gives each
|
// loadScores puts into server's AbuseIPDB the score scores gives each
|
||||||
// client, an IPv4 address, fetched at verdictsFetched, as
|
// client, an IPv4 address, fetched at verdictsFetched, as
|
||||||
// abuseipdb-scores.json would at start.
|
// abuseipdb-scores.json would at start.
|
||||||
func loadScores(server *waf.Server, scores map[string]int64) {
|
func loadScores(server *wafAndParts, scores map[string]int64) {
|
||||||
kept := make([]reputation.Score, 0, len(scores))
|
kept := make([]reputation.Score, 0, len(scores))
|
||||||
for client, score := range scores {
|
for client, score := range scores {
|
||||||
kept = append(kept, reputation.Score{
|
kept = append(kept, reputation.Score{
|
||||||
@@ -1295,7 +1294,7 @@ func loadScores(server *waf.Server, scores map[string]int64) {
|
|||||||
|
|
||||||
// wantAbuseIPDBChecks checks how many clients server has checked with
|
// wantAbuseIPDBChecks checks how many clients server has checked with
|
||||||
// AbuseIPDB.
|
// AbuseIPDB.
|
||||||
func wantAbuseIPDBChecks(t *testing.T, server *waf.Server, want int) {
|
func wantAbuseIPDBChecks(t *testing.T, server *wafAndParts, want int) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
if got := server.AbuseIPDB.Checked(); got != want {
|
if got := server.AbuseIPDB.Checked(); got != want {
|
||||||
@@ -1307,7 +1306,7 @@ func wantAbuseIPDBChecks(t *testing.T, server *waf.Server, want int) {
|
|||||||
// its lines, fetched at listsFetched, as lists.json would at start.
|
// its lines, fetched at listsFetched, as lists.json would at start.
|
||||||
func loadLists(
|
func loadLists(
|
||||||
t *testing.T,
|
t *testing.T,
|
||||||
server *waf.Server,
|
server *wafAndParts,
|
||||||
copies map[string][]string,
|
copies map[string][]string,
|
||||||
) {
|
) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
||||||
"sneak.berlin/go/smallwebwaf/internal/waf"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// routeWatchURL is SWW_WAF_ROUTEWATCH_URL, and tagsMissing the log line's field
|
// routeWatchURL is SWW_WAF_ROUTEWATCH_URL, and tagsMissing the log line's field
|
||||||
@@ -58,7 +57,7 @@ func startRouteWatch(t *testing.T, routes map[string]standInRoute) string {
|
|||||||
// waitForRouteWatch waits until routewatch's answer about from, in the client
|
// waitForRouteWatch waits until routewatch's answer about from, in the client
|
||||||
// group of the given length, is kept, so that the next request gets its tags.
|
// group of the given length, is kept, so that the next request gets its tags.
|
||||||
func waitForRouteWatch(
|
func waitForRouteWatch(
|
||||||
t *testing.T, server *waf.Server, from string, bits int,
|
t *testing.T, server *wafAndParts, from string, bits int,
|
||||||
) {
|
) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
|
|||||||
+1
-13
@@ -104,12 +104,10 @@ type Report struct {
|
|||||||
// is far above rproxy's, and a question refused for its size would have rproxy
|
// is far above rproxy's, and a question refused for its size would have rproxy
|
||||||
// let the request through.
|
// let the request through.
|
||||||
func New(params Params) *Server {
|
func New(params Params) *Server {
|
||||||
h := newHandler(params)
|
|
||||||
|
|
||||||
return &Server{
|
return &Server{
|
||||||
Server: &http.Server{
|
Server: &http.Server{
|
||||||
Addr: params.Config.ListenAddr,
|
Addr: params.Config.ListenAddr,
|
||||||
Handler: h,
|
Handler: newHandler(params),
|
||||||
ErrorLog: slog.NewLogLogger(
|
ErrorLog: slog.NewLogLogger(
|
||||||
params.ProcessLog.Handler(),
|
params.ProcessLog.Handler(),
|
||||||
slog.LevelWarn,
|
slog.LevelWarn,
|
||||||
@@ -118,16 +116,6 @@ func New(params Params) *Server {
|
|||||||
WriteTimeout: wafTimeout,
|
WriteTimeout: wafTimeout,
|
||||||
IdleTimeout: wafIdleTimeout,
|
IdleTimeout: wafIdleTimeout,
|
||||||
},
|
},
|
||||||
Ledger: h.ledger,
|
|
||||||
Limiter: h.limiter,
|
|
||||||
GeoJS: h.geojs,
|
|
||||||
Anomalies: h.anomalies,
|
|
||||||
LookupFile: h.lookupFile,
|
|
||||||
Lists: h.lists,
|
|
||||||
DNSBL: h.dnsbl,
|
|
||||||
AbuseIPDB: h.abuseIPDB,
|
|
||||||
RouteWatch: h.routeWatch,
|
|
||||||
Metrics: h.metrics,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ type WAF struct {
|
|||||||
t *testing.T
|
t *testing.T
|
||||||
addr string
|
addr string
|
||||||
out *output
|
out *output
|
||||||
server *waf.Server
|
server *wafAndParts
|
||||||
clock *clock
|
clock *clock
|
||||||
queue *alerts.Queue
|
queue *alerts.Queue
|
||||||
}
|
}
|
||||||
@@ -69,14 +69,14 @@ func startWAF(
|
|||||||
) *WAF {
|
) *WAF {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
return startWAFWith(t, geojsURL, env, func(*waf.Server) {})
|
return startWAFWith(t, geojsURL, env, func(*wafAndParts) {})
|
||||||
}
|
}
|
||||||
|
|
||||||
// startWAFWith is startWAF with change applied to the server before
|
// startWAFWith is startWAF with change applied to the server before
|
||||||
// it starts.
|
// it starts.
|
||||||
func startWAFWith(
|
func startWAFWith(
|
||||||
t *testing.T, geojsURL string, env map[string]string,
|
t *testing.T, geojsURL string, env map[string]string,
|
||||||
change func(*waf.Server),
|
change func(*wafAndParts),
|
||||||
) *WAF {
|
) *WAF {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
@@ -663,7 +663,7 @@ func TestQuestionWhoseBodyStopsArrivingGetsNoAnswer(t *testing.T) {
|
|||||||
t,
|
t,
|
||||||
"",
|
"",
|
||||||
map[string]string{metricsToken: token},
|
map[string]string{metricsToken: token},
|
||||||
func(server *waf.Server) { server.ReadTimeout = readTimeout },
|
func(server *wafAndParts) { server.ReadTimeout = readTimeout },
|
||||||
)
|
)
|
||||||
|
|
||||||
conn := dial(t, s.addr)
|
conn := dial(t, s.addr)
|
||||||
|
|||||||
Reference in new issue
Block a user