|
9 | 9 | """ |
10 | 10 |
|
11 | 11 | import json |
| 12 | +import shutil |
12 | 13 | import re |
13 | 14 | import threading |
14 | 15 | import time |
@@ -1226,6 +1227,111 @@ async def test_restricted_server_rejects_symlink_escape(tmp_path: Path, repo: gi |
1226 | 1227 | other.close() |
1227 | 1228 |
|
1228 | 1229 |
|
| 1230 | +def uses_outside(result: dict[str, Any], used: Path, allowed: Path) -> bool: |
| 1231 | + """The result is the error for a repository git opened outside `allowed`.""" |
| 1232 | + text = result["content"][0]["text"] |
| 1233 | + return ( |
| 1234 | + result["isError"] is True |
| 1235 | + and f"uses '{used}'" in text |
| 1236 | + and f"which is outside the allowed repository '{allowed}'" in text |
| 1237 | + ) |
| 1238 | + |
| 1239 | + |
| 1240 | +async def test_restricted_server_rejects_gitdir_file_pointing_outside( |
| 1241 | + tmp_path: Path, repo: git.Repo |
| 1242 | +): |
| 1243 | + # A `.git` file inside the allowed repository passes the path check, but |
| 1244 | + # git follows its `gitdir:` line to the repository it names. Neither a read |
| 1245 | + # nor a write may reach that repository. |
| 1246 | + root = root_of(repo) |
| 1247 | + other = make_repo(tmp_path / "other") |
| 1248 | + nested = root / "nested" |
| 1249 | + nested.mkdir() |
| 1250 | + (nested / ".git").write_text(f"gitdir: {other.git_dir}\n") |
| 1251 | + try: |
| 1252 | + shown = await call( |
| 1253 | + root, "git_show", {"repo_path": str(nested), "revision": "HEAD"} |
| 1254 | + ) |
| 1255 | + assert uses_outside(shown, Path(other.git_dir), root) |
| 1256 | + branched = await call( |
| 1257 | + root, |
| 1258 | + "git_create_branch", |
| 1259 | + {"repo_path": str(nested), "branch_name": "escaped"}, |
| 1260 | + ) |
| 1261 | + assert uses_outside(branched, Path(other.git_dir), root) |
| 1262 | + assert "escaped" not in {head.name for head in other.heads} |
| 1263 | + finally: |
| 1264 | + other.close() |
| 1265 | + |
| 1266 | + |
| 1267 | +async def test_restricted_server_rejects_commondir_pointing_outside( |
| 1268 | + tmp_path: Path, repo: git.Repo |
| 1269 | +): |
| 1270 | + # A worktree-style git directory inside the allowed repository whose |
| 1271 | + # `commondir` names another repository's `.git` reads that repository's |
| 1272 | + # objects and refs. |
| 1273 | + root = root_of(repo) |
| 1274 | + other = make_repo(tmp_path / "other") |
| 1275 | + other.git.worktree("add", str(tmp_path / "other-wt")) |
| 1276 | + admin = root / "admin" |
| 1277 | + shutil.copytree( |
| 1278 | + Path(other.git_dir) / "worktrees" / "other-wt", |
| 1279 | + admin, |
| 1280 | + ignore=shutil.ignore_patterns("*.ipc"), # git's fsmonitor socket |
| 1281 | + ) |
| 1282 | + (admin / "commondir").write_text(f"{other.git_dir}\n") |
| 1283 | + nested = root / "nested" |
| 1284 | + nested.mkdir() |
| 1285 | + (nested / ".git").write_text(f"gitdir: {admin}\n") |
| 1286 | + try: |
| 1287 | + result = await call(root, "git_log", {"repo_path": str(nested)}) |
| 1288 | + assert uses_outside(result, Path(other.git_dir), root) |
| 1289 | + finally: |
| 1290 | + other.close() |
| 1291 | + |
| 1292 | + |
| 1293 | +async def test_restricted_server_rejects_worktree_config_pointing_outside( |
| 1294 | + tmp_path: Path, repo: git.Repo |
| 1295 | +): |
| 1296 | + # `core.worktree` in a nested repository moves its working tree, and every |
| 1297 | + # checkout or add with it, to wherever the setting names. |
| 1298 | + root = root_of(repo) |
| 1299 | + elsewhere = tmp_path / "elsewhere" |
| 1300 | + elsewhere.mkdir() |
| 1301 | + nested = make_repo(root / "nested") |
| 1302 | + with nested.config_writer() as config: |
| 1303 | + config.set_value("core", "worktree", str(elsewhere)) |
| 1304 | + nested.close() |
| 1305 | + result = await call(root, "git_status", {"repo_path": str(root / "nested")}) |
| 1306 | + assert uses_outside(result, elsewhere, root) |
| 1307 | + |
| 1308 | + |
| 1309 | +async def test_restricted_server_serves_a_linked_worktree(tmp_path: Path): |
| 1310 | + # A linked worktree's git directory lives under the main repository's |
| 1311 | + # `.git`, outside the worktree, and is still the allowed repository's own. |
| 1312 | + main = make_repo(tmp_path / "main") |
| 1313 | + worktree = tmp_path / "worktree" |
| 1314 | + main.git.worktree("add", str(worktree)) |
| 1315 | + try: |
| 1316 | + result = await call(worktree, "git_status", {"repo_path": str(worktree)}) |
| 1317 | + assert result["isError"] is False |
| 1318 | + finally: |
| 1319 | + main.close() |
| 1320 | + |
| 1321 | + |
| 1322 | +async def test_restricted_server_accepts_nested_bare_repository(repo: git.Repo): |
| 1323 | + # A bare repository has no working tree; its git directory is inside. |
| 1324 | + root = root_of(repo) |
| 1325 | + bare = git.Repo.init(root / "bare.git", bare=True) |
| 1326 | + bare.close() |
| 1327 | + result = await call( |
| 1328 | + root, |
| 1329 | + "git_branch", |
| 1330 | + {"repo_path": str(root / "bare.git"), "branch_type": "local"}, |
| 1331 | + ) |
| 1332 | + assert result["isError"] is False |
| 1333 | + |
| 1334 | + |
1229 | 1335 | async def test_restricted_server_accepts_nested_repository(repo: git.Repo): |
1230 | 1336 | # Characterization: the restriction is a path containment check, so a |
1231 | 1337 | # separate repository nested inside the allowed one is served. |
|
0 commit comments