Prevent resource exhaustion when decoding images
masterWhen processing arbitrary or untrusted images, you should follow a two-step decoding process to prevent resource exhaustion (e.g., memory exhaustion from extremely large images).
- Call
DecodeConfigfirst to retrieve the image header, which contains the dimensions (width and height). - Inspect the dimensions returned in the header.
- Only call
Decodeif the dimensions are within safe limits for your application's available resources.
Note that calling Decode on an image that results in an extremely large allocation is not considered a security vulnerability of the package itself; the responsibility for resource management lies with the consumer.