T09 · Insecure Skill Coding Practices
- Location
scripts/novel_manager.py:53- Finding
Path Traversal Through Unsanitized Novel Directory Names
- Content
View full analysis
Path: """Get the chapter directory path.""" return get_novels_dir() / novel_name / "chapters" / f"{volume:02d}-{chapter:03d}" ``` ```python # lines 290–294 chapters_dir = get_novels_dir() / novel_name / "chapters" if not chapters_dir.exists(): return [] ``` ```python # lines 353–354 meta_path = get_novels_dir() / novel_name / "meta.md" ``` ```python # lines 377–378 novel_dir = get_novels_dir() / novel_name if not novel_dir.exists(): ``` ### Technical Analysis The application constructs filesystem paths by directly joining caller-controlled `name_en` and `novel_name` values with the relative `novels` root. The `sanitize_name()` function is applied only when `name_en` is omitted. An explicitly supplied English name is accepted without validation. Furthermore, subsequent operations such as chapter creation, status retrieval, backlog generation, and metadata updates use `novel_name` without sanitization or canonical containment checks. Python's `pathlib.Path` join operation does not remove or reject parent-directory components. For example: ```text novels/../../tmp/attacker-book ``` resolves outside the intended `novels/` directory. Absolute path components may also replace the preceding path during path composition. The vulnerable path is subsequently used by directory-creation, file-read, and file-write operations. Chapter creation is especially relevant because it creates missing parent directories and writes templates using `write_text()`, overwriting files with the expected fix ...[truncated 2554 chars]- Remediation
View remediation
str: if not name or not SAFE_NOVEL_NAME.fullmatch(name): raise ValueError("Invalid novel name") return name ``` 2. **Apply validation consistently** Validate both `name_en` and every `novel_name` argument, regardless of whether the value was explicitly supplied or generated from another name. 3. **Enforce canonical path containment** Resolve the root and candidate path before every filesystem operation and confirm that the candidate remains beneath the root: ```python def safe_novel_dir(name: str) -> Path: validated_name = validate_novel_name(name) root = get_novels_dir().resolve() candidate = (root / validated_name).resolve() if not candidate.is_relative_to(root): raise ValueError("Novel path escapes the configured root") return candidate ``` For Python versions without `Path.is_relative_to()`: ```python try: candidate.relative_to(root) except ValueError: raise ValueError("Novel path escapes the configured root") ``` 4. **Use the safe helper everywhere** Replace direct expressions such as: ```python get_novels_dir() / novel_name ``` with `safe_novel_dir(novel_name)` in creation, chapter, backlog, status, metadata, chapter-writer, and consistency-checker paths. 5. **Prevent accidental overwrite** Before writing chapter files, either reject existing files or require an explicit overwrite option: ```python content_path = chapter_dir / "content.md" if content_path.exists() and not overwrite: raise FileExistsError(f"Refusing to overwrite {content_path}") ...[truncated 315 chars]
