diff --git a/SPEC.md b/SPEC.md index 5849f66..9c66300 100644 --- a/SPEC.md +++ b/SPEC.md @@ -74,9 +74,9 @@ never as a time. and stops their background work. - `internal/config`: reads the settings, the one place they are read. - `internal/waf`: `waf`'s server. It runs the checks in `check`, and holds - `waf`'s question, answer and report. It answers `waf`'s own endpoints in - `answerOwnEndpoint`. It counts bytes, refusals and anomalies, and writes - the log lines. + `waf`'s question, answer and report. Its `routes.go` lists `waf`'s own + endpoints. It counts bytes, refusals and anomalies, and writes the log + lines. - `internal/rproxy`: the reverse proxy in front of one app that asks `waf` about each request. It passes a request to the app with the standard library's `httputil.ReverseProxy`, within the time and size limits. @@ -119,6 +119,7 @@ never as a time. default `SWW_WAF_CORE_RULE_SET_DISABLED_RULES`, are built for that version. They are checked again before the version changes, since rule ids, and what each rule matches, change between versions; + - `github.com/go-chi/chi/v5` to route the requests to `waf`'s own endpoints; - `github.com/hashicorp/golang-lru/v2` for the bounded tables; - `github.com/oschwald/maxminddb-golang/v2` to read the lookup database; - `github.com/fsnotify/fsnotify` to notice edited files; diff --git a/go.mod b/go.mod index 959cd68..3bcafef 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/corazawaf/coraza-coreruleset/v4 v4.25.0 github.com/corazawaf/coraza/v3 v3.8.2-0.20261022102553-d028be399c68 github.com/fsnotify/fsnotify v1.10.1 + github.com/go-chi/chi/v5 v5.5.0 github.com/hashicorp/golang-lru/v2 v2.0.7 github.com/maxmind/mmdbwriter v1.2.0 github.com/oschwald/maxminddb-golang/v2 v2.7.0 diff --git a/go.sum b/go.sum index f9328ba..0294fd2 100644 --- a/go.sum +++ b/go.sum @@ -20,6 +20,8 @@ github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHk github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= github.com/fsnotify/fsnotify v1.10.1 h1:b0/UzAf9yR5rhf3RPm9gf3ehBPpf0oZKIjtpKrx59Ho= github.com/fsnotify/fsnotify v1.10.1/go.mod h1:TLheqan6HD6GBK6PrDWyDPBaEV8LspOxvPSjC+bVfgo= +github.com/go-chi/chi/v5 v5.5.0 h1:xdKQ1dJmVJsmgahrXSKhUx8NyVPzpwdlyrrQFpMPJgg= +github.com/go-chi/chi/v5 v5.5.0/go.mod h1:d1IG7GtKPSjPyBP365PH8sAGVHk3qfB/IhjFNRH1wWM= github.com/go-viper/mapstructure/v2 v2.4.0 h1:EBsztssimR/CONLSZZ04E8qAkxNYq4Qp9LvH92wZUgs= github.com/go-viper/mapstructure/v2 v2.4.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM= github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4= diff --git a/internal/waf/admin.go b/internal/waf/admin.go index 99ddec2..75d5516 100644 --- a/internal/waf/admin.go +++ b/internal/waf/admin.go @@ -2,7 +2,6 @@ package waf import ( "bytes" - "crypto/subtle" "encoding/json" "errors" "fmt" @@ -15,7 +14,6 @@ import ( "sneak.berlin/go/smallwebwaf/internal/bans" "sneak.berlin/go/smallwebwaf/internal/config" "sneak.berlin/go/smallwebwaf/internal/ratelimit" - "sneak.berlin/go/smallwebwaf/internal/requestlog" "sneak.berlin/go/smallwebwaf/internal/state" ) @@ -41,80 +39,23 @@ var ( errNotAddress = errors.New("is not an address, such as 203.0.113.9") ) -// answerHealth answers a request for the health endpoint. -func (rq *request) answerHealth() { - rq.line.Action = requestlog.ActionOwnEndpoint - // Set here rather than left to Go's server, which would set it only - // after the log line has taken the response's headers. - rq.out.Header().Set("Content-Type", "text/plain; charset=utf-8") - _, _ = io.WriteString(rq.out, "ok\n") -} - -// answerAdmin answers a request for one of waf's own endpoints. Each -// needs a token, sent as Authorization: Bearer : the metrics -// SWW_WAF_METRICS_TOKEN, the others SWW_WAF_ADMIN_TOKEN. A request without it is -// refused with 401. An endpoint whose token is unset answers 404, as any other -// path does. -func (rq *request) answerAdmin() { - rq.line.Action = requestlog.ActionOwnEndpoint - - token, answer := rq.endpoint() - - switch { - case token == "": - http.Error( - rq.out, - http.StatusText(http.StatusNotFound), - http.StatusNotFound, - ) - case !hasToken(rq.in, token): - rq.out.Header().Set("WWW-Authenticate", "Bearer") - http.Error( - rq.out, - http.StatusText(http.StatusUnauthorized), - http.StatusUnauthorized, - ) - default: - answer() +// handleHealth answers GET HealthPath. +func handleHealth() http.HandlerFunc { + return func(w http.ResponseWriter, _ *http.Request) { + // Set here rather than left to Go's server, which would set it only + // after the log line has taken the response's headers. + w.Header().Set("Content-Type", "text/plain; charset=utf-8") + _, _ = io.WriteString(w, "ok\n") } } -// endpoint returns the token the request's endpoint needs, and what -// answers the request there; "" when there is no such endpoint. -func (rq *request) endpoint() (string, func()) { - cfg := rq.h.config - method, path := rq.in.Method, rq.in.URL.Path - - switch { - case method == http.MethodGet && path == MetricsPath: - return cfg.MetricsToken, func() { rq.h.metrics.ServeHTTP(rq.out, rq.in) } - case method == http.MethodGet && path == BansPath: - return cfg.AdminToken, rq.listBans - case method == http.MethodPost && path == BansPath: - return cfg.AdminToken, rq.addBan - case method == http.MethodDelete && strings.HasPrefix(path, BansPath+"/"): - return cfg.AdminToken, rq.liftBans - case method == http.MethodGet && strings.HasPrefix(path, ClientsPath): - return cfg.AdminToken, rq.showClient - default: - return "", nil +// handleListBans answers GET BansPath with every ban held. +func (h *handler) handleListBans() http.HandlerFunc { + return func(w http.ResponseWriter, _ *http.Request) { + h.answerBans(w, h.ledger.Snapshot()) } } -// hasToken reports whether r carries token, as Authorization: Bearer -// . -func hasToken(r *http.Request, token string) bool { - scheme, sent, _ := strings.Cut(r.Header.Get("Authorization"), " ") - - return strings.EqualFold(scheme, "Bearer") && - subtle.ConstantTimeCompare([]byte(sent), []byte(token)) == 1 -} - -// listBans answers GET BansPath with every ban held. -func (rq *request) listBans() { - rq.answerBans(rq.h.ledger.Snapshot()) -} - // banToAdd is the body of POST BansPath. type banToAdd struct { // Netblock is a netblock, or a client's address, which stands for the @@ -126,44 +67,44 @@ type banToAdd struct { Reason string `json:"reason"` } -// addBan answers POST BansPath: it bans the netblock the body names, as -// an admin, from now for the duration the body gives, with its reason, +// handleAddBan answers POST BansPath: it bans the netblock the body names, +// as an admin, from now for the duration the body gives, with its reason, // and answers with that ban. -func (rq *request) addBan() { - toAdd, err := rq.readBanToAdd() +func (h *handler) handleAddBan() http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + toAdd, err := readBanToAdd(r) - var ( - netblock netip.Prefix - expires time.Time - now = rq.h.now() - ) + var ( + netblock netip.Prefix + expires time.Time + now = h.now() + ) - if err == nil { - netblock, err = rq.h.parseBanNetblock(toAdd.Netblock) + if err == nil { + netblock, err = h.parseBanNetblock(toAdd.Netblock) + } + + if err == nil { + expires, err = expiry(toAdd.Duration, now) + } + + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) + + return + } + + ban := h.ledger.BanForAdmin(netblock, now, expires, toAdd.Reason) + h.answerBans(w, []bans.Ban{ban}) } - - if err == nil { - expires, err = expiry(toAdd.Duration, now) - } - - if err != nil { - http.Error(rq.out, err.Error(), http.StatusBadRequest) - - return - } - - ban := rq.h.ledger.BanForAdmin(netblock, now, expires, toAdd.Reason) - rq.answerBans([]bans.Ban{ban}) } // readBanToAdd reads the body of POST BansPath: a JSON object with // nothing but whitespace after it, in at most banBodyMaxBytes. -func (rq *request) readBanToAdd() (banToAdd, error) { +func readBanToAdd(r *http.Request) (banToAdd, error) { data, err := io.ReadAll( - http.MaxBytesReader(nil, rq.in.Body, banBodyMaxBytes), + http.MaxBytesReader(nil, r.Body, banBodyMaxBytes), ) - rq.requestBytes = int64(len(data)) - if err != nil { return banToAdd{}, fmt.Errorf("%w: %w", errNotBanToAdd, err) } @@ -246,29 +187,31 @@ func expiry(duration string, now time.Time) (time.Time, error) { return now.Add(length), nil } -// liftBans answers DELETE BansPath/: it lifts every ban active on -// a netblock the client's address is in, and answers with those bans, or -// with 404 when none is active. -func (rq *request) liftBans() { - client, err := pathAddress(rq.in.URL.Path, BansPath+"/") - if err != nil { - http.Error(rq.out, err.Error(), http.StatusBadRequest) +// handleLiftBans answers DELETE BansPath/: it lifts every ban +// active on a netblock the client's address is in, and answers with those +// bans, or with 404 when none is active. +func (h *handler) handleLiftBans() http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + client, err := pathAddress(r.URL.Path, BansPath+"/") + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) - return + return + } + + lifted := h.ledger.Lift(client, h.now()) + if len(lifted) == 0 { + http.Error( + w, + "no ban is active on "+client.String(), + http.StatusNotFound, + ) + + return + } + + h.answerBans(w, lifted) } - - lifted := rq.h.ledger.Lift(client, rq.h.now()) - if len(lifted) == 0 { - http.Error( - rq.out, - "no ban is active on "+client.String(), - http.StatusNotFound, - ) - - return - } - - rq.answerBans(lifted) } // clientAnswer is the answer to GET ClientsPath: the client the @@ -280,27 +223,29 @@ type clientAnswer struct { Bans []state.BanEntry `json:"bans"` } -// showClient answers GET ClientsPath with what waf knows of the client: -// its counters, its history, which holds its country as last looked up and its -// offences, and its bans with their notes. -func (rq *request) showClient() { - addr, err := pathAddress(rq.in.URL.Path, ClientsPath) - if err != nil { - http.Error(rq.out, err.Error(), http.StatusBadRequest) +// handleShowClient answers GET ClientsPath with what waf knows of the +// client: its counters, its history, which holds its country as last looked +// up and its offences, and its bans with their notes. +func (h *handler) handleShowClient() http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + addr, err := pathAddress(r.URL.Path, ClientsPath) + if err != nil { + http.Error(w, err.Error(), http.StatusBadRequest) - return + return + } + + answer := clientAnswer{ + Bans: state.BanEntries(h.ledger.Covering(addr)), + } + + client, seen := h.limiter.Client(clientGroup(h.config, addr)) + if seen { + answer.Client = &client + } + + h.answerJSON(w, answer) } - - answer := clientAnswer{ - Bans: state.BanEntries(rq.h.ledger.Covering(addr)), - } - - client, seen := rq.h.limiter.Client(clientGroup(rq.h.config, addr)) - if seen { - answer.Client = &client - } - - rq.answerJSON(answer) } // pathAddress reads the client's address that follows prefix in path. @@ -316,27 +261,27 @@ func pathAddress(path, prefix string) (netip.Addr, error) { } // answerBans answers with held under bans, as bans.json lists them. -func (rq *request) answerBans(held []bans.Ban) { - rq.answerJSON(struct { +func (h *handler) answerBans(w http.ResponseWriter, held []bans.Ban) { + h.answerJSON(w, struct { Bans []state.BanEntry `json:"bans"` }{state.BanEntries(held)}) } // answerJSON answers with value as indented JSON. -func (rq *request) answerJSON(value any) { +func (h *handler) answerJSON(w http.ResponseWriter, value any) { body, err := json.MarshalIndent(value, "", " ") if err != nil { - rq.h.processLog.Error( + h.processLog.Error( "encoding an answer failed", "error", err.Error(), ) - http.Error(rq.out, http.StatusText(http.StatusInternalServerError), + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return } - rq.out.Header().Set("Content-Type", "application/json") - _, _ = rq.out.Write(append(body, '\n')) + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write(append(body, '\n')) } diff --git a/internal/waf/health_test.go b/internal/waf/health_test.go index 70d3532..258e79f 100644 --- a/internal/waf/health_test.go +++ b/internal/waf/health_test.go @@ -53,3 +53,14 @@ func TestHealthEndpointCountsTowardNoLimit(t *testing.T) { } } } + +func TestHealthEndpointIsFoundAtItsPathPercentEncoded(t *testing.T) { + t.Parallel() + + // %68 is h. An endpoint is found by the decoded path. + s, _, _ := startWithClock(t, "", nil) + + got := s.ownEndpointRequest("", http.MethodGet, "/_sww/%68ealthz", "", + http.StatusOK) + wantBody(t, got, "ok\n") +} diff --git a/internal/waf/proxy.go b/internal/waf/proxy.go index 966fe8b..88eddb6 100644 --- a/internal/waf/proxy.go +++ b/internal/waf/proxy.go @@ -141,7 +141,7 @@ func newHandler(params Params) *handler { panic(err) // New fails only for a size below one } - return &handler{ + h := &handler{ config: params.Config, requestLog: params.RequestLog, processLog: params.ProcessLog, @@ -172,6 +172,9 @@ func newHandler(params Params) *handler { besideLookupTimeLimit, ), } + h.ownEndpoints = h.routes() + + return h } // NewCoreRuleSet returns the Core Rule Set at SWW_WAF_CORE_RULE_SET_PARANOIA_LEVEL, @@ -237,4 +240,7 @@ type handler struct { // awaitingReport holds the decision on each answered question by its id // until its report comes, and nil for a question still being handled. awaitingReport *lru.Cache[string, *decision] + // ownEndpoints answers every request that is not a question, as routes + // lists the endpoints. + ownEndpoints http.Handler } diff --git a/internal/waf/routes.go b/internal/waf/routes.go new file mode 100644 index 0000000..a05533f --- /dev/null +++ b/internal/waf/routes.go @@ -0,0 +1,141 @@ +package waf + +import ( + "crypto/subtle" + "io" + "net/http" + "strings" + + "github.com/go-chi/chi/v5" + + "sneak.berlin/go/smallwebwaf/internal/requestlog" +) + +// routes returns the router of waf's own endpoints. A question never reaches +// it, since ServeHTTP answers a question first. No check runs on a request +// here: waf's address is on the private network, where the peers are rproxy +// and the admin's tools, not visitors. +func (h *handler) routes() http.Handler { + r := chi.NewRouter() + r.Use(matchDecodedPath) + + // The report writes no log line of its own: it finishes the line of the + // request it reports. + r.Post(ReportPath, h.handleReport()) + + r.Group(func(r chi.Router) { + r.Use(h.logOwnEndpoint) + // The answer to a path with no endpoint, and to a method the path has + // no endpoint for, is set in this group, so that it is logged too. + r.NotFound(answerNotFound) + r.MethodNotAllowed(answerNotFound) + + r.Get(HealthPath, handleHealth()) + + r.Group(func(r chi.Router) { + r.Use(requireToken(h.config.MetricsToken)) + r.Get(MetricsPath, h.metrics.ServeHTTP) + }) + + // The handlers under BansPath/ and ClientsPath read the address from + // the path themselves, so that one that is not an address gets 400 + // after the token check. + r.Group(func(r chi.Router) { + r.Use(requireToken(h.config.AdminToken)) + r.Get(BansPath, h.handleListBans()) + r.Post(BansPath, h.handleAddBan()) + r.Delete(BansPath+"/*", h.handleLiftBans()) + r.Get(ClientsPath+"*", h.handleShowClient()) + }) + }) + + return r +} + +// matchDecodedPath has the router match the decoded path, which the handlers +// read. chi matches the path as sent when it is encoded in an unusual way, +// which would make /_sww/%68ealthz a path with no endpoint. +func matchDecodedPath(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + chi.RouteContext(r.Context()).RoutePath = r.URL.Path + next.ServeHTTP(w, r) + }) +} + +// logOwnEndpoint writes one log line for each request, with the action +// own_endpoint. The line's request_bytes is how much of the body the handler +// read. +func (h *handler) logOwnEndpoint(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + peer := peerAddress(r) + + rq := h.startRequest(w, r, peer, peer, false) + defer rq.finish() + + rq.line.Action = requestlog.ActionOwnEndpoint + + counted := r.Clone(r.Context()) + counted.Body = &countedBody{ + ReadCloser: r.Body, + read: &rq.requestBytes, + } + + next.ServeHTTP(rq.out, counted) + }) +} + +// countedBody is a request's body that adds the bytes read of it to read. +type countedBody struct { + io.ReadCloser + + read *int64 +} + +// Read reads part of the body. +func (b *countedBody) Read(p []byte) (int, error) { + n, err := b.ReadCloser.Read(p) + *b.read += int64(n) + + return n, err +} + +// requireToken returns middleware that lets a request through only when it +// carries token, as Authorization: Bearer . It answers 401 to a request +// without it. While token is unset, it answers 404, as a path with no endpoint +// does. +func requireToken(token string) func(http.Handler) http.Handler { + return func(next http.Handler) http.Handler { + return http.HandlerFunc( + func(w http.ResponseWriter, r *http.Request) { + switch { + case token == "": + answerNotFound(w, r) + case !hasToken(r, token): + w.Header().Set("WWW-Authenticate", "Bearer") + http.Error( + w, + http.StatusText(http.StatusUnauthorized), + http.StatusUnauthorized, + ) + default: + next.ServeHTTP(w, r) + } + }, + ) + } +} + +// hasToken reports whether r carries token, as Authorization: Bearer +// . +func hasToken(r *http.Request, token string) bool { + scheme, sent, _ := strings.Cut(r.Header.Get("Authorization"), " ") + + return strings.EqualFold(scheme, "Bearer") && + subtle.ConstantTimeCompare([]byte(sent), []byte(token)) == 1 +} + +// answerNotFound answers 404, for a path with no endpoint, and for a method +// the path has no endpoint for. +func answerNotFound(w http.ResponseWriter, _ *http.Request) { + http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound) +} diff --git a/internal/waf/waf.go b/internal/waf/waf.go index ec8d9ea..d3b0407 100644 --- a/internal/waf/waf.go +++ b/internal/waf/waf.go @@ -132,8 +132,8 @@ func New(params Params) *Server { } // ServeHTTP handles one request to waf: a request carrying -// ClientHeader is rproxy's question, and any other is for waf -// itself. +// ClientHeader is rproxy's question, and any other is for one of waf's +// own endpoints. func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { if len(r.Header.Values(ClientHeader)) > 0 { h.answerQuestion(w, r) @@ -141,7 +141,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } - h.answerOwnEndpoint(w, r) + h.ownEndpoints.ServeHTTP(w, r) } // refusedWhateverTheAnswer reports whether the request is one that does not wait @@ -358,50 +358,29 @@ func (rq *request) deny(r refusal) { http.Error(rq.out, http.StatusText(r.status), r.status) } -// answerOwnEndpoint answers a request to waf's own address. No check -// runs on it: the address is on the private network, where the peers are -// rproxy and the admin's tools, not visitors. -func (h *handler) answerOwnEndpoint(w http.ResponseWriter, r *http.Request) { - if r.Method == http.MethodPost && r.URL.Path == ReportPath { - h.takeReport(w, r) - - return - } - - peer := peerAddress(r) - - rq := h.startRequest(w, r, peer, peer, false) - defer rq.finish() - - if r.Method == http.MethodGet && r.URL.Path == HealthPath { - rq.answerHealth() - - return - } - - rq.answerAdmin() -} - -// takeReport answers POST ReportPath: it finishes the request the report +// handleReport answers POST ReportPath: it finishes the request the report // names, if it holds that request's decision, and ignores the report // otherwise. -func (h *handler) takeReport(w http.ResponseWriter, r *http.Request) { - report, err := readReport(w, r) - if err != nil { - h.processLog.Warn("a report is refused", "error", err.Error()) - http.Error(w, "the body is not a report", http.StatusBadRequest) +func (h *handler) handleReport() http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + report, err := readReport(w, r) + if err != nil { + h.processLog.Warn("a report is refused", "error", err.Error()) + http.Error(w, "the body is not a report", http.StatusBadRequest) - return + return + } + + held, found := h.awaitingReport.Peek(report.RequestID) + // Remove reports true to one report only, of several that name the + // id. A question still being handled is held as nil. + if found && held != nil && + h.awaitingReport.Remove(report.RequestID) { + h.finishWith(r.Context(), held, report) + } + + w.WriteHeader(http.StatusNoContent) } - - held, found := h.awaitingReport.Peek(report.RequestID) - // Remove reports true to one report only, of several that name the id. A - // question still being handled is held as nil. - if found && held != nil && h.awaitingReport.Remove(report.RequestID) { - h.finishWith(r.Context(), held, report) - } - - w.WriteHeader(http.StatusNoContent) } // readReport reads a report from r's body, which must be one JSON object with