T09 · Insecure Skill Coding Practices
- Location
proposal_gen.py:21- Finding
Path Traversal Enables File Access Outside the Proposal Directory
- Content
View full analysis
- Remediation
View remediation
str: if not PROJECT_NAME_RE.fullmatch(name): raise ValueError("Invalid project name") return name ``` 2. **Resolve and verify every target path.** Use `pathlib.Path.resolve()` and confirm that the final path remains under the proposal directory: ```python from pathlib import Path BASE_DIR = (Path(__file__).resolve().parent / "proposals").resolve() BASE_DIR.mkdir(parents=True, exist_ok=True) def proposal_path(name: str, suffix: str) -> Path: validate_project_name(name) target = (BASE_DIR / f"{name}{suffix}").resolve() if target.parent != BASE_DIR: raise ValueError("Path escapes the proposal directory") return target ``` 3. **Use the centralized safe-path function for every operation:** ```python def load(name): path = proposal_path(name, ".json") if path.exists(): with path.open("r", encoding="utf-8") as handle: return json.load(handle) return None def save(name, data): path = proposal_path(name, ".json") with path.open("w", encoding="utf-8") as handle: json.dump(data, handle, ensure_ascii=False, indent=2) ``` 4. **Protect Markdown generation with the same validation:** ```python output = proposal_path(args.project, ".md") ``` 5. **Avoid unintended overwrites.** For creation operations, use exclusive creation mode (`"x"`) or require explicit overwrite confirmation. For updates, write to a temporary file within `BASE_DIR` and atomically replace the intended target. 6. **Enforce JSON schema validation.** Validate loaded proposal documents before using keys such as `name`, `type`, `section ...[truncated 555 chars]
