T09 · Insecure Skill Coding Practices
- Location
src/robin/config.py:27- Finding
Configurable storage paths can escape the Robin state directory
- Content
View full analysis
Path: return state_dir(explicit_state_dir) / config.get("topics_dir", "topics") def media_path(config: dict, explicit_state_dir: str | None = None) -> Path: return state_dir(explicit_state_dir) / config.get("media_dir", "media") ``` The resulting topic path is subsequently used for file creation and overwriting: ```python def _add_to_topic(config: dict, explicit_state_dir: str | None, topic: str, entry_text: str) -> str: base = topics_path(config, explicit_state_dir) base.mkdir(parents=True, exist_ok=True) filepath = base / topic_to_filename(topic) if filepath.exists(): content = filepath.read_text(encoding="utf-8").rstrip() out = content + SEPARATOR + entry_text else: out = entry_text filepath.write_text(out + "\n", encoding="utf-8") return filepath.name ``` It is also used by entry deletion logic: ```python def _topic_files(config: dict, explicit_state_dir: str | None) -> list[Path]: base = topics_path(config, explicit_state_dir) if not base.exists(): return [] return sorted(base.glob("*.md")) def _write_chunks(filepath: Path, chunks: list[str]) -> None: if chunks: filepath.write_text(SEPARATOR.join(chunks) + "\n", encoding="utf-8") elif filepath.exists(): filepath.unlink() ``` ### Technical Analysis The `topics_dir` and `media_dir` configuration fields are joined directly to the resolved state directory without validating their types or requiring them to remain beneath that directory. Python path composition does not provide containment by itself: - If the configured compo ...[truncated 2464 chars]- Remediation
View remediation
Path: value = configured if configured is not None else default if not isinstance(value, str) or not value.strip(): raise SystemExit(f"{default} path must be a non-empty string.") relative = Path(value) if relative.is_absolute() or ".." in relative.parts: raise SystemExit(f"{default} path must remain inside the state directory.") root = root.resolve() target = (root / relative).resolve() try: target.relative_to(root) except ValueError as exc: raise SystemExit(f"{default} path escapes the state directory.") from exc return target ``` 4. Use this containment helper in both `topics_path()` and `media_path()`. 5. Revalidate containment immediately before destructive operations to reduce symlink-related time-of-check/time-of-use risk. 6. Validate that `robin-config.json` is a JSON object and that every supported field has the expected type. 7. Add regression tests covering: - Absolute Unix and Windows paths. - `../` traversal. - Nested traversal. - Symlinks leading outside the state directory. - Non-string path fields. - Add, move, delete, search, and media-copy operations under malicious configurations. ]]>
