T09 · Insecure Skill Coding Practices
- Location
scripts/clear_cache.py:7- Finding
Unvalidated Recursive Deletion of the Matplotlib Cache Directory
- Content
View full analysis
Vulnerability Details
File Location:
scripts/clear_cache.py, lines 7–12
Vulnerability Type: Unvalidated recursive directory deletion
Risk Level: Mediumpython cache_dir = matplotlib.get_cachedir() print(f"字体缓存目录: {cache_dir}") if os.path.exists(cache_dir): shutil.rmtree(cache_dir) print("✅ 字体缓存已清除")Technical Analysis
The script passes the directory returned by
matplotlib.get_cachedir()directly toshutil.rmtree(). It does not verify that the resolved path is a dedicated Matplotlib cache directory, that it resides beneath an expected trusted parent, or that it is not a symbolic link or an overly broad directory such as the user's home directory.Matplotlib's cache location can depend on runtime configuration and environment settings. If an attacker, unsafe launcher, or compromised execution environment can influence that location, the script may recursively delete an unrelated directory. The existence check does not establish that the target is safe; it only confirms that a filesystem entry exists.
Attack Path
- An attacker gains control over, or influences, the environment or Matplotlib configuration used to launch the script.
- The attacker causes
matplotlib.get_cachedir()to resolve to an existing directory containing valuable user-accessible files. - The user follows the documented troubleshooting guidance and runs
python3 scripts/clear_cache.py. - The script calls
shutil.rmtree(cache_dir)without validating the target. - The target directory and its contents are recursively deleted with the privileges of the user running the script.
Impact Assessment
Successful exploitation can cause destructive loss of files and directories writable by the executing user. The vulnerability does not independently grant elevated privileges, remote code execution, or access beyond the current process's permissions. Its scope is limited by the filesystem privileges of ...[truncated 155 chars]
- Remediation
View remediation
Remediation Suggestions
- Resolve the target to a canonical absolute path before deletion.
- Allow deletion only when the path is beneath an explicitly trusted cache parent and has the expected Matplotlib-specific directory structure.
- Reject empty paths, filesystem roots, user home directories, current working directories, and other broad or sensitive locations.
- Detect and reject symbolic links in the target path and relevant parent components.
- Prefer deleting only known Matplotlib cache files rather than recursively deleting the entire directory.
- Display the validated target and require explicit user confirmation before destructive removal.
- Fail closed when path validation is inconclusive.
- Consider using a guarded implementation such as:
python from pathlib import Path import matplotlib import shutil cache_dir = Path(matplotlib.get_cachedir()).expanduser() resolved = cache_dir.resolve(strict=True) home = Path.home().resolve() forbidden = { Path("/").resolve(), home, Path.cwd().resolve(), } if resolved in forbidden: raise RuntimeError(f"Refusing to delete unsafe path: {resolved}") if resolved.is_symlink() or "matplotlib" not in resolved.name.lower(): raise RuntimeError(f"Unexpected Matplotlib cache path: {resolved}") confirmation = input(f"Delete Matplotlib cache directory '{resolved}'? [y/N] ") if confirmation.lower() == "y": shutil.rmtree(resolved)The exact trusted-parent policy should be tailored to the operating systems supported by the Skill rather than relying solely on a directory-name check.
