Skip to content

Commit

Permalink
Merge pull request #5912 from radarhere/fix-pdf-redos-carriage-return
Browse files Browse the repository at this point in the history
Exclude carriage return in PDF regex to help prevent ReDoS
  • Loading branch information
radarhere committed Dec 28, 2021
2 parents 282d825 + dd80493 commit 43b800d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
5 changes: 3 additions & 2 deletions Tests/test_file_pdf.py
Expand Up @@ -313,8 +313,9 @@ def test_pdf_append_to_bytesio():


@pytest.mark.timeout(1)
def test_redos():
malicious = b" trailer<<>>" + b"\n" * 3456
@pytest.mark.parametrize("newline", (b"\r", b"\n"))
def test_redos(newline):
malicious = b" trailer<<>>" + newline * 3456

# This particular exception isn't relevant here.
# The important thing is it doesn't timeout, cause a ReDoS (CVE-2021-25292).
Expand Down
3 changes: 2 additions & 1 deletion src/PIL/PdfParser.py
Expand Up @@ -582,7 +582,8 @@ def next_object_id(self, offset=None):
whitespace_or_hex = br"[\000\011\012\014\015\0400-9a-fA-F]"
whitespace_optional = whitespace + b"*"
whitespace_mandatory = whitespace + b"+"
whitespace_optional_no_nl = br"[\000\011\014\015\040]*" # no "\012" aka "\n"
# No "\012" aka "\n" or "\015" aka "\r":
whitespace_optional_no_nl = br"[\000\011\014\040]*"
newline_only = br"[\r\n]+"
newline = whitespace_optional_no_nl + newline_only + whitespace_optional_no_nl
re_trailer_end = re.compile(
Expand Down

0 comments on commit 43b800d

Please sign in to comment.