T09 · Insecure Skill Coding Practices
- Location
scripts/benchmark.py:74- Finding
Unsafe Recursive Deletion of a Reused Fixed Directory
- Content
View full analysis
Vulnerability Details
File Location:
scripts/benchmark.py, lines 74–75 and 109–110
Vulnerability Type: Unsafe temporary-directory handling and recursive deletion
Risk Level: MediumVulnerable Code
python # Create test environment test_dir = Path("D:/Test-Benchmark") test_dir.mkdir(exist_ok=True) # Simulate large state data state_data = { "active_projects": [ { "name": f"project_{i}", "status": "active", "progress": i * 10, "tags": ["development", "testing", "optimization"] } for i in range(100) ], "learned_lessons": [ { "id": f"lesson_{j}", "description": f"Important lesson {j}", "impact": "high", "date": "2026-03-05" } for j in range(50) ] } # Save state state_file = test_dir / "benchmark-state.json" with open(state_file, 'w', encoding='utf-8') as f: json.dump(state_data, f, indent=2) # Test load performance start = time.time() with open(state_file, 'r', encoding='utf-8') as f: loaded_data = json.load(f) load_time = time.time() - start print(f"Loaded state in: {load_time:.3f} seconds") # Cleanup import shutil shutil.rmtree(test_dir)Technical Analysis
The benchmark uses a fixed path,
D:/Test-Benchmark, as though it were an isolated temporary directory. Callingmkdir(exist_ok=True)silently reuses that directory when it already exists. The cleanup then passes the whole directory toshutil.rmtree, recursively deleting every file and subdirectory rather than only the benchmark-created state file.On Windows, the path refers to
D:\Test-Benchmark. On platforms whereD:is not interpreted as a drive designator, it may resolve as a relative path beneath the current working directory. In either case, the code does not verify that the directory was newly created, that it is owned exclusively by this benchmark run, or that it contains no unrelated data....[truncated 1409 chars]
- Remediation
View remediation
Remediation Suggestions
Use a unique, automatically managed temporary directory instead of a fixed path:
python import json import tempfile import time from pathlib import Path def benchmark_state_recovery(): with tempfile.TemporaryDirectory(prefix="ultimate-agent-benchmark-") as temp_dir: test_dir = Path(temp_dir) state_file = test_dir / "benchmark-state.json" with state_file.open("w", encoding="utf-8") as f: json.dump(state_data, f, indent=2) start = time.time() with state_file.open("r", encoding="utf-8") as f: loaded_data = json.load(f) load_time = time.time() - start return load_timeAdditional hardening measures:
- Never recursively delete a fixed or potentially pre-existing directory as benchmark cleanup.
- If a configurable output path is required, resolve it with
Path.resolve()and refuse to continue when it already exists. - Track and delete only files created by the current run.
- Use a
try/finallyblock orTemporaryDirectorycontext manager so cleanup occurs safely after exceptions. - Avoid following or traversing attacker-controlled links during cleanup.
- Add a regression test that pre-populates the candidate path with an unrelated sentinel file and verifies that the benchmark neither reuses nor deletes it.
