From 12cf53479dd67ac67a0e37c2e4c188ae9448f6ab Mon Sep 17 00:00:00 2001 From: Nirmal Khedkar Date: Fri, 6 Nov 2026 14:59:30 -0800 Subject: [PATCH] fix: bound consumer API JSON request bodies Read complete request bodies within configurable byte budgets before any write handler decodes or mutates state. Reject declared and streamed overflow with HTTP 413, including excess bytes after a valid signed object. Preserve accepted bytes and close the original body before dispatch. Default structural requests to 64 KiB and narrative claims to 1 MiB. Expose positive deployment overrides through cloudyd flags and an explicit server constructor. Document the deployment boundary and conformance scope. Cover all ten write routes, exact boundaries, real chunked HTTP, signed registration and claims, read limits, and read/close failures. Race-enabled Go tests, build, vet, formatting and Qlty baseline comparison pass. Signed-off-by: Nirmal Khedkar --- CONFORMANCE.md | 10 ++ README.md | 6 + cmd/cloudyd/main.go | 5 +- cmd/cloudyd/request_limits_test.go | 34 ++++ docs/consumer-request-limits.md | 60 +++++++ internal/consumerapi/request_limits.go | 64 +++++++ .../request_limits_boundary_test.go | 120 +++++++++++++ .../consumerapi/request_limits_http_test.go | 65 +++++++ .../consumerapi/request_limits_io_test.go | 107 ++++++++++++ internal/consumerapi/request_limits_test.go | 162 ++++++++++++++++++ internal/consumerapi/server.go | 44 ++--- 11 files changed, 655 insertions(+), 22 deletions(-) create mode 100644 cmd/cloudyd/request_limits_test.go create mode 100644 docs/consumer-request-limits.md create mode 100644 internal/consumerapi/request_limits.go create mode 100644 internal/consumerapi/request_limits_boundary_test.go create mode 100644 internal/consumerapi/request_limits_http_test.go create mode 100644 internal/consumerapi/request_limits_io_test.go create mode 100644 internal/consumerapi/request_limits_test.go diff --git a/CONFORMANCE.md b/CONFORMANCE.md index 6f2272a..4cf9fa8 100644 --- a/CONFORMANCE.md +++ b/CONFORMANCE.md @@ -77,3 +77,13 @@ signed-field tampering. The existing summary response remains unchanged. This proves signed authorship, not truth or independent witnessing; the Technology Tree remains its documented unwitnessed single-writer stand-in. See `docs/signed-claim-export.md` for the format and verification boundaries. + +The consumer API applies configurable deployment byte limits before all ten JSON +write handlers. `internal/consumerapi/request_limits.go` bounds the complete +encoded body before signature validation or any store mutation, while preserving +accepted signed bytes and the existing commons/Locker separation. The HTTP and +signed-artifact regressions in `request_limits_test.go` and +`request_limits_boundary_test.go`, `request_limits_http_test.go` and +`request_limits_io_test.go` bind this resource boundary. It is not a wire, +settlement or covenant rule; public reads and `/v0/` relay traffic retain their +existing contracts. See `docs/consumer-request-limits.md` for defaults and scope. diff --git a/README.md b/README.md index a0358b1..5ab737f 100644 --- a/README.md +++ b/README.md @@ -165,3 +165,9 @@ independent witnessing. AGPL-3.0-or-later. *Network Theory Applied Research Institute, Inc. — 501(c)(3) — EIN 92-3047136 — info@ntari.org* + +## Consumer API deployment limits + +The member-facing JSON write routes enforce configurable body limits before +decoding or mutation. See [consumer request limits](docs/consumer-request-limits.md) +for defaults, daemon flags and compatibility boundaries. diff --git a/cmd/cloudyd/main.go b/cmd/cloudyd/main.go index 0d2cf77..4640920 100644 --- a/cmd/cloudyd/main.go +++ b/cmd/cloudyd/main.go @@ -33,6 +33,9 @@ import ( func main() { addr := flag.String("addr", ":8088", "listen address for the consumer JSON API") platform := flag.String("platform", "cloudy", "platform identity (scopes member ids and every artifact)") + limits := consumerapi.DefaultRequestLimits() + flag.Int64Var(&limits.StructuredBytes, "request-bytes", limits.StructuredBytes, "maximum encoded JSON body bytes for structural consumer API writes") + flag.Int64Var(&limits.ClaimBytes, "claim-request-bytes", limits.ClaimBytes, "maximum encoded JSON body bytes for claims including member-local narrative") var rc relayConfig flag.StringVar(&rc.Orchestrator, "orchestrator", "", "orchestrator base URL; when set, relay members' nodes to it (decision 0001)") flag.StringVar(&rc.OperatorID, "operator-id", "", "operator id this frontend enrolled under (cloudy-enroll -slug)") @@ -42,7 +45,7 @@ func main() { flag.DurationVar(&rc.Poll, "relay-poll", 0, "poll the orchestrator at most once per node per this interval (default 30s)") flag.Parse() - srv, err := consumerapi.NewServer(*platform) + srv, err := consumerapi.NewServerWithLimits(*platform, limits) if err != nil { log.Fatalf("cloudyd: constructing server: %v", err) } diff --git a/cmd/cloudyd/request_limits_test.go b/cmd/cloudyd/request_limits_test.go new file mode 100644 index 0000000..97f422c --- /dev/null +++ b/cmd/cloudyd/request_limits_test.go @@ -0,0 +1,34 @@ +package main + +import ( + "bytes" + "context" + "os/exec" + "testing" + "time" +) + +func TestRequestLimitFlagsAreAvailable(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + defer cancel() + output, err := exec.CommandContext(ctx, "go", "run", ".", "-help").CombinedOutput() + if err != nil { + t.Fatalf("help failed: %v\n%s", err, output) + } + for _, flag := range []string{"-request-bytes", "-claim-request-bytes"} { + if !bytes.Contains(output, []byte(flag)) { + t.Errorf("help does not expose %s\n%s", flag, output) + } + } +} + +func TestRequestLimitFlagsRejectInvalidConfiguration(t *testing.T) { + for _, args := range [][]string{{"-request-bytes", "0"}, {"-claim-request-bytes", "-1"}} { + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + output, err := exec.CommandContext(ctx, "go", append([]string{"run", "."}, args...)...).CombinedOutput() + cancel() + if err == nil || !bytes.Contains(output, []byte("request byte limits must be positive")) { + t.Errorf("invalid limits %v: err=%v output=%s", args, err, output) + } + } +} diff --git a/docs/consumer-request-limits.md b/docs/consumer-request-limits.md new file mode 100644 index 0000000..eba73d0 --- /dev/null +++ b/docs/consumer-request-limits.md @@ -0,0 +1,60 @@ +# Consumer API request limits + +The member-facing JSON write routes enforce a complete encoded-body byte limit +before JSON decoding, signature checks or store mutation. An oversized body +receives HTTP 413 with a JSON error. This applies with `Content-Length` and with +chunked or otherwise unknown-length bodies, including excess whitespace after a +valid signed object. The boundary is inclusive: a body exactly at the limit +continues to the existing validation and signature checks. + +| POST routes | Default encoded bytes | Daemon flag | +| --- | ---: | --- | +| `/api/v1/claims` | 1,048,576 (1 MiB) | `-claim-request-bytes` | +| `/api/v1/members`, `/api/v1/references`, `/api/v1/market/listings`, `/api/v1/drops`, `/api/v1/credit/spends`, `/api/v1/assessments`, `/api/v1/assessments/{id}/answers`, `/api/v1/disputes`, `/api/v1/disputes/{id}/withdraw` | 65,536 (64 KiB) | `-request-bytes` | + +Structural requests consist of keys, hashes, signatures, timestamps and labels. +The existing signed request fixtures fit well below the structural budget. Claims +also transport three arbitrary member-local narrative strings, so their default +budget is larger. Neither class has an existing domain-wide maximum: these are +adjustable frontend deployment budgets, not consensus rules, new enrollment +conditions, fees or limits on the canonical signed artifacts. + +For a deployment that needs a larger narrative envelope: + +```sh +go run ./cmd/cloudyd -request-bytes 65536 -claim-request-bytes 2097152 +``` + +Both settings must be positive; zero or a negative value fails daemon startup. +Callers embedding the API may pass `consumerapi.RequestLimits` to +`consumerapi.NewServerWithLimits`. Existing `NewServer` callers use +`consumerapi.DefaultRequestLimits` without a source change. Configure a server +before exposing its handler; the settings are immutable during requests. + +The count includes the whole encoded JSON envelope, escapes, ignored fields and +trailing bytes, not merely decoded narrative lengths. HTTP chunk framing is not +part of the body count. No request decompression is added. The guard reads at +most the configured limit plus one byte to detect an unknown-length overflow; +a declared oversized body is rejected without an application read. The original +body is closed on success, overflow and read failure. A body read or close failure returns +HTTP 400 without dispatching the write handler. + +Accepted bytes are replayed unchanged to the existing decoder. Existing JSON +field handling and signature verification remain intact; this change adds no +end-of-document rule for an in-bounds body. Signatures, public structural reads, +member-local narrative separation and the escrow policy keep their existing +meaning. These controls apply only to the ten consumer JSON write routes, not +node relay `/v0/` traffic, unrelated witness daemons or response sizes. + +Memory use is bounded per request, not across simultaneous requests. HTTP timeouts, +request concurrency limits and reverse-proxy policies remain separate deployment +concerns. Stores and operator keys retain their existing in-memory/ephemeral +limitations; request bounds do not make this a durable deployment. + +Verification lives in `internal/consumerapi/request_limits_test.go`, +`internal/consumerapi/request_limits_boundary_test.go`, +`internal/consumerapi/request_limits_http_test.go`, +`internal/consumerapi/request_limits_io_test.go` and +`cmd/cloudyd/request_limits_test.go`. Tests cover every mounted JSON write route, +known/unknown lengths, real chunked HTTP, exact boundaries, signed registration +and narrative claims, unchanged stores on rejection, read bounds and body cleanup. diff --git a/internal/consumerapi/request_limits.go b/internal/consumerapi/request_limits.go new file mode 100644 index 0000000..e9e2973 --- /dev/null +++ b/internal/consumerapi/request_limits.go @@ -0,0 +1,64 @@ +package consumerapi + +import ( + "bytes" + "errors" + "io" + "net/http" +) + +// RequestLimits bounds encoded JSON bytes at this frontend's HTTP ingress. +// These are deployment controls, not limits on signed artifacts in the commons. +type RequestLimits struct { + StructuredBytes int64 + ClaimBytes int64 +} + +// DefaultRequestLimits allows larger claim bodies for member-local narrative. +// Structural requests carry keys, hashes, signatures and short labels instead. +func DefaultRequestLimits() RequestLimits { + return RequestLimits{StructuredBytes: 64 << 10, ClaimBytes: 1 << 20} +} + +// NewServerWithLimits constructs the same member-facing composition as NewServer +// with explicit positive HTTP body limits. It does not alter signed data. +func NewServerWithLimits(platform string, limits RequestLimits) (*Server, error) { + if limits.StructuredBytes <= 0 || limits.ClaimBytes <= 0 { + return nil, errors.New("consumerapi: request byte limits must be positive") + } + s, err := NewServer(platform) + if err != nil { + return nil, err + } + s.requestLimits = limits + return s, nil +} + +// limitRequest reads the entire bounded body before a handler can mutate state. +// Merely bounding Decode would miss excess bytes after its first JSON value. +func limitRequest(limit int64, next http.HandlerFunc) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + body, err := readRequestBody(w, r, limit) + if err != nil { + var oversized *http.MaxBytesError + if errors.As(err, &oversized) { + writeErr(w, http.StatusRequestEntityTooLarge, "request body exceeds configured byte limit") + } else { + writeErr(w, http.StatusBadRequest, "invalid JSON body") + } + return + } + r.Body = io.NopCloser(bytes.NewReader(body)) + next(w, r) + } +} + +// readRequestBody closes the original body before any write handler runs. +func readRequestBody(w http.ResponseWriter, r *http.Request, limit int64) ([]byte, error) { + bounded := http.MaxBytesReader(w, r.Body, limit) + if r.ContentLength > limit { + return nil, errors.Join(&http.MaxBytesError{Limit: limit}, bounded.Close()) + } + body, err := io.ReadAll(bounded) + return body, errors.Join(err, bounded.Close()) +} diff --git a/internal/consumerapi/request_limits_boundary_test.go b/internal/consumerapi/request_limits_boundary_test.go new file mode 100644 index 0000000..4aa0e9e --- /dev/null +++ b/internal/consumerapi/request_limits_boundary_test.go @@ -0,0 +1,120 @@ +package consumerapi + +import ( + "bytes" + "net/http" + "net/http/httptest" + "reflect" + "testing" +) + +func configuredRequestServer(t *testing.T) *Server { + t.Helper() + s, err := NewServerWithLimits(platform, RequestLimits{StructuredBytes: 1024, ClaimBytes: 2048}) + if err != nil { + t.Fatal(err) + } + return s +} + +func TestRequestLimitsConfiguredBoundaries(t *testing.T) { + for _, path := range jsonWritePaths { + for _, extra := range []int{-1, 0, 1} { + for _, declared := range []bool{true, false} { + checkConfiguredBoundary(t, path, extra, declared) + } + } + } +} + +func checkConfiguredBoundary(t *testing.T, path string, extra int, declared bool) { + t.Helper() + limit := 1024 + if path == "/api/v1/claims" { + limit = 2048 + } + s := configuredRequestServer(t) + body := append([]byte("{}"), bytes.Repeat([]byte(" "), limit+extra-2)...) + req := httptest.NewRequest(http.MethodPost, path, bytes.NewReader(body)) + if !declared { + req.ContentLength = -1 + } + rec := httptest.NewRecorder() + s.Handler().ServeHTTP(rec, req) + want := http.StatusBadRequest + if extra > 0 { + want = http.StatusRequestEntityTooLarge + } + if rec.Code != want { + t.Errorf("%s size=%d declared=%v: got %d, want %d", path, len(body), declared, rec.Code, want) + } +} + +func TestRequestLimitsSignedRegistrationBoundary(t *testing.T) { + for _, extra := range []int{-1, 0, 1} { + for _, declared := range []bool{true, false} { + checkSignedRegistrationBoundary(t, extra, declared) + } + } +} + +func checkSignedRegistrationBoundary(t *testing.T, extra int, declared bool) { + t.Helper() + s := configuredRequestServer(t) + before := ingressState(t, s) + body := signedRegistration(t, s) + body = append(body, bytes.Repeat([]byte(" "), 1024+extra-len(body))...) + req := httptest.NewRequest(http.MethodPost, memberWritePath, bytes.NewReader(body)) + if !declared { + req.ContentLength = -1 + } + rec := httptest.NewRecorder() + s.Handler().ServeHTTP(rec, req) + want := http.StatusOK + if extra > 0 { + want = http.StatusRequestEntityTooLarge + } + if rec.Code != want { + t.Errorf("extra=%d declared=%v got %d, want %d", extra, declared, rec.Code, want) + } + if extra > 0 && !reflect.DeepEqual(before, ingressState(t, s)) { + t.Error("overflow mutated members") + } +} + +func TestRequestLimitsSignedClaimBoundary(t *testing.T) { + for _, extra := range []int{-1, 0, 1} { + s, err := NewServerWithLimits(platform, RequestLimits{1024, 2048}) + if err != nil { + t.Fatal(err) + } + h := s.Handler() + pub, priv := key(94) + registerMember(t, h, pub, priv) + narrative := "member-local narrative with escaped characters: \n\t and café" + c, body := signedClaim(t, pub, priv, narrative) + body = append(body, bytes.Repeat([]byte(" "), 2048+extra-len(body))...) + before := ingressState(t, s) + req := httptest.NewRequest(http.MethodPost, "/api/v1/claims", bytes.NewReader(body)) + req.ContentLength = -1 + rec := httptest.NewRecorder() + h.ServeHTTP(rec, req) + if extra > 0 { + if rec.Code != http.StatusRequestEntityTooLarge || !reflect.DeepEqual(before, ingressState(t, s)) { + t.Fatalf("overflow changed state or got %d", rec.Code) + } + continue + } + if rec.Code != http.StatusCreated { + t.Fatalf("got %d: %s", rec.Code, rec.Body.String()) + } + id := c.ID() + get, view := do(t, h, http.MethodGet, "/api/v1/claims/"+hx(id[:]), nil) + if get.Code != http.StatusOK { + t.Fatalf("stored signed claim unreadable: %d", get.Code) + } + if view["narrative"].(map[string]any)["inputs"] != narrative { + t.Error("signed narrative changed") + } + } +} diff --git a/internal/consumerapi/request_limits_http_test.go b/internal/consumerapi/request_limits_http_test.go new file mode 100644 index 0000000..38b92e2 --- /dev/null +++ b/internal/consumerapi/request_limits_http_test.go @@ -0,0 +1,65 @@ +package consumerapi + +import ( + "bytes" + "encoding/json" + "io" + "net/http" + "net/http/httptest" + "testing" +) + +func TestRequestLimitsRealHTTP(t *testing.T) { + for _, extra := range []int{0, 1} { + for _, chunked := range []bool{false, true} { + checkRealRequestBoundary(t, extra, chunked) + } + } +} + +func checkRealRequestBoundary(t *testing.T, extra int, chunked bool) { + t.Helper() + s := configuredRequestServer(t) + server := httptest.NewServer(s.Handler()) + defer server.Close() + body := signedRegistration(t, s) + body = append(body, bytes.Repeat([]byte(" "), 1024+extra-len(body))...) + req, err := http.NewRequest(http.MethodPost, server.URL+memberWritePath, bytes.NewReader(body)) + if err != nil { + t.Fatal(err) + } + if chunked { + req.ContentLength = -1 + req.TransferEncoding = []string{"chunked"} + } + response, err := server.Client().Do(req) + if err != nil { + t.Fatal(err) + } + data, err := io.ReadAll(response.Body) + closeErr := response.Body.Close() + if err != nil { + t.Fatal(err) + } + if closeErr != nil { + t.Fatal(closeErr) + } + want := http.StatusOK + if extra > 0 { + want = http.StatusRequestEntityTooLarge + } + if response.StatusCode != want || !json.Valid(data) { + t.Errorf("extra=%d chunked=%v: status=%d body=%s", extra, chunked, response.StatusCode, data) + } +} + +func TestRequestLimitsInvalidConfiguration(t *testing.T) { + for _, limits := range []RequestLimits{{0, 1024}, {1024, 0}, {-1, 1024}, {1024, -1}} { + if _, err := NewServerWithLimits(platform, limits); err == nil { + t.Errorf("accepted invalid limits %+v", limits) + } + } + if _, err := NewServerWithLimits("", RequestLimits{1024, 2048}); err == nil { + t.Error("accepted empty platform") + } +} diff --git a/internal/consumerapi/request_limits_io_test.go b/internal/consumerapi/request_limits_io_test.go new file mode 100644 index 0000000..9ca5350 --- /dev/null +++ b/internal/consumerapi/request_limits_io_test.go @@ -0,0 +1,107 @@ +package consumerapi + +import ( + "bytes" + "errors" + "io" + "net/http" + "net/http/httptest" + "reflect" + "testing" +) + +type observedRequestBody struct { + io.Reader + read int + closed bool + closeErr error +} + +func (b *observedRequestBody) Read(p []byte) (int, error) { + n, err := b.Reader.Read(p) + b.read += n + return n, err +} +func (b *observedRequestBody) Close() error { b.closed = true; return b.closeErr } + +type failingRequestReader struct{} + +func (failingRequestReader) Read([]byte) (int, error) { return 0, errors.New("body unavailable") } + +func TestRequestLimitsBoundReadsAndCloseBody(t *testing.T) { + for _, declared := range []bool{true, false} { + s, err := NewServerWithLimits(platform, RequestLimits{1024, 2048}) + if err != nil { + t.Fatal(err) + } + body := &observedRequestBody{Reader: io.LimitReader(&endlessSpaces{}, 1<<30)} + req := httptest.NewRequest(http.MethodPost, memberWritePath, body) + req.ContentLength = -1 + if declared { + req.ContentLength = 1 << 30 + } + rec := httptest.NewRecorder() + s.Handler().ServeHTTP(rec, req) + if rec.Code != http.StatusRequestEntityTooLarge { + t.Errorf("got %d want 413", rec.Code) + } + maxRead := 1025 + if declared { + maxRead = 0 + } + if body.read > maxRead { + t.Errorf("consumed %d bytes, maximum %d", body.read, maxRead) + } + if !body.closed { + t.Error("rejected body not closed") + } + } +} + +type endlessSpaces struct{} + +func (*endlessSpaces) Read(p []byte) (int, error) { + for i := range p { + p[i] = ' ' + } + return len(p), nil +} + +func TestRequestLimitsReadFailureDoesNotRegister(t *testing.T) { + s, err := NewServerWithLimits(platform, RequestLimits{1024, 2048}) + if err != nil { + t.Fatal(err) + } + body := &observedRequestBody{Reader: io.MultiReader(bytes.NewReader(signedRegistration(t, s)), failingRequestReader{})} + req := httptest.NewRequest(http.MethodPost, memberWritePath, body) + before := ingressState(t, s) + rec := httptest.NewRecorder() + s.Handler().ServeHTTP(rec, req) + if rec.Code != http.StatusBadRequest { + t.Errorf("got %d, want 400", rec.Code) + } + if !body.closed { + t.Error("failed body not closed") + } + if !reflect.DeepEqual(before, ingressState(t, s)) { + t.Error("partial body registered member") + } +} + +func TestRequestLimitsCloseFailureDoesNotRegister(t *testing.T) { + s, err := NewServerWithLimits(platform, RequestLimits{1024, 2048}) + if err != nil { + t.Fatal(err) + } + body := &observedRequestBody{Reader: bytes.NewReader(signedRegistration(t, s)), closeErr: errors.New("close failed")} + req := httptest.NewRequest(http.MethodPost, memberWritePath, body) + before := ingressState(t, s) + rec := httptest.NewRecorder() + s.Handler().ServeHTTP(rec, req) + if rec.Code != http.StatusBadRequest { + t.Errorf("got %d want 400", rec.Code) + } + if !reflect.DeepEqual(before, ingressState(t, s)) { + t.Error("close failure registered member") + } +} diff --git a/internal/consumerapi/request_limits_test.go b/internal/consumerapi/request_limits_test.go new file mode 100644 index 0000000..5a215f7 --- /dev/null +++ b/internal/consumerapi/request_limits_test.go @@ -0,0 +1,162 @@ +package consumerapi + +import ( + "bytes" + "crypto/ed25519" + "encoding/hex" + "encoding/json" + "fmt" + "net/http" + "net/http/httptest" + "reflect" + "strings" + "testing" + "time" + + "github.com/NTARI-RAND/Cloudy/internal/record" + "github.com/NTARI-RAND/Cloudy/internal/techtree" +) + +const memberWritePath = "/api/v1/members" + +var jsonWritePaths = []string{ + memberWritePath, "/api/v1/claims", "/api/v1/references", + "/api/v1/market/listings", "/api/v1/drops", "/api/v1/credit/spends", + "/api/v1/assessments", "/api/v1/assessments/" + strings.Repeat("0", 64) + "/answers", + "/api/v1/disputes", "/api/v1/disputes/" + strings.Repeat("0", 64) + "/withdraw", +} + +// An omitted route guard would return a domain/JSON error instead of 413. +func TestRequestLimitsEveryJSONWrite(t *testing.T) { + payload := []byte(`{"unused":"` + strings.Repeat("x", 2<<20) + `"}`) + for _, path := range jsonWritePaths { + for _, declared := range []bool{true, false} { + t.Run(fmt.Sprintf("%s/declared=%v", path, declared), func(t *testing.T) { + checkOversizedWrite(t, path, declared, payload) + }) + } + } +} + +func checkOversizedWrite(t *testing.T, path string, declared bool, payload []byte) { + t.Helper() + s, err := NewServer(platform) + if err != nil { + t.Fatal(err) + } + before := ingressState(t, s) + req := httptest.NewRequest(http.MethodPost, path, bytes.NewReader(payload)) + if !declared { + req.ContentLength = -1 + req.TransferEncoding = []string{"chunked"} + } + rec := httptest.NewRecorder() + s.Handler().ServeHTTP(rec, req) + if rec.Code != http.StatusRequestEntityTooLarge { + t.Errorf("oversized write: got %d, want 413: %s", rec.Code, rec.Body.String()) + } + if rec.Header().Get("Content-Type") != "application/json" || !json.Valid(rec.Body.Bytes()) { + t.Errorf("error is not JSON: %s", rec.Body.String()) + } + if after := ingressState(t, s); !reflect.DeepEqual(before, after) { + t.Errorf("rejected request mutated stores: before %v, after %v", before, after) + } +} + +func ingressState(t *testing.T, s *Server) []any { + t.Helper() + records, err := s.ecoStore.All() + if err != nil { + t.Fatal(err) + } + recordCount, err := s.recStore.Len() + if err != nil { + t.Fatal(err) + } + lifecycleCount, err := s.lifeStore.Len() + if err != nil { + t.Fatal(err) + } + return []any{len(s.byMember), len(s.byAccount), s.tree.Head(), s.catalog.Head(), + recordCount, lifecycleCount, len(records), len(s.manifest), len(s.disputesByExchange)} +} + +func signedRegistration(t *testing.T, s *Server) []byte { + t.Helper() + pub, priv := key(91) + body, err := json.Marshal(registerRequest{PublicKey: hex.EncodeToString(pub), + Signature: hex.EncodeToString(ed25519.Sign(priv, s.registerChallenge(pub)))}) + if err != nil { + t.Fatal(err) + } + return body +} + +// Reading only the first JSON value would admit this real signed registration. +func TestRequestLimitsRejectSignedRegistrationWithOversizedSuffix(t *testing.T) { + for _, declared := range []bool{true, false} { + s, err := NewServer(platform) + if err != nil { + t.Fatal(err) + } + before := ingressState(t, s) + body := append(signedRegistration(t, s), bytes.Repeat([]byte(" "), 2<<20)...) + req := httptest.NewRequest(http.MethodPost, memberWritePath, bytes.NewReader(body)) + if !declared { + req.ContentLength = -1 + } + rec := httptest.NewRecorder() + s.Handler().ServeHTTP(rec, req) + if rec.Code != http.StatusRequestEntityTooLarge { + t.Errorf("got %d, want 413", rec.Code) + } + if !reflect.DeepEqual(before, ingressState(t, s)) { + t.Error("oversized registration changed member stores") + } + } +} + +func signedClaim(t *testing.T, pub ed25519.PublicKey, priv ed25519.PrivateKey, narrative string) (techtree.Claim, []byte) { + t.Helper() + c, err := techtree.NewClaim(platform, pub, techtree.KindFact, techtree.HashNarrative([]byte(narrative)), + techtree.HashNarrative(nil), techtree.HashNarrative(nil), time.Now().UTC()) + if err != nil { + t.Fatal(err) + } + if err = c.Sign(priv); err != nil { + t.Fatal(err) + } + body, err := json.Marshal(claimDTO{Platform: c.Platform, Claimant: hx(c.Claimant), Kind: string(c.Kind), + InputsHash: hx(c.InputsHash[:]), MethodHash: hx(c.MethodHash[:]), ResultHash: hx(c.ResultHash[:]), + Nonce: hx(c.Nonce[:]), AssertedAtNs: c.AssertedAt.UnixNano(), Signature: hx(c.Signature), Inputs: narrative}) + if err != nil { + t.Fatal(err) + } + return c, body +} + +func TestRequestLimitsRejectSignedClaimBeforeLockerWrite(t *testing.T) { + s, err := NewServer(platform) + if err != nil { + t.Fatal(err) + } + h := s.Handler() + pub, priv := key(92) + registerMember(t, h, pub, priv) + narrative := strings.Repeat("n", 2<<20) + _, body := signedClaim(t, pub, priv, narrative) + before := ingressState(t, s) + req := httptest.NewRequest(http.MethodPost, "/api/v1/claims", bytes.NewReader(body)) + req.ContentLength = -1 + rec := httptest.NewRecorder() + h.ServeHTTP(rec, req) + if rec.Code != http.StatusRequestEntityTooLarge { + t.Errorf("got %d, want 413: %s", rec.Code, rec.Body.String()) + } + if !reflect.DeepEqual(before, ingressState(t, s)) { + t.Error("oversized claim changed stores") + } + if _, exists := s.locker.Get(record.HashContent([]byte(narrative))); exists { + t.Error("rejected narrative entered Locker") + } +} diff --git a/internal/consumerapi/server.go b/internal/consumerapi/server.go index 8e2cc97..b9b98c5 100644 --- a/internal/consumerapi/server.go +++ b/internal/consumerapi/server.go @@ -60,7 +60,8 @@ const domainRegister = "cloudy/consumerapi/register/v0" // safe for concurrent use — a single mutex guards the in-memory stores and the // member-local narrative manifest, which is sufficient for slice 1. type Server struct { - platform string + platform string + requestLimits RequestLimits // The operator's ephemeral record-log keypair. The operator legitimately // holds its own checkpoint-signing key — what it can never hold is a key @@ -268,16 +269,17 @@ func NewServer(platform string) (*Server, error) { return nil, err } s := &Server{ - platform: platform, - operatorPub: operatorPub, - operatorPriv: operatorPriv, - byMember: make(map[covenant.MemberID]ed25519.PublicKey), - byAccount: make(map[economy.AccountID]ed25519.PublicKey), - tree: tree, - catalog: catalog, - locker: record.NewMemLocker(), - index: make(map[record.Hash]uint64), - manifest: make(map[string]narrativeRefs), + platform: platform, + requestLimits: DefaultRequestLimits(), + operatorPub: operatorPub, + operatorPriv: operatorPriv, + byMember: make(map[covenant.MemberID]ed25519.PublicKey), + byAccount: make(map[economy.AccountID]ed25519.PublicKey), + tree: tree, + catalog: catalog, + locker: record.NewMemLocker(), + index: make(map[record.Hash]uint64), + manifest: make(map[string]narrativeRefs), } s.recStore = record.NewMemStore() s.opLog, err = record.OpenLog(operatorPub, s.recStore) @@ -329,30 +331,30 @@ func NewServer(platform string) (*Server, error) { // Handler returns the mounted HTTP router for the API. func (s *Server) Handler() http.Handler { mux := http.NewServeMux() - mux.HandleFunc("POST /api/v1/members", s.handleRegister) + mux.HandleFunc("POST /api/v1/members", limitRequest(s.requestLimits.StructuredBytes, s.handleRegister)) mux.HandleFunc("GET /api/v1/members/{id}/standing", s.handleStanding) - mux.HandleFunc("POST /api/v1/claims", s.handleAnchorClaim) + mux.HandleFunc("POST /api/v1/claims", limitRequest(s.requestLimits.ClaimBytes, s.handleAnchorClaim)) mux.HandleFunc("GET /api/v1/claims/{id}", s.handleGetClaim) mux.HandleFunc("GET /api/v1/claims/{id}/export", s.handleExportClaim) - mux.HandleFunc("POST /api/v1/references", s.handleAddReference) + mux.HandleFunc("POST /api/v1/references", limitRequest(s.requestLimits.StructuredBytes, s.handleAddReference)) mux.HandleFunc("GET /api/v1/market/categories", s.handleCategories) - mux.HandleFunc("POST /api/v1/market/listings", s.handleCreateListing) + mux.HandleFunc("POST /api/v1/market/listings", limitRequest(s.requestLimits.StructuredBytes, s.handleCreateListing)) mux.HandleFunc("GET /api/v1/market/listings", s.handleBrowseListings) mux.HandleFunc("GET /api/v1/market/listings/{id}", s.handleGetListing) mux.HandleFunc("GET /api/v1/drops/log", s.handleDropsLog) mux.HandleFunc("GET /api/v1/drops/checkpoints", s.handleDropsCheckpoints) - mux.HandleFunc("POST /api/v1/drops", s.handleAppendDrop) + mux.HandleFunc("POST /api/v1/drops", limitRequest(s.requestLimits.StructuredBytes, s.handleAppendDrop)) mux.HandleFunc("GET /api/v1/drops/{id}", s.handleGetDrop) mux.HandleFunc("GET /api/v1/credit/policy", s.handleCreditPolicy) mux.HandleFunc("GET /api/v1/credit/accounts/{id}/balance", s.handleBalance) mux.HandleFunc("GET /api/v1/credit/accounts/{id}/history", s.handleHistory) - mux.HandleFunc("POST /api/v1/credit/spends", s.handlePostSpend) - mux.HandleFunc("POST /api/v1/assessments", s.handleRecordAssessment) - mux.HandleFunc("POST /api/v1/assessments/{id}/answers", s.handleAnswerAssessment) + mux.HandleFunc("POST /api/v1/credit/spends", limitRequest(s.requestLimits.StructuredBytes, s.handlePostSpend)) + mux.HandleFunc("POST /api/v1/assessments", limitRequest(s.requestLimits.StructuredBytes, s.handleRecordAssessment)) + mux.HandleFunc("POST /api/v1/assessments/{id}/answers", limitRequest(s.requestLimits.StructuredBytes, s.handleAnswerAssessment)) mux.HandleFunc("GET /api/v1/assessments/{id}/answer", s.handleGetAnswer) - mux.HandleFunc("POST /api/v1/disputes", s.handleOpenDispute) + mux.HandleFunc("POST /api/v1/disputes", limitRequest(s.requestLimits.StructuredBytes, s.handleOpenDispute)) mux.HandleFunc("GET /api/v1/disputes/{id}", s.handleGetDispute) - mux.HandleFunc("POST /api/v1/disputes/{id}/withdraw", s.handleWithdrawDispute) + mux.HandleFunc("POST /api/v1/disputes/{id}/withdraw", limitRequest(s.requestLimits.StructuredBytes, s.handleWithdrawDispute)) mux.HandleFunc("GET /api/v1/lifecycle/checkpoints", s.handleLifecycleCheckpoints) mux.HandleFunc("GET /api/v1/drops/checkpoints/consistency", s.handleDropsConsistency) mux.HandleFunc("GET /api/v1/lifecycle/checkpoints/consistency", s.handleLifecycleConsistency)