From 65d43e1d0de86dcae84a468817c46cc02470c8b3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Thu, 18 Dec 2025 12:00:20 +0100 Subject: [PATCH] resources/images: Don't trust the file extension when decoding JPEG and PNG images Which is it behaved before we added the extended WebP support. Testing on some sites shows that it's not uncommon to store JPEGs with PNG extensions and vice versa. This an error situation that needs to be reported, but let us push that to a future Hugo version to reduce the noise in this one. --- resources/images/codec.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/resources/images/codec.go b/resources/images/codec.go index 6d84a682f..2bd93b579 100644 --- a/resources/images/codec.go +++ b/resources/images/codec.go @@ -131,10 +131,14 @@ func (d *Codec) EncodeTo(conf ImageConfig, w io.Writer, img image.Image) error { func (d *Codec) DecodeFormat(f Format, r io.Reader) (image.Image, error) { switch f { - case JPEG: - return jpeg.Decode(r) - case PNG: - return png.Decode(r) + case JPEG, PNG: + // TODO(bep) we reworked this decode/encode setup to get full WebP support in v0.153.0. + // In the first take of that we used f to decide whether to call png.Decode or jpeg.Decode here, + // but testing it on some sites, it seems that it's not uncommon to store JPEGs with PNG extensions and vice versa. + // So, to reduce some noise in that release, we fallback to the standard library here, + // which will read the magic bytes and decode accordingly. + img, _, err := image.Decode(r) + return img, err case GIF: g, err := gif.DecodeAll(r) if err != nil { -- 2.39.5