T09 · Insecure Skill Coding Practices
- Location
scripts/update_memory.py:122- Finding
Unrestricted Destination Path and Symlink Traversal Enable Arbitrary File Modification
- Content
View full analysis
Tuple[str, str]: """ Upsert content into /. Returns (action, filepath_str) where action ∈ {"updated", "appended", "would_update", "would_append"}. """ filepath = Path(workspace) / filename if not filepath.exists(): if not dry_run: filepath.parent.mkdir(parents=True, exist_ok=True) filepath.write_text(f"# {filename.replace('.md', '')}\n\n", encoding="utf-8") # When dry-run on a non-existing file, treat as "would_append" return ("would_append" if dry_run else "appended", str(filepath)) raw = filepath.read_text(encoding="utf-8") lines = raw.splitlines(keepends=True) heading_line = match_by_heading(lines, key) if heading_line is not None: if dry_run: return ("would_update", str(filepath)) start, end = get_section_range(lines, heading_line) lines[start:end] = [content.rstrip("\n") + "\n"] filepath.write_text("".join(lines), encoding="utf-8") return ("updated", str(filepath)) if dry_run: return ("would_append", str(filepath)) suffix = "\n" if raw and not raw.endswith("\n") else "" filepath.write_text(raw + suffix + "\n" + content.rstrip("\n") + "\n", encoding="utf-8") return ("appended", str(filepath)) ``` ### Technical Analysis The destination is formed directly from an accepted workspace and an unvalidated filename: ```python filepath = Path(workspace) / filename ``` The implementation does not reject absolute filenames, `..` traversal components, or destinations reached through symbolic links. With `pathlib`, an absolute right-hand ...[truncated 1978 chars]- Remediation
View remediation
