T09 · Insecure Skill Coding Practices
- Location
scripts/__init__.py:96- Finding
Path Traversal Allows Arbitrary Local File Reading
- Content
View full analysis
Vulnerability Details
File Location:
scripts/__init__.py, lines 96-100
Vulnerability Type: Path traversal and unrestricted file read
Risk Level: MediumVulnerable Code
python filepath = self.kp_path / filename if not filepath.exists(): return [] content = filepath.read_text(encoding="utf-8")Technical Analysis
The
parse_kp_file()method accepts a caller-suppliedfilenameand joins it directly toself.kp_pathwithout checking whether the resulting path remains inside the configured knowledge-point directory.A filename containing parent-directory components, such as
../../sensitive-file, can escape the intended directory. In addition, Python'spathlibdiscards the preceding base path when the second operand is absolute, so an absolute filename can directly select another file.The existence check does not mitigate this issue because it occurs only after the unsafe path has been constructed. The subsequent
read_text()operation reads the selected file under the privileges of the running process. The file must be readable as UTF-8 for the method to return its contents successfully.Attack Path
- An attacker gains control over the
filenameargument passed toKnowledgePointParser.parse_kp_file(). - The attacker supplies a traversal path such as
../../outside-directory/file.md, or an absolute path. self.kp_path / filenameresolves to a location outside the intended knowledge-point directory.- The existence check succeeds for an accessible target.
read_text()reads the target file.- The file content is passed to the Markdown parser, potentially exposing parsed content to the caller or downstream report processing.
Impact Assessment
An attacker can cause the process to read arbitrary UTF-8 text files that are accessible to the operating-system account running the skill. The scope is limited by that account's filesystem permissions and by ...[truncated 255 chars]
- An attacker gains control over the
- Remediation
View remediation
Remediation Suggestions
Resolve and validate all caller-provided paths before reading:
python base = self.kp_path.resolve() supplied = Path(filename) if supplied.is_absolute() or ".." in supplied.parts: raise ValueError("Invalid knowledge-point filename") target = (base / supplied).resolve() if target.parent != base: raise ValueError("File must remain in the knowledge-point directory") content = target.read_text(encoding="utf-8")Additional hardening measures:
- Accept only simple filenames rather than paths.
- Enforce the documented naming convention, such as
knowledge-points-YYYY-MM-DD.md. - Reject path separators, absolute paths,
.components, and..components. - If subdirectories must be supported, use
target.is_relative_to(base)after resolving both paths. - Check that the resolved target is a regular file and apply an appropriate maximum file-size limit.
- Add tests covering absolute paths, traversal paths, symbolic links, invalid encodings, and paths outside the configured base directory.
