Description:
                    Unbounded loop controlled by untrusted input (image count) enables uncontrolled resource consumption.
                
                
                    Line Range:
                    57-72
                
                
                    Exploitable:
                    True
                
                
                    Explanation:
                    STEP 1: Vulnerable function is ICO.calculate(input) in lib/types/ico.ts. It reads nbImages = readUInt16LE(input, 4) from the input buffer and uses it directly as the loop bound: for (let imageIndex = 0; imageIndex < nbImages; imageIndex += 1) { ... } without any upper limit.
STEP 2: Parameters/variables that could be attacker-controlled: input (Uint8Array) and nbImages derived from it.
STEP 3: Callers: Not shown in provided code; ICO is exported and intended to be called by the surrounding image type detection pipeline or directly by consumers. Within this file, calculate is the function that performs the unbounded loop.
STEP 4: External input sources: Any caller that passes a Uint8Array from untrusted sources (e.g., files uploaded by users, HTTP request bodies, or other network-derived buffers) will flow into ICO.calculate. No bounds are enforced here.
STEP 5: Validation/sanitization: ICO.validate() only checks reserved==0, imageCount!=0, and imageType==TYPE_ICON. It does not constrain imageCount (nbImages) nor verify that the directory size fits within input.length. Thus, an attacker can supply a large image count value that passes validate().
Conclusion: The code is reachable with attacker-controlled input in typical usage and the unbounded loop is directly controlled by nbImages, making the issue exploitable for CPU and memory exhaustion.
                
                
                    Impact Assessment:
                    Primary impact is denial of service. A crafted buffer with a large image count triggers excessive looping and growth of the images array, causing high CPU usage and potential memory exhaustion. Secondary risk: potential out-of-bounds reads in getImageSize if computed offsets exceed input.length, leading to exceptions and additional DoS.
                
                
                    Attack Complexity:
                    Low
                
                
                    Attack Vectors:
                    • Providing a crafted ICO/CUR buffer to any API that routes to ICO.calculate(input)
• Supplying a malicious file (processed into a Uint8Array) from user uploads or other untrusted sources
• Passing untrusted network data as the input buffer to image size parsing routines
                
                
                    Remediation:
                    Add strict bounds before iteration: (1) Enforce a sane maximum for nbImages (e.g., cap to 64 or 256) and reject larger values. (2) Verify structure size fits the buffer: SIZE_HEADER + nbImages * SIZE_IMAGE_ENTRY <= input.length. (3) In validate(), reject when imageCount exceeds the cap or directory size overflows the buffer. (4) In calculate(), iterate only after successful structural validation; optionally avoid constructing a potentially large images array or make listing images optional/limited. (5) Add bounds checks in getImageSize to prevent out-of-bounds reads.
                
                
                    Risk Type:
                    Insecure Design
                
                
                    Risk Severity:
                    high