T09 · Insecure Skill Coding Practices
- Location
scripts/config_manager.py:64- Finding
Unsanitized User Identifiers Permit Filesystem Path Traversal
- Content
View full analysis
bool: try: user_dir = self.users_dir / user_id user_dir.mkdir(exist_ok=True) config_file = user_dir / "current_config.json" if config_file.exists(): self._backup_user_config(user_id) encrypted_config = self._encrypt_sensitive_fields(initial_config.copy()) with open(config_file, 'w', encoding='utf-8') as f: json.dump(encrypted_config, f, ensure_ascii=False, indent=2) return True ``` The same unvalidated identifier is used when loading and backing up configurations: ```python config_file = self.users_dir / user_id / "current_config.json" ``` ```python backup_dir = self.backups_dir / user_id backup_dir.mkdir(exist_ok=True) ``` It is also used for restoration and history files: ```python backup_file = self.backups_dir / user_id / f"config_backup_{backup_timestamp}.json" config_file = self.users_dir / user_id / "current_config.json" shutil.copy2(backup_file, config_file) ``` ```python user_dir = self.users_dir / user_id log_file = user_dir / "optimization_log.json" ``` ```python user_dir = self.users_dir / user_id history_file = user_dir / "feedback_history.json" ``` ### Technical Analysis `user_id` is treated as a safe directory name without validation. Python's `pathlib` does not automatically constrain a joined path to its intended parent. A value containing path separators, `..` components, or an absolute path can therefore resolve outside `data/users` or `dat ...[truncated 1497 chars]- Remediation
View remediation
Path: if not re.fullmatch(r"[A-Za-z0-9_-]{1,64}", user_id): raise ValueError("Invalid user identifier") resolved_root = root.resolve() target = (resolved_root / user_id).resolve() if not target.is_relative_to(resolved_root): raise ValueError("User path escapes storage root") return target ``` 4. Apply equivalent validation to `backup_timestamp`, preferably parsing it as a fixed timestamp format rather than using it as an arbitrary filename component. 5. Obtain the authenticated user identity from trusted framework context rather than accepting it directly from untrusted input. 6. Add authorization checks before loading, restoring, or updating another user's data. 7. Add regression tests covering `../`, nested traversal, absolute paths, Windows drive paths, UNC paths, and encoded separators. ]]>
