T09 · Insecure Skill Coding Practices
- Location
scripts/zombie_killer.py:50- Finding
Unrestricted File Truncation Through Untrusted Session Metadata
- Content
View full analysis
Vulnerability Details
File Location:
scripts/zombie_killer.py:50-52, 63-72, 84-86
Vulnerability Type: Arbitrary file truncation caused by insufficient path validation
Risk Level: HighVulnerable Code
python for sk, meta in sessions_meta.items(): jsonl_path = meta.get("sessionFile", "") if not jsonl_path or not os.path.exists(jsonl_path): continue updated_at_ms = meta.get("updatedAt", 0) if not updated_at_ms: continue updated_at = datetime.fromtimestamp(updated_at_ms / 1000, tz=timezone.utc) age_hours = (now - updated_at).total_seconds() / 3600 file_size = os.path.getsize(jsonl_path) if age_hours <= min_age_hours or file_size < min_size_bytes: skipped += 1 continue # ... if args.dry_run: print(f"[zombie_killer] DRY RUN — would reset: {sk[:40]} ({age_hours:.1f}h old, {file_size/1_000_000:.1f}MB, {message_count} msgs)") else: try: open(jsonl_path, "w").close()Technical Analysis
The
sessionFilepath is read directly fromsessions.jsonand used as the target of a destructive write operation. The script only checks that the path exists. It does not verify that:- The canonical path is contained within the configured
sessions_dir. - The target is a regular session JSONL file.
- The target is not a symbolic link.
- The path still refers to the same file immediately before truncation.
- The metadata file and target are owned by an expected user.
Opening a path with mode
"w"immediately truncates it. Consequently, a malicious or corruptedsessions.jsonentry can point to any writable file visible to the account running the scheduled job. A symbolic link inside or outside the sessions directory can produce the same result.The age and size conditions do not establish that the target is a legitimate session file. An attacker can supply an old `u ...[truncated 1337 chars]
- The canonical path is contained within the configured
- Remediation
View remediation
Remediation Suggestions
- Resolve both
sessions_dirand each candidate path withos.path.realpath()and enforce containment withos.path.commonpath(). - Reject paths outside the canonical sessions directory.
- Require an expected
.jsonlsuffix and verify that the target is a regular file. - Reject symbolic links with
os.path.islink()and use descriptor-based opening withO_NOFOLLOWwhere supported. - Revalidate file identity immediately before any destructive operation to reduce time-of-check/time-of-use exposure.
- Prefer atomically renaming the file into a restricted quarantine directory instead of truncating it.
- Retain recoverable backups for a defined period before permanent deletion.
- Validate the ownership and permissions of
sessions.json,sessions_dir, and each target. - Run the scheduled task under a dedicated, unprivileged account with write access limited to the intended session and state directories.
- Resolve both
