T09 · Insecure Skill Coding Practices
- Location
travel_planner.py:723- Finding
Caller-Controlled Filenames Permit Arbitrary File Overwrite
- Content
View full analysis
Vulnerability Details
File Locations:
travel_planner.py:723-734travel_planner_extensions.py:632-641travel_planner_extensions.py:967-976
Vulnerability Type: Path traversal and unrestricted file write
Risk Level: MediumVulnerable Code
python def save_itinerary(self, itinerary: Dict, filename: Optional[str] = None) -> str: if not filename: timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") filename = f"itinerary_{timestamp}.json" filepath = os.path.join(self.data_dir, filename) serializable_itinerary = self._make_serializable(itinerary) with open(filepath, 'w', encoding='utf-8') as f: json.dump(serializable_itinerary, f, ensure_ascii=False, indent=2) return filepathpython def save_pdf_content(self, content: str, filename: Optional[str] = None) -> str: if not filename: timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") filename = f"itinerary_{timestamp}.md" filepath = os.path.join(self.data_dir, filename) with open(filepath, 'w', encoding='utf-8') as f: f.write(content) return filepathpython def save_diary_template(self, content: str, filename: Optional[str] = None) -> str: if not filename: timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") filename = f"travel_diary_{timestamp}.md" filepath = os.path.join(self.data_dir, "diaries", filename) with open(filepath, 'w', encoding='utf-8') as f: f.write(content) return filepathTechnical Analysis
These public save methods directly combine a caller-supplied filename with an intended storage directory. They do not reject absolute paths, parent-directory components, path separators, symbolic-link targets, or filenames resolving outside the expected directory.
A relative value containing parent-directory components can escape `~/.travel-planne ...[truncated 1796 chars]
- Remediation
View remediation
Remediation Suggestions
- Prefer generating all storage filenames internally rather than accepting paths from callers.
- If custom names are required, accept only a basename and reject absolute paths, parent-directory components, path separators, null bytes, and unexpected extensions.
- Resolve both the base directory and candidate path with
pathlib.Path.resolve(), then verify that the candidate remains beneath the intended base directory. - Use separate validation for the itinerary, export, and diary directories.
- Consider exclusive creation mode when overwriting existing files is not required.
- Protect against symbolic-link attacks where untrusted users can modify the storage directory.
- Apply restrictive directory and file permissions appropriate to the sensitivity of travel and expense data.
- Add tests covering absolute paths, parent traversal, nested traversal, symbolic links, alternate path separators, and valid filenames.
Example hardening pattern:
python from pathlib import Path def safe_output_path(base_directory: str, filename: str, suffix: str) -> Path: if not filename or Path(filename).name != filename: raise ValueError("A plain filename is required") if not filename.endswith(suffix): raise ValueError("Unexpected file extension") base = Path(base_directory).expanduser().resolve() candidate = (base / filename).resolve() if candidate.parent != base: raise ValueError("The output path is outside the allowed directory") return candidate
