T09 · Insecure Skill Coding Practices
- Location
tools/skill_writer.py:43- Finding
Unvalidated memorial slug permits filesystem path traversal
- Content
View full analysis
str: return os.path.join(MEMORIALS_DIR, slug) def ensure_dirs(slug: str) -> None: base = memorial_dir(slug) for subdir in ["versions", "materials"]: os.makedirs(os.path.join(base, subdir), exist_ok=True) ``` The command-line interface accepts the value without validation: ```python parser.add_argument("--slug", help="目录 slug(自动生成或手动指定)") ... if args.action == "create": if not args.name: parser.error("--action create 需要 --name 参数") action_create(args.name, args.slug) elif args.action == "combine": if not args.slug: parser.error("--action combine 需要 --slug 参数") action_combine(args.slug) elif args.action == "update": if not args.slug: parser.error("--action update 需要 --slug 参数") action_update(args.slug, args.source or "") ``` The same unsafe construction is used by version management: ```python def memorial_dir(slug: str) -> str: return os.path.join(MEMORIALS_DIR, slug) def versions_dir(slug: str) -> str: return os.path.join(memorial_dir(slug), "versions") ``` ### Technical Analysis The user-controlled `slug` is concatenated with `MEMORIALS_DIR` using `os.path.join()` without validating its characters or verifying that the resolved path remains inside the memorial directory. `os.path.join()` does not provide path confinement. A value containing parent-directory components, such as `../../target`, resolves outside `memorials/`. An absolute path can also discard the intended base directory on supported platforms. Multiple write-capable operations inherit this weakness: - Archive creation writes `meta.j ...[truncated 2065 chars]- Remediation
View remediation
str: if not SLUG_RE.fullmatch(slug): raise ValueError("Invalid memorial slug") return slug ``` 2. Resolve and verify every derived path: ```python from pathlib import Path MEMORIALS_ROOT = Path(MEMORIALS_DIR).resolve() def memorial_dir(slug: str) -> Path: validate_slug(slug) candidate = (MEMORIALS_ROOT / slug).resolve() if not candidate.is_relative_to(MEMORIALS_ROOT): raise ValueError("Memorial path escapes the archive root") return candidate ``` 3. Use the same shared validator in `skill_writer.py`, `version_manager.py`, `voice_trainer.py`, and `voice_synthesizer.py`. 4. Before recursive deletion, verify containment again and reject symlinked archive or version directories. 5. Use atomic file creation where overwriting is not required. 6. Add negative tests for: - `../` - `../../target` - Absolute paths - Windows drive and UNC paths - Encoded or mixed path separators - Symlink-based escapes ]]>
