T09 · Insecure Skill Coding Practices
- Location
scripts/index_knowledge.py:218- Finding
Arbitrary Recursive Directory Deletion Through Unvalidated Output Path
- Content
View full analysis
Vulnerability Details
File Location:
scripts/index_knowledge.py:218-225andscripts/index_knowledge_ollama.py:273-280
Vulnerability Type: Unrestricted recursive directory deletion
Risk Level: HighVulnerable Code
scripts/index_knowledge.py:218-225:python # Check whether a forced rebuild was requested output_path = Path(args.output_dir) if output_path.exists() and args.rebuild: print(f"\n⚠️ Existing vector store detected; deleting it...") import shutil shutil.rmtree(output_path) print("✓ Old data removed")The Ollama implementation contains the equivalent behavior at
scripts/index_knowledge_ollama.py:273-280:python # Check whether a forced rebuild was requested output_path = Path(args.output_dir) if output_path.exists() and args.rebuild: print(f"\n⚠️ Existing vector store detected; deleting it...") import shutil shutil.rmtree(output_path) print("✓ Old data removed")Technical Analysis
The user-controlled
--output-dirargument is converted directly into aPath. When--rebuildis supplied, the application recursively deletes that path usingshutil.rmtree().The code does not:
- Resolve and validate the canonical destination.
- Restrict deletion to a designated workspace or vector-store directory.
- Verify that the destination contains a valid application-generated vector store.
- Reject dangerous targets such as a home directory, project directory, mounted data directory, or a path reached through a symbolic link.
- Require explicit confirmation before destructive deletion.
As a result, any existing directory writable by the current process can be selected for recursive deletion. The same flaw exists in both indexing variants.
Attack Path
- An attacker persuades a user or agent to invoke an indexing script with a malicious
--output-dirvalue. - The attacker also causes the
--rebuildop ...[truncated 1031 chars]
- Remediation
View remediation
Remediation Suggestions
- Resolve the destination with
Path.resolve()before performing any operation. - Restrict output directories to an explicitly approved workspace root and verify containment with
Path.is_relative_to()or an equivalent robust check. - Reject dangerous targets, including the file-system root, user home, project root, workspace root, and shallow parent directories.
- Create a unique marker file when initializing a vector store and permit rebuilding only when that marker exists and has the expected contents.
- Detect and reject symbolic-link destinations and unexpected path components.
- Require interactive confirmation showing the canonical path unless a separately protected noninteractive option is used.
- Prefer deleting only known Chroma artifacts rather than recursively deleting the entire selected directory.
- Apply the same validation helper to both indexing implementations and add tests covering traversal, symbolic links, root paths, home paths, and unrelated directories.
- Resolve the destination with
