package wafprotocol import ( "bytes" "context" "errors" "fmt" "io" "net/http" "net/netip" "net/url" "strconv" "unicode/utf8" ) // maxRequestIDBytes is the longest id a question may give. const maxRequestIDBytes = 128 // ErrBodyStoppedArriving is the error ReadQuestion wraps when the question's // body stops arriving. Every other error it returns is why waf refuses the // question. var ErrBodyStoppedArriving = errors.New("the body stopped arriving") // Why waf refuses a question. Each is also the body of the refusal. var ( errClientNotAnAddress = errors.New( ClientHeader + " is not an address without a zone", ) errRequestIDNotValid = errors.New(RequestIDHeader + " is not a valid id") errBodyTooLarge = errors.New("the body is over 64 KiB") ) // QuestionParams are what NewQuestion needs. type QuestionParams struct { // Visitor is the visitor's request. Visitor *http.Request // PathAsSent is Visitor's path as the visitor sent it, which the question // sends unchanged. PathAsSent string // Body is the part of Visitor's body the question carries, at most // QuestionBodyMaxBytes. Body []byte // WAFBackend is waf's address. The question takes its scheme and host. WAFBackend *url.URL // Client is the visitor's address, and Peer that of rproxy's own TCP // peer. Client, Peer netip.Addr // RequestID is the request's id. RequestID string // Scheme is how the visitor reached rproxy, or the trusted proxy in front // of it. Scheme string } // Question is a question as waf reads it. type Question struct { // Client is the visitor's address. Client netip.Addr // Peer is the address of rproxy's own TCP peer. It is not valid when the // question gives none that can be read. Peer netip.Addr // RequestID is the request's id. RequestID string // Visitor is the visitor's request, as asTheVisitorSentIt gives it. Visitor *http.Request } // NewQuestion returns the question about a visitor's request, as "The // question" in SPEC.md gives it: the request as the visitor sent it, with the // part of the body it carries and rproxy's headers. func NewQuestion( ctx context.Context, params QuestionParams, ) (*http.Request, error) { in := params.Visitor question, err := http.NewRequestWithContext( ctx, in.Method, params.WAFBackend.String(), bytes.NewReader(params.Body), ) if err != nil { return nil, fmt.Errorf("make the question: %w", err) } backend := question.URL target := *in.URL target.Scheme = backend.Scheme target.Host = backend.Host target.Opaque = params.PathAsSent question.URL = &target question.Host = in.Host // The length NewRequestWithContext set is that of the body carried. This // line makes a body whose length the visitor did not announce go chunked, // as "The question" in SPEC.md requires. question.TransferEncoding = in.TransferEncoding question.Header = in.Header.Clone() question.Header.Set(ClientHeader, params.Client.String()) question.Header.Set(PeerHeader, params.Peer.String()) question.Header.Del(ContentLengthHeader) announced := in.Header.Get("Content-Length") if announced != "" { question.Header.Set(ContentLengthHeader, announced) } question.Header.Set(RequestIDHeader, params.RequestID) question.Header.Set("X-Forwarded-Proto", params.Scheme) // Go's client adds a User-Agent of its own to a request without the key. _, sent := question.Header["User-Agent"] if !sent { question.Header["User-Agent"] = nil } return question, nil } // ReadQuestion reads the question r, with the body it carries. An error // wrapping ErrBodyStoppedArriving means the body stopped arriving. Any other // error is why waf refuses the question, and the question's body is then not // read. The body is read whole, since Go's server does not send an answer // until it has read the body. func ReadQuestion(w http.ResponseWriter, r *http.Request) (Question, error) { client, err := netip.ParseAddr(r.Header.Get(ClientHeader)) if err != nil || client.Zone() != "" { return Question{}, errClientNotAnAddress } id := r.Header.Get(RequestIDHeader) if id == "" || len(id) > maxRequestIDBytes || !utf8.ValidString(id) { return Question{}, errRequestIDNotValid } body, err := io.ReadAll( http.MaxBytesReader(w, r.Body, QuestionBodyMaxBytes), ) if _, tooLarge := errors.AsType[*http.MaxBytesError](err); tooLarge { return Question{}, errBodyTooLarge } if err != nil { return Question{}, fmt.Errorf("%w: %w", ErrBodyStoppedArriving, err) } // One that cannot be read leaves the log line's peer_ip invalid. peer, _ := netip.ParseAddr(r.Header.Get(PeerHeader)) return Question{ Client: client.Unmap(), Peer: peer.Unmap(), RequestID: id, Visitor: asTheVisitorSentIt(r, body), }, nil } // asTheVisitorSentIt returns a copy of r, a question, as the checks and the // log line are to see it: without the headers rproxy adds for waf, // with body, which has been read of r, and with the length the visitor // announced, as -1 when it announced none. The question's own length covers // only the part of the body it carries. func asTheVisitorSentIt(r *http.Request, body []byte) *http.Request { sent := r.Clone(r.Context()) announced := sent.Header.Get(ContentLengthHeader) sent.Header.Del(ClientHeader) sent.Header.Del(PeerHeader) sent.Header.Del(ContentLengthHeader) sent.Header.Del("Content-Length") sent.ContentLength = -1 length, err := strconv.ParseInt(announced, 10, 64) if err == nil && length >= 0 { sent.ContentLength = length sent.Header.Set("Content-Length", announced) } sent.Body = http.NoBody if len(body) > 0 { sent.Body = io.NopCloser(bytes.NewReader(body)) } return sent }