T09 · Insecure Skill Coding Practices
- Location
axiom_image_metadata_stripper.py:92- Finding
Malformed JPEG Segment Length Can Cause Infinite-Loop Denial of Service
- Content
View full analysis
H", data[i:i+2])[0] segment = data[i-2:i+length] if marker in METADATA_MARKERS: # Skip this metadata segment pass else: # Keep this segment output.extend(segment) i += length - 2 # -2 because we already consumed 2 bytes (length) ``` The analysis path contains the same unsafe cursor calculation: ```python if i + 1 >= len(data): break length = struct.unpack(">H", data[i:i+2])[0] if marker in METADATA_MARKERS: result["metadata_chunks"].append({ "marker": marker.hex(), "name": METADATA_MARKERS[marker], "size": length, }) i += length - 2 if marker == JPEG_SOS: break ``` ### Technical Analysis The parser trusts the two-byte JPEG segment length without checking that it is at least the format-mandated minimum or that the declared segment fits within the input buffer. When the declared length is zero, the statement `i += length - 2` moves the cursor backward by two bytes. This returns the cursor to the same marker and causes the loop to parse that marker repeatedly without making forward progress. The same condition affects both `strip_jpeg()` and `analyze()`. The parser also does not reject truncated segments where the declared length extends beyond the available input. Python slicing prevents an immediate out-of-bounds exception, but this permissive behavior can produce corrupted output and makes parser state inconsistent. ### Attack Path 1. An attacker creates a file beginning with valid JPEG magic bytes. 2. The file contains a non-standalone JPEG marker whose segment-length field is `0x0000`. 3. The file is submitted to a serv ...[truncated 819 chars]- Remediation
View remediation
len(data): raise ValueError("Truncated JPEG segment length") length = struct.unpack(">H", data[i:i + 2])[0] if length < 2: raise ValueError(f"Invalid JPEG segment length: {length}") segment_end = i + length if segment_end > len(data): raise ValueError("JPEG segment extends beyond input") previous_i = i # Process the validated segment and move to its exact end. i = segment_end if i <= previous_i: raise ValueError("JPEG parser made no forward progress") ``` ]]>
