T09 · Insecure Skill Coding Practices
- Location
handler.py:101- Finding
Upload Size Limit Is Enforced Only After the Entire File Is Written
- Content
View full analysis
MAX_FILE_SIZE: file_path.unlink() # Delete file return { 'success': False, 'error': f'File too large ({format_size(file_size)}); maximum supported size is {format_size(MAX_FILE_SIZE)}' } ``` ### Technical Analysis The configured 500 MB maximum is not enforced while receiving the stream. The handler continues reading and writing until the input reaches end-of-file, regardless of how much data has already been received. It then checks the accumulated size and deletes the file if the limit was exceeded. Consequently, the limit controls whether a completed upload is accepted, but it does not bound temporary disk consumption. A client able to invoke this handler can send a file substantially larger than 500 MB or, depending on the surrounding Gateway behavior, provide a long-running stream. The application will continue consuming disk space until the stream ends, an upstream request limit interrupts it, or the filesystem becomes full. Deletion after completion does not mitigate exhaustion during the upload. Exceptions during reading or writing can also leave a partial file because the function has no exception-safe cleanup block. ### Attack Path 1. An attacker initiates an upload through a Gateway endpoint that passes the reque ...[truncated 1184 chars]- Remediation
View remediation
Tuple[Path, int]: ensure_dirs() dest_path = PENDING_DIR / f"{uuid.uuid4().hex}.upload" total_size = 0 try: with open(dest_path, 'xb') as f: while True: chunk = file_stream.read(CHUNK_SIZE) if not chunk: break if total_size + len(chunk) > MAX_FILE_SIZE: raise ValueError("File exceeds the maximum permitted size") f.write(chunk) total_size += len(chunk) return dest_path, total_size except Exception: dest_path.unlink(missing_ok=True) raise ``` 2. Configure a request-body limit at the Gateway or reverse-proxy layer so oversized requests are rejected before they reach Python. 3. Apply upload timeouts, rate limits, concurrent-upload limits, and per-user storage quotas. 4. Reserve adequate filesystem capacity and monitor pending-storage usage. 5. Ensure partial files are removed in exception and cancellation paths. 6. Consider storing uploads on a dedicated quota-limited volume so exhaustion cannot affect the operating system or unrelated services. ]]>
