From: Bjørn Erik Pedersen Date: Thu, 18 Dec 2025 11:00:20 +0000 (+0100) Subject: resources/images: Don't trust the file extension when decoding JPEG and PNG images X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=65d43e1d0de86dcae84a468817c46cc02470c8b3;p=brevno-suite%2Fhugo 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. --- 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 {