|
9 | 9 | """ |
10 | 10 |
|
11 | 11 | import json |
| 12 | +import shutil |
12 | 13 | import re |
13 | 14 | import threading |
14 | 15 | import time |
@@ -1175,6 +1176,111 @@ async def test_restricted_server_rejects_symlink_escape(tmp_path: Path, repo: gi |
1175 | 1176 | other.close() |
1176 | 1177 |
|
1177 | 1178 |
|
| 1179 | +def uses_outside(result: dict[str, Any], used: Path, allowed: Path) -> bool: |
| 1180 | + """The result is the error for a repository git opened outside `allowed`.""" |
| 1181 | + text = result["content"][0]["text"] |
| 1182 | + return ( |
| 1183 | + result["isError"] is True |
| 1184 | + and f"uses '{used}'" in text |
| 1185 | + and f"which is outside the allowed repository '{allowed}'" in text |
| 1186 | + ) |
| 1187 | + |
| 1188 | + |
| 1189 | +async def test_restricted_server_rejects_gitdir_file_pointing_outside( |
| 1190 | + tmp_path: Path, repo: git.Repo |
| 1191 | +): |
| 1192 | + # A `.git` file inside the allowed repository passes the path check, but |
| 1193 | + # git follows its `gitdir:` line to the repository it names. Neither a read |
| 1194 | + # nor a write may reach that repository. |
| 1195 | + root = root_of(repo) |
| 1196 | + other = make_repo(tmp_path / "other") |
| 1197 | + nested = root / "nested" |
| 1198 | + nested.mkdir() |
| 1199 | + (nested / ".git").write_text(f"gitdir: {other.git_dir}\n") |
| 1200 | + try: |
| 1201 | + shown = await call( |
| 1202 | + root, "git_show", {"repo_path": str(nested), "revision": "HEAD"} |
| 1203 | + ) |
| 1204 | + assert uses_outside(shown, Path(other.git_dir), root) |
| 1205 | + branched = await call( |
| 1206 | + root, |
| 1207 | + "git_create_branch", |
| 1208 | + {"repo_path": str(nested), "branch_name": "escaped"}, |
| 1209 | + ) |
| 1210 | + assert uses_outside(branched, Path(other.git_dir), root) |
| 1211 | + assert "escaped" not in {head.name for head in other.heads} |
| 1212 | + finally: |
| 1213 | + other.close() |
| 1214 | + |
| 1215 | + |
| 1216 | +async def test_restricted_server_rejects_commondir_pointing_outside( |
| 1217 | + tmp_path: Path, repo: git.Repo |
| 1218 | +): |
| 1219 | + # A worktree-style git directory inside the allowed repository whose |
| 1220 | + # `commondir` names another repository's `.git` reads that repository's |
| 1221 | + # objects and refs. |
| 1222 | + root = root_of(repo) |
| 1223 | + other = make_repo(tmp_path / "other") |
| 1224 | + other.git.worktree("add", str(tmp_path / "other-wt")) |
| 1225 | + admin = root / "admin" |
| 1226 | + shutil.copytree( |
| 1227 | + Path(other.git_dir) / "worktrees" / "other-wt", |
| 1228 | + admin, |
| 1229 | + ignore=shutil.ignore_patterns("*.ipc"), # git's fsmonitor socket |
| 1230 | + ) |
| 1231 | + (admin / "commondir").write_text(f"{other.git_dir}\n") |
| 1232 | + nested = root / "nested" |
| 1233 | + nested.mkdir() |
| 1234 | + (nested / ".git").write_text(f"gitdir: {admin}\n") |
| 1235 | + try: |
| 1236 | + result = await call(root, "git_log", {"repo_path": str(nested)}) |
| 1237 | + assert uses_outside(result, Path(other.git_dir), root) |
| 1238 | + finally: |
| 1239 | + other.close() |
| 1240 | + |
| 1241 | + |
| 1242 | +async def test_restricted_server_rejects_worktree_config_pointing_outside( |
| 1243 | + tmp_path: Path, repo: git.Repo |
| 1244 | +): |
| 1245 | + # `core.worktree` in a nested repository moves its working tree, and every |
| 1246 | + # checkout or add with it, to wherever the setting names. |
| 1247 | + root = root_of(repo) |
| 1248 | + elsewhere = tmp_path / "elsewhere" |
| 1249 | + elsewhere.mkdir() |
| 1250 | + nested = make_repo(root / "nested") |
| 1251 | + with nested.config_writer() as config: |
| 1252 | + config.set_value("core", "worktree", str(elsewhere)) |
| 1253 | + nested.close() |
| 1254 | + result = await call(root, "git_status", {"repo_path": str(root / "nested")}) |
| 1255 | + assert uses_outside(result, elsewhere, root) |
| 1256 | + |
| 1257 | + |
| 1258 | +async def test_restricted_server_serves_a_linked_worktree(tmp_path: Path): |
| 1259 | + # A linked worktree's git directory lives under the main repository's |
| 1260 | + # `.git`, outside the worktree, and is still the allowed repository's own. |
| 1261 | + main = make_repo(tmp_path / "main") |
| 1262 | + worktree = tmp_path / "worktree" |
| 1263 | + main.git.worktree("add", str(worktree)) |
| 1264 | + try: |
| 1265 | + result = await call(worktree, "git_status", {"repo_path": str(worktree)}) |
| 1266 | + assert result["isError"] is False |
| 1267 | + finally: |
| 1268 | + main.close() |
| 1269 | + |
| 1270 | + |
| 1271 | +async def test_restricted_server_accepts_nested_bare_repository(repo: git.Repo): |
| 1272 | + # A bare repository has no working tree; its git directory is inside. |
| 1273 | + root = root_of(repo) |
| 1274 | + bare = git.Repo.init(root / "bare.git", bare=True) |
| 1275 | + bare.close() |
| 1276 | + result = await call( |
| 1277 | + root, |
| 1278 | + "git_branch", |
| 1279 | + {"repo_path": str(root / "bare.git"), "branch_type": "local"}, |
| 1280 | + ) |
| 1281 | + assert result["isError"] is False |
| 1282 | + |
| 1283 | + |
1178 | 1284 | async def test_restricted_server_accepts_nested_repository(repo: git.Repo): |
1179 | 1285 | # Characterization: the restriction is a path containment check, so a |
1180 | 1286 | # separate repository nested inside the allowed one is served. |
|
0 commit comments