{"id":1816,"cve":"CVE-2026-41314","cve_url":"https:\/\/www.cvedetails.com\/cve\/CVE-2026-41314\/","cwe":"CWE-789","cwe_number":789,"cwe_url":"https:\/\/cwe.mitre.org\/data\/definitions\/789","cvss_score":4.8,"published":"2026-04-22","owasp":"A04 Insecure Design","odc":"Checking","cdc":"Missing Check","category":"Resource Management","subcategory":"Resource Exhaustion","scope":"Remote","impact":"Denial of Service (DoS)","summary":"pypdf Denial of Service via crafted PDF with a large FlateDecode image.","permalink":"https:\/\/netpack.pt\/vaitp\/vulnerability\/CVE-2026-41314\/","description":"pypdf is a free and open-source pure-python PDF library. An attacker who uses a vulnerability present in versions prior to 6.10.2 can craft a PDF which leads to the RAM being exhausted. This requires accessing an image using `\/FlateDecode` with large size values. This has been fixed in pypdf 6.10.2. As a workaround, one may apply the changes from the patch manually.","solution":"Upgrade pypdf to version 6.10.2 or later.","affected_component":"pypdf","fixed_by_upgrading":true,"vulnerable_code":"import zlib\n\n# This code represents the vulnerable logic inspired by CVE-2023-41314.\n# In the actual library, this logic would be part of a larger class structure\n# for parsing PDF stream objects.\n\ndef vulnerable_decode_stream_data(stream_object):\n    \"\"\"\n    A simplified function simulating how a vulnerable version of pypdf\n    would handle a Flate-decoded stream. It trusts the '\/Length'\n    parameter from the PDF without any safety checks.\n    \"\"\"\n    # 1. Get the compressed data from the stream object.\n    # In a real scenario, this would be read from the PDF file.\n    compressed_data = stream_object[\"_data\"]\n\n    # 2. Get the DECLARED uncompressed length from the stream's dictionary.\n    # This value is controlled by the attacker in a malicious PDF.\n    declared_length = stream_object[\"\/Length\"]\n\n    # 3. THE VULNERABLE STEP:\n    # Allocate a buffer using the attacker-controlled 'declared_length'.\n    # If this value is excessively large (e.g., several gigabytes),\n    # this line will attempt to allocate that much memory, leading to\n    # a MemoryError and causing a Denial of Service (DoS).\n    output_buffer = bytearray(declared_length)\n\n    # 4. Attempt to decompress the (small) actual data into the (huge) buffer.\n    # The program would typically crash on the allocation above before this.\n    decompressed_data = zlib.decompress(compressed_data)\n    output_buffer[:len(decompressed_data)] = decompressed_data\n    \n    return output_buffer\n\n\n# --- Setup to demonstrate the vulnerability ---\n\n# An attacker crafts a PDF object where the actual data is tiny,\n# but the declared uncompressed length is enormous.\n\n# The actual data is very small (15 bytes).\nreal_content = b\"vulnerable data\"\ncompressed_content = zlib.compress(real_content)\n\n# The malicious PDF stream object claims the data will decompress to 2 GB.\nmalicious_stream_object = {\n    # Untrusted length value from the crafted PDF: 2 GB\n    \"\/Length\": 2 * 1024 * 1024 * 1024,\n    \n    # The actual compressed data, which is only a few bytes long.\n    \"_data\": compressed_content,\n}\n\n\n# --- Triggering the vulnerability ---\n# This call will cause the program to try and allocate 2 GB of RAM,\n# which will likely exhaust available memory and crash the application.\nvulnerable_decode_stream_data(malicious_stream_object)","patched_code":"def fixed_flate_decode_with_size_check(params, max_pixels=100_000_000):\n    \"\"\"\n    This function represents the patched logic. A vulnerable version would\n    lack the size validation check before allocating memory.\n    \"\"\"\n    try:\n        # Extract width and height from the image's dictionary parameters.\n        # A real implementation handles PDF-specific name objects.\n        width = int(params.get(\"\/Width\", 1))\n        height = int(params.get(\"\/Height\", 1))\n    except (ValueError, TypeError):\n        # Malformed parameters could also be part of an attack.\n        raise ValueError(\"Invalid image parameters provided.\")\n\n    # --- THE FIX ---\n    # Before allocating memory, calculate the number of pixels the image\n    # claims to have and check it against a reasonable maximum limit.\n    if width > 0 and height > 0 and width * height > max_pixels:\n        # If the size is too large, raise an error instead of proceeding.\n        # This prevents the massive memory allocation that would exhaust RAM.\n        raise MemoryError(\n            f\"Image dimensions ({width}x{height}) exceed the security limit \"\n            f\"of {max_pixels} pixels, preventing a potential DoS attack.\"\n        )\n\n    # If the check passes, the program would continue with decoding\n    # and memory allocation, which is now known to be of a reasonable size.\n    # For example:\n    # buffer = bytearray(width * height * 3) # Assuming 3 bytes\/pixel\n    # decompress_stream_into_buffer(...)\n    return True","payload_needed":true,"payload":null,"payload_status":"model_refused","published_fix":[{"repository":"py-pdf\/pypdf","commit_url":"https:\/\/github.com\/py-pdf\/pypdf\/commit\/ac734dab4eef92bcce50d503949b4d9887d89f11","commit_subject":"SEC: Introduce limits for FlateDecode parameters and image decoding (#3734)","units":[{"file":"pypdf\/filters.py","function":null},{"file":"pypdf\/filters.py","function":"FlateDecode.decode"},{"file":"pypdf\/filters.py","function":"FlateDecode._decode_png_prediction"},{"file":"pypdf\/filters.py","function":"FlateDecode._get_parameters"},{"file":"pypdf\/generic\/_image_xobject.py","function":null},{"file":"pypdf\/generic\/_image_xobject.py","function":"bits2byte"}]}],"references":null}