Skip to content

Commit c143eb6

Browse files
committed
Use _accept check in _open
1 parent 8261432 commit c143eb6

2 files changed

Lines changed: 13 additions & 2 deletions

File tree

Tests/test_file_webp.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ def test_version(self) -> None:
4949
assert version is not None
5050
assert re.search(r"\d+\.\d+\.\d+$", version)
5151

52+
def test_invalid_file(self) -> None:
53+
invalid_file = "Tests/images/flower.jpg"
54+
55+
with pytest.raises(SyntaxError):
56+
WebPImagePlugin.WebPImageFile(invalid_file)
57+
5258
def test_read_rgb(self) -> None:
5359
"""
5460
Can we read a RGB mode WebP file without error?

src/PIL/WebPImagePlugin.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,15 @@ class WebPImageFile(ImageFile.ImageFile):
4343
__logical_frame = 0
4444

4545
def _open(self) -> None:
46+
assert self.fp is not None
47+
s = self.fp.read()
48+
if not _accept(s):
49+
msg = "not a WEBP file"
50+
raise SyntaxError(msg)
51+
4652
# Use the newer AnimDecoder API to parse the (possibly) animated file,
4753
# and access muxed chunks like ICC/EXIF/XMP.
48-
assert self.fp is not None
49-
self._decoder = _webp.WebPAnimDecoder(self.fp.read())
54+
self._decoder = _webp.WebPAnimDecoder(s)
5055

5156
# Get info from decoder
5257
self._size, self.info["loop"], bgcolor, self.n_frames, self.rawmode = (

0 commit comments

Comments
 (0)