Use _accept check in WebP _open (#9605)

This commit is contained in:
Andrew Murray 2026-05-12 12:11:38 +10:00 committed by GitHub
parent ea5901535d
commit 3ce681240f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 2 deletions

View File

@ -49,6 +49,12 @@ class TestFileWebp:
assert version is not None
assert re.search(r"\d+\.\d+\.\d+$", version)
def test_invalid_file(self) -> None:
invalid_file = "Tests/images/flower.jpg"
with pytest.raises(SyntaxError):
WebPImagePlugin.WebPImageFile(invalid_file)
def test_read_rgb(self) -> None:
"""
Can we read a RGB mode WebP file without error?

View File

@ -43,10 +43,15 @@ class WebPImageFile(ImageFile.ImageFile):
__logical_frame = 0
def _open(self) -> None:
assert self.fp is not None
s = self.fp.read()
if not _accept(s):
msg = "not a WEBP file"
raise SyntaxError(msg)
# Use the newer AnimDecoder API to parse the (possibly) animated file,
# and access muxed chunks like ICC/EXIF/XMP.
assert self.fp is not None
self._decoder = _webp.WebPAnimDecoder(self.fp.read())
self._decoder = _webp.WebPAnimDecoder(s)
# Get info from decoder
self._size, self.info["loop"], bgcolor, self.n_frames, self.rawmode = (