T09 · Insecure Skill Coding Practices
Error
- Location
- scripts/init_context_project.py:99
- Finding
- Unvalidated Project Name Enables Path Traversal and Arbitrary File Creation<![CDATA[ ## Vulnerability Details **File Location**: `scripts/init_context_project.py`, lines 99-149 **Vulnerability Type**: Path traversal and insufficient input validation **Risk Level**: High ### Vulnerable Code ```python def main(): parser = argparse.ArgumentParser(description="Initialize a team-style project context directory.") parser.add_argument("--project", required=True, help="Project name (folder name).") parser.add_argument("--code-dir", required=True, help="Absolute path to the code directory.") parser.add_argument( "--target-root", default=str(Path.home() / "clawDir" / "team"), help="Target root for team context (default: ~/clawDir/team).", ) args = parser.parse_args() target_root = Path(args.target_root).expanduser().resolve() code_dir = Path(args.code_dir).expanduser().resolve() project_root = target_root / "projects" / args.project date = datetime.now().strftime("%Y-%m-%d") created = [] created.append(write_if_missing(target_root / "readme.md", "# Team Directory Guide\n\n- Keep navigation here.\n")) append_project_index(target_root / "projects" / "projects.md", args.project) write_if_missing(project_root / "readme.md", TEMPLATE_README.format(project=args.project, code_dir=code_dir, project_root=project_root)) write_if_missing(project_root / "goals.md", TEMPLATE_GOALS) write_if_missing(project_root / "skill.md", TEMPLATE_SKILL) write_if_missing(project_root / "project_status.md", TEMPLATE_STATUS.format(date=date)) write_if_missing(project_root / "decisions.md", TEMPLATE_DECISIONS.format(date=date)) write_if_missing(project_root / "agents" / "agents.md", TEMPLATE_AGENTS) write_if_missing(project_root / "modules" / "README.md", TEMPLATE_MODULES_README) write_if_missing(project_root / "references" / "entrypoints.md", "# Entrypoints\n\n- TODO: record key entrypoints and indices.\n") modules = infer_modules(code_dir) for module i ...[truncated 3106 chars]
- Remediation
- <![CDATA[ ## Remediation Suggestions 1. Restrict `--project` to a single safe filesystem identifier: ```python import re PROJECT_NAME_PATTERN = re.compile(r"^[A-Za-z0-9][A-Za-z0-9._-]*$") if not PROJECT_NAME_PATTERN.fullmatch(args.project): parser.error( "--project must contain only letters, digits, periods, underscores, " "or hyphens and must begin with a letter or digit" ) ``` 2. Explicitly reject dangerous path forms: - Absolute paths. - `/` and `\` path separators. - Empty names. - `.` and `..`. - Names containing control characters. 3. Resolve and verify destination containment before creating files: ```python projects_root = (target_root / "projects").resolve() project_root = (projects_root / args.project).resolve() if not project_root.is_relative_to(projects_root): parser.error("--project resolves outside the projects directory") ``` 4. Perform the containment check immediately before file creation to reduce the risk of path changes or unsafe refactoring. 5. Where the environment may be controlled by an attacker, consider defenses against symbolic-link traversal, such as rejecting symlinked destination components or using directory-relative, no-follow file operations. 6. Add automated tests for: - `../outside` - `../../outside` - Absolute Unix and Windows paths - Embedded forward and backward slashes - `.` and `..` - Valid names containing permitted punctuation ]]>
