larksuite/cli · error

invalid content range: end %d is outside total %d

Error message

invalid content range: end %d is outside total %d

What it means

parseContentRange validates a Content-Range header parsed from a partial-download response in extension/download. The total length is exclusive-bound: a valid range end index must be < total. This error means the header advertises an end byte at or beyond the declared total size, which cannot describe a real byte range.

Source

Thrown at extension/download/download.go:714

	end, err := strconv.ParseInt(bounds[1], 10, 64)
	if err != nil {
		return contentRange{}, fmt.Errorf("parse range end: %w", err)
	}
	total, err := strconv.ParseInt(parts[1], 10, 64)
	if err != nil {
		return contentRange{}, fmt.Errorf("parse total size: %w", err)
	}
	if total <= 0 {
		return contentRange{}, fmt.Errorf("invalid total size: %d", total)
	}
	if start < 0 || end < 0 {
		return contentRange{}, fmt.Errorf("invalid negative content range: %d-%d", start, end)
	}
	if start > end {
		return contentRange{}, fmt.Errorf("invalid content range: start %d is after end %d", start, end)
	}
	if end >= total {
		return contentRange{}, fmt.Errorf("invalid content range: end %d is outside total %d", end, total)
	}
	return contentRange{start: start, end: end, total: total}, nil
}

// strongETag returns a validator suitable for If-Range.
func strongETag(header http.Header) (string, bool) {
	values := header.Values("ETag")
	if len(values) != 1 {
		return "", false
	}
	tag := strings.TrimSpace(values[0])
	if len(tag) < 2 || tag[0] != '"' || tag[len(tag)-1] != '"' {
		return "", false
	}
	for i := 1; i < len(tag)-1; i++ {
		if c := tag[i]; c != 0x21 && !(c >= 0x23 && c <= 0x7E) && c < 0x80 {
			return "", false
		}

View on GitHub (pinned to 7fd6ef3c07)

Solutions

  1. Check the server's Content-Range header for off-by-one values; end must be lastByteIndex, not byteCount
  2. Verify the total in the header matches the resource's actual Content-Length
  3. Correct test fixtures/mocks that fabricate invalid range headers
  4. If you control the server, fix the range construction to use end = start + len - 1

Example fix

// before (invalid: end 1024 outside total 1024)
Content-Range: bytes 0-1024/1024
// after
Content-Range: bytes 0-1023/1024
Defensive patterns

Strategy: validation

Validate before calling

func validContentRange(hdr string) bool {
  var s, e, t int64
  if _, err := fmt.Sscanf(hdr, "bytes %d-%d/%d", &s, &e, &t); err != nil { return false }
  return s <= e && e < t
}

Type guard

func isPartialRange(r contentRange, total int64) bool { return r.end < total && r.start <= r.end }

Prevention

When it happens

Trigger: A server (or proxy) returns a Content-Range header whose end value equals or exceeds the total length, e.g. 'bytes 0-1024/1024' instead of 'bytes 0-1023/1024', and openPartial/openNext feed it to parseContentRange.

Common situations: Broken or non-compliant HTTP servers/CDNs behind a proxy; unit tests with hand-written Content-Range strings; mis-parsed totals when a server reports Content-Length different from the range total.

Related errors


AI-assisted analysis of larksuite/cli@7fd6ef3c07 (2026-09-04). Data as JSON: /api/errors/2af93991c39f3069. Report an issue: GitHub.