T05 · Unauthorized Access and Privilege Escalation
- Location
main.py:27- Finding
Pattern Rules Permit File Moves Outside the Target Directory
- Content
View full analysis
dict: if not s: return {} result = {} for part in s.split(","): if ":" in part: folder, pattern = part.split(":", 1) result[pattern.strip().lower()] = folder.strip() return result ``` ```python elif method == "pattern": name_lower = fp.stem.lower() folder = "Unmatched" for pat, fld in patterns.items(): if pat in name_lower: folder = fld break else: stats["errors"] += 1 continue dest_dir = target / folder dest_path = dest_dir / fp.name if dest_path.exists(): stem, suffix = fp.stem, fp.suffix c = 1 while dest_path.exists(): dest_path = dest_dir / f"{stem}_{c}{suffix}" c += 1 if dry_run: stats["details"].append(f"[DRY] {fp.name} -> {folder}/") else: dest_dir.mkdir(parents=True, exist_ok=True) shutil.move(str(fp), str(dest_path)) ``` ### Technical Analysis The folder parsed from the `--patterns` argument is used directly as part of the destination path. The implementation does not reject: - Absolute paths - `..` parent-directory components - Paths that resolve through symbolic links outside the selected directory - Other destination values that escape the intended organization root In `pathlib`, joining a base path with an absolute path discards the base path. Relative paths containing `..` can similarly resolve outside the target directory. Consequently, `dest_dir.mkdir()` and `shutil.move()` can operate anywhere writable by the current process. The parser also reverses the documented `keyword:folder` format. It treats the text before the colon as the destination folder and the text after it as the filename pattern. This inconsistency increases the likelihood of une ...[truncated 1482 chars]- Remediation
View remediation
