T09 · Insecure Skill Coding Practices
- Location
scripts/convert.py:117- Finding
Predictable Temporary Image Files Permit File Overwrite and Data Exposure
- Content
View full analysis
Vulnerability Details
File Location:
scripts/convert.py:117-119; incomplete cleanup atscripts/convert.py:276-282
Vulnerability Type: Predictable and unsafe temporary-file handling
Risk Level: MediumVulnerable Code
python img_path = f"_temp_img_{page.number}_{img_index}.{image_ext}" with open(img_path, "wb") as f: f.write(image_bytes) content['images'].append({ 'path': img_path, 'width': base_image["width"], 'height': base_image["height"] })The cleanup logic is:
python for img_info in content['images']: try: word_doc.add_picture(img_info['path'], width=Inches(4)) os.remove(img_info['path']) except: passTechnical Analysis
Images extracted from a PDF are written to the current working directory using deterministic names composed only of the page number, image index, and image extension. The files are opened in ordinary write mode, which truncates existing files and follows symbolic links.
The implementation does not use a private temporary directory, securely randomized names, exclusive file creation, or symbolic-link protections. Consequently, another local user who can write to the working directory can predict the filename and create a symbolic link before conversion begins.
Cleanup is also not guaranteed. The temporary image is removed only after
word_doc.add_picture()succeeds. Because both insertion and deletion are inside a broadtry/exceptblock, any failure is silently ignored and can leave extracted images on disk. An exception elsewhere in conversion can have the same result.Attack Path
- An attacker obtains write access to the directory from which the converter will run.
- The attacker predicts a generated filename, such as
_temp_img_0_0.png. - The attacker creates that path as a symbolic link to another file writable by the converter process.
- A victim runs the converter against a PDF whose first page contains an image with the matchi ...[truncated 955 chars]
- Remediation
View remediation
Remediation Suggestions
- Avoid writing extracted images to predictable paths in the current working directory.
- Prefer passing image data directly to
python-docxthrough anio.BytesIOstream:
python from io import BytesIO image_stream = BytesIO(image_bytes) word_doc.add_picture(image_stream, width=Inches(4))- If filesystem-backed temporary files are required, create a private temporary directory with
tempfile.TemporaryDirectory()and generate files inside it. - Ensure cleanup through a
try/finallyblock or a temporary-directory context manager so that failures cannot leave extracted images behind. - Do not reuse existing paths, and avoid APIs or modes that follow attacker-created symbolic links. Where supported, use exclusive creation and no-follow protections.
- Replace broad, silent
except:handlers with specific exception handling and clear error reporting so cleanup and conversion failures remain visible. - Run conversion in a directory inaccessible to untrusted local users and under a minimally privileged account as defense in depth.
