Skip to main content

CVE-2022-24859: CWE-835: Loop with Unreachable Exit Condition ('Infinite Loop') in py-pdf PyPDF2

Medium
Published: Mon Apr 18 2022 (04/18/2022, 00:00:00 UTC)
Source: CVE
Vendor/Project: py-pdf
Product: PyPDF2

Description

PyPDF2 is an open source python PDF library capable of splitting, merging, cropping, and transforming the pages of PDF files. In versions prior to 1.27.5 an attacker who uses this vulnerability can craft a PDF which leads to an infinite loop if the PyPDF2 if the code attempts to get the content stream. The reason is that the last while-loop in `ContentStream._readInlineImage` only terminates when it finds the `EI` token, but never actually checks if the stream has already ended. This issue has been resolved in version `1.27.5`. Users unable to upgrade should validate and PDFs prior to iterating over their content stream.

AI-Powered Analysis

AILast updated: 06/22/2025, 02:20:56 UTC

Technical Analysis

CVE-2022-24859 is a vulnerability identified in the PyPDF2 library, an open-source Python package widely used for manipulating PDF files, including splitting, merging, cropping, and transforming PDF pages. The vulnerability exists in versions prior to 1.27.5 and is classified under CWE-835, which pertains to loops with unreachable exit conditions, commonly resulting in infinite loops. Specifically, the issue arises in the ContentStream._readInlineImage method, where a while-loop is designed to terminate upon encountering the 'EI' token, signaling the end of an inline image stream within a PDF. However, the loop lacks a condition to detect the end of the stream itself, meaning if the 'EI' token is never found due to a crafted malicious PDF, the loop will continue indefinitely. This can cause the application using PyPDF2 to hang or become unresponsive when processing such a PDF file. The vulnerability does not require authentication or user interaction beyond processing the malicious PDF. The issue was resolved in version 1.27.5 by adding appropriate checks to ensure the loop terminates correctly even if the 'EI' token is absent. For users unable to upgrade, it is recommended to validate PDFs before processing their content streams to avoid triggering the infinite loop. There are no known exploits in the wild at this time, and no CVSS score has been assigned to this vulnerability.

Potential Impact

The primary impact of this vulnerability is a denial-of-service (DoS) condition caused by an infinite loop, which can lead to application hangs or crashes when processing maliciously crafted PDF files. For European organizations that rely on PyPDF2 for automated PDF processing—such as document management systems, automated report generation, or data extraction pipelines—this could disrupt business operations, delay workflows, or cause resource exhaustion on affected systems. While the vulnerability does not directly compromise confidentiality or integrity, the availability impact could be significant, especially in environments processing large volumes of PDFs or where uptime is critical. Additionally, if PyPDF2 is integrated into web services or APIs that accept user-uploaded PDFs, attackers could exploit this vulnerability to degrade service availability or cause denial of service to legitimate users. The lack of authentication or user interaction requirements means that any system processing untrusted PDFs is potentially vulnerable. Given the widespread use of Python and open-source libraries in European enterprises, especially in sectors like finance, legal, and public administration, the risk of operational disruption is notable.

Mitigation Recommendations

1. Upgrade PyPDF2 to version 1.27.5 or later, where the vulnerability has been fixed. This is the most effective and straightforward mitigation. 2. For environments where upgrading is not immediately possible, implement strict validation and sanitization of PDF files prior to processing. This includes checking for malformed or suspicious inline image streams and rejecting PDFs that do not conform to expected structure. 3. Employ timeouts and resource limits on PDF processing routines to prevent infinite loops from causing prolonged hangs or resource exhaustion. 4. Isolate PDF processing tasks in sandboxed environments or separate containers to contain potential denial-of-service impacts. 5. Monitor application logs and system performance for signs of hangs or excessive resource consumption during PDF processing. 6. Educate developers and system integrators about this vulnerability to ensure that any custom code using PyPDF2 includes defensive programming practices against malformed PDFs. 7. Consider alternative PDF libraries with robust parsing and security features if PyPDF2 cannot be upgraded or adequately secured.

Need more detailed analysis?Get Pro

Technical Details

Data Version
5.1
Assigner Short Name
GitHub_M
Date Reserved
2022-02-10T00:00:00.000Z
Cisa Enriched
true

Threat ID: 682d9848c4522896dcbf63b2

Added to database: 5/21/2025, 9:09:28 AM

Last enriched: 6/22/2025, 2:20:56 AM

Last updated: 7/30/2025, 9:06:46 PM

Views: 11

Actions

PRO

Updates to AI analysis are available only with a Pro account. Contact root@offseq.com for access.

Please log in to the Console to use AI analysis features.

Need enhanced features?

Contact root@offseq.com for Pro access with improved analysis and higher rate limits.

Latest Threats