|
| 1 | +"""Tests for CommandRegistrar directory traversal guards around issue #2229.""" |
| 2 | + |
| 3 | +import errno |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from specify_cli.agents import CommandRegistrar |
| 9 | + |
| 10 | + |
| 11 | +TRAVERSAL_PAYLOADS = [ |
| 12 | + "../pwned", |
| 13 | + "../../etc/passwd", |
| 14 | + "subdir/../../escape", |
| 15 | + "/absolute/evil", |
| 16 | +] |
| 17 | + |
| 18 | + |
| 19 | +def _write_source(ext_dir: Path) -> Path: |
| 20 | + ext_dir.mkdir(parents=True, exist_ok=True) |
| 21 | + (ext_dir / "commands").mkdir(exist_ok=True) |
| 22 | + (ext_dir / "commands" / "cmd.md").write_text( |
| 23 | + "---\ndescription: test\n---\n\nbody\n", encoding="utf-8" |
| 24 | + ) |
| 25 | + return ext_dir |
| 26 | + |
| 27 | + |
| 28 | +def _cmd(name: str, aliases: list[str] | None = None) -> dict[str, object]: |
| 29 | + return { |
| 30 | + "name": name, |
| 31 | + "file": "commands/cmd.md", |
| 32 | + "aliases": list(aliases or []), |
| 33 | + } |
| 34 | + |
| 35 | + |
| 36 | +def _project_and_source(tmp_path): |
| 37 | + project = tmp_path / "project" |
| 38 | + project.mkdir() |
| 39 | + ext_dir = _write_source(tmp_path / "ext-src") |
| 40 | + return project, ext_dir |
| 41 | + |
| 42 | + |
| 43 | +def _assert_no_stray_files(tmp_root: Path, marker: str) -> None: |
| 44 | + """Fail if a file matching ``marker`` exists outside the project tree.""" |
| 45 | + stray = [ |
| 46 | + p for p in tmp_root.rglob("*") |
| 47 | + if p.is_file() and marker in p.name and "project" not in p.parts |
| 48 | + ] |
| 49 | + assert stray == [], ( |
| 50 | + f"Traversal payload leaked files outside the project tree: {stray}" |
| 51 | + ) |
| 52 | + |
| 53 | + |
| 54 | +class TestPrimaryCommandTraversal: |
| 55 | + """Primary command names must not escape the agent's commands directory.""" |
| 56 | + |
| 57 | + @pytest.mark.parametrize("bad_name", TRAVERSAL_PAYLOADS) |
| 58 | + def test_gemini_rejects_traversal_in_primary_name(self, tmp_path, bad_name): |
| 59 | + project, ext_dir = _project_and_source(tmp_path) |
| 60 | + (project / ".gemini" / "commands").mkdir(parents=True) |
| 61 | + |
| 62 | + registrar = CommandRegistrar() |
| 63 | + with pytest.raises(ValueError, match="escapes|outside|Invalid"): |
| 64 | + registrar.register_commands( |
| 65 | + "gemini", [_cmd(bad_name)], "myext", ext_dir, project |
| 66 | + ) |
| 67 | + |
| 68 | + _assert_no_stray_files(tmp_path, Path(bad_name).name.replace("/", "")) |
| 69 | + |
| 70 | + @pytest.mark.parametrize("bad_name", TRAVERSAL_PAYLOADS) |
| 71 | + def test_copilot_rejects_traversal_in_primary_name(self, tmp_path, bad_name): |
| 72 | + project, ext_dir = _project_and_source(tmp_path) |
| 73 | + (project / ".github" / "agents").mkdir(parents=True) |
| 74 | + (project / ".github" / "prompts").mkdir(parents=True) |
| 75 | + |
| 76 | + registrar = CommandRegistrar() |
| 77 | + with pytest.raises(ValueError, match="escapes|outside|Invalid"): |
| 78 | + registrar.register_commands( |
| 79 | + "copilot", [_cmd(bad_name)], "myext", ext_dir, project |
| 80 | + ) |
| 81 | + |
| 82 | + _assert_no_stray_files(tmp_path, Path(bad_name).name.replace("/", "")) |
| 83 | + |
| 84 | + |
| 85 | +class TestAliasTraversal: |
| 86 | + """Free-form aliases must not escape commands_dir (regression for b67b285).""" |
| 87 | + |
| 88 | + @pytest.mark.parametrize("bad_alias", TRAVERSAL_PAYLOADS) |
| 89 | + def test_gemini_rejects_traversal_in_alias(self, tmp_path, bad_alias): |
| 90 | + project, ext_dir = _project_and_source(tmp_path) |
| 91 | + (project / ".gemini" / "commands").mkdir(parents=True) |
| 92 | + |
| 93 | + registrar = CommandRegistrar() |
| 94 | + with pytest.raises(ValueError, match="escapes|outside|Invalid"): |
| 95 | + registrar.register_commands( |
| 96 | + "gemini", |
| 97 | + [_cmd("speckit.myext.ok", [bad_alias])], |
| 98 | + "myext", |
| 99 | + ext_dir, |
| 100 | + project, |
| 101 | + ) |
| 102 | + |
| 103 | + _assert_no_stray_files(tmp_path, Path(bad_alias).name.replace("/", "")) |
| 104 | + |
| 105 | + @pytest.mark.parametrize("bad_alias", TRAVERSAL_PAYLOADS) |
| 106 | + def test_copilot_rejects_traversal_in_alias(self, tmp_path, bad_alias): |
| 107 | + project, ext_dir = _project_and_source(tmp_path) |
| 108 | + (project / ".github" / "agents").mkdir(parents=True) |
| 109 | + (project / ".github" / "prompts").mkdir(parents=True) |
| 110 | + |
| 111 | + registrar = CommandRegistrar() |
| 112 | + with pytest.raises(ValueError, match="escapes|outside|Invalid"): |
| 113 | + registrar.register_commands( |
| 114 | + "copilot", |
| 115 | + [_cmd("speckit.myext.ok", [bad_alias])], |
| 116 | + "myext", |
| 117 | + ext_dir, |
| 118 | + project, |
| 119 | + ) |
| 120 | + |
| 121 | + _assert_no_stray_files(tmp_path, Path(bad_alias).name.replace("/", "")) |
| 122 | + |
| 123 | + |
| 124 | +class TestCopilotPromptTraversal: |
| 125 | + """`write_copilot_prompt` is a public static method — guard it directly.""" |
| 126 | + |
| 127 | + @pytest.mark.parametrize("bad_name", TRAVERSAL_PAYLOADS) |
| 128 | + def test_rejects_traversal_names(self, tmp_path, bad_name): |
| 129 | + project = tmp_path / "project" |
| 130 | + (project / ".github" / "prompts").mkdir(parents=True) |
| 131 | + |
| 132 | + with pytest.raises(ValueError, match="escapes|outside|Invalid"): |
| 133 | + CommandRegistrar.write_copilot_prompt(project, bad_name) |
| 134 | + |
| 135 | + _assert_no_stray_files(tmp_path, Path(bad_name).name.replace("/", "")) |
| 136 | + |
| 137 | + |
| 138 | +class TestSafeRegistration: |
| 139 | + """Positive regression — well-formed names continue to register.""" |
| 140 | + |
| 141 | + def test_symlinked_subdir_under_commands_dir_is_preserved(self, tmp_path): |
| 142 | + """Lexical check must not block legitimately symlinked sub-directories. |
| 143 | +
|
| 144 | + Teams sometimes symlink shared skills into their agent commands dir |
| 145 | + (e.g. ``.gemini/commands/shared -> /team/shared-commands``). The |
| 146 | + guard is purely lexical, so such a setup continues to work even though |
| 147 | + the resolved target lives outside commands_dir on disk. |
| 148 | + """ |
| 149 | + project, ext_dir = _project_and_source(tmp_path) |
| 150 | + commands_dir = project / ".gemini" / "commands" |
| 151 | + commands_dir.mkdir(parents=True) |
| 152 | + |
| 153 | + external_shared = tmp_path / "external-shared" |
| 154 | + external_shared.mkdir() |
| 155 | + try: |
| 156 | + (commands_dir / "shared").symlink_to( |
| 157 | + external_shared, target_is_directory=True |
| 158 | + ) |
| 159 | + except OSError as exc: |
| 160 | + if exc.errno in {errno.EPERM, errno.EACCES}: |
| 161 | + pytest.skip("symlink creation is not permitted in this environment") |
| 162 | + raise |
| 163 | + |
| 164 | + registrar = CommandRegistrar() |
| 165 | + registered = registrar.register_commands( |
| 166 | + "gemini", |
| 167 | + [_cmd("shared/hello")], |
| 168 | + "myext", |
| 169 | + ext_dir, |
| 170 | + project, |
| 171 | + ) |
| 172 | + |
| 173 | + assert registered == ["shared/hello"] |
| 174 | + assert (external_shared / "hello.toml").exists() |
| 175 | + |
| 176 | + def test_safe_command_and_alias_still_register(self, tmp_path): |
| 177 | + project, ext_dir = _project_and_source(tmp_path) |
| 178 | + (project / ".claude" / "skills").mkdir(parents=True) |
| 179 | + |
| 180 | + registrar = CommandRegistrar() |
| 181 | + registered = registrar.register_commands( |
| 182 | + "claude", |
| 183 | + [_cmd("speckit.myext.hello", ["speckit.myext.hi"])], |
| 184 | + "myext", |
| 185 | + ext_dir, |
| 186 | + project, |
| 187 | + ) |
| 188 | + |
| 189 | + assert "speckit.myext.hello" in registered |
| 190 | + assert "speckit.myext.hi" in registered |
| 191 | + assert ( |
| 192 | + project |
| 193 | + / ".claude" |
| 194 | + / "skills" |
| 195 | + / "speckit-myext-hello" |
| 196 | + / "SKILL.md" |
| 197 | + ).exists() |
| 198 | + assert ( |
| 199 | + project |
| 200 | + / ".claude" |
| 201 | + / "skills" |
| 202 | + / "speckit-myext-hi" |
| 203 | + / "SKILL.md" |
| 204 | + ).exists() |
0 commit comments