T09 · Insecure Skill Coding Practices
- Location
ecom-price-monitor/src/main.py:463- Finding
Unrestricted Export Path Allows Arbitrary JSON File Overwrite
- Content
View full analysis
Vulnerability Details
File Location:
ecom-price-monitor/src/main.py, lines 463-481
Vulnerability Type: Unrestricted file path and arbitrary file overwrite
Risk Level: MediumVulnerable Code
python def export_data(self, format: str = "json", filename: str = None) -> str: """Export data""" if filename is None: timestamp = datetime.now().strftime('%Y%m%d_%H%M%S') filename = f"price_monitor_{timestamp}" data = { "products": {pid: p.to_dict() for pid, p in self.products.items()}, "alerts": [asdict(a) for a in self.alerts], "export_time": datetime.now().isoformat() } if format == "json": filepath = f"{filename}.json" with open(filepath, 'w', encoding='utf-8') as f: json.dump(data, f, ensure_ascii=False, indent=2) return filepath return ""Technical Analysis
The
filenameparameter is incorporated directly into the destination path without validating whether it is an absolute path, contains parent-directory traversal components, or resolves outside an approved export directory. The file is then opened in write mode, which truncates an existing file before writing the exported JSON.An attacker or untrusted agent workflow that can influence the
filenameargument can therefore select any path writable by the process, subject only to the automatic.jsonsuffix. Path normalization and containment checks are absent. Symbolic links can also expand the affected scope if an attacker can prepare a link at the selected destination.Attack Path
- An attacker gains control over, or influences, the filename passed to
PriceMonitor.export_data(). - The attacker supplies an absolute or traversal-based path, such as
../../target. - The method constructs
../../target.json. open(filepath, 'w')follows the supplied path and truncates the destination ...[truncated 862 chars]
- An attacker gains control over, or influences, the filename passed to
- Remediation
View remediation
Remediation Suggestions
- Create a dedicated export directory with appropriately restrictive permissions.
- Accept only a basename rather than an arbitrary path.
- Reject absolute paths, parent-directory components, path separators, and empty filenames.
- Resolve both the export directory and destination with
pathlib.Path.resolve(), then verify that the destination remains under the approved directory. - Use exclusive creation mode when overwriting is not explicitly intended.
- If overwriting is supported, require explicit caller confirmation and avoid following attacker-controlled symbolic links.
- Add tests for absolute paths,
../traversal, nested traversal, symbolic links, and existing-file overwrite attempts.
Example hardening approach:
python from pathlib import Path export_root = Path("exports").resolve() export_root.mkdir(mode=0o700, parents=True, exist_ok=True) safe_name = Path(filename).name if safe_name != filename or not safe_name: raise ValueError("Invalid export filename") destination = (export_root / f"{safe_name}.json").resolve() if export_root not in destination.parents: raise ValueError("Export path escapes the approved directory") with destination.open("x", encoding="utf-8") as f: json.dump(data, f, ensure_ascii=False, indent=2)
