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
124 lines
3.8 KiB
Go
124 lines
3.8 KiB
Go
package wafprotocol
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
)
|
|
|
|
const (
|
|
// reportBodyMaxBytes is the most of a report's body that is read. It is
|
|
// far above any report's.
|
|
reportBodyMaxBytes = 1 << 20
|
|
// maxReportedBytes is the most bytes a report may give for each way, 1
|
|
// PiB, so that adding them up cannot overflow.
|
|
maxReportedBytes = 1 << 50
|
|
// maxReportedMilliseconds is the longest duration a report may give,
|
|
// about 31 years, so that it fits a time.Duration.
|
|
maxReportedMilliseconds = 1e12
|
|
)
|
|
|
|
var (
|
|
errReportFieldMissing = errors.New("a field is missing")
|
|
errReportValue = errors.New("a value is out of range")
|
|
)
|
|
|
|
// Report is the JSON object rproxy posts to ReportPath once the visitor's
|
|
// answer has ended, its fields named as the request log's of the same
|
|
// meaning. RequestID is the question's id. Status is what the visitor got, 0
|
|
// if nothing. The durations are in milliseconds.
|
|
//
|
|
//nolint:tagliatelle // docs/request-log.md names the fields in snake_case
|
|
type Report struct {
|
|
RequestID string `json:"request_id"`
|
|
Status int `json:"status"`
|
|
RequestBytes int64 `json:"request_bytes"`
|
|
ResponseBytes int64 `json:"response_bytes"`
|
|
DurationTotalMS float64 `json:"duration_total_ms"`
|
|
DurationUpstreamTotalMS float64 `json:"duration_upstream_total_ms"`
|
|
}
|
|
|
|
// NewReportRequest returns the request that posts report to reportURL, which
|
|
// is waf's ReportPath.
|
|
func NewReportRequest(
|
|
ctx context.Context,
|
|
reportURL string,
|
|
report *Report,
|
|
) (*http.Request, error) {
|
|
body, err := json.Marshal(report)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("encode the report: %w", err)
|
|
}
|
|
|
|
req, err := http.NewRequestWithContext(
|
|
ctx, http.MethodPost, reportURL, bytes.NewReader(body))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("make the request: %w", err)
|
|
}
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
return req, nil
|
|
}
|
|
|
|
// ReadReport reads a report from r's body, which must be one JSON object with
|
|
// all of Report's fields, in the ranges SPEC.md gives.
|
|
func ReadReport(w http.ResponseWriter, r *http.Request) (Report, error) {
|
|
var report Report
|
|
|
|
body, err := io.ReadAll(
|
|
http.MaxBytesReader(w, r.Body, reportBodyMaxBytes),
|
|
)
|
|
if err != nil {
|
|
return report, fmt.Errorf("read the body: %w", err)
|
|
}
|
|
|
|
var fields map[string]json.RawMessage
|
|
|
|
err = json.Unmarshal(body, &fields)
|
|
if err != nil {
|
|
return report, fmt.Errorf("decode the body: %w", err)
|
|
}
|
|
|
|
for _, name := range []string{
|
|
"request_id", "status", "request_bytes", "response_bytes",
|
|
"duration_total_ms", "duration_upstream_total_ms",
|
|
} {
|
|
value, there := fields[name]
|
|
if !there || string(value) == "null" {
|
|
return report, fmt.Errorf("%w: %s", errReportFieldMissing, name)
|
|
}
|
|
}
|
|
|
|
err = json.Unmarshal(body, &report)
|
|
if err != nil {
|
|
return report, fmt.Errorf("decode the body: %w", err)
|
|
}
|
|
|
|
return report, report.checkRanges()
|
|
}
|
|
|
|
// checkRanges returns an error for a value of the report that cannot be true.
|
|
func (report *Report) checkRanges() error {
|
|
switch {
|
|
case report.Status != 0 &&
|
|
(report.Status < http.StatusContinue || report.Status > ReportStatusMax):
|
|
return fmt.Errorf("%w: status", errReportValue)
|
|
case report.RequestBytes < 0 || report.RequestBytes > maxReportedBytes:
|
|
return fmt.Errorf("%w: request_bytes", errReportValue)
|
|
case report.ResponseBytes < 0 || report.ResponseBytes > maxReportedBytes:
|
|
return fmt.Errorf("%w: response_bytes", errReportValue)
|
|
case report.DurationTotalMS < 0 || report.DurationTotalMS > maxReportedMilliseconds:
|
|
return fmt.Errorf("%w: duration_total_ms", errReportValue)
|
|
case report.DurationUpstreamTotalMS < 0 ||
|
|
report.DurationUpstreamTotalMS > maxReportedMilliseconds:
|
|
return fmt.Errorf("%w: duration_upstream_total_ms", errReportValue)
|
|
}
|
|
|
|
return nil
|
|
}
|