T09 · Insecure Skill Coding Practices
- Location
scripts/__init__.py:72- Finding
Path Traversal Enables Arbitrary Local File Read and Write
- Content
View full analysis
Vulnerability Details
File Location:
scripts/__init__.py:72andscripts/__init__.py:299-317
Vulnerability Type: Unvalidated path traversal
Risk Level: HighVulnerable Code
python # WikiParser.parse_wiki_file() filepath = self.wiki_path / filename if not filepath.exists(): return [] entries = [] lines = filepath.read_text(encoding="utf-8").split("\n")python # CompilePipeline.save_knowledge_points() if filename is None: date_str = datetime.now().strftime("%Y-%m-%d") filename = f"knowledge-points-{date_str}.md" filepath = self.output_path / filename lines = [ f"# Knowledge Points - {datetime.now().strftime('%Y-%m-%d')}", "", f"Generated {len(knowledge_points)} knowledge points", "", "---", "" ] for kp in knowledge_points: lines.append(kp.to_markdown()) lines.append("") lines.append("---") lines.append("") with open(filepath, "w", encoding="utf-8") as f: f.write("\n".join(lines))Technical Analysis
Both
parse_wiki_file()andsave_knowledge_points()accept a filename and append it to a configured base directory without validating the resulting path. Python'spathlibpermits..components to escape the base directory. If the supplied path is absolute, the absolute path supersedes the base directory entirely.Consequently, a caller capable of controlling
filenamecan cause:parse_wiki_file()to read an arbitrary accessible UTF-8 file.save_knowledge_points()to create or overwrite an arbitrary writable file.
No canonicalization, containment check, filename allowlist, extension restriction, or safe file-creation policy is applied.
Attack Path
- An attacker obtains access to an integration, API, or calling component that passes a filename to either affected method.
- For arbitrary reads, the attacker supplies a traversal value such as
../../sensitive-file. self.wiki_path / filenameresolves outside the intended Wiki d ...[truncated 876 chars]
- Remediation
View remediation
Remediation Suggestions
- Reject absolute filenames and any filename containing traversal components.
- Resolve both the base directory and candidate path before use, then enforce containment:
python base = self.wiki_path.resolve() candidate = (base / filename).resolve() try: candidate.relative_to(base) except ValueError: raise ValueError("Filename escapes the permitted directory")- Apply the same validation independently to the output directory.
- Restrict filenames to the expected naming convention and
.mdextension. - For outputs, consider exclusive creation or atomic replacement through a temporary file inside the validated output directory.
- Avoid exposing raw filename parameters to untrusted callers; accept a generated identifier where possible.
- Add tests covering
../, nested traversal, absolute paths, symbolic links, and valid in-directory files.
