|
1 | 1 | import asyncio |
| 2 | +import builtins |
2 | 3 | import ntpath |
| 4 | +import os |
3 | 5 | import posixpath |
4 | 6 | import zipfile |
5 | 7 | from dataclasses import dataclass, field |
@@ -397,6 +399,113 @@ def test_plugin_unzip_file_accepts_metadata_yml(tmp_path: Path) -> None: |
397 | 399 | assert not zip_path.exists() |
398 | 400 |
|
399 | 401 |
|
| 402 | +@pytest.mark.parametrize("target_kind", ["absolute", "relative"]) |
| 403 | +def test_plugin_unzip_file_handles_long_archive_paths( |
| 404 | + tmp_path: Path, |
| 405 | + monkeypatch: pytest.MonkeyPatch, |
| 406 | + target_kind: str, |
| 407 | +) -> None: |
| 408 | + zip_path = tmp_path / "plugin.zip" |
| 409 | + target_dir = tmp_path / "plugin" |
| 410 | + archive_root = "demo-plugin-" + "a" * 40 |
| 411 | + # The original destination is 270 characters; shortening the SHA saves 32. |
| 412 | + filename_length = 270 - len(str(target_dir / archive_root)) - 1 |
| 413 | + assert 3 < filename_length <= 255 |
| 414 | + member_path = "x" * (filename_length - 3) + ".py" |
| 415 | + |
| 416 | + with zipfile.ZipFile(zip_path, "w") as archive: |
| 417 | + archive.writestr( |
| 418 | + f"{archive_root}/metadata.yaml", |
| 419 | + "name: demo\ndesc: Demo plugin\nversion: 1.0.0\nauthor: AstrBot Team\n", |
| 420 | + ) |
| 421 | + archive.writestr(f"{archive_root}/{member_path}", "VALUE = 1\n") |
| 422 | + archive.writestr(f"{archive_root}/empty/", "") |
| 423 | + |
| 424 | + original_open = builtins.open |
| 425 | + |
| 426 | + def open_with_path_limit(file, mode="r", *args, **kwargs): |
| 427 | + # Enforce MAX_PATH even on hosts that support longer paths. |
| 428 | + if isinstance(file, (str, os.PathLike)): |
| 429 | + path = os.fspath(file) |
| 430 | + if len(os.path.abspath(path)) >= 260: |
| 431 | + raise FileNotFoundError(2, "Path exceeds MAX_PATH", path) |
| 432 | + return original_open(file, mode, *args, **kwargs) |
| 433 | + |
| 434 | + monkeypatch.setattr(builtins, "open", open_with_path_limit) |
| 435 | + target = str(target_dir) |
| 436 | + if target_kind == "relative": |
| 437 | + monkeypatch.chdir(tmp_path) |
| 438 | + target = "plugin" |
| 439 | + |
| 440 | + updater = _PluginUpdater.__new__(_PluginUpdater) |
| 441 | + updater._extract_plugin_archive(str(zip_path), target) |
| 442 | + |
| 443 | + assert (target_dir / member_path).read_text(encoding="utf-8") == "VALUE = 1\n" |
| 444 | + assert (target_dir / "metadata.yaml").is_file() |
| 445 | + assert (target_dir / "empty").is_dir() |
| 446 | + assert {entry.name for entry in target_dir.iterdir()} == { |
| 447 | + "metadata.yaml", |
| 448 | + member_path, |
| 449 | + "empty", |
| 450 | + } |
| 451 | + assert not zip_path.exists() |
| 452 | + |
| 453 | + |
| 454 | +@pytest.mark.parametrize( |
| 455 | + ("archive_root", "extracted_root"), |
| 456 | + [ |
| 457 | + ("demo-" + "a" * 40, "demo-" + "a" * 8), |
| 458 | + ("demo-" + "A" * 40 + "/plugin", "demo-" + "A" * 8 + "/plugin"), |
| 459 | + ("demo-main", "demo-main"), |
| 460 | + ("demo-v1.0.0", "demo-v1.0.0"), |
| 461 | + ("demo-12345678", "demo-12345678"), |
| 462 | + ("demo-" + "g" * 40, "demo-" + "g" * 40), |
| 463 | + ("./demo-" + "a" * 40, "demo-" + "a" * 40), |
| 464 | + ("", ""), |
| 465 | + ], |
| 466 | +) |
| 467 | +def test_plugin_unzip_file_only_shortens_full_root_sha( |
| 468 | + tmp_path: Path, |
| 469 | + monkeypatch: pytest.MonkeyPatch, |
| 470 | + archive_root: str, |
| 471 | + extracted_root: str, |
| 472 | +) -> None: |
| 473 | + zip_path = tmp_path / "plugin.zip" |
| 474 | + target_dir = tmp_path / "plugin" |
| 475 | + prefix = f"{archive_root}/" if archive_root else "" |
| 476 | + internal_path = "src/nested-" + "a" * 40 + "/main.py" |
| 477 | + with zipfile.ZipFile(zip_path, "w") as archive: |
| 478 | + if archive_root: |
| 479 | + archive.writestr(f"{archive_root}/", "") |
| 480 | + archive.writestr( |
| 481 | + f"{prefix}metadata.yaml", |
| 482 | + "name: demo\ndesc: Demo plugin\nversion: 1.0.0\nauthor: AstrBot Team\n", |
| 483 | + ) |
| 484 | + archive.writestr(f"{prefix}{internal_path}", "VALUE = 1\n") |
| 485 | + |
| 486 | + original_open = builtins.open |
| 487 | + destinations = [] |
| 488 | + |
| 489 | + def record_destination(file, mode="r", *args, **kwargs): |
| 490 | + if mode == "wb" and isinstance(file, (str, os.PathLike)): |
| 491 | + destinations.append(Path(file)) |
| 492 | + return original_open(file, mode, *args, **kwargs) |
| 493 | + |
| 494 | + monkeypatch.setattr(builtins, "open", record_destination) |
| 495 | + updater = _PluginUpdater.__new__(_PluginUpdater) |
| 496 | + updater._extract_plugin_archive(str(zip_path), str(target_dir)) |
| 497 | + |
| 498 | + assert destinations == [ |
| 499 | + target_dir / extracted_root / "metadata.yaml", |
| 500 | + target_dir / extracted_root / internal_path, |
| 501 | + ] |
| 502 | + assert (target_dir / internal_path).read_text(encoding="utf-8") == "VALUE = 1\n" |
| 503 | + assert (target_dir / "metadata.yaml").is_file() |
| 504 | + if extracted_root: |
| 505 | + assert not (target_dir / extracted_root).exists() |
| 506 | + assert not zip_path.exists() |
| 507 | + |
| 508 | + |
400 | 509 | def test_plugin_unzip_file_rejects_archive_without_metadata(tmp_path: Path) -> None: |
401 | 510 | zip_path = tmp_path / "plugin.zip" |
402 | 511 | target_dir = tmp_path / "plugin" |
|
0 commit comments