Skip to content

Commit

Permalink
Apply ImageFont.MAX_STRING_LENGTH to ImageFont.getmask()
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Dec 29, 2023
1 parent aaf99d1 commit 0434fd9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Tests/test_imagefont.py
Original file line number Diff line number Diff line change
Expand Up @@ -1058,6 +1058,8 @@ def test_too_many_characters(font):
imagefont.getlength("A" * 1_000_001)
with pytest.raises(ValueError):
imagefont.getbbox("A" * 1_000_001)
with pytest.raises(ValueError):
imagefont.getmask("A" * 1_000_001)


@pytest.mark.parametrize(
Expand Down
12 changes: 12 additions & 0 deletions docs/releasenotes/10.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ Restricted environment keys for ImageMath.eval
arbitrary code. To prevent this, keys matching the names of builtins and keys
containing double underscores will now raise a :py:exc:`ValueError`.

Applied ImageFont.MAX_STRING_LENGTH to ImageFont.getmask
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

To protect against potential DOS attacks when using arbitrary strings as text
input, Pillow will now raise a :py:exc:`ValueError` if the number of characters
passed into :py:meth:`PIL.ImageFont.ImageFont.getmask` is over a certain limit,
:py:data:`PIL.ImageFont.MAX_STRING_LENGTH`.

This threshold can be changed by setting
:py:data:`PIL.ImageFont.MAX_STRING_LENGTH`. It can be disabled by setting
``ImageFont.MAX_STRING_LENGTH = None``.

Other Changes
=============

Expand Down
1 change: 1 addition & 0 deletions src/PIL/ImageFont.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ def getmask(self, text, mode="", *args, **kwargs):
:return: An internal PIL storage memory instance as defined by the
:py:mod:`PIL.Image.core` interface module.
"""
_string_length_check(text)
return self.font.getmask(text, mode)

def getbbox(self, text, *args, **kwargs):
Expand Down

0 comments on commit 0434fd9

Please sign in to comment.