waf: list its own endpoints on a chi router in routes.go (closes #426) #459
No files matched your search
@@ -74,9 +74,9 @@ never as a time.
|
|||||||
`go.uber.org/fx`. fx builds the parts of `waf` and `rproxy`, and starts
|
`go.uber.org/fx`. fx builds the parts of `waf` and `rproxy`, and starts
|
||||||
and stops their background work.
|
and stops their background work.
|
||||||
- `internal/config`: reads the settings, the one place they are read.
|
- `internal/config`: reads the settings, the one place they are read.
|
||||||
- `internal/waf`: `waf`'s server. It runs the checks in `check`, and answers
|
- `internal/waf`: `waf`'s server. It runs the checks in `check`. Its
|
||||||
`waf`'s own endpoints in `answerOwnEndpoint`. It counts bytes, refusals
|
`routes.go` lists `waf`'s own endpoints. It counts bytes, refusals and
|
||||||
and anomalies, and writes the log lines.
|
anomalies, and writes the log lines.
|
||||||
- `internal/rproxy`: the reverse proxy in front of one app that asks `waf`
|
- `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
|
about each request. It passes a request to the app with the standard
|
||||||
library's `httputil.ReverseProxy`, within the time and size limits.
|
library's `httputil.ReverseProxy`, within the time and size limits.
|
||||||
@@ -128,6 +128,7 @@ never as a time.
|
|||||||
default `SWW_WAF_CORE_RULE_SET_DISABLED_RULES`, are built for that
|
default `SWW_WAF_CORE_RULE_SET_DISABLED_RULES`, are built for that
|
||||||
version. They are checked again before the version changes, since rule
|
version. They are checked again before the version changes, since rule
|
||||||
ids, and what each rule matches, change between versions;
|
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/hashicorp/golang-lru/v2` for the bounded tables;
|
||||||
- `github.com/oschwald/maxminddb-golang/v2` to read the lookup database;
|
- `github.com/oschwald/maxminddb-golang/v2` to read the lookup database;
|
||||||
- `github.com/fsnotify/fsnotify` to notice edited files;
|
- `github.com/fsnotify/fsnotify` to notice edited files;
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ require (
|
|||||||
github.com/corazawaf/coraza-coreruleset/v4 v4.25.0
|
github.com/corazawaf/coraza-coreruleset/v4 v4.25.0
|
||||||
github.com/corazawaf/coraza/v3 v3.8.2-0.20261022102553-d028be399c68
|
github.com/corazawaf/coraza/v3 v3.8.2-0.20261022102553-d028be399c68
|
||||||
github.com/fsnotify/fsnotify v1.10.1
|
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/hashicorp/golang-lru/v2 v2.0.7
|
||||||
github.com/maxmind/mmdbwriter v1.2.0
|
github.com/maxmind/mmdbwriter v1.2.0
|
||||||
github.com/oschwald/maxminddb-golang/v2 v2.7.0
|
github.com/oschwald/maxminddb-golang/v2 v2.7.0
|
||||||
|
|||||||
@@ -21,6 +21,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/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 h1:b0/UzAf9yR5rhf3RPm9gf3ehBPpf0oZKIjtpKrx59Ho=
|
||||||
github.com/fsnotify/fsnotify v1.10.1/go.mod h1:TLheqan6HD6GBK6PrDWyDPBaEV8LspOxvPSjC+bVfgo=
|
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 h1:EBsztssimR/CONLSZZ04E8qAkxNYq4Qp9LvH92wZUgs=
|
||||||
github.com/go-viper/mapstructure/v2 v2.4.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
|
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=
|
github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4=
|
||||||
|
|||||||
+92
-147
@@ -2,7 +2,6 @@ package waf
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"crypto/subtle"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -15,7 +14,6 @@ import (
|
|||||||
"sneak.berlin/go/smallwebwaf/internal/bans"
|
"sneak.berlin/go/smallwebwaf/internal/bans"
|
||||||
"sneak.berlin/go/smallwebwaf/internal/config"
|
"sneak.berlin/go/smallwebwaf/internal/config"
|
||||||
"sneak.berlin/go/smallwebwaf/internal/ratelimit"
|
"sneak.berlin/go/smallwebwaf/internal/ratelimit"
|
||||||
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
|
||||||
"sneak.berlin/go/smallwebwaf/internal/state"
|
"sneak.berlin/go/smallwebwaf/internal/state"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -41,80 +39,23 @@ var (
|
|||||||
errNotAddress = errors.New("is not an address, such as 203.0.113.9")
|
errNotAddress = errors.New("is not an address, such as 203.0.113.9")
|
||||||
)
|
)
|
||||||
|
|
||||||
// answerHealth answers a request for the health endpoint.
|
// handleHealth answers GET wafprotocol.HealthPath.
|
||||||
func (rq *request) answerHealth() {
|
func handleHealth() http.HandlerFunc {
|
||||||
rq.line.Action = requestlog.ActionOwnEndpoint
|
return func(w http.ResponseWriter, _ *http.Request) {
|
||||||
// Set here rather than left to Go's server, which would set it only
|
// Set here rather than left to Go's server, which would set it only
|
||||||
// after the log line has taken the response's headers.
|
// after the log line has taken the response's headers.
|
||||||
rq.out.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||||||
_, _ = io.WriteString(rq.out, "ok\n")
|
_, _ = io.WriteString(w, "ok\n")
|
||||||
}
|
|
||||||
|
|
||||||
// answerAdmin answers a request for one of waf's own endpoints. Each
|
|
||||||
// needs a token, sent as Authorization: Bearer <token>: 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()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// endpoint returns the token the request's endpoint needs, and what
|
// handleListBans answers GET BansPath with every ban held.
|
||||||
// answers the request there; "" when there is no such endpoint.
|
func (h *handler) handleListBans() http.HandlerFunc {
|
||||||
func (rq *request) endpoint() (string, func()) {
|
return func(w http.ResponseWriter, _ *http.Request) {
|
||||||
cfg := rq.h.config
|
h.answerBans(w, h.ledger.Snapshot())
|
||||||
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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// hasToken reports whether r carries token, as Authorization: Bearer
|
|
||||||
// <token>.
|
|
||||||
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.
|
// banToAdd is the body of POST BansPath.
|
||||||
type banToAdd struct {
|
type banToAdd struct {
|
||||||
// Netblock is a netblock, or a client's address, which stands for the
|
// Netblock is a netblock, or a client's address, which stands for the
|
||||||
@@ -126,44 +67,44 @@ type banToAdd struct {
|
|||||||
Reason string `json:"reason"`
|
Reason string `json:"reason"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// addBan answers POST BansPath: it bans the netblock the body names, as
|
// handleAddBan answers POST BansPath: it bans the netblock the body names,
|
||||||
// an admin, from now for the duration the body gives, with its reason,
|
// as an admin, from now for the duration the body gives, with its reason,
|
||||||
// and answers with that ban.
|
// and answers with that ban.
|
||||||
func (rq *request) addBan() {
|
func (h *handler) handleAddBan() http.HandlerFunc {
|
||||||
toAdd, err := rq.readBanToAdd()
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
toAdd, err := readBanToAdd(r)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
netblock netip.Prefix
|
netblock netip.Prefix
|
||||||
expires time.Time
|
expires time.Time
|
||||||
now = rq.h.now()
|
now = h.now()
|
||||||
)
|
)
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
netblock, err = rq.h.parseBanNetblock(toAdd.Netblock)
|
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
|
// readBanToAdd reads the body of POST BansPath: a JSON object with
|
||||||
// nothing but whitespace after it, in at most banBodyMaxBytes.
|
// 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(
|
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 {
|
if err != nil {
|
||||||
return banToAdd{}, fmt.Errorf("%w: %w", errNotBanToAdd, err)
|
return banToAdd{}, fmt.Errorf("%w: %w", errNotBanToAdd, err)
|
||||||
}
|
}
|
||||||
@@ -250,29 +191,31 @@ func expiry(duration string, now time.Time) (time.Time, error) {
|
|||||||
return now.Add(length), nil
|
return now.Add(length), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// liftBans answers DELETE BansPath/<client>: it lifts every ban active on
|
// handleLiftBans answers DELETE BansPath/<client>: it lifts every ban
|
||||||
// a netblock the client's address is in, and answers with those bans, or
|
// active on a netblock the client's address is in, and answers with those
|
||||||
// with 404 when none is active.
|
// bans, or with 404 when none is active.
|
||||||
func (rq *request) liftBans() {
|
func (h *handler) handleLiftBans() http.HandlerFunc {
|
||||||
client, err := pathAddress(rq.in.URL.Path, BansPath+"/")
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
if err != nil {
|
client, err := pathAddress(r.URL.Path, BansPath+"/")
|
||||||
http.Error(rq.out, err.Error(), http.StatusBadRequest)
|
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<ip>: the client the
|
// clientAnswer is the answer to GET ClientsPath<ip>: the client the
|
||||||
@@ -284,29 +227,31 @@ type clientAnswer struct {
|
|||||||
Bans []state.BanEntry `json:"bans"`
|
Bans []state.BanEntry `json:"bans"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// showClient answers GET ClientsPath<ip> with what waf knows of the client:
|
// handleShowClient answers GET ClientsPath<ip> with what waf knows of the
|
||||||
// its counters, its history, which holds its country as last looked up and its
|
// client: its counters, its history, which holds its country as last looked
|
||||||
// offences, and its bans with their notes.
|
// up and its offences, and its bans with their notes.
|
||||||
func (rq *request) showClient() {
|
func (h *handler) handleShowClient() http.HandlerFunc {
|
||||||
addr, err := pathAddress(rq.in.URL.Path, ClientsPath)
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
if err != nil {
|
addr, err := pathAddress(r.URL.Path, ClientsPath)
|
||||||
http.Error(rq.out, err.Error(), http.StatusBadRequest)
|
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.IPv6GroupPrefix, 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.IPv6GroupPrefix, addr),
|
|
||||||
)
|
|
||||||
if seen {
|
|
||||||
answer.Client = &client
|
|
||||||
}
|
|
||||||
|
|
||||||
rq.answerJSON(answer)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// pathAddress reads the client's address that follows prefix in path.
|
// pathAddress reads the client's address that follows prefix in path.
|
||||||
@@ -322,27 +267,27 @@ func pathAddress(path, prefix string) (netip.Addr, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// answerBans answers with held under bans, as bans.json lists them.
|
// answerBans answers with held under bans, as bans.json lists them.
|
||||||
func (rq *request) answerBans(held []bans.Ban) {
|
func (h *handler) answerBans(w http.ResponseWriter, held []bans.Ban) {
|
||||||
rq.answerJSON(struct {
|
h.answerJSON(w, struct {
|
||||||
Bans []state.BanEntry `json:"bans"`
|
Bans []state.BanEntry `json:"bans"`
|
||||||
}{state.BanEntries(held)})
|
}{state.BanEntries(held)})
|
||||||
}
|
}
|
||||||
|
|
||||||
// answerJSON answers with value as indented JSON.
|
// 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, "", " ")
|
body, err := json.MarshalIndent(value, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
rq.h.processLog.Error(
|
h.processLog.Error(
|
||||||
"encoding an answer failed",
|
"encoding an answer failed",
|
||||||
"error",
|
"error",
|
||||||
err.Error(),
|
err.Error(),
|
||||||
)
|
)
|
||||||
http.Error(rq.out, http.StatusText(http.StatusInternalServerError),
|
http.Error(w, http.StatusText(http.StatusInternalServerError),
|
||||||
http.StatusInternalServerError)
|
http.StatusInternalServerError)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
rq.out.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
_, _ = rq.out.Write(append(body, '\n'))
|
_, _ = w.Write(append(body, '\n'))
|
||||||
}
|
}
|
||||||
@@ -14,6 +14,7 @@ import (
|
|||||||
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
||||||
"sneak.berlin/go/smallwebwaf/internal/state"
|
"sneak.berlin/go/smallwebwaf/internal/state"
|
||||||
"sneak.berlin/go/smallwebwaf/internal/waf"
|
"sneak.berlin/go/smallwebwaf/internal/waf"
|
||||||
|
"sneak.berlin/go/smallwebwaf/internal/wafprotocol"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -333,6 +334,39 @@ func TestBanToAddThatCannotBeReadIsRefused(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBanToAddIsLoggedWithTheBytesOfItsBody(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
s, _, _ := startWithClock(
|
||||||
|
t,
|
||||||
|
"",
|
||||||
|
map[string]string{adminToken: adminSecret},
|
||||||
|
)
|
||||||
|
|
||||||
|
s.admin(http.MethodPost, waf.BansPath, banOtherClient, http.StatusOK)
|
||||||
|
|
||||||
|
line := s.out.endedLines(t, s.sent)[s.sent-1]
|
||||||
|
if line.RequestBytes != int64(len(banOtherClient)) {
|
||||||
|
t.Errorf(
|
||||||
|
"the ban to add was logged with request_bytes %d, want %d",
|
||||||
|
line.RequestBytes,
|
||||||
|
len(banOtherClient),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
// The health endpoint reads none of a body sent to it.
|
||||||
|
s.ownEndpointRequest("", http.MethodGet, wafprotocol.HealthPath, "ok",
|
||||||
|
http.StatusOK)
|
||||||
|
|
||||||
|
line = s.out.endedLines(t, s.sent)[s.sent-1]
|
||||||
|
if line.RequestBytes != 0 {
|
||||||
|
t.Errorf(
|
||||||
|
"the health request was logged with request_bytes %d, want 0",
|
||||||
|
line.RequestBytes,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestClientEndpointShowsTheClientAndItsBans(t *testing.T) {
|
func TestClientEndpointShowsTheClientAndItsBans(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
|||||||
@@ -58,3 +58,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")
|
||||||
|
}
|
||||||
@@ -268,6 +268,8 @@ func newHandler(params Params) *handler {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
h.ownEndpoints = h.routes()
|
||||||
|
|
||||||
return h
|
return h
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -293,6 +295,9 @@ type handler struct {
|
|||||||
// awaitingReport holds the decision on each answered question by its id
|
// awaitingReport holds the decision on each answered question by its id
|
||||||
// until its report comes, and nil for a question still being handled.
|
// until its report comes, and nil for a question still being handled.
|
||||||
awaitingReport *lru.Cache[string, *decision]
|
awaitingReport *lru.Cache[string, *decision]
|
||||||
|
// ownEndpoints answers every request that is not a question, as routes
|
||||||
|
// lists the endpoints.
|
||||||
|
ownEndpoints http.Handler
|
||||||
|
|
||||||
// The lists of heuristics, each in the order its heuristics run. A client
|
// The lists of heuristics, each in the order its heuristics run. A client
|
||||||
// in SWW_WAF_ALLOW_NETS skips them all.
|
// in SWW_WAF_ALLOW_NETS skips them all.
|
||||||
|
|||||||
@@ -0,0 +1,142 @@
|
|||||||
|
package waf
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/subtle"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/go-chi/chi/v5"
|
||||||
|
|
||||||
|
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
||||||
|
"sneak.berlin/go/smallwebwaf/internal/wafprotocol"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 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(wafprotocol.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(wafprotocol.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 <token>. 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
|
||||||
|
// <token>.
|
||||||
|
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)
|
||||||
|
}
|
||||||
+24
-45
@@ -58,8 +58,8 @@ func New(params Params) *Server {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ServeHTTP handles one request to waf: a request carrying
|
// ServeHTTP handles one request to waf: a request carrying
|
||||||
// wafprotocol.ClientHeader is rproxy's question, and any other is for waf
|
// wafprotocol.ClientHeader is rproxy's question, and any other is for one of
|
||||||
// itself.
|
// waf's own endpoints.
|
||||||
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
if len(r.Header.Values(wafprotocol.ClientHeader)) > 0 {
|
if len(r.Header.Values(wafprotocol.ClientHeader)) > 0 {
|
||||||
h.answerQuestion(w, r)
|
h.answerQuestion(w, r)
|
||||||
@@ -67,7 +67,7 @@ func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
h.answerOwnEndpoint(w, r)
|
h.ownEndpoints.ServeHTTP(w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
// refusedWhateverTheAnswer reports whether the request is one that does not wait
|
// refusedWhateverTheAnswer reports whether the request is one that does not wait
|
||||||
@@ -240,50 +240,29 @@ func (rq *request) deny(r refusal) {
|
|||||||
http.Error(rq.out, http.StatusText(r.status), r.status)
|
http.Error(rq.out, http.StatusText(r.status), r.status)
|
||||||
}
|
}
|
||||||
|
|
||||||
// answerOwnEndpoint answers a request to waf's own address. No check
|
// handleReport answers POST wafprotocol.ReportPath: it finishes the request
|
||||||
// runs on it: the address is on the private network, where the peers are
|
// the report names, if it holds that request's decision, and ignores the
|
||||||
// rproxy and the admin's tools, not visitors.
|
// report otherwise.
|
||||||
func (h *handler) answerOwnEndpoint(w http.ResponseWriter, r *http.Request) {
|
func (h *handler) handleReport() http.HandlerFunc {
|
||||||
if r.Method == http.MethodPost && r.URL.Path == wafprotocol.ReportPath {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
h.takeReport(w, r)
|
report, err := wafprotocol.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)
|
||||||
}
|
}
|
||||||
|
|
||||||
peer := peerAddress(r)
|
|
||||||
|
|
||||||
rq := h.startRequest(w, r, peer, peer, false)
|
|
||||||
defer rq.finish()
|
|
||||||
|
|
||||||
if r.Method == http.MethodGet && r.URL.Path == wafprotocol.HealthPath {
|
|
||||||
rq.answerHealth()
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
rq.answerAdmin()
|
|
||||||
}
|
|
||||||
|
|
||||||
// takeReport answers POST wafprotocol.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 := wafprotocol.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
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// finishWith finishes a request with what its report says, and writes the
|
// finishWith finishes a request with what its report says, and writes the
|
||||||
|
|||||||
Reference in new issue
Block a user