apache/answer · error

decode webp image config error: %v

Error message

decode webp image config error: %v

What it means

webpImageConfigCheck wraps errors from golang.org/x/image/webp's DecodeConfig at pkg/checker/file_type.go:134. Thrown when a file with the .webp extension cannot have its VP8/VP8L/VP8X header parsed as WebP.

Source

Thrown at pkg/checker/file_type.go:134

	case "jpg", "jpeg":
		_, err = jpeg.Decode(file)
	case "png":
		_, err = png.Decode(file)
	case "gif":
		_, err = gif.Decode(file)
	default:
		return fmt.Errorf("unsupported image format: %s", ext)
	}
	if err != nil {
		return fmt.Errorf("decode image error: %v", err)
	}
	return nil
}

func webpImageConfigCheck(file io.Reader, _ string, maxImageMegapixel int) error {
	config, err := webp.DecodeConfig(file)
	if err != nil {
		return fmt.Errorf("decode webp image config error: %v", err)
	}
	if imageSizeTooLarge(config, maxImageMegapixel) {
		return fmt.Errorf("image size too large")
	}
	return nil
}

func webpImageCheck(file io.Reader, _ string, _ int) error {
	_, err := webp.Decode(file)
	if err != nil {
		return fmt.Errorf("decode webp image error: %v", err)
	}
	return nil
}

func imageSizeTooLarge(config image.Config, maxImageMegapixel int) bool {
	return config.Width*config.Height > maxImageMegapixel
}

View on GitHub (pinned to 3b9f137061)

Solutions

  1. Verify the file is real WebP (RIFF....WEBP magic bytes) before upload
  2. Re-encode the image as lossy/lossless WebP without extended features (ffmpeg -c:v libwebp)
  3. Upgrade golang.org/x/image to the latest version for newer WebP feature support
  4. Fall back to another format (PNG/JPEG) if the WebP variant is unsupported

Example fix

// before: trust extension
if ext == "webp" { /* decode */ }
// after: check magic bytes
head := make([]byte, 12)
io.ReadFull(f, head)
if string(head[0:4]) != "RIFF" || string(head[8:12]) != "WEBP" { return errors.New("not webp") }
Defensive patterns

Strategy: validation

Validate before calling

head := make([]byte, 12)
io.ReadFull(f, head)
if string(head[0:4]) != "RIFF" || string(head[8:12]) != "WEBP" {
	return errors.New("file is not WebP despite extension")
}

Type guard

func isWebPDecodeErr(err error) bool { return err != nil && strings.HasPrefix(err.Error(), "decode webp image config error") }

Try / catch

if err := webpImageConfigCheck(f, "webp", maxMP); err != nil {
	return fmt.Errorf("webp rejected: %w", err)
}

Prevention

When it happens

Trigger: A .webp file fails webp.DecodeConfig: not actually WebP content (renamed JPEG/PNG), truncated header, or a WebP variant unsupported by x/image/webp (e.g. extended-format features the library rejects).

Common situations: Users rename other formats to .webp; animated WebP or alpha-channel VP8X files produced by modern encoders hit limitations of the older x/image/webp decoder.

Related errors


AI-assisted analysis of apache/answer@3b9f137061 (2026-09-05). Data as JSON: /api/errors/af859640563335e3. Report an issue: GitHub.