T09 · Insecure Skill Coding Practices
- Location
scripts/download_arxiv.py:101- Finding
Arbitrary Filesystem Write and Overwrite Through Unsanitized Output Paths
- Content
View full analysis
str: """从 URL 或 ID 中提取 arXiv ID""" # 匹配 URLs like https://arxiv.org/abs/2301.12345 url_pattern = r'arxiv\.org/(?:abs|pdf|ps)/([\d\.]+)' match = re.search(url_pattern, identifier) if match: return match.group(1) # 匹配纯 ID,如 2301.12345 或 arXiv:2301.12345 id_pattern = r'(?:arXiv:)?(\d{4}\.\d{4,5})(v\d+)?' match = re.search(id_pattern, identifier) if match: return match.group(1) return identifier ``` ```python # 确定输出路径 output_dir = Path(args.output_dir).expanduser().resolve() if args.name: if args.latex: ext = '.tar.gz' else: ext = '.pdf' filename = args.name if args.name.endswith(ext) else args.name + ext else: filename = f"{arxiv_id}.tar.gz" if args.latex else f"{arxiv_id}.pdf" output_path = output_dir / filename # 下载 url = get_latex_url(arxiv_id) if args.latex else get_pdf_url(arxiv_id) success = download_file(url, output_path) ``` The resulting path is subsequently created and opened for replacement: ```python output_path.parent.mkdir(parents=True, exist_ok=True) with open(output_path, 'wb') as f: f.write(content) ``` ### Technical Analysis The script uses the user-controlled `--name` value directly as a path component. It does not reject absolute paths, parent-directory components such as `..`, directory separators, or symbolic-link-based escapes. With `pathlib`, joining an absolute `filename` to `output_dir` discards the intended base directory. A relative name containing traversal components can similarly resolve outside the requested output directory. The script then creates missing parent directories and opens the destination in `wb` mode, which truncates and overwrites an existing ...[truncated 1895 chars]- Remediation
View remediation
