T09 · Insecure Skill Coding Practices
- Location
scripts/micro-compact.py:51- Finding
Unvalidated Session Identifier Enables Path Traversal and Out-of-Scope JSONL File Access
- Content
View full analysis
Vulnerability Details
File Locations:
scripts/micro-compact.py:51-57— constructs a session path from an unvalidated identifier.scripts/micro-compact.py:228-242— backs up and overwrites the selected file.scripts/compact-trigger.py:47-53— constructs a session path from an unvalidated identifier.scripts/compact-trigger.py:128-134— constructs source and archive paths from the same unvalidated identifier.
Vulnerability Type: Path traversal and unrestricted filesystem access
Risk Level: HighVulnerable Code
scripts/micro-compact.py:51-57:python def read_session(session_id): """读取会话文件""" sessions_dir = Path(CONFIG['sessions_dir']) session_file = sessions_dir / f"{session_id}.jsonl" if not session_file.exists(): return Nonescripts/micro-compact.py:228-242:python def save_session(session_data, modified_messages): """保存修改后的会话""" session_file = session_data['file_path'] # 备份原文件 backup_file = session_file.with_suffix('.jsonl.backup') if session_file.exists(): import shutil shutil.copy2(session_file, backup_file) print(f" 📦 备份原文件: {backup_file}") # 写入新文件 with open(session_file, 'w', encoding='utf-8') as f: for item in modified_messages: f.write(json.dumps(item['data'], ensure_ascii=False) + '\n')scripts/compact-trigger.py:47-53:python def read_session(session_id): """读取会话文件""" sessions_dir = get_sessions_dir() session_file = sessions_dir / f"{session_id}.jsonl" if not session_file.exists(): return Nonescripts/compact-trigger.py:128-134:python def archive_session(session_data, archive_dir): """归档会话到归档目录""" session_id = session_data['session_id'] archive_path = archive_dir / f"{session_id}.jsonl" sessions_dir = get_session ...[truncated 3282 chars]- Remediation
View remediation
Remediation Suggestions
-
Treat the session argument strictly as an identifier, not as a path. Enforce an allowlist such as
^[A-Za-z0-9_-]+$and reject empty values, path separators, drive prefixes, absolute paths, and..components. -
Resolve the trusted base directory and candidate path before access, then require the candidate to be a direct child of the base directory:
python import re from pathlib import Path SESSION_ID_PATTERN = re.compile(r"^[A-Za-z0-9_-]+$") def safe_session_path(session_id): if not SESSION_ID_PATTERN.fullmatch(session_id): raise ValueError("Invalid session identifier") base = Path(CONFIG["sessions_dir"]).expanduser().resolve() candidate = (base / f"{session_id}.jsonl").resolve() if candidate.parent != base: raise ValueError("Session path escapes the session directory") return candidate -
Apply an independent containment check to every archive destination. Do not assume that validating a source path automatically validates a separately constructed destination.
-
Reject symlinked session files or securely verify their final resolved targets if symlinks are not required by the Skill.
-
Replace direct truncating writes with an atomic update: write to a securely created temporary file in the validated session directory, flush and synchronize it, then use
os.replace(). -
Preserve the original file on parsing or writing failure and assign restrictive permissions to backups because session archives may contain credentials, source code, tool output, and other sensitive conversation data.
-
Add tests covering absolute paths,
../traversal, nested paths, symlinks, malformed identifiers, archive-path escape attempts, and valid session identifiers.
-
