T09 · Insecure Skill Coding Practices
- Location
scripts/ocr.py:29- Finding
Predictable Shared Temporary File Enables Symlink and Stale-File Attacks
- Content
View full analysis
Vulnerability Details
File Location:
scripts/ocr.py, lines 29-38
Vulnerability Type: Insecure temporary-file handling
Risk Level: MediumVulnerable Code
python subprocess.run(["xclip", "-selection", "clipboard", "-t", "image/png", "-o", "/tmp/clipboard.png"], check=True, capture_output=True) return "/tmp/clipboard.png"python subprocess.run(["pbpaste", ">", "/tmp/clipboard.png"], shell=True, capture_output=True) if os.path.exists("/tmp/clipboard.png"): return "/tmp/clipboard.png"Technical Analysis
The clipboard image is stored at the fixed, publicly predictable path
/tmp/clipboard.png. The code does not securely create the file, verify its ownership or type, assign restrictive permissions, or remove it after OCR processing.The macOS branch performs shell redirection to this predictable path. Shell redirection follows an existing symbolic link, so a local attacker can pre-create
/tmp/clipboard.pngas a symlink to another file writable by the OCR process. The target can then be overwritten with clipboard output when the victim invokes clipboard OCR.The
pbpasteoperation is also executed withoutcheck=True. The subsequent logic only checks whether the path exists. Consequently, a failed clipboard operation can cause an old or attacker-prepared file at that path to be accepted and processed.The Linux
xclipinvocation does not redirect captured standard output into the target file. Supplying/tmp/clipboard.pngas an extra argument does not securely create or populate a temporary output file and may cause the command to fail, after which the vulnerable macOS fallback is attempted.Attack Path
- A local attacker predicts the fixed
/tmp/clipboard.pngpath. - The attacker creates one of the following before the victim runs the tool:
- A symbolic link from
/tmp/clipboard.pngto a file writable by the victim. - A regular file c ...[truncated 1132 chars]
- A symbolic link from
- A local attacker predicts the fixed
- Remediation
View remediation
Remediation Suggestions
- Create temporary files with Python's
tempfilemodule using unpredictable names and restrictive permissions. - Avoid
shell=Trueand shell redirection. Capture command output directly and write it through an already-secured file descriptor. - Require successful command completion with
check=True. - Confirm that generated output is a non-empty regular file owned by the current user before opening it.
- Remove temporary clipboard data in a
finallyblock, including after OCR failures. - Keep temporary file descriptors private and avoid reopening paths when possible, reducing time-of-check/time-of-use race conditions.
A safer implementation pattern is:
python import os import subprocess import tempfile path = None try: result = subprocess.run( ["pbpaste"], check=True, capture_output=True, ) with tempfile.NamedTemporaryFile( mode="wb", suffix=".png", delete=False, ) as output: output.write(result.stdout) path = output.name if not result.stdout: raise ValueError("Clipboard did not contain image data") return path except Exception: if path and os.path.exists(path): os.unlink(path) return NoneThe caller should delete the returned file in a
finallyblock after recognition completes.- Create temporary files with Python's
