T09 · Insecure Skill Coding Practices
- Location
scripts/load_session.py:26- Finding
Path Traversal Allows Reading JSONL Files Outside the Session Directory
- Content
View full analysis
Vulnerability Details
File Location:
scripts/load_session.py, lines 26-34
Vulnerability Type: Unrestricted path construction and local file disclosure
Risk Level: HighVulnerable Code
python def find_session_file(session_id): for project_dir in PROJECTS_DIR.iterdir(): if not project_dir.is_dir(): continue candidate = project_dir / (session_id + ".jsonl") if candidate.exists(): return candidate return NoneTechnical Analysis
The value of
session_idcomes from the--idcommand-line argument and is used to construct a filesystem path without validating that it is a UUID or simple filename. The code does not reject path separators, absolute paths, or..traversal components.With Python's
pathlib, joining a base directory with an absolute path causes the absolute component to replace the preceding base path. Relative paths containing../can similarly escape the intended project directory. The program only appends.jsonl, so exploitation is limited to readable files with that suffix or targets addressable through filesystem links, but no canonical-path containment check prevents access outside~/.codebuddy/projects.The selected file is subsequently opened by
parse_session(). Any valid JSONL records whosetypeismessageortopicare parsed and printed.Attack Path
- An attacker identifies an arbitrary readable JSONL file containing compatible records.
- The attacker supplies an absolute path or traversal path as the
--idvalue, excluding the.jsonlsuffix because the program appends it. find_session_file()constructs a path outside the intended CodeBuddy project directory.candidate.exists()accepts the escaped path.parse_session()opens the file and extracts recognized message or topic fields.- The selected output mode prints the extracted content to the terminal and pote ...[truncated 525 chars]
- Remediation
View remediation
Remediation Suggestions
- Validate
session_idagainst a strict UUID format before using it in a path. For example, parse it withuuid.UUIDand require its canonical string representation. - Reject values containing
/,\,.., null bytes, or other path syntax. - Resolve the candidate and approved base paths with
Path.resolve(), then verify that the candidate remains beneath the approved project directory. - Require the candidate to be a regular file rather than merely checking
exists(). - Avoid constructing paths directly from user input. Build an index of valid session files and select only exact IDs present in that index.
- Add tests covering absolute paths, traversal components, symlinks, malformed UUIDs, and valid session identifiers.
- Validate
