JuliusBrussee/caveman · error

clickhouse response byte limit must be positive

Error message

clickhouse response byte limit must be positive

What it means

ReadBodyBounded requires a strictly positive maxBytes because it reads limit+1 bytes to detect truncation. A zero or negative limit is a programming error, so it fails closed immediately instead of reading an unbounded or empty body.

Solutions

  1. Pass a positive byte limit, e.g. ReadBodyBounded(resp.Body, 16*1024*1024) for a 16 MiB ceiling.
  2. Load the limit from config with an explicit nonzero default and validate it at startup.
  3. Check for ignored strconv/parse errors that leave the limit at its zero value.

Example fix

// before
limit := envMaxBytes // 0 if unset
body, err := chhttp.ReadBodyBounded(resp.Body, limit)
// after
limit := envMaxBytes
if limit <= 0 {
    limit = 16 * 1024 * 1024
}
body, err := chhttp.ReadBodyBounded(resp.Body, limit)
Defensive patterns

Strategy: validation

Validate before calling

if limit <= 0 {
    return fmt.Errorf("configured response limit must be positive, got %d", limit)
}

Try / catch

body, err := chhttp.ReadBodyBounded(resp.Body, limit)
if err != nil {
    if strings.Contains(err.Error(), "byte limit must be positive") {
        return fmt.Errorf("config bug: response limit %d invalid: %w", limit, err)
    }
    return err
}

Prevention

When it happens

Trigger: Calling chhttp.ReadBodyBounded(r, 0) or with a negative value, typically when maxBytes comes from an unset/zero-valued config variable or a failed int conversion.

Common situations: An env var like CLICKHOUSE_MAX_RESPONSE_BYTES missing so the default 0 leaks through; a strconv.Atoi error ignored leaving limit at 0; copy-pasted test code passing 0 accidentally.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of JuliusBrussee/caveman@3ee70a1026 (2026-09-20). Data as JSON: /api/errors/252e8c79586e5732. Report an issue: GitHub.

Appendix: source

Thrown at shared/platform/chhttp/chhttp.go:49

	"sync"
	"time"

	"github.com/JuliusBrussee/caveman/shared/platform/cabundle"
	"github.com/JuliusBrussee/caveman/shared/platform/env"
)

const (
	defaultInsertTimeoutMS = 5000
	defaultQueryTimeoutMS  = 30000
)

// ReadBodyBounded reads an HTTP response/error body with a hard byte ceiling.
// It deliberately reads one byte beyond the limit so callers can distinguish a
// truncated body from an exact-limit body and fail closed before parsing or
// logging attacker-controlled content.
func ReadBodyBounded(r io.Reader, maxBytes int64) ([]byte, error) {
	if maxBytes <= 0 {
		return nil, fmt.Errorf("clickhouse response byte limit must be positive")
	}
	data, err := io.ReadAll(io.LimitReader(r, maxBytes+1))
	if err != nil {
		return nil, err
	}
	if int64(len(data)) > maxBytes {
		return nil, fmt.Errorf("clickhouse response exceeds %d byte limit", maxBytes)
	}
	return data, nil
}

// The TLS knobs. All three are unset by default, in which case the clients keep
// stock net/http behaviour: system roots, hostname verified against the URL host.
const (
	// serverNameEnv overrides tls.Config.ServerName. This is the managed-
	// ClickHouse cutover case: the private .internal DNS record is dialled while
	// the deployment's certificate carries only *.dtwh SANs, so verification must
	// run against the name the certificate actually holds. Chain AND hostname

View on GitHub (pinned to 3ee70a1026)