package waf import ( "context" "errors" "log/slog" "net" "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 { s := &Server{h: newHandler(params)} s.httpServer = &http.Server{ Addr: params.Config.ListenAddr, Handler: s, ErrorLog: slog.NewLogLogger( params.ProcessLog.Handler(), slog.LevelWarn, ), ReadTimeout: wafTimeout, WriteTimeout: wafTimeout, 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 // wafprotocol.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(wafprotocol.ClientHeader)) > 0 { h.answerQuestion(w, r) return } h.ownEndpoints.ServeHTTP(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: a heuristic of afterLookup that implements refusesWithoutLookup // says it will refuse it. 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 } for _, each := range rq.h.afterLookup { knows, ok := each.(refusesWithoutLookup) if ok && knows.refusesWhateverTheLookupAnswers(rq, now) { return true } } return false } // 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(), rq.h.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(), rq.h.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.lookupAnswer.ASN answer.Country = rq.lookupAnswer.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) } // handleReport 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) handleReport() http.HandlerFunc { return func(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, rq.h.onReport) rq.h.metrics.RequestReported( &rq.line, time.Duration(report.DurationTotalMS*float64(time.Millisecond)), time.Duration( report.DurationUpstreamTotalMS*float64(time.Millisecond), ), ) } rq.writeLine() }