T09 · Insecure Skill Coding Practices
- Location
scripts/write_memory.py:27- Finding
Path Traversal Enables Unauthorized Access to Markdown Files
- Content
View full analysis
str: """ Get the memory file path. Args: date: Date string, defaults to the current date. Returns: Memory file path. """ if date is None: date = datetime.now().strftime('%Y-%m-%d') return os.path.join(self.memory_dir, f'{date}.md') ``` The generated path is subsequently used by the public file operations: ```python def write(self, content: str, date: str = None, append: bool = True) -> bool: file_path = self.get_memory_file_path(date) ... with open(file_path, mode, encoding='utf-8') as f: f.write(content) ``` ```python def exists(self, date: str = None) -> bool: return os.path.exists(self.get_memory_file_path(date)) ``` ```python def read(self, date: str = None) -> Optional[str]: file_path = self.get_memory_file_path(date) if os.path.exists(file_path): with open(file_path, 'r', encoding='utf-8') as f: return f.read() return None ``` ### Technical Analysis The `date` parameter is treated as a trusted filename component without validation or canonicalization. It can contain directory traversal sequences such as `../`, directory separators, or an absolute path. For example, a value such as `../../notes/private` produces a path ending in: ```text /memory/../../notes/private.md ``` After filesystem path resolution, this points outside the intended memory directory. If the supplied value begins with an absolute path separator, `os.path.join()` can discard the configured memory directory entirely. The flaw affects `write()`, `read()`, and `exists()`. The `.md` suffix limits direct access to paths ending in that extension, but it does not enforce containment within the m ...[truncated 1462 chars]- Remediation
View remediation
str: parsed = datetime.strptime(date, "%Y-%m-%d") normalized = parsed.strftime("%Y-%m-%d") if normalized != date: raise ValueError("Invalid date format") return normalized ``` 2. Reject absolute paths, directory separators, traversal components, null bytes, and any value that is not a valid date. 3. Resolve both the memory directory and target path, then enforce containment: ```python from pathlib import Path base = Path(self.memory_dir).resolve() target = (base / f"{validated_date}.md").resolve() if target.parent != base: raise ValueError("Target path escapes the memory directory") ``` 4. Define a policy for symbolic links. For sensitive storage, reject symlink targets and open files using platform-supported no-follow protections where available. 5. Run the Skill under a dedicated, least-privileged account rather than root. 6. Add tests covering `../`, absolute paths, nested paths, malformed dates, and symlink-based escapes. ]]>
