T09 · Insecure Skill Coding Practices
Warning
- Location
- scripts/pdf_to_long_image.py:53
- Finding
- Unbounded PDF Rendering Can Exhaust Memory, CPU, and Disk Resources<![CDATA[ ## Vulnerability Details **File Location**: `scripts/pdf_to_long_image.py:53-81`; scale input is accepted without bounds at `scripts/pdf_to_long_image.py:111-112` **Vulnerability Type**: Uncontrolled resource consumption **Risk Level**: Medium ### Vulnerable Code ```python # Render each page to image images = [] max_width = 0 total_height = 0 for page_num in range(page_count): page = doc[page_num] # Get page dimensions and apply scale mat = fitz.Matrix(scale, scale) pix = page.get_pixmap(matrix=mat) # Convert to PIL Image img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples) images.append(img) max_width = max(max_width, img.width) total_height += img.height print(f" Page {page_num + 1}/{page_count}: {img.width}x{img.height}") # Create combined image print(f"Creating long image: {max_width}x{total_height} pixels...") result = Image.new("RGB", (max_width, total_height), "white") y_offset = 0 for img in images: # Center images that are narrower than max width x_offset = (max_width - img.width) // 2 result.paste(img, (x_offset, y_offset)) y_offset += img.height ``` The scale parameter is exposed without a safe range: ```python parser.add_argument("--scale", type=float, default=2.0, help="Scale factor for rendering (default: 2.0)") ``` ### Technical Analysis The conversion routine does not impose limits on the input file size, page count, page dimensions, aggregate pixel count, output dimensions, or rendering scale. Each PDF page is rasterized and retained in the `images` list. After all page images are resident in memory, the script allocates an additional image large enough to contain every rendered page. Consequently, peak memory consumption includes the PyMuPDF pixel buffers, all retained Pillow page images, and the final combined image. Memory usage grows further with the square of the scale factor for ordinary two-dimensional pages. PNG compressio ...[truncated 1516 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions 1. Require `scale` to be finite and positive, and enforce a conservative upper bound. 2. Inspect page dimensions before rasterization and calculate the projected width, height, per-page pixels, and aggregate pixels. 3. Reject PDFs that exceed configured limits for input size, page count, individual page dimensions, total rendered pixels, or final output dimensions. 4. Account for multiple in-memory copies when estimating required memory; do not base limits solely on compressed PDF size. 5. Avoid retaining every page image simultaneously. Use bounded batches, temporary image tiles, or an output approach that supports incremental processing. 6. Set execution-level memory, CPU, timeout, and writable-storage quotas when processing untrusted files. 7. Catch Pillow and PyMuPDF allocation/decompression errors and remove partial output files on failure. 8. Consider offering separate per-page images when the projected long-image dimensions exceed safe limits. ]]>
