T09 · Insecure Skill Coding Practices
- Location
scripts/pdf_ocr.swift:259- Finding
Reversed PDF Page Range Causes a Process Crash
- Content
View full analysis
= 1 { pages.append(page) } } } } ``` ### Technical Analysis The page-range parser accepts user-controlled start and end values but does not verify that the start is less than or equal to the effective end. Swift's closed-range operator requires its lower bound not to exceed its upper bound. For example, the input `5-1` evaluates the following expression: ```swift 5...1 ``` Constructing this invalid range triggers a Swift runtime precondition failure and terminates the process. Limiting the end with `min(end, totalPages)` does not prevent the problem and can also make an otherwise increasing range invalid when the start exceeds the document's page count. ### Attack Path 1. An attacker or untrusted caller provides a valid PDF and a reversed or invalid page range, such as: ```bash swift scripts/pdf_ocr.swift document.pdf -p 5-1 ``` 2. `parsePages` converts the two range components to integers. 3. The script evaluates `start...min(end, totalPages)` without validating their order. 4. Swift raises a fatal runtime error. 5. The OCR process terminates before returning a result. ### Impact Assessment The issue affects process availability. It allows an untrusted page-range argument to reliably crash the OCR invocation and interrupt the current Agent task. The vulnerability does not provide code execution, privilege escalation, persistence, or access to additional files. Its s ...[truncated 108 chars]- Remediation
View remediation
= 1, end >= start, start <= totalPages else { // Reject the range and return a clear validation error. continue } let effectiveEnd = min(end, totalPages) for page in start...effectiveEnd { pages.append(page) } ``` Additional hardening should include: 1. Return an explicit error for malformed or reversed ranges instead of silently treating them as all pages. 2. Reject negative, zero, empty, and excessively large page values. 3. Deduplicate parsed page numbers before processing. 4. Add tests for `5-1`, `0-1`, `999-1000`, empty ranges, malformed ranges, and ranges beyond the document page count. 5. Distinguish an invalid page specification from the absence of a page specification so invalid input cannot unexpectedly fall back to processing every page. ]]>
