T09 · Insecure Skill Coding Practices
- Location
mx_search.py:157- Finding
Predictable Output Files Permit Symlink-Based File Overwrite
- Content
View full analysis
= 3: query = " ".join(sys.argv[1:-1]) output_dir = Path(sys.argv[-1]) else: query = " ".join(sys.argv[1:]) output_dir = DEFAULT_OUTPUT_DIR # 确保输出目录存在 output_dir.mkdir(parents=True, exist_ok=True) try: mx = MXSearch() result = mx.search(query) # 终端显示格式化结果 print(mx.format_pretty(result)) # 提取纯文本保存为 .txt 文件 content = mx.extract_content(result) if content.strip(): filename = output_dir / f"mx_search_{safe_filename(query)}.txt" with open(filename, "w", encoding="utf-8") as f: f.write(content) print(f"\n✅ 纯文本结果已保存到: {filename}") # 同时保存原始 JSON 结果 json_filename = output_dir / f"mx_search_{safe_filename(query)}.json" with open(json_filename, "w", encoding="utf-8") as f: json.dump(result, f, ensure_ascii=False, indent=2) ``` ### Technical Analysis The command-line interface accepts an arbitrary output directory and derives both output filenames deterministically from the search query. It then opens those paths in write mode without checking whether the destination already exists or is a symbolic link. Python's ordinary `open(path, "w")` follows symbolic links and truncates an existing destination. Consequently, if another local user or process can write to the selected output directory, it can predict the filename, place a symbolic link at that path, and cause the process to truncate and replace the symlink target. The same deterministic filename also causes ordinary executions of identical or colliding sanitized queries to overwrite previous search results silently. Exploitati ...[truncated 1281 chars]- Remediation
View remediation
