Skip to content
Open
22 changes: 22 additions & 0 deletions Tests/test_file_jpeg2k.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,28 @@ def test_header_errors() -> None:
pass


def test_oversized_box_length() -> None:
# Do not raise an OverflowError() when seeking to the end of a box
data = (
b"\x00\x00\x00\x0cjP \x0d\x0a\x87\x0a" # JP2 signature box
+ struct.pack(">I4s", 1, b"") # box with 64-bit length
+ struct.pack(">Q", 2**63 - 12) # length beyond the seek range
)
with pytest.raises(SyntaxError, match="Box length too large"):
Jpeg2KImagePlugin.Jpeg2KImageFile(BytesIO(data))


def test_oversized_box_length_when_reading() -> None:
# Do not raise an OverflowError() when reading the contents of a box
data = (
b"\x00\x00\x00\x0cjP \x0d\x0a\x87\x0a" # JP2 signature box
+ struct.pack(">I4s", 1, b"jp2h") # box read into with a 64-bit length
+ struct.pack(">Q", 16 + 2**63) # length whose contents overflow fp.read()
)
with pytest.raises(SyntaxError, match="Box length too large"):
Jpeg2KImagePlugin.Jpeg2KImageFile(BytesIO(data))


def test_layers_type(card: ImageFile.ImageFile, tmp_path: Path) -> None:
outfile = tmp_path / "temp_layers.jp2"
for quality_layers in [[100, 50, 10], (100, 50, 10), None]:
Expand Down
6 changes: 6 additions & 0 deletions src/PIL/Jpeg2KImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ def next_box_type(self) -> bytes:
msg = "Invalid header length"
raise SyntaxError(msg)

# Reject a box whose contents would extend beyond the maximum position
# that can be sought or read, before it is stored and later consumed
if self.fp.tell() + lbox - hlen >= 2**63:
msg = "Box length too large"
raise SyntaxError(msg)

self.remaining_in_box = lbox - hlen
return tbox

Expand Down
Loading