d2lang/d2 · error

decoded image exceeds maximum size of %d bytes

Error message

decoded image exceeds maximum size of %d bytes

What it means

readDecoded decompresses a response body while guarding against decompression bombs: it reads at most maxImageSize+1 bytes via LimitReader, and if the decoded output exceeds maxImageSize it returns this error. This protects the bundler from tiny compressed payloads that expand to enormous images.

Source

Thrown at lib/imgbundler/imgbundler.go:304

}

func inflate(buf []byte) ([]byte, error) {
	if zr, err := zlib.NewReader(bytes.NewReader(buf)); err == nil {
		defer zr.Close()
		return readDecoded(zr)
	}
	fr := flate.NewReader(bytes.NewReader(buf))
	defer fr.Close()
	return readDecoded(fr)
}

func readDecoded(r io.Reader) ([]byte, error) {
	buf, err := io.ReadAll(io.LimitReader(r, maxImageSize+1))
	if err != nil {
		return nil, err
	}
	if int64(len(buf)) > maxImageSize {
		return nil, fmt.Errorf("decoded image exceeds maximum size of %d bytes", maxImageSize)
	}
	return buf, nil
}

// sniffMimeType sniffs the mime type of href based on its file extension and contents.
func sniffMimeType(href, buf []byte, isRemote bool) string {
	p := string(href)
	if isRemote {
		u, err := url.Parse(html.UnescapeString(p))
		if err != nil {
			p = ""
		} else {
			p = u.Path
		}
	}
	mimeType := mime.TypeByExtension(path.Ext(p))
	if mimeType == "" {
		mimeType = http.DetectContentType(buf)

View on GitHub (pinned to 0d69dca6f5)

Solutions

  1. Reduce the image size at the source (resize/optimize the asset) if you control it
  2. Raise maxImageSize if your use case legitimately requires larger images and you have the memory headroom
  3. Serve the asset uncompressed or pre-sized via CDN transforms so decoded size stays under the cap
  4. Treat as malicious input if untrusted pages are bundled — keep the cap and reject the page
Defensive patterns

Strategy: validation

Validate before calling

head, err := http.Head(imgURL)
if err == nil && head.ContentLength > maxImageSize {
	return fmt.Errorf("image %s too large: %d > %d", imgURL, head.ContentLength, maxImageSize)
}

Try / catch

if err != nil {
	if strings.Contains(err.Error(), "exceeds maximum size") {
		// reject page or serve a downscaled variant
	}
}

Prevention

When it happens

Trigger: A fetched image's decompressed content (after gzip/brotli/deflate) is larger than maxImageSize — either a genuinely oversized image or a crafted/accidental decompression bomb.

Common situations: Pages embedding extremely large screenshots or uncompressed-format images behind compression; malicious pages deliberately serving zip bombs to exhaust bundler memory.

Related errors


AI-assisted analysis of d2lang/d2@0d69dca6f5 (2026-08-31). Data as JSON: /api/errors/338594e2a5beb564. Report an issue: GitHub.