{"record":{"id":"1b20565303179782","repo":"apache/answer","slug":"decode-image-config-error-v","errorCode":null,"errorMessage":"decode image config error: %v","messagePattern":"decode image config error: (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/checker/file_type.go","lineNumber":104,"sourceCode":"// formatSpecificConfigCheck decodes image config using a format-specific decoder\n// based on the file extension. This avoids calling image.DecodeConfig() which\n// dispatches by magic bytes and can invoke unintended decoders (e.g., TIFF)\n// registered by transitive dependencies.\nfunc formatSpecificConfigCheck(file io.Reader, ext string, maxImageMegapixel int) error {\n\tvar config image.Config\n\tvar err error\n\tswitch ext {\n\tcase \"jpg\", \"jpeg\":\n\t\tconfig, err = jpeg.DecodeConfig(file)\n\tcase \"png\":\n\t\tconfig, err = png.DecodeConfig(file)\n\tcase \"gif\":\n\t\tconfig, err = gif.DecodeConfig(file)\n\tdefault:\n\t\treturn fmt.Errorf(\"unsupported image format: %s\", ext)\n\t}\n\tif err != nil {\n\t\treturn fmt.Errorf(\"decode image config error: %v\", err)\n\t}\n\tif imageSizeTooLarge(config, maxImageMegapixel) {\n\t\treturn fmt.Errorf(\"image size too large\")\n\t}\n\treturn nil\n}\n\n// formatSpecificImageCheck fully decodes the image using a format-specific decoder.\nfunc formatSpecificImageCheck(file io.Reader, ext string, _ int) error {\n\tvar err error\n\tswitch ext {\n\tcase \"jpg\", \"jpeg\":\n\t\t_, err = jpeg.Decode(file)\n\tcase \"png\":\n\t\t_, err = png.Decode(file)\n\tcase \"gif\":\n\t\t_, err = gif.Decode(file)\n\tdefault:","sourceCodeStart":86,"sourceCodeEnd":122,"githubUrl":"https://github.com/apache/answer/blob/3b9f1370612e690a0b7f230f05e688930db4c6d3/pkg/checker/file_type.go#L86-L122","documentation":"formatSpecificConfigCheck wraps any error returned by the format-specific image header decoders (jpeg.DecodeConfig, png.DecodeConfig, gif.DecodeConfig) in pkg/checker/file_type.go:104. It is thrown when the file cannot have its width/height header parsed as the extension claims (jpg/jpeg, png, or gif). It means the file is corrupt, truncated, or not actually the image format its extension suggests.","triggerScenarios":"DecodeAndCheckImageFile is called on a file with ext jpg/jpeg/png/gif and the corresponding DecodeConfig fails: truncated or corrupt header, empty file, HTML/text/renamed file with an image extension, or a partial upload.","commonSituations":"Users upload renamed files (e.g. a .webp or SVG renamed to .png), uploads interrupted mid-transfer, or files corrupted by bad proxy/storage handling. Also hit when a dependency-registered decoder mismatch causes header parse failure.","solutions":["Verify the uploaded file is genuinely a valid JPEG/PNG/GIF (check magic bytes before saving or calling the checker)","Re-upload the file; ask the user to retry the upload since truncation is a common cause","Ensure the file extension matches the actual content; reject mismatched files at upload time","Log the wrapped underlying error (%v) to identify the exact decoder failure"],"exampleFix":"// before: accept any upload then decode\nerr := checker.DecodeAndCheckImageFile(path, maxMegapixel)\n// after: pre-validate magic bytes\nhead := make([]byte, 8)\nf, _ := os.Open(path); io.ReadFull(f, head); f.Close()\nif !isJPEG(head) && !isPNG(head) && !isGIF(head) { return errors.New(\"not a real image\") }","handlingStrategy":"validation","validationCode":"func looksLikeImage(head []byte) bool {\n\tswitch {\n\tcase len(head) >= 3 && head[0] == 0xFF && head[1] == 0xD8 && head[2] == 0xFF: return true // jpeg\n\tcase len(head) >= 8 && string(head[1:4]) == \"PNG\": return true\n\tcase len(head) >= 6 && string(head[0:3]) == \"GIF\": return true\n\t}\n\treturn false\n}","typeGuard":"func isImageDecodeErr(err error) bool { return err != nil && strings.HasPrefix(err.Error(), \"decode image config error\") }","tryCatchPattern":"if err := checker.DecodeAndCheckImageFile(path, maxMP); !ok {\n\tlog.Printf(\"rejecting upload %s: invalid or corrupt image\", path)\n\treturn fmt.Errorf(\"invalid image file\")\n}","preventionTips":["Validate magic bytes before accepting any upload","Reject files whose extension does not match detected content type","Detect truncated uploads via Content-Length comparison","Always log the wrapped underlying decode error for diagnosis"],"tags":["go","image-decoding","file-upload","validation"],"backgroundTag":"image-decode-failed","analyzedSha":"3b9f1370612e690a0b7f230f05e688930db4c6d3","analyzedAt":"2026-09-05T18:18:39.533Z","contentChangedAt":"2026-09-05T18:18:39.533Z","schemaVersion":2},"datasetVersion":"2026-09-12T22:17:10.623Z"}