T09 · Insecure Skill Coding Practices
- Location
backup_wrapper.py:25- Finding
Project Directory Traversal Through Unvalidated Relative Paths
- Content
View full analysis
Vulnerability Details
File Location:
backup_wrapper.py, lines 25-42
Vulnerability Type: Path traversal and insufficient path validation
Risk Level: HighVulnerable Code
python if action == 'backup': rel_path = sys.argv[2] full_path = os.path.join(PROJECT_PATH, rel_path) element = sys.argv[3] if len(sys.argv) > 3 else None result = sb.backup(full_path, element) print(result) elif action == 'list': rel_path = sys.argv[2] full_path = os.path.join(PROJECT_PATH, rel_path) versions = sb.list_versions(full_path) for i, v in enumerate(versions): print(f"{i}: {v['time']} | {v.get('element', 'baseline')}") elif action == 'restore': rel_path = sys.argv[2] full_path = os.path.join(PROJECT_PATH, rel_path) idx = int(sys.argv[3]) if len(sys.argv) > 3 else 0 result = sb.restore(full_path, idx) print(result)Technical Analysis
The wrapper treats
sys.argv[2]as a project-relative path but does not verify that it is actually relative or that its canonical destination remains insidePROJECT_PATH.os.path.join()performs path construction only; it does not enforce directory containment.A path containing parent-directory components, such as
..\..\sensitive-file, can resolve outside the intended project directory. On supported platforms, an absolute path can also override or otherwise escape the intended base-path semantics. The resulting path is passed toSmartBackup.backup(),SmartBackup.list_versions(), orSmartBackup.restore()without validation.The exact filesystem behavior of those methods cannot be established because
backup_coreis not included in the audited project. Nevertheless, the wrapper violates its own trust boundary and provides the external component with attacker-selected out-of-project paths.Attack Path
...[truncated 1422 chars]
- Remediation
View remediation
Remediation Suggestions
- Reject absolute input paths.
- Canonicalize both the project root and candidate path with
os.path.realpath(). - Verify containment with
os.path.commonpath()before invoking any backup-engine method. - Apply the validation uniformly to
backup,list, andrestore. - Reject paths that resolve through symbolic links outside the project.
- Add tests covering parent traversal, absolute paths, mixed separators, drive-qualified paths, symbolic links, and valid project-relative paths.
- Implement equivalent containment validation inside
SmartBackupas defense in depth.
Example hardening pattern:
python def resolve_project_path(rel_path): if os.path.isabs(rel_path): raise ValueError("Absolute paths are not allowed") project_root = os.path.realpath(PROJECT_PATH) candidate = os.path.realpath(os.path.join(project_root, rel_path)) try: contained = os.path.commonpath([project_root, candidate]) == project_root except ValueError: contained = False if not contained: raise ValueError("Path escapes the project directory") return candidateReplace each direct
os.path.join(PROJECT_PATH, rel_path)call withresolve_project_path(rel_path).
