T09 · Insecure Skill Coding Practices
- Location
scripts/system_integrator.py:211- Finding
Path Traversal Through Unvalidated Session Identifiers
- Content
View full analysis
Vulnerability Details
File Location:
scripts/system_integrator.py:211-228andscripts/system_integrator.py:784-804
Vulnerability Type: Path traversal in local state save/load operations
Risk Level: MediumVulnerable Code
python def save(self, path=None): save_path = path or (DATA_DIR / f"state_{self.session_id}.json") try: with open(save_path, "w", encoding="utf-8") as fh: fh.write(json.dumps(self.to_dict(), ensure_ascii=False, indent=2)) @classmethod def load(cls, session_id, path=None): load_path = path or (DATA_DIR / f"state_{session_id}.json") if not Path(load_path).exists(): return cls(session_id=session_id) try: with open(load_path, "r", encoding="utf-8") as fh: data = json.loads(fh.read())The same issue exists in
StateManager:python def save(self, path=None): save_path = path or (DATA_DIR / f"state_manager_{self.session_id}.json") try: data = { "session_id": self.session_id, "conversation_state": self.conversation_state.to_dict(), "mode_transitions": self.mode_transitions[-20:], } with open(save_path, "w", encoding="utf-8") as fh: fh.write(json.dumps(data, ensure_ascii=False, indent=2)) def load(self, path=None): load_path = path or (DATA_DIR / f"state_manager_{self.session_id}.json") if not Path(load_path).exists(): return try: with open(load_path, "r", encoding="utf-8") as fh: data = json.loads(fh.read())Technical Analysis
The
session_idvalue is interpolated directly into a filesystem path without validation, canonicalization, or containment checking. Path separators and parent-directory components are therefore accepted.The fixed
state_andstate_manager_prefixes make exploitation conditional: the first prefixed directory ...[truncated 1994 chars]- Remediation
View remediation
Remediation Suggestions
- Restrict session identifiers to a conservative allowlist:
python import re SESSION_ID_PATTERN = re.compile(r"^[A-Za-z0-9_-]{1,64}$") def validate_session_id(session_id: str) -> str: if not SESSION_ID_PATTERN.fullmatch(session_id): raise ValueError("Invalid session identifier") return session_id- Resolve every generated path and verify containment:
python def state_path(prefix: str, session_id: str) -> Path: safe_id = validate_session_id(session_id) base = DATA_DIR.resolve() candidate = (base / f"{prefix}{safe_id}.json").resolve() if candidate.parent != base: raise ValueError("State path escapes the data directory") return candidate- Reject all path separators, absolute paths, null bytes, and
.or..components. - Do not allow externally supplied
patharguments in untrusted interfaces. If custom paths are required, apply the same containment check. - Use atomic writes through a securely created temporary file in
DATA_DIR, followed byos.replace. - Add regression tests for identifiers containing
/,\,.., absolute paths, Unicode separator variants, and excessively long values.
