check / check (push) Waiting to run
internal/wafprotocol writes and reads the question, the answer and the report, and declares once each header, path and limit both daemons use. rproxy no longer imports internal/waf. Nothing on the wire changes. Model: opus-5-5
330 lines
9.5 KiB
Go
330 lines
9.5 KiB
Go
package waf
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"log/slog"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"sneak.berlin/go/smallwebwaf/internal/requestlog"
|
|
"sneak.berlin/go/smallwebwaf/internal/wafprotocol"
|
|
)
|
|
|
|
const (
|
|
// maxAwaitingReport is the most answered requests held until their
|
|
// reports come. Past it, the request answered longest ago is dropped.
|
|
maxAwaitingReport = 10000
|
|
// wafTimeout bounds how long a request to waf takes to
|
|
// arrive, and its answer to leave, and wafIdleTimeout how long a
|
|
// kept-open connection waits for its next request.
|
|
wafTimeout = 60 * time.Second
|
|
wafIdleTimeout = 120 * time.Second
|
|
)
|
|
|
|
// New returns the server `sww waf` runs. It answers
|
|
// rproxy's questions, takes rproxy's reports and answers its own endpoints. It
|
|
// passes nothing to an app. rproxy applies the size and time limits to the
|
|
// visitor. Go's default limit on a request's line and headers, 1 MiB, stays: it
|
|
// is far above rproxy's, and a question refused for its size would have rproxy
|
|
// let the request through.
|
|
func New(params Params) *Server {
|
|
h := newHandler(params)
|
|
|
|
return &Server{
|
|
Server: &http.Server{
|
|
Addr: params.Config.ListenAddr,
|
|
Handler: h,
|
|
ErrorLog: slog.NewLogLogger(
|
|
params.ProcessLog.Handler(),
|
|
slog.LevelWarn,
|
|
),
|
|
ReadTimeout: wafTimeout,
|
|
WriteTimeout: wafTimeout,
|
|
IdleTimeout: wafIdleTimeout,
|
|
},
|
|
Ledger: h.ledger,
|
|
Limiter: h.limiter,
|
|
GeoJS: h.geojs,
|
|
Anomalies: h.anomalies,
|
|
LookupFile: h.lookupFile,
|
|
Lists: h.lists,
|
|
DNSBL: h.dnsbl,
|
|
AbuseIPDB: h.abuseIPDB,
|
|
RouteWatch: h.routeWatch,
|
|
Metrics: h.metrics,
|
|
}
|
|
}
|
|
|
|
// ServeHTTP handles one request to waf: a request carrying
|
|
// wafprotocol.ClientHeader is rproxy's question, and any other is for waf
|
|
// itself.
|
|
func (h *handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
if len(r.Header.Values(wafprotocol.ClientHeader)) > 0 {
|
|
h.answerQuestion(w, r)
|
|
|
|
return
|
|
}
|
|
|
|
h.answerOwnEndpoint(w, r)
|
|
}
|
|
|
|
// refusedWhateverTheAnswer reports whether the request is one that does not wait
|
|
// for GeoJS, since the checks will refuse it at now whatever GeoJS answers about
|
|
// its client: the heuristic blocklists or rate_limits will refuse it, as a
|
|
// function in that heuristic's file says. In observe mode, which refuses
|
|
// nothing, none is. rproxy lets a request through when waf answers too late.
|
|
func (rq *request) refusedWhateverTheAnswer(now time.Time) bool {
|
|
if rq.h.config.Observe {
|
|
return false
|
|
}
|
|
|
|
return rq.refusedByBlocklists(rq.h.lists.ListedBy(rq.client)) ||
|
|
rq.overAWholeRateLimit(now)
|
|
}
|
|
|
|
// answerQuestion runs the checks on the visitor's request a question
|
|
// carries, and answers rproxy what to do with it. A denied request's counting
|
|
// is finished here, and an allowed one's by its report.
|
|
func (h *handler) answerQuestion(w http.ResponseWriter, r *http.Request) {
|
|
question, ok := h.readQuestion(w, r)
|
|
if !ok {
|
|
return
|
|
}
|
|
|
|
rq := h.startRequest(
|
|
w,
|
|
question.Visitor,
|
|
question.Client,
|
|
question.Peer,
|
|
true,
|
|
)
|
|
// These run even when a check panics. finish runs first, so that what is
|
|
// held has the finished log line. Go's server sends the answer only once
|
|
// this function has returned, so the report cannot come before it is held.
|
|
defer func() {
|
|
rq.finish()
|
|
rq.holdForReport()
|
|
}()
|
|
|
|
rq.site = r.Host
|
|
rq.line.WAFLine = requestlog.WAFLineDecision
|
|
refused := rq.check(r.Context())
|
|
rq.checked = time.Now()
|
|
underOwnPrefix := strings.HasPrefix(
|
|
r.URL.Path,
|
|
wafprotocol.OwnEndpointPrefix,
|
|
)
|
|
|
|
if refused == nil && !underOwnPrefix {
|
|
rq.allow()
|
|
// In observe mode, what enforce mode would have refused counts here.
|
|
rq.runBanningHeuristics(r.Context(), afterAnswer())
|
|
|
|
return
|
|
}
|
|
|
|
if refused != nil {
|
|
rq.deny(*refused)
|
|
} else {
|
|
// The app never sees this prefix. waf's own endpoints are at its
|
|
// own address, so this answers as a path with no endpoint does.
|
|
rq.line.Action = requestlog.ActionOwnEndpoint
|
|
wafprotocol.SetAnswerHeaders(
|
|
rq.out.Header(),
|
|
wafprotocol.Answer{Disposition: wafprotocol.DispositionDeny},
|
|
)
|
|
http.Error(
|
|
rq.out,
|
|
http.StatusText(http.StatusNotFound),
|
|
http.StatusNotFound,
|
|
)
|
|
}
|
|
|
|
rq.banAndCount(r.Context(), afterAnswer())
|
|
}
|
|
|
|
// readQuestion reads a question, and reports whether waf takes it. It answers
|
|
// a question it does not take with 400 and no disposition. rproxy passes that
|
|
// 400 to the visitor, and SWW_RPROXY_WAF_BACKEND_NO_ANSWER_DISPOSITION does
|
|
// not apply. The id of a question waf takes is held from here on, without a
|
|
// decision yet, so that another question with that id is refused while this
|
|
// one is being handled.
|
|
//
|
|
// A question whose body stops arriving gets no answer, and is not looked at.
|
|
func (h *handler) readQuestion(
|
|
w http.ResponseWriter, r *http.Request,
|
|
) (wafprotocol.Question, bool) {
|
|
question, err := wafprotocol.ReadQuestion(w, r)
|
|
if errors.Is(err, wafprotocol.ErrBodyStoppedArriving) {
|
|
h.processLog.Warn(
|
|
"a question's body stopped arriving",
|
|
"error",
|
|
err.Error(),
|
|
)
|
|
// This panic has Go's server close the connection without an answer,
|
|
// and log nothing.
|
|
panic(http.ErrAbortHandler)
|
|
}
|
|
|
|
if err != nil {
|
|
h.refuseQuestion(w, err.Error())
|
|
|
|
return question, false
|
|
}
|
|
|
|
held, _ := h.awaitingReport.ContainsOrAdd(question.RequestID, nil)
|
|
if held {
|
|
h.refuseQuestion(
|
|
w,
|
|
wafprotocol.RequestIDHeader+" is the id of a request waf holds",
|
|
)
|
|
|
|
return question, false
|
|
}
|
|
|
|
return question, true
|
|
}
|
|
|
|
// refuseQuestion answers a question waf does not take, for reason.
|
|
func (h *handler) refuseQuestion(w http.ResponseWriter, reason string) {
|
|
h.processLog.Warn("a question is refused", "reason", reason)
|
|
http.Error(w, reason, http.StatusBadRequest)
|
|
}
|
|
|
|
// allow answers a question with wafprotocol.DispositionAllow and, while
|
|
// SWW_WAF_ADD_LOOKUP_HEADERS is true, the client's AS number and country.
|
|
func (rq *request) allow() {
|
|
rq.allowed = true
|
|
|
|
answer := wafprotocol.Answer{Disposition: wafprotocol.DispositionAllow}
|
|
if rq.h.config.AddLookupHeaders {
|
|
answer.ASN = rq.line.ASN
|
|
answer.Country = rq.line.Country
|
|
}
|
|
|
|
wafprotocol.SetAnswerHeaders(rq.out.Header(), answer)
|
|
rq.out.WriteHeader(http.StatusOK)
|
|
}
|
|
|
|
// holdForReport holds a copy of what the report's steps need, by the request's
|
|
// id. A report only ever reaches that copy, never the request being handled.
|
|
func (rq *request) holdForReport() {
|
|
held := rq.decision
|
|
rq.h.awaitingReport.Add(rq.line.RequestID, &held)
|
|
}
|
|
|
|
// deny answers a question with what the visitor is to get for r.
|
|
func (rq *request) deny(r refusal) {
|
|
rq.refused = &r
|
|
|
|
if r.status == 0 {
|
|
// SWW_WAF_BAN_RESPONSE is close. Written past rq.out, since the log line's
|
|
// status stays 0: the visitor is sent nothing.
|
|
wafprotocol.SetAnswerHeaders(
|
|
rq.out.Header(),
|
|
wafprotocol.Answer{Disposition: wafprotocol.DispositionClose},
|
|
)
|
|
rq.out.ResponseWriter.WriteHeader(http.StatusOK)
|
|
|
|
return
|
|
}
|
|
|
|
wafprotocol.SetAnswerHeaders(
|
|
rq.out.Header(),
|
|
wafprotocol.Answer{Disposition: wafprotocol.DispositionDeny},
|
|
)
|
|
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 == wafprotocol.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 == 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
|
|
// request's last log line, which holds its decision's fields and the report's.
|
|
// For an allowed request it first runs the heuristics of onReport, and adds the
|
|
// request to its client's history, the anomaly counters and the metrics, with
|
|
// the status and bytes the report gives. A denied request's counting was
|
|
// finished at its decision. ctx is the report's own context.
|
|
func (h *handler) finishWith(
|
|
ctx context.Context,
|
|
held *decision,
|
|
report wafprotocol.Report,
|
|
) {
|
|
rq := &request{
|
|
decision: *held,
|
|
h: h,
|
|
out: &responseWriter{
|
|
status: report.Status,
|
|
bytes: report.ResponseBytes,
|
|
},
|
|
requestBytes: report.RequestBytes,
|
|
}
|
|
|
|
rq.line.WAFLine = requestlog.WAFLineReport
|
|
rq.line.Status = report.Status
|
|
rq.line.RequestBytes = report.RequestBytes
|
|
rq.line.ResponseBytes = report.ResponseBytes
|
|
rq.line.DurationTotalMS = report.DurationTotalMS
|
|
|
|
if rq.allowed {
|
|
rq.line.DurationUpstreamTotalMS = &report.DurationUpstreamTotalMS
|
|
|
|
rq.banAndCount(ctx, onReport())
|
|
|
|
rq.h.metrics.RequestReported(
|
|
&rq.line,
|
|
time.Duration(report.DurationTotalMS*float64(time.Millisecond)),
|
|
time.Duration(
|
|
report.DurationUpstreamTotalMS*float64(time.Millisecond),
|
|
),
|
|
)
|
|
}
|
|
|
|
rq.writeLine()
|
|
}
|