T09 · Insecure Skill Coding Practices
- Location
scripts/epub_converter.py:109- Finding
Predictable Shared Temporary Directory Used for EPUB Processing
- Content
View full analysis
or first/
title = "" t = re.search(r"]*>(.*?)", raw, re.DOTALL | re.IGNORECASE) if t: title = _unhtml_entity(t.group(1).strip()) ``` ### Technical Analysis The converter extracts EPUB metadata into the fixed, globally predictable directory `/tmp/_epub_tmp`. This directory is shared by every conversion and is not created through a secure temporary-directory API. It is also never removed after processing. The extracted `container.xml` controls the OPF location used by `_parse_meta`. Chapter and cover paths from that OPF file are subsequently joined to `opf_dir` and read from the filesystem. The code does not verify that resolved paths remain inside a private extraction directory. This design creates several related weaknesses: - Concurrent conversions can overwrite or consume one another's temporary files. - Files left by earlier conversions can affect later conversions. - Another local process with access to the shared temporary hierarchy may pre-create files, directories, or symbolic links to influence filesystem reads. - Extracted ebook metadata remains on disk after conversion. - Manifest-relative paths are not normalized and checked against an approved root befo ...[truncated 1574 chars]
- Remediation
View remediation
Path: root = root.resolve() candidate = candidate.resolve() if candidate != root and root not in candidate.parents: raise ValueError("EPUB path escapes the extraction directory") return candidate ``` 3. Prefer reading ZIP members directly with `ZipFile.read()` rather than extracting attacker-controlled archive content to the filesystem. 4. Reject absolute paths, parent-directory components such as `..`, and manifest references that are not valid archive members. 5. Ensure cleanup occurs automatically on success and failure. `TemporaryDirectory` provides this behavior when used as a context manager. 6. Apply resource limits for EPUB size, member count, per-member uncompressed size, and total uncompressed size to reduce archive-based denial-of-service risk. ]]>
