Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ad7ea4608b |
No files matched your search
@@ -1,6 +1,14 @@
|
|||||||
package rproxy
|
package rproxy
|
||||||
|
|
||||||
import "time"
|
import (
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// HTTPServer is Go's server s holds, for tests that read its settings.
|
||||||
|
func (s *Server) HTTPServer() *http.Server {
|
||||||
|
return s.httpServer
|
||||||
|
}
|
||||||
|
|
||||||
// StopBreakerClock has the circuit breaker of s read at as the time, so that how
|
// StopBreakerClock has the circuit breaker of s read at as the time, so that how
|
||||||
// long requests take cannot matter to it. It is called before s serves.
|
// long requests take cannot matter to it. It is called before s serves.
|
||||||
|
|||||||
@@ -56,7 +56,7 @@ func TestServerHasTheFixedLimits(t *testing.T) {
|
|||||||
InstanceName: "test",
|
InstanceName: "test",
|
||||||
Level: slog.LevelInfo,
|
Level: slog.LevelInfo,
|
||||||
}),
|
}),
|
||||||
})
|
}).HTTPServer()
|
||||||
|
|
||||||
if server.Addr != ":8080" || server.MaxHeaderBytes != 28<<10 ||
|
if server.Addr != ":8080" || server.MaxHeaderBytes != 28<<10 ||
|
||||||
server.IdleTimeout != 2*time.Minute || server.ReadHeaderTimeout != time.Minute {
|
server.IdleTimeout != 2*time.Minute || server.ReadHeaderTimeout != time.Minute {
|
||||||
@@ -709,7 +709,7 @@ func TestABodyLeftUnreadIsReadOnlyUntilTheRequestTimeLimit(t *testing.T) {
|
|||||||
|
|
||||||
before := time.Now()
|
before := time.Now()
|
||||||
|
|
||||||
server.Handler.ServeHTTP(w, req)
|
server.ServeHTTP(w, req)
|
||||||
|
|
||||||
if w.Code != http.StatusNoContent {
|
if w.Code != http.StatusNoContent {
|
||||||
t.Errorf("status %d, want the app's 204", w.Code)
|
t.Errorf("status %d, want the app's 204", w.Code)
|
||||||
@@ -762,7 +762,7 @@ func TestABodyLeftUnreadIsReadNoMoreOnceTheTimeToSendTheAnswerRunsOut(
|
|||||||
|
|
||||||
before := time.Now()
|
before := time.Now()
|
||||||
|
|
||||||
server.Handler.ServeHTTP(w, req)
|
server.ServeHTTP(w, req)
|
||||||
|
|
||||||
if w.Code != http.StatusOK {
|
if w.Code != http.StatusOK {
|
||||||
t.Errorf("status %d, want the app's 200", w.Code)
|
t.Errorf("status %d, want the app's 200", w.Code)
|
||||||
@@ -815,7 +815,7 @@ func TestAnAnswerRProxyGivesItselfHasTheResponseTimeToBeSent(t *testing.T) {
|
|||||||
|
|
||||||
before := time.Now()
|
before := time.Now()
|
||||||
|
|
||||||
server.Handler.ServeHTTP(w, req)
|
server.ServeHTTP(w, req)
|
||||||
|
|
||||||
if w.Code != tc.status {
|
if w.Code != tc.status {
|
||||||
t.Errorf("status %d, want %d", w.Code, tc.status)
|
t.Errorf("status %d, want %d", w.Code, tc.status)
|
||||||
@@ -856,7 +856,7 @@ func TestAnAnswerRProxyGivesItselfHasTenSecondsToBeSentAtLeast(
|
|||||||
|
|
||||||
before := time.Now()
|
before := time.Now()
|
||||||
|
|
||||||
server.Handler.ServeHTTP(w, req)
|
server.ServeHTTP(w, req)
|
||||||
|
|
||||||
if w.writeDeadline.Before(before.Add(least)) {
|
if w.writeDeadline.Before(before.Add(least)) {
|
||||||
t.Errorf(
|
t.Errorf(
|
||||||
|
|||||||
@@ -583,7 +583,7 @@ func TestAQuestionNamesNoZone(t *testing.T) {
|
|||||||
req.RemoteAddr = tc.remoteAddr
|
req.RemoteAddr = tc.remoteAddr
|
||||||
req.Header.Set(forwardedForHeader, tc.forwardedFor)
|
req.Header.Set(forwardedForHeader, tc.forwardedFor)
|
||||||
|
|
||||||
server.Handler.ServeHTTP(httptest.NewRecorder(), req)
|
server.ServeHTTP(httptest.NewRecorder(), req)
|
||||||
|
|
||||||
asked := standIn.question(t)
|
asked := standIn.question(t)
|
||||||
if asked.header.Get(wafprotocol.ClientHeader) != tc.client ||
|
if asked.header.Get(wafprotocol.ClientHeader) != tc.client ||
|
||||||
|
|||||||
+36
-13
@@ -16,6 +16,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"slices"
|
"slices"
|
||||||
@@ -63,9 +64,8 @@ type Params struct {
|
|||||||
|
|
||||||
// Server is the server `sww rproxy` runs.
|
// Server is the server `sww rproxy` runs.
|
||||||
type Server struct {
|
type Server struct {
|
||||||
*http.Server
|
httpServer *http.Server
|
||||||
|
h *handler
|
||||||
h *handler
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// New returns the server `sww rproxy` runs. Go's server itself refuses
|
// New returns the server `sww rproxy` runs. Go's server itself refuses
|
||||||
@@ -94,17 +94,40 @@ func New(params Params) *Server {
|
|||||||
breakerClock: time.Now,
|
breakerClock: time.Now,
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Server{
|
s := &Server{h: h}
|
||||||
Server: &http.Server{
|
s.httpServer = &http.Server{
|
||||||
Addr: params.Config.ListenAddr,
|
Addr: params.Config.ListenAddr,
|
||||||
Handler: h,
|
Handler: s,
|
||||||
ErrorLog: errorLog,
|
ErrorLog: errorLog,
|
||||||
ReadHeaderTimeout: params.Config.MaxRequestTime,
|
ReadHeaderTimeout: params.Config.MaxRequestTime,
|
||||||
IdleTimeout: idleTimeout,
|
IdleTimeout: idleTimeout,
|
||||||
MaxHeaderBytes: requestHeaderMaxBytes - goServerExtraHeaderBytes,
|
MaxHeaderBytes: requestHeaderMaxBytes - goServerExtraHeaderBytes,
|
||||||
},
|
|
||||||
h: h,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
// Serve serves requests on listener until Shutdown or Close, as Go's
|
||||||
|
// http.Server.Serve does.
|
||||||
|
func (s *Server) Serve(listener net.Listener) error {
|
||||||
|
return s.httpServer.Serve(listener)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Shutdown stops serving once the requests in progress have finished, or
|
||||||
|
// when ctx is done, as Go's http.Server.Shutdown does.
|
||||||
|
func (s *Server) Shutdown(ctx context.Context) error {
|
||||||
|
return s.httpServer.Shutdown(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close stops serving at once and closes every connection, as Go's
|
||||||
|
// http.Server.Close does.
|
||||||
|
func (s *Server) Close() error {
|
||||||
|
return s.httpServer.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
// ServeHTTP handles one request from a visitor, as handler.ServeHTTP says.
|
||||||
|
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
s.h.ServeHTTP(w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
// WaitForReports returns once the reports being sent have gone or failed,
|
// WaitForReports returns once the reports being sent have gone or failed,
|
||||||
|
|||||||
@@ -710,7 +710,7 @@ func TestAnAnswerWithoutAContentTypeGetsNone(t *testing.T) {
|
|||||||
)
|
)
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
server.Handler.ServeHTTP(w, req)
|
server.ServeHTTP(w, req)
|
||||||
|
|
||||||
// Go's server guesses a type when the first part of the body goes before the
|
// Go's server guesses a type when the first part of the body goes before the
|
||||||
// headers are flushed, which is a matter of timing here. So this looks for what
|
// headers are flushed, which is a matter of timing here. So this looks for what
|
||||||
|
|||||||
@@ -95,12 +95,20 @@ func background(loop func(context.Context)) fx.Hook {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// daemonServer is the server a daemon runs, waf.Server or rproxy.Server: the
|
||||||
|
// methods of Go's http.Server that serve starts and stops it with.
|
||||||
|
type daemonServer interface {
|
||||||
|
Serve(listener net.Listener) error
|
||||||
|
Shutdown(ctx context.Context) error
|
||||||
|
Close() error
|
||||||
|
}
|
||||||
|
|
||||||
// serve serves requests on listener from the start. At the stop it gives the
|
// serve serves requests on listener from the start. At the stop it gives the
|
||||||
// requests in progress shutdownTimeout to finish, then closes their
|
// requests in progress shutdownTimeout to finish, then closes their
|
||||||
// connections. When serving fails, it logs why and sends the error to
|
// connections. When serving fails, it logs why and sends the error to
|
||||||
// failed.
|
// failed.
|
||||||
func serve(
|
func serve(
|
||||||
lc fx.Lifecycle, server *http.Server, listener net.Listener,
|
lc fx.Lifecycle, server daemonServer, listener net.Listener,
|
||||||
processLog *slog.Logger, failed servingFailed,
|
processLog *slog.Logger, failed servingFailed,
|
||||||
) {
|
) {
|
||||||
served := make(chan error, 1)
|
served := make(chan error, 1)
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"go.uber.org/fx"
|
"go.uber.org/fx"
|
||||||
@@ -44,8 +43,13 @@ func RunRProxy(ctx context.Context, params Params) int {
|
|||||||
app := fx.New(
|
app := fx.New(
|
||||||
fx.Supply(cfg, processLog, requestLog{params.Stdout}, failed),
|
fx.Supply(cfg, processLog, requestLog{params.Stdout}, failed),
|
||||||
fx.Provide(
|
fx.Provide(
|
||||||
newRProxyServer,
|
// run waits for rproxy.Server's reports, and serve takes it as a
|
||||||
func(server *rproxy.Server) *http.Server { return server.Server },
|
// daemonServer.
|
||||||
|
fx.Annotate(
|
||||||
|
newRProxyServer,
|
||||||
|
fx.As(fx.Self()),
|
||||||
|
fx.As(new(daemonServer)),
|
||||||
|
),
|
||||||
newRProxyListener,
|
newRProxyListener,
|
||||||
),
|
),
|
||||||
fx.Invoke(serve),
|
fx.Invoke(serve),
|
||||||
|
|||||||
+2
-3
@@ -6,7 +6,6 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"go.uber.org/fx"
|
"go.uber.org/fx"
|
||||||
@@ -97,8 +96,8 @@ func RunWAF(ctx context.Context, params Params) int {
|
|||||||
newAlertQueue, newRuleFiles, newCoreRuleSet, newLookupFile,
|
newAlertQueue, newRuleFiles, newCoreRuleSet, newLookupFile,
|
||||||
newLimiter, newLedger, newAnomalyCounters,
|
newLimiter, newLedger, newAnomalyCounters,
|
||||||
newLists, newDNSBL, newAbuseIPDB, newRouteWatch,
|
newLists, newDNSBL, newAbuseIPDB, newRouteWatch,
|
||||||
newMetrics, newGeoJS, newWAFServer,
|
newMetrics, newGeoJS,
|
||||||
func(server *waf.Server) *http.Server { return server.Server },
|
fx.Annotate(newWAFServer, fx.As(new(daemonServer))),
|
||||||
newWAFListener, newStateFiles,
|
newWAFListener, newStateFiles,
|
||||||
),
|
),
|
||||||
// fx stops hooks in the reverse of the order they were added in. The
|
// fx stops hooks in the reverse of the order they were added in. The
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
package waf
|
package waf
|
||||||
|
|
||||||
// The heuristic abuseipdb. It reads SWW_WAF_ABUSEIPDB_KEY,
|
// The heuristic abuseipdb. It reads SWW_WAF_ABUSEIPDB_KEY,
|
||||||
// SWW_WAF_ABUSEIPDB_MIN_SCORE, SWW_WAF_DNSBL_ABUSEIPDB_ACTION and
|
// SWW_WAF_ABUSEIPDB_MIN_SCORE and SWW_WAF_DNSBL_ABUSEIPDB_ACTION.
|
||||||
// SWW_WAF_BAN_RESPONSE.
|
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -17,11 +16,10 @@ import (
|
|||||||
|
|
||||||
// abuseIPDB adds the tag abuseipdb and notes AbuseIPDB, as noteHit does, with
|
// abuseIPDB adds the tag abuseipdb and notes AbuseIPDB, as noteHit does, with
|
||||||
// the score, when its score of the client is a hit, and refuses the request
|
// the score, when its score of the client is a hit, and refuses the request
|
||||||
// with SWW_WAF_BAN_RESPONSE when SWW_WAF_DNSBL_ABUSEIPDB_ACTION is deny, as
|
// when SWW_WAF_DNSBL_ABUSEIPDB_ACTION is deny, as dnsbl does for a zone. While
|
||||||
// dnsbl does for a zone. While SWW_WAF_ABUSEIPDB_KEY is unset it does nothing.
|
// SWW_WAF_ABUSEIPDB_KEY is unset it does nothing. A client without a score is
|
||||||
// A client without a score is checked in the background, by the request's
|
// checked in the background, by the request's address, if its history counts
|
||||||
// address, if its history counts an offence, and the request does not wait for
|
// an offence, and the request does not wait for the answer. The score is then
|
||||||
// the answer. The score is then
|
|
||||||
// used for each address of the client. Its figures are the score and
|
// used for each address of the client. Its figures are the score and
|
||||||
// SWW_WAF_ABUSEIPDB_MIN_SCORE.
|
// SWW_WAF_ABUSEIPDB_MIN_SCORE.
|
||||||
type abuseIPDB struct {
|
type abuseIPDB struct {
|
||||||
@@ -32,7 +30,6 @@ type abuseIPDB struct {
|
|||||||
keySet bool // SWW_WAF_ABUSEIPDB_KEY is set
|
keySet bool // SWW_WAF_ABUSEIPDB_KEY is set
|
||||||
minScore int64 // SWW_WAF_ABUSEIPDB_MIN_SCORE
|
minScore int64 // SWW_WAF_ABUSEIPDB_MIN_SCORE
|
||||||
action config.ReputationAction // SWW_WAF_DNSBL_ABUSEIPDB_ACTION
|
action config.ReputationAction // SWW_WAF_DNSBL_ABUSEIPDB_ACTION
|
||||||
banResponse int // SWW_WAF_BAN_RESPONSE
|
|
||||||
ipv6GroupPrefix int // SWW_WAF_IPV6_GROUP_PREFIX
|
ipv6GroupPrefix int // SWW_WAF_IPV6_GROUP_PREFIX
|
||||||
hitNoter reputationHitNoter
|
hitNoter reputationHitNoter
|
||||||
}
|
}
|
||||||
@@ -74,7 +71,6 @@ func (a abuseIPDB) check(
|
|||||||
|
|
||||||
return &verdict{
|
return &verdict{
|
||||||
action: requestlog.ActionClientDenied,
|
action: requestlog.ActionClientDenied,
|
||||||
status: a.banResponse,
|
|
||||||
figures: map[string]any{
|
figures: map[string]any{
|
||||||
figureScore: score, "min_score": a.minScore,
|
figureScore: score, "min_score": a.minScore,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -717,7 +717,7 @@ func askDirectly(
|
|||||||
recorder := httptest.NewRecorder()
|
recorder := httptest.NewRecorder()
|
||||||
began := time.Now()
|
began := time.Now()
|
||||||
|
|
||||||
server.Handler.ServeHTTP(recorder, req)
|
server.ServeHTTP(recorder, req)
|
||||||
|
|
||||||
return recorder.Code, time.Since(began)
|
return recorder.Code, time.Since(began)
|
||||||
}
|
}
|
||||||
|
|||||||
+13
-11
@@ -1,8 +1,7 @@
|
|||||||
package waf
|
package waf
|
||||||
|
|
||||||
// The heuristic active_ban, and banMaker, which makes the bans of several
|
// The heuristic active_ban, and banMaker, which makes the bans of several
|
||||||
// heuristics. active_ban reads SWW_WAF_MODE and SWW_WAF_BAN_RESPONSE; the bans
|
// heuristics. active_ban reads SWW_WAF_MODE; the bans are the ban ledger's.
|
||||||
// are the ban ledger's.
|
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -17,11 +16,16 @@ import (
|
|||||||
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
||||||
)
|
)
|
||||||
|
|
||||||
// activeBan refuses a client a ban covers, with SWW_WAF_BAN_RESPONSE: a ban on
|
// banResponse is a refusal answered with SWW_WAF_BAN_RESPONSE, and logged
|
||||||
// a netblock the client is in, in force at now. It notes for the log line when
|
// with action.
|
||||||
// that ban ends. A request that makes the ban permanent, or in observe mode
|
func (rq *request) banResponse(action requestlog.Action) *refusal {
|
||||||
// would have, raises the alert for it. Its figures are the ban's netblock and
|
return &refusal{status: rq.h.config.BanResponse, action: action}
|
||||||
// when it ends.
|
}
|
||||||
|
|
||||||
|
// activeBan refuses a client a ban covers: a ban on a netblock the client is
|
||||||
|
// in, in force at now. It notes for the log line when that ban ends. A request
|
||||||
|
// that makes the ban permanent, or in observe mode would have, raises the alert
|
||||||
|
// for it. Its figures are the ban's netblock and when it ends.
|
||||||
type activeBan struct {
|
type activeBan struct {
|
||||||
banMaker banMaker
|
banMaker banMaker
|
||||||
}
|
}
|
||||||
@@ -59,7 +63,6 @@ func (a activeBan) check(
|
|||||||
|
|
||||||
return &verdict{
|
return &verdict{
|
||||||
action: banAction(ban.Permanent()),
|
action: banAction(ban.Permanent()),
|
||||||
status: a.banMaker.banResponse,
|
|
||||||
figures: map[string]any{
|
figures: map[string]any{
|
||||||
figureNetblock: ban.Netblock.String(), "ban_expires": expires,
|
figureNetblock: ban.Netblock.String(), "ban_expires": expires,
|
||||||
},
|
},
|
||||||
@@ -115,9 +118,8 @@ type banMaker struct {
|
|||||||
alerts *alerts.Queue
|
alerts *alerts.Queue
|
||||||
// observe is true in observe mode (SWW_WAF_MODE).
|
// observe is true in observe mode (SWW_WAF_MODE).
|
||||||
observe bool
|
observe bool
|
||||||
// banResponse is SWW_WAF_BAN_RESPONSE: the status of a request refused
|
// banResponse is SWW_WAF_BAN_RESPONSE, the status the notes of a ban for a
|
||||||
// under a ban or for a clear sign of attack, which the notes of a ban for
|
// clear sign of attack give.
|
||||||
// a clear sign of attack give.
|
|
||||||
banResponse int
|
banResponse int
|
||||||
// banScopeV4Prefix is SWW_WAF_BAN_SCOPE_V4_PREFIX and ipv6GroupPrefix
|
// banScopeV4Prefix is SWW_WAF_BAN_SCOPE_V4_PREFIX and ipv6GroupPrefix
|
||||||
// SWW_WAF_IPV6_GROUP_PREFIX, which set the netblock a ban covers.
|
// SWW_WAF_IPV6_GROUP_PREFIX, which set the netblock a ban covers.
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ package waf
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net/http"
|
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -162,7 +161,6 @@ func (rq *request) applyFindings(
|
|||||||
func (rq *request) timedOut(running string) *verdict {
|
func (rq *request) timedOut(running string) *verdict {
|
||||||
v := &verdict{
|
v := &verdict{
|
||||||
action: requestlog.ActionChecksTimedOut,
|
action: requestlog.ActionChecksTimedOut,
|
||||||
status: http.StatusServiceUnavailable,
|
|
||||||
figures: map[string]any{
|
figures: map[string]any{
|
||||||
"time_limit_ms": rq.h.besideLookupLimit.Milliseconds(),
|
"time_limit_ms": rq.h.besideLookupLimit.Milliseconds(),
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -592,7 +592,7 @@ func serveFromDE(
|
|||||||
req.Header.Set(wafprotocol.ClientHeader, fromDE)
|
req.Header.Set(wafprotocol.ClientHeader, fromDE)
|
||||||
req.Header.Set(requestIDHeader, "the-id")
|
req.Header.Set(requestIDHeader, "the-id")
|
||||||
|
|
||||||
server.Handler.ServeHTTP(httptest.NewRecorder(), req)
|
server.ServeHTTP(httptest.NewRecorder(), req)
|
||||||
}
|
}
|
||||||
|
|
||||||
// wantPercent checks a limit percentage that a log line or a ban's notes
|
// wantPercent checks a limit percentage that a log line or a ban's notes
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
package waf
|
package waf
|
||||||
|
|
||||||
// The heuristic blocklists. It reads SWW_WAF_BLOCKLIST_ACTION and
|
// The heuristic blocklists. It reads SWW_WAF_BLOCKLIST_ACTION, and the blocklists
|
||||||
// SWW_WAF_BAN_RESPONSE, and the blocklists through the reputation lists.
|
// through the reputation lists.
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -13,15 +13,13 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// blocklists notes the blocklists that list the client, as noteListed does,
|
// blocklists notes the blocklists that list the client, as noteListed does,
|
||||||
// and refuses the request with SWW_WAF_BAN_RESPONSE when
|
// and refuses the request when SWW_WAF_BLOCKLIST_ACTION is deny. Being limit, it
|
||||||
// SWW_WAF_BLOCKLIST_ACTION is deny. Being limit, it lowers the client's limits
|
// lowers the client's limits instead (see limitPercentages), and being log, it
|
||||||
// instead (see limitPercentages), and being log, it does nothing more. Its
|
// does nothing more. Its figure is the blocklists that list the client.
|
||||||
// figure is the blocklists that list the client.
|
|
||||||
type blocklists struct {
|
type blocklists struct {
|
||||||
lists *reputation.Lists
|
lists *reputation.Lists
|
||||||
action config.ReputationAction // SWW_WAF_BLOCKLIST_ACTION
|
action config.ReputationAction // SWW_WAF_BLOCKLIST_ACTION
|
||||||
banResponse int // SWW_WAF_BAN_RESPONSE
|
hitNoter reputationHitNoter
|
||||||
hitNoter reputationHitNoter
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (blocklists) name() string { return "blocklists" }
|
func (blocklists) name() string { return "blocklists" }
|
||||||
@@ -41,7 +39,6 @@ func (b blocklists) check(
|
|||||||
|
|
||||||
return &verdict{
|
return &verdict{
|
||||||
action: requestlog.ActionClientDenied,
|
action: requestlog.ActionClientDenied,
|
||||||
status: b.banResponse,
|
|
||||||
figures: map[string]any{"lists": listedBy},
|
figures: map[string]any{"lists": listedBy},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ package waf
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net/http"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/alerts"
|
"sneak.berlin/go/smallwebwaf/internal/alerts"
|
||||||
@@ -78,7 +77,6 @@ func (c coreRuleSet) check(
|
|||||||
|
|
||||||
return &verdict{
|
return &verdict{
|
||||||
action: requestlog.ActionBlockedByCoreRuleSet,
|
action: requestlog.ActionBlockedByCoreRuleSet,
|
||||||
status: http.StatusForbidden,
|
|
||||||
figures: map[string]any{
|
figures: map[string]any{
|
||||||
figureScore: result.Score,
|
figureScore: result.Score,
|
||||||
"threshold": threshold,
|
"threshold": threshold,
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package waf
|
package waf
|
||||||
|
|
||||||
// The heuristic countries. It reads SWW_WAF_EXCLUSIVELY_ALLOWED_COUNTRIES and
|
// The heuristic countries. It reads SWW_WAF_EXCLUSIVELY_ALLOWED_COUNTRIES.
|
||||||
// SWW_WAF_BAN_RESPONSE.
|
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -14,12 +13,10 @@ import (
|
|||||||
|
|
||||||
// countries refuses a request when SWW_WAF_EXCLUSIVELY_ALLOWED_COUNTRIES is set and
|
// 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
|
// 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. It refuses with
|
// country, or whose country cannot be found, is refused. Its figures are the
|
||||||
// SWW_WAF_BAN_RESPONSE. Its figures are the country and the setting that
|
// country and the setting that refused it.
|
||||||
// refused it.
|
|
||||||
type countries struct {
|
type countries struct {
|
||||||
allowed []string // SWW_WAF_EXCLUSIVELY_ALLOWED_COUNTRIES
|
allowed []string // SWW_WAF_EXCLUSIVELY_ALLOWED_COUNTRIES
|
||||||
banResponse int // SWW_WAF_BAN_RESPONSE
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (countries) name() string { return "countries" }
|
func (countries) name() string { return "countries" }
|
||||||
@@ -38,7 +35,6 @@ func (c countries) check(
|
|||||||
|
|
||||||
return &verdict{
|
return &verdict{
|
||||||
action: requestlog.ActionCountryDenied,
|
action: requestlog.ActionCountryDenied,
|
||||||
status: c.banResponse,
|
|
||||||
figures: map[string]any{
|
figures: map[string]any{
|
||||||
"country": country,
|
"country": country,
|
||||||
"setting": config.ExclusivelyAllowedCountriesSetting,
|
"setting": config.ExclusivelyAllowedCountriesSetting,
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package waf
|
package waf
|
||||||
|
|
||||||
// The heuristic deny_nets. It reads SWW_WAF_DENY_NETS and SWW_WAF_BAN_RESPONSE.
|
// The heuristic deny_nets. It reads SWW_WAF_DENY_NETS.
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -10,11 +10,10 @@ import (
|
|||||||
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
||||||
)
|
)
|
||||||
|
|
||||||
// denyNets refuses a client in SWW_WAF_DENY_NETS, with SWW_WAF_BAN_RESPONSE.
|
// denyNets refuses a client in SWW_WAF_DENY_NETS. Its figure is the netblock of
|
||||||
// Its figure is the netblock of the setting the client is in.
|
// the setting the client is in.
|
||||||
type denyNets struct {
|
type denyNets struct {
|
||||||
netblocks []netip.Prefix // SWW_WAF_DENY_NETS
|
netblocks []netip.Prefix // SWW_WAF_DENY_NETS
|
||||||
banResponse int // SWW_WAF_BAN_RESPONSE
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (denyNets) name() string { return "deny_nets" }
|
func (denyNets) name() string { return "deny_nets" }
|
||||||
@@ -28,7 +27,6 @@ func (d denyNets) check(
|
|||||||
if netblock.Contains(rq.client) {
|
if netblock.Contains(rq.client) {
|
||||||
return &verdict{
|
return &verdict{
|
||||||
action: requestlog.ActionClientDenied,
|
action: requestlog.ActionClientDenied,
|
||||||
status: d.banResponse,
|
|
||||||
figures: map[string]any{figureNetblock: netblock.String()},
|
figures: map[string]any{figureNetblock: netblock.String()},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+10
-14
@@ -1,8 +1,7 @@
|
|||||||
package waf
|
package waf
|
||||||
|
|
||||||
// The heuristic dnsbl. It reads SWW_WAF_DNSBL_ABUSEIPDB_ACTION and
|
// The heuristic dnsbl. It reads SWW_WAF_DNSBL_ABUSEIPDB_ACTION, and the DNSBL zones'
|
||||||
// SWW_WAF_BAN_RESPONSE, and the DNSBL zones' verdicts through the DNSBL
|
// verdicts through the DNSBL checker.
|
||||||
// checker.
|
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -16,17 +15,15 @@ import (
|
|||||||
|
|
||||||
// dnsbl notes the DNSBL zones whose verdict lists the client, as noteListed
|
// dnsbl notes the DNSBL zones whose verdict lists the client, as noteListed
|
||||||
// does, adds the tag dnsbl=<zone> for each, the zone's key masked, and refuses
|
// does, adds the tag dnsbl=<zone> for each, the zone's key masked, and refuses
|
||||||
// the request with SWW_WAF_BAN_RESPONSE when SWW_WAF_DNSBL_ABUSEIPDB_ACTION is
|
// the request when SWW_WAF_DNSBL_ABUSEIPDB_ACTION is deny. Being
|
||||||
// deny. Being limit, it lowers the client's limits instead (see
|
// limit, it lowers the client's limits instead (see limitPercentages), and
|
||||||
// limitPercentages), and being log, it does nothing more. A zone without a
|
// being log, it does nothing more. A zone without a verdict on the client is
|
||||||
// verdict on the client is asked about it in the background, and the request
|
// asked about it in the background, and the request does not wait for the
|
||||||
// does not wait for the answer. Its figure is the zones that list the client,
|
// answer. Its figure is the zones that list the client, their keys masked.
|
||||||
// their keys masked.
|
|
||||||
type dnsbl struct {
|
type dnsbl struct {
|
||||||
dnsbl *reputation.DNSBL
|
dnsbl *reputation.DNSBL
|
||||||
action config.ReputationAction // SWW_WAF_DNSBL_ABUSEIPDB_ACTION
|
action config.ReputationAction // SWW_WAF_DNSBL_ABUSEIPDB_ACTION
|
||||||
banResponse int // SWW_WAF_BAN_RESPONSE
|
hitNoter reputationHitNoter
|
||||||
hitNoter reputationHitNoter
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (dnsbl) name() string { return "dnsbl" }
|
func (dnsbl) name() string { return "dnsbl" }
|
||||||
@@ -50,7 +47,6 @@ func (d dnsbl) check(
|
|||||||
|
|
||||||
return &verdict{
|
return &verdict{
|
||||||
action: requestlog.ActionClientDenied,
|
action: requestlog.ActionClientDenied,
|
||||||
status: d.banResponse,
|
|
||||||
figures: map[string]any{"zones": listedBy},
|
figures: map[string]any{"zones": listedBy},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,11 @@
|
|||||||
package waf
|
package waf
|
||||||
|
|
||||||
|
import "net/http"
|
||||||
|
|
||||||
// BesideLookupTimeLimit is besideLookupTimeLimit.
|
// BesideLookupTimeLimit is besideLookupTimeLimit.
|
||||||
const BesideLookupTimeLimit = besideLookupTimeLimit
|
const BesideLookupTimeLimit = besideLookupTimeLimit
|
||||||
|
|
||||||
|
// HTTPServer is Go's server s holds, for tests that read its settings.
|
||||||
|
func (s *Server) HTTPServer() *http.Server {
|
||||||
|
return s.httpServer
|
||||||
|
}
|
||||||
@@ -4,7 +4,6 @@ package waf
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net/http"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -40,7 +39,6 @@ func (headerLimits) check(
|
|||||||
if requestLine > maxLineBytes {
|
if requestLine > maxLineBytes {
|
||||||
return &verdict{
|
return &verdict{
|
||||||
action: requestlog.ActionRequestLineTooLong,
|
action: requestlog.ActionRequestLineTooLong,
|
||||||
status: http.StatusRequestURITooLong,
|
|
||||||
figures: map[string]any{
|
figures: map[string]any{
|
||||||
"request_line_bytes": requestLine,
|
"request_line_bytes": requestLine,
|
||||||
"request_line_bytes_limit": maxLineBytes,
|
"request_line_bytes_limit": maxLineBytes,
|
||||||
@@ -58,7 +56,6 @@ func (headerLimits) check(
|
|||||||
|
|
||||||
return &verdict{
|
return &verdict{
|
||||||
action: requestlog.ActionRequestHeaderTooLarge,
|
action: requestlog.ActionRequestHeaderTooLarge,
|
||||||
status: http.StatusRequestHeaderFieldsTooLarge,
|
|
||||||
figures: map[string]any{
|
figures: map[string]any{
|
||||||
"longest_header_line_bytes": longest,
|
"longest_header_line_bytes": longest,
|
||||||
"header_line_bytes_limit": maxLineBytes,
|
"header_line_bytes_limit": maxLineBytes,
|
||||||
|
|||||||
@@ -42,13 +42,10 @@ type refusesWithoutLookup interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// verdict is a heuristic refusing a request, or banning its client: the action
|
// verdict is a heuristic refusing a request, or banning its client: the action
|
||||||
// the log line names, the status the visitor is answered with, and the figures
|
// the log line names, and the figures the heuristic acted on, by name, such as
|
||||||
// the heuristic acted on, by name, such as the count and the limit. A status of
|
// the count and the limit.
|
||||||
// 0 closes the visitor's connection without an answer. A verdict of afterAnswer
|
|
||||||
// or onReport refuses nothing, and has neither action nor status.
|
|
||||||
type verdict struct {
|
type verdict struct {
|
||||||
action requestlog.Action
|
action requestlog.Action
|
||||||
status int
|
|
||||||
figures map[string]any
|
figures map[string]any
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+13
-19
@@ -109,7 +109,8 @@ type CoreRuleSetInspector interface {
|
|||||||
|
|
||||||
// Server is the server waf runs.
|
// Server is the server waf runs.
|
||||||
type Server struct {
|
type Server struct {
|
||||||
*http.Server
|
httpServer *http.Server
|
||||||
|
h *handler
|
||||||
}
|
}
|
||||||
|
|
||||||
// newHandler returns the handler with what every request shares, the parts
|
// newHandler returns the handler with what every request shares, the parts
|
||||||
@@ -189,25 +190,20 @@ func newHandler(params Params) *handler {
|
|||||||
routeWatch: params.RouteWatch,
|
routeWatch: params.RouteWatch,
|
||||||
ipv6GroupPrefix: cfg.IPv6GroupPrefix,
|
ipv6GroupPrefix: cfg.IPv6GroupPrefix,
|
||||||
},
|
},
|
||||||
denyNets{netblocks: cfg.DenyNets, banResponse: cfg.BanResponse},
|
denyNets{netblocks: cfg.DenyNets},
|
||||||
activeBan{banMaker: sharedBanMaker},
|
activeBan{banMaker: sharedBanMaker},
|
||||||
}
|
}
|
||||||
h.afterLookup = []heuristic{
|
h.afterLookup = []heuristic{
|
||||||
countries{
|
countries{allowed: cfg.ExclusivelyAllowedCountries},
|
||||||
allowed: cfg.ExclusivelyAllowedCountries,
|
|
||||||
banResponse: cfg.BanResponse,
|
|
||||||
},
|
|
||||||
blocklists{
|
blocklists{
|
||||||
lists: params.Lists,
|
lists: params.Lists,
|
||||||
action: cfg.BlocklistAction,
|
action: cfg.BlocklistAction,
|
||||||
banResponse: cfg.BanResponse,
|
hitNoter: hitNoter,
|
||||||
hitNoter: hitNoter,
|
|
||||||
},
|
},
|
||||||
dnsbl{
|
dnsbl{
|
||||||
dnsbl: params.DNSBL,
|
dnsbl: params.DNSBL,
|
||||||
action: cfg.DNSBLAbuseIPDBAction,
|
action: cfg.DNSBLAbuseIPDBAction,
|
||||||
banResponse: cfg.BanResponse,
|
hitNoter: hitNoter,
|
||||||
hitNoter: hitNoter,
|
|
||||||
},
|
},
|
||||||
abuseIPDB{
|
abuseIPDB{
|
||||||
abuseIPDB: params.AbuseIPDB,
|
abuseIPDB: params.AbuseIPDB,
|
||||||
@@ -215,11 +211,10 @@ func newHandler(params Params) *handler {
|
|||||||
keySet: cfg.AbuseIPDBKey != "",
|
keySet: cfg.AbuseIPDBKey != "",
|
||||||
minScore: cfg.AbuseIPDBMinScore,
|
minScore: cfg.AbuseIPDBMinScore,
|
||||||
action: cfg.DNSBLAbuseIPDBAction,
|
action: cfg.DNSBLAbuseIPDBAction,
|
||||||
banResponse: cfg.BanResponse,
|
|
||||||
ipv6GroupPrefix: cfg.IPv6GroupPrefix,
|
ipv6GroupPrefix: cfg.IPv6GroupPrefix,
|
||||||
hitNoter: hitNoter,
|
hitNoter: hitNoter,
|
||||||
},
|
},
|
||||||
tagDeny{tags: cfg.TagDeny, banResponse: cfg.BanResponse},
|
tagDeny{tags: cfg.TagDeny},
|
||||||
rateLimits{
|
rateLimits{
|
||||||
limiter: params.Limiter,
|
limiter: params.Limiter,
|
||||||
exemptNets: cfg.RateLimitExemptNets,
|
exemptNets: cfg.RateLimitExemptNets,
|
||||||
@@ -241,13 +236,12 @@ func newHandler(params Params) *handler {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
h.besideLookup = []heuristic{h.trapPaths, h.ruleFiles, h.coreRuleSet}
|
h.besideLookup = []heuristic{h.trapPaths, h.ruleFiles, h.coreRuleSet}
|
||||||
h.afterBesideLookup = []heuristic{
|
h.afterBesideLookup = []heuristic{tagDeny{tags: cfg.TagDeny}}
|
||||||
tagDeny{tags: cfg.TagDeny, banResponse: cfg.BanResponse},
|
|
||||||
}
|
|
||||||
h.afterAnswer = []heuristic{
|
h.afterAnswer = []heuristic{
|
||||||
refusalLimit{
|
refusalLimit{
|
||||||
limiter: params.Limiter,
|
limiter: params.Limiter,
|
||||||
perMinute: cfg.RefusalsLimitPerMinute,
|
perMinute: cfg.RefusalsLimitPerMinute,
|
||||||
|
banResponse: cfg.BanResponse,
|
||||||
ipv6GroupPrefix: cfg.IPv6GroupPrefix,
|
ipv6GroupPrefix: cfg.IPv6GroupPrefix,
|
||||||
banMaker: sharedBanMaker,
|
banMaker: sharedBanMaker,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -344,8 +344,8 @@ func startProxyWithAlerts(
|
|||||||
return startRProxy(t, server.Addr, appURL, env), out, server, alertQueue
|
return startRProxy(t, server.Addr, appURL, env), out, server, alertQueue
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
||||||
// Addr to where it listens.
|
// server.Addr to where it listens.
|
||||||
func serve(t *testing.T, server *wafAndParts) {
|
func serve(t *testing.T, server *wafAndParts) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
@@ -423,10 +423,13 @@ func startRProxy(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// wafAndParts is waf's server with the Params it was built from, so that a
|
// wafAndParts is waf's server with the Params it was built from, so that a
|
||||||
// test can read the parts the server uses.
|
// test can read the parts the server uses. Addr is where it listens once serve
|
||||||
|
// has it serving.
|
||||||
type wafAndParts struct {
|
type wafAndParts struct {
|
||||||
*waf.Server
|
*waf.Server
|
||||||
waf.Params
|
waf.Params
|
||||||
|
|
||||||
|
Addr string
|
||||||
}
|
}
|
||||||
|
|
||||||
// banAbuseIPDBBlacklist does on w what a fetch of AbuseIPDB's blacklist with
|
// banAbuseIPDBBlacklist does on w what a fetch of AbuseIPDB's blacklist with
|
||||||
|
|||||||
@@ -17,9 +17,9 @@ import (
|
|||||||
// countedByRateLimits exempts it, by the client's limit percentages, which it
|
// countedByRateLimits exempts it, by the client's limit percentages, which it
|
||||||
// notes for the log line with the client's counts. A request that takes the
|
// notes for the log line with the client's counts. A request that takes the
|
||||||
// client over a rate limit, as its limit percentage lowers it, breaks it: it is
|
// client over a rate limit, as its limit percentage lowers it, breaks it: it is
|
||||||
// refused with SWW_WAF_BAN_RESPONSE, and bans the client. A request exempt
|
// refused, and bans the client. A request exempt from the rate limits is exempt
|
||||||
// from the rate limits is exempt from the byte limits too. Its figures are the
|
// from the byte limits too. Its figures are the window, the count and the
|
||||||
// window, the count and the limit.
|
// limit.
|
||||||
type rateLimits struct {
|
type rateLimits struct {
|
||||||
limiter *ratelimit.Limiter
|
limiter *ratelimit.Limiter
|
||||||
exemptNets []netip.Prefix // SWW_WAF_RATE_LIMIT_EXEMPT_NETS
|
exemptNets []netip.Prefix // SWW_WAF_RATE_LIMIT_EXEMPT_NETS
|
||||||
@@ -63,9 +63,7 @@ func (r rateLimits) check(
|
|||||||
r.banMaker.banForLimit(rq, now, hit, r.banResponse)
|
r.banMaker.banForLimit(rq, now, hit, r.banResponse)
|
||||||
|
|
||||||
return &verdict{
|
return &verdict{
|
||||||
action: requestlog.ActionClientIsRateLimited,
|
action: requestlog.ActionClientIsRateLimited, figures: limitFigures(hit),
|
||||||
status: r.banResponse,
|
|
||||||
figures: limitFigures(hit),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +1,15 @@
|
|||||||
package waf
|
package waf
|
||||||
|
|
||||||
// The heuristic refusal_limit. It reads SWW_WAF_REFUSALS_LIMIT_PER_MINUTE.
|
// The heuristic refusal_limit. It reads SWW_WAF_REFUSALS_LIMIT_PER_MINUTE and
|
||||||
|
// SWW_WAF_BAN_RESPONSE.
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/ratelimit"
|
"sneak.berlin/go/smallwebwaf/internal/ratelimit"
|
||||||
|
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
||||||
)
|
)
|
||||||
|
|
||||||
// refusalLimit counts the request once it has been answered, if waf
|
// refusalLimit counts the request once it has been answered, if waf
|
||||||
@@ -15,11 +18,11 @@ import (
|
|||||||
// SWW_WAF_REFUSALS_LIMIT_PER_MINUTE such refusals of the client within a minute break
|
// SWW_WAF_REFUSALS_LIMIT_PER_MINUTE such refusals of the client within a minute break
|
||||||
// a limit, which bans the client. A client in SWW_WAF_ALLOW_NETS, which the
|
// a limit, which bans the client. A client in SWW_WAF_ALLOW_NETS, which the
|
||||||
// heuristics skip, is not counted, and nothing is while the limit is off.
|
// heuristics skip, is not counted, and nothing is while the limit is off.
|
||||||
// The ban's notes give the status the client was sent, or in observe mode
|
// Its figures are the window, the count and the limit.
|
||||||
// would have been. Its figures are the window, the count and the limit.
|
|
||||||
type refusalLimit struct {
|
type refusalLimit struct {
|
||||||
limiter *ratelimit.Limiter
|
limiter *ratelimit.Limiter
|
||||||
perMinute int64 // SWW_WAF_REFUSALS_LIMIT_PER_MINUTE
|
perMinute int64 // SWW_WAF_REFUSALS_LIMIT_PER_MINUTE
|
||||||
|
banResponse int // SWW_WAF_BAN_RESPONSE
|
||||||
ipv6GroupPrefix int // SWW_WAF_IPV6_GROUP_PREFIX
|
ipv6GroupPrefix int // SWW_WAF_IPV6_GROUP_PREFIX
|
||||||
banMaker banMaker
|
banMaker banMaker
|
||||||
}
|
}
|
||||||
@@ -48,7 +51,27 @@ func (r refusalLimit) check(
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
r.banMaker.banForLimit(rq, now, hit, rq.refusalStatus)
|
// What the client was sent, or in observe mode would have been.
|
||||||
|
status := rq.out.status
|
||||||
|
|
||||||
|
switch rq.wouldAction {
|
||||||
|
case requestlog.ActionBlockedByBlockRule,
|
||||||
|
requestlog.ActionBlockedByCoreRuleSet:
|
||||||
|
status = http.StatusForbidden
|
||||||
|
case requestlog.ActionTemporarilyBanned,
|
||||||
|
requestlog.ActionPermanentlyBanned:
|
||||||
|
status = r.banResponse
|
||||||
|
case requestlog.ActionForward,
|
||||||
|
requestlog.ActionRequestLineTooLong,
|
||||||
|
requestlog.ActionRequestHeaderTooLarge,
|
||||||
|
requestlog.ActionClientIsRateLimited,
|
||||||
|
requestlog.ActionClientDenied,
|
||||||
|
requestlog.ActionCountryDenied,
|
||||||
|
requestlog.ActionChecksTimedOut,
|
||||||
|
requestlog.ActionOwnEndpoint:
|
||||||
|
}
|
||||||
|
|
||||||
|
r.banMaker.banForLimit(rq, now, hit, status)
|
||||||
|
|
||||||
return &verdict{figures: limitFigures(hit)}
|
return &verdict{figures: limitFigures(hit)}
|
||||||
}
|
}
|
||||||
+25
-12
@@ -69,10 +69,6 @@ type decision struct {
|
|||||||
// its client's history counts. trapPaths and ruleFiles set attack,
|
// its client's history counts. trapPaths and ruleFiles set attack,
|
||||||
// ruleFiles blockedByBlockRule, and coreRuleSet blockedByCoreRuleSet.
|
// ruleFiles blockedByBlockRule, and coreRuleSet blockedByCoreRuleSet.
|
||||||
attack, blockedByBlockRule, blockedByCoreRuleSet bool
|
attack, blockedByBlockRule, blockedByCoreRuleSet bool
|
||||||
// refusalStatus is the status of the verdict that refused the request, or
|
|
||||||
// in observe mode would have: what the visitor was answered with, or would
|
|
||||||
// have been. check sets it.
|
|
||||||
refusalStatus int
|
|
||||||
// wouldAction is, in observe mode, the action enforce mode would have
|
// wouldAction is, in observe mode, the action enforce mode would have
|
||||||
// refused the request with, one of requestlog's Action constants, and ""
|
// refused the request with, one of requestlog's Action constants, and ""
|
||||||
// otherwise. check sets it.
|
// otherwise. check sets it.
|
||||||
@@ -207,8 +203,9 @@ func requestHeaders(r *http.Request, names []string) map[string]string {
|
|||||||
|
|
||||||
// check is the one place where a request can be refused. It returns nil to let
|
// check is the one place where a request can be refused. It returns nil to let
|
||||||
// the request through. The heuristics of checkClient decide: a request they
|
// the request through. The heuristics of checkClient decide: a request they
|
||||||
// refuse is answered with the status and logged with the action of the verdict
|
// refuse is answered with SWW_WAF_BAN_RESPONSE, 403 for a block rule or the Core
|
||||||
// that refused it. In observe mode nothing is refused, and the log line names
|
// Rule Set, 503 for heuristics that ran out of time, or 414 or 431 for the
|
||||||
|
// header limits. In observe mode nothing is refused, and the log line names
|
||||||
// what enforce mode would have done. ctx is the request's own context.
|
// what enforce mode would have done. ctx is the request's own context.
|
||||||
func (rq *request) check(ctx context.Context) *refusal {
|
func (rq *request) check(ctx context.Context) *refusal {
|
||||||
v := rq.checkClient(ctx)
|
v := rq.checkClient(ctx)
|
||||||
@@ -216,16 +213,32 @@ func (rq *request) check(ctx context.Context) *refusal {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
rq.refusalStatus = v.status
|
action := v.action
|
||||||
|
|
||||||
if rq.h.config.Observe {
|
switch {
|
||||||
rq.wouldAction = v.action
|
case rq.h.config.Observe:
|
||||||
rq.line.WouldAction = v.action
|
rq.wouldAction = action
|
||||||
|
rq.line.WouldAction = action
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
case action == requestlog.ActionBlockedByBlockRule ||
|
||||||
|
action == requestlog.ActionBlockedByCoreRuleSet:
|
||||||
|
return &refusal{status: http.StatusForbidden, action: action}
|
||||||
|
case action == requestlog.ActionChecksTimedOut:
|
||||||
|
return &refusal{
|
||||||
|
status: http.StatusServiceUnavailable,
|
||||||
|
action: action,
|
||||||
|
}
|
||||||
|
case action == requestlog.ActionRequestLineTooLong:
|
||||||
|
return &refusal{status: http.StatusRequestURITooLong, action: action}
|
||||||
|
case action == requestlog.ActionRequestHeaderTooLarge:
|
||||||
|
return &refusal{
|
||||||
|
status: http.StatusRequestHeaderFieldsTooLarge,
|
||||||
|
action: action,
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return rq.banResponse(action)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &refusal{status: v.status, action: v.action}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// checkClient runs the heuristics on the request, and returns the first
|
// checkClient runs the heuristics on the request, and returns the first
|
||||||
|
|||||||
@@ -1,12 +1,10 @@
|
|||||||
package waf
|
package waf
|
||||||
|
|
||||||
// The heuristic rule_files. It reads the rules of the rule files, which
|
// The heuristic rule_files. It reads the rules of the rule files, which
|
||||||
// SWW_WAF_RULE_FILES_DIR and SWW_WAF_RULE_FILES_ENABLED name, and
|
// SWW_WAF_RULE_FILES_DIR and SWW_WAF_RULE_FILES_ENABLED name.
|
||||||
// SWW_WAF_BAN_RESPONSE.
|
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"net/http"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/bans"
|
"sneak.berlin/go/smallwebwaf/internal/bans"
|
||||||
@@ -19,9 +17,8 @@ import (
|
|||||||
// ruleFiles takes the rules of the rule files the request matched, which
|
// ruleFiles takes the rules of the rule files the request matched, which
|
||||||
// findBesideLookup found, notes their ids in the log line, adds the tag
|
// findBesideLookup found, notes their ids in the log line, adds the tag
|
||||||
// exploitsignature when it matches any, and refuses the request
|
// exploitsignature when it matches any, and refuses the request
|
||||||
// when the last rule it matches refuses it: with 403 and
|
// when the last rule it matches refuses it: with ActionBlockedByBlockRule for a
|
||||||
// ActionBlockedByBlockRule for a block rule, and for a ban rule with
|
// block rule, and for a ban rule with the action for the ban it makes. A ban
|
||||||
// SWW_WAF_BAN_RESPONSE and the action for the ban it makes. A ban
|
|
||||||
// rule bans the client's netblock for a clear sign of attack, or in observe
|
// rule bans the client's netblock for a clear sign of attack, or in observe
|
||||||
// mode raises the alert for the ban
|
// mode raises the alert for the ban
|
||||||
// it would have made. Either rule's match is noted as an offence, for the
|
// it would have made. Either rule's match is noted as an offence, for the
|
||||||
@@ -62,7 +59,6 @@ func (r ruleFiles) check(
|
|||||||
|
|
||||||
return &verdict{
|
return &verdict{
|
||||||
action: requestlog.ActionBlockedByBlockRule,
|
action: requestlog.ActionBlockedByBlockRule,
|
||||||
status: http.StatusForbidden,
|
|
||||||
figures: figures,
|
figures: figures,
|
||||||
}
|
}
|
||||||
case rules.ActionBan:
|
case rules.ActionBan:
|
||||||
@@ -74,7 +70,6 @@ func (r ruleFiles) check(
|
|||||||
now,
|
now,
|
||||||
bans.Notes{RuleID: last.ID, Target: last.Target},
|
bans.Notes{RuleID: last.ID, Target: last.Target},
|
||||||
),
|
),
|
||||||
status: r.banMaker.banResponse,
|
|
||||||
figures: figures,
|
figures: figures,
|
||||||
}
|
}
|
||||||
case rules.ActionLog:
|
case rules.ActionLog:
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package waf
|
package waf
|
||||||
|
|
||||||
// The heuristic tag_deny. It reads SWW_WAF_TAG_DENY and SWW_WAF_BAN_RESPONSE.
|
// The heuristic tag_deny. It reads SWW_WAF_TAG_DENY.
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -10,11 +10,10 @@ import (
|
|||||||
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
||||||
)
|
)
|
||||||
|
|
||||||
// tagDeny refuses a request that carries a tag in SWW_WAF_TAG_DENY, with
|
// tagDeny refuses a request that carries a tag in SWW_WAF_TAG_DENY. Its figure is
|
||||||
// SWW_WAF_BAN_RESPONSE. Its figure is the first such tag, in sorted order.
|
// the first such tag, in sorted order.
|
||||||
type tagDeny struct {
|
type tagDeny struct {
|
||||||
tags []string // SWW_WAF_TAG_DENY
|
tags []string // SWW_WAF_TAG_DENY
|
||||||
banResponse int // SWW_WAF_BAN_RESPONSE
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tagDeny) name() string { return "tag_deny" }
|
func (tagDeny) name() string { return "tag_deny" }
|
||||||
@@ -28,7 +27,6 @@ func (t tagDeny) check(
|
|||||||
if slices.Contains(t.tags, tag) {
|
if slices.Contains(t.tags, tag) {
|
||||||
return &verdict{
|
return &verdict{
|
||||||
action: requestlog.ActionClientDenied,
|
action: requestlog.ActionClientDenied,
|
||||||
status: t.banResponse,
|
|
||||||
figures: map[string]any{"tag": tag},
|
figures: map[string]any{"tag": tag},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package waf
|
package waf
|
||||||
|
|
||||||
// The heuristic trap_paths. It reads SWW_WAF_TRAP_PATHS and
|
// The heuristic trap_paths. It reads SWW_WAF_TRAP_PATHS.
|
||||||
// SWW_WAF_BAN_RESPONSE.
|
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
@@ -13,10 +12,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// trapPaths refuses a request for a path in SWW_WAF_TRAP_PATHS, as trapPath
|
// trapPaths refuses a request for a path in SWW_WAF_TRAP_PATHS, as trapPath
|
||||||
// finds it, with SWW_WAF_BAN_RESPONSE. Such a request is a clear sign of
|
// finds it. Such a request is a clear sign of attack, as a ban rule's match is:
|
||||||
// attack, as a ban rule's match is: it bans the client's netblock, or in
|
// it bans the client's netblock, or in observe mode raises the alert for the
|
||||||
// observe mode raises the alert for the ban it would have made. The ban's
|
// ban it would have made. The ban's notes give the path as sent, and so does
|
||||||
// notes give the path as sent, and so does its figure.
|
// its figure.
|
||||||
type trapPaths struct {
|
type trapPaths struct {
|
||||||
paths []string // SWW_WAF_TRAP_PATHS
|
paths []string // SWW_WAF_TRAP_PATHS
|
||||||
banMaker banMaker
|
banMaker banMaker
|
||||||
@@ -42,7 +41,6 @@ func (t trapPaths) check(
|
|||||||
now,
|
now,
|
||||||
bans.Notes{TrapPath: path},
|
bans.Notes{TrapPath: path},
|
||||||
),
|
),
|
||||||
status: t.banMaker.banResponse,
|
|
||||||
figures: map[string]any{"path": path},
|
figures: map[string]any{"path": path},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+37
-12
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -30,19 +31,43 @@ const (
|
|||||||
// 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 {
|
||||||
return &Server{
|
s := &Server{h: newHandler(params)}
|
||||||
Server: &http.Server{
|
s.httpServer = &http.Server{
|
||||||
Addr: params.Config.ListenAddr,
|
Addr: params.Config.ListenAddr,
|
||||||
Handler: newHandler(params),
|
Handler: s,
|
||||||
ErrorLog: slog.NewLogLogger(
|
ErrorLog: slog.NewLogLogger(
|
||||||
params.ProcessLog.Handler(),
|
params.ProcessLog.Handler(),
|
||||||
slog.LevelWarn,
|
slog.LevelWarn,
|
||||||
),
|
),
|
||||||
ReadTimeout: wafTimeout,
|
ReadTimeout: wafTimeout,
|
||||||
WriteTimeout: wafTimeout,
|
WriteTimeout: wafTimeout,
|
||||||
IdleTimeout: wafIdleTimeout,
|
IdleTimeout: wafIdleTimeout,
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
// Serve serves requests on listener until Shutdown or Close, as Go's
|
||||||
|
// http.Server.Serve does.
|
||||||
|
func (s *Server) Serve(listener net.Listener) error {
|
||||||
|
return s.httpServer.Serve(listener)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Shutdown stops serving once the requests in progress have finished, or
|
||||||
|
// when ctx is done, as Go's http.Server.Shutdown does.
|
||||||
|
func (s *Server) Shutdown(ctx context.Context) error {
|
||||||
|
return s.httpServer.Shutdown(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close stops serving at once and closes every connection, as Go's
|
||||||
|
// http.Server.Close does.
|
||||||
|
func (s *Server) Close() error {
|
||||||
|
return s.httpServer.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
// ServeHTTP handles one request to waf, as handler.ServeHTTP says.
|
||||||
|
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
s.h.ServeHTTP(w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServeHTTP handles one request to waf: a request carrying
|
// ServeHTTP handles one request to waf: a request carrying
|
||||||
|
|||||||
@@ -703,7 +703,9 @@ func TestQuestionWhoseBodyStopsArrivingGetsNoAnswer(t *testing.T) {
|
|||||||
t,
|
t,
|
||||||
"",
|
"",
|
||||||
map[string]string{metricsToken: token},
|
map[string]string{metricsToken: token},
|
||||||
func(server *wafAndParts) { server.ReadTimeout = readTimeout },
|
func(server *wafAndParts) {
|
||||||
|
server.HTTPServer().ReadTimeout = readTimeout
|
||||||
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
conn := dial(t, s.addr)
|
conn := dial(t, s.addr)
|
||||||
@@ -1834,16 +1836,16 @@ func TestLogLevelHoldsBackNoRequestLine(t *testing.T) {
|
|||||||
func TestServerHasTheTimeLimitsSPECGives(t *testing.T) {
|
func TestServerHasTheTimeLimitsSPECGives(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
s := startWAF(t, "", nil)
|
server := startWAF(t, "", nil).server.HTTPServer()
|
||||||
|
|
||||||
if s.server.ReadTimeout != time.Minute ||
|
if server.ReadTimeout != time.Minute ||
|
||||||
s.server.WriteTimeout != time.Minute ||
|
server.WriteTimeout != time.Minute ||
|
||||||
s.server.IdleTimeout != 2*time.Minute {
|
server.IdleTimeout != 2*time.Minute {
|
||||||
t.Errorf(
|
t.Errorf(
|
||||||
"the read, write and idle limits are %v, %v and %v, want 1m, 1m and 2m",
|
"the read, write and idle limits are %v, %v and %v, want 1m, 1m and 2m",
|
||||||
s.server.ReadTimeout,
|
server.ReadTimeout,
|
||||||
s.server.WriteTimeout,
|
server.WriteTimeout,
|
||||||
s.server.IdleTimeout,
|
server.IdleTimeout,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user