T09 · Insecure Skill Coding Practices
- Location
scripts/memory.py:101- Finding
Path Traversal Through Unsanitized User Identifiers
- Content
View full analysis
str: return os.path.join(self.storage_dir, f"{user_id}.json") def _load(self, user_id: str) -> dict: path = self._filepath(user_id) if not os.path.exists(path): return {"user_id": user_id, "memories": []} try: with open(path, "r", encoding="utf-8") as f: return json.load(f) except (json.JSONDecodeError, OSError): return {"user_id": user_id, "memories": []} def _save(self, user_id: str, data: dict): path = self._filepath(user_id) with open(path, "w", encoding="utf-8") as f: json.dump(data, f, ensure_ascii=False, indent=2) ``` #### `scripts/profiles.py:298-324` ```python def save_composite(user_id: str, data: dict): comp_dir = PROFILES_DIR / "composite" comp_dir.mkdir(parents=True, exist_ok=True) path = comp_dir / f"{user_id}.json" composite = { "user_id": user_id, "name": data.get("name", ""), "first_seen": data.get("first_seen", int(time.time())), "last_updated": data.get("last_updated", int(time.time())), "update_count": data.get("update_count", 0), "group_roles": data.get("group_roles", {}), } with open(path, "w", encoding="utf-8") as f: json.dump(composite, f, ensure_ascii=False, indent=2) def load_composite(user_id: str) -> dict: path = PROFILES_DIR / "composite" / f"{user_id}.json" if not path.exists(): return {} try: with open(path, "r", encoding="utf-8") as f: return json.load(f) except (json.JSONDecodeError, OSError): return {} ``` #### `scripts/affection.py:103-119` ```python def _log_ ...[truncated 3635 chars]- Remediation
View remediation
str: value = str(user_id) if not re.fullmatch(r"[0-9]{1,32}", value): raise ValueError("Invalid user identifier") return value ``` 2. Resolve and verify every generated path: ```python from pathlib import Path def safe_user_json_path(root: Path, user_id: str) -> Path: safe_id = validate_user_id(user_id) root = root.resolve() candidate = (root / f"{safe_id}.json").resolve() if candidate.parent != root: raise ValueError("Path escapes the storage directory") return candidate ``` 3. Use this single helper in `memory.py`, `profiles.py`, and `affection.py` rather than implementing path construction separately. 4. Reject path separators, traversal components, absolute paths, null bytes, and platform-specific separator variants even if support for nonnumeric identifiers is required. 5. Run the Skill under a dedicated account with write permission only to its data directories. Project source, configuration, and unrelated user files should be read-only where practical. 6. Use atomic writes through a temporary file created inside the validated destination directory, followed by `os.replace()`. 7. Add regression tests for identifiers including: ```text ../config ../../config ..\config /absolute/path C:\absolute\path user/other user\other ``` Each test should verify that the operation is rejected and that no file outside the designated storage root is read or modified. ]]>
