Bound the response body before decoding JSON: read through io.LimitReader with a budget of maxBytes + 1, reject when that extra byte arrives, and pass only an in-budget body to json.Unmarshal. The extra byte distinguishes an exact fit from an oversized response without buffering the full response. A correct patch also checks the HTTP status, closes the body on every path, and gives the request a deadline.
Last reviewed: 2026-11-20 (Asia/Shanghai).
Put the limit at the body read
A Content-Length check is a useful early rejection, but it is not a body-size gate. The value can be unknown, as with a streamed response, and a client cannot assume that a header proves how many bytes its reader will receive. The Go HTTP package documentation
describes response bodies as streams read on demand and documents that the body must be closed. Read the body through a limit even when a header is present.
The standard library’s io.LimitReader
returns a reader that stops with EOF after the requested number of bytes. If you give it exactly the allowed limit and then parse what it returned, a valid JSON value at the front of an oversized body can look complete. For example, a JSON object followed by enough whitespace may still decode when the reader truncates the rest. Reading one byte beyond the allowance makes that case observable: maxBytes + 1 bytes means reject; at most maxBytes followed by EOF means the body fit.
Pick a conservative cap from the response contract, not from a convenient default. A 256 KiB allowance might suit a small settings object, while a large catalogue may need pagination or a different endpoint. Account for concurrent requests as well as the per-request limit: 200 concurrent reads with a 1 MiB cap can retain roughly 200 MiB of body bytes before decoded objects and other request state. The decoded object can use substantially more memory than its JSON representation, so the byte cap is one control in a wider budget.
Read, reject, then decode
Keep the helper narrowly scoped to a destination the application has already authorized. The example checks a successful status and returns a sentinel error for the size violation. A request context controls the request and response lifecycle; the calling service should also configure a client timeout appropriate for its own overall policy. The HTTP package documents that Client.Do does not turn a non-2xx status into an error and that Client.Timeout includes reading the response body.
var ErrResponseTooLarge = errors.New("response body exceeds configured limit")
const maxInt64 = 1<<63 - 1
func GetJSON(ctx context.Context, client *http.Client, endpoint string, maxBytes int64, dst any) error {
if client == nil {
return errors.New("HTTP client is required")
}
if maxBytes < 1 || maxBytes == maxInt64 {
return errors.New("invalid response size limit")
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return err
}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices {
return fmt.Errorf("unexpected HTTP status: %s", resp.Status)
}
body, err := io.ReadAll(io.LimitReader(resp.Body, maxBytes+1))
if err != nil {
return err
}
if int64(len(body)) > maxBytes {
return ErrResponseTooLarge
}
return json.Unmarshal(body, dst)
}
These imports are required in the source file: context, encoding/json, errors, fmt, io, and net/http. The configured limit must be a realistic positive value; avoiding maxInt64 also prevents overflow when adding one. ReadAll is bounded to at most one byte beyond the limit and reports read errors instead of turning partial data into success. The deferred close covers success, status errors, read errors, oversized bodies, and JSON errors.
The example lets json.Unmarshal decide whether the entire admitted byte slice is valid JSON. Add schema-specific validation after unmarshalling if the application needs required fields, enum checks, or rejection of unknown fields. Do not treat valid syntax as proof that the response satisfies the application contract.
Test the boundary, not the header
Use httptest to exercise the helper with deterministic bodies. This test makes the success case exactly 16 bytes and sends the overflow case as a chunked body. The second body begins with valid JSON and adds whitespace until it is one byte over budget. A check that reads only 16 bytes could incorrectly accept that prefix; the limit-plus-one check must reject it.
func TestGetJSONBodyLimit(t *testing.T) {
const maxBytes = 16
valid := `{"ok":true}`
cases := []struct {
name string
body string
chunked bool
wantErr error
}{
{
name: "exact limit",
body: strings.Repeat(" ", maxBytes-len(valid)) + valid,
},
{
name: "one byte over with valid JSON prefix",
body: valid + strings.Repeat(" ", maxBytes+1-len(valid)),
chunked: true,
wantErr: ErrResponseTooLarge,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if tc.chunked {
w.(http.Flusher).Flush()
}
_, _ = io.WriteString(w, tc.body)
}))
defer server.Close()
var got struct {
OK bool `json:"ok"`
}
err := GetJSON(context.Background(), server.Client(), server.URL, maxBytes, &got)
if tc.wantErr != nil {
if !errors.Is(err, tc.wantErr) {
t.Fatalf("got error %v, want %v", err, tc.wantErr)
}
return
}
if err != nil {
t.Fatalf("GetJSON() error = %v", err)
}
if !got.OK {
t.Fatal("decoded result has OK = false")
}
})
}
}
Place the helper in the same package, put the test in a _test.go file, then run go test ./.... The expected result is that the exact-limit response decodes with OK == true and the one-byte-over response returns ErrResponseTooLarge. This is a proposed test procedure, not a report that these tests were run here. Extend it with an unknown-length stream and a body read error if the application transport can produce those conditions. Keep a separate test for non-2xx status, since status handling is a different failure from body size.
Budget decompression, time, and concurrency
Choose the byte boundary you intend to protect. With Go’s usual HTTP transport behavior, a transparently decompressed response body is presented to the reader, and Response.Uncompressed reports that decompression occurred. The net/http response documentation
describes this behavior. Keep the cap around the bytes passed to JSON decoding, which prevents a compressed response from expanding beyond the decode budget. If a custom transport disables automatic decompression, a limit on its still-compressed body does not bound the later decompressed data; add a separate decoded-byte cap at that layer.
Set an overall deadline as well as the byte cap. A peer can send a small body very slowly, so size alone does not bound how long a goroutine or connection stays occupied. Use the request context for the caller’s deadline and an http.Client timeout or transport limits for the service-wide policy. Reuse configured clients rather than creating one per request, and ensure cancellation reaches the body read.
A body that exceeds the cap should be closed and discarded, not partially decoded or retried with a larger allowance. Do not drain an unbounded body just to reuse a connection. Record a stable outcome such as response_too_large, the configured cap, status class, and elapsed-time bucket; avoid logging the full URL, query, headers, or body. If the error path logs the underlying request error, check whether a caller-controlled endpoint could place sensitive data in that message.
Keep destination authorization separate from response budgeting. This helper assumes the endpoint and redirect policy have already been reviewed. It does not make an arbitrary URL safe to fetch or replace network egress controls. If a patch accepts URLs from model output, repository files, or other untrusted input, use the site’s URL-fetcher SSRF review as a separate gate.
Reject incomplete evidence
A useful review asks the coding agent to show the configured maximum, the exact read boundary, the overflow error, and tests that assert both the accepted limit and rejected limit-plus-one response. Inspect that no second path reads resp.Body without the cap and that decoding happens only after the size check. Verify the caller handles the size error without silently falling back to an unlimited read.
Treat these results as failures, not passes:
Content-Lengthexceeds the cap, but the streamed read has no independent limit.- A JSON decoder accepts a prefix while unread bytes remain.
- The reader hits its cap but the code parses the truncated bytes as though they were complete.
- A read error is ignored and partial data is decoded.
- A timeout cancels the caller but leaves the body or background retry running.
- The test covers only a small in-memory string and never exercises the HTTP response body.
If the endpoint regularly exceeds the cap, decide whether to paginate, select fewer fields, or define a reviewed higher budget. Do not increase the limit just to make one fixture pass. When the response stream is coupled to a producer, also check that the producer respects downstream pressure; the current Node.js export backpressure test covers that separate lifecycle failure. For cleanup evidence after a request ends, see the resource-leak testing guide .
Sources checked
These official Go package references were fetched live on 2026-11-19 UTC. Their standard-library API contracts support this evergreen review task; the article makes no release-specific claim and does not report a test run. Use the documentation and Go version supported by the repository when adapting the example.
- Go
iopackage documentsLimitReaderstopping after N bytes andReadAllreturning the read data and any encountered error. - Go
net/httppackage documents response-body streaming and closure, non-2xx status handling, client and request deadlines, and automatic decompression behavior. - Go
encoding/jsonpackage documents unmarshalling JSON-encoded bytes and reporting syntax errors.
Reader next step
Choose one changed Go client and write down its response schema, per-response byte cap, total concurrency budget, and deadline. Add the exact-limit and limit-plus-one cases around the real request path, then attach the test output and configured Go version to the pull request. If the task is actually about request destinations rather than response size, route it to a destination-authorization review instead of treating a body cap as a security boundary.