archive_safe.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. """Safe ``tar.gz`` primitives shared by the profile and kanban transfer paths."""
  2. from __future__ import annotations
  3. import os
  4. import shutil
  5. import tarfile
  6. import tempfile
  7. from contextlib import suppress
  8. from pathlib import Path, PurePosixPath, PureWindowsPath
  9. def normalize_archive_parts(member_name: str) -> list[str]:
  10. """Return safe path parts for an archive member, or raise ``ValueError``.
  11. Rejects absolute paths (POSIX and Windows, including drive letters), empty names, and any ``..``
  12. component. Backslashes are folded to ``/`` first so a Windows-authored archive can't smuggle a
  13. separator past the POSIX parse.
  14. """
  15. normalized_name = member_name.replace("\\", "/")
  16. posix_path = PurePosixPath(normalized_name)
  17. windows_path = PureWindowsPath(member_name)
  18. parts = [part for part in posix_path.parts if part not in {"", "."}]
  19. if (not normalized_name or posix_path.is_absolute() or windows_path.is_absolute()
  20. or windows_path.drive or not parts or ".." in parts):
  21. raise ValueError(f"Unsafe archive member path: {member_name}")
  22. return parts
  23. def make_targz(base: str, root_dir: str, base_dir: str) -> str:
  24. """Create ``<base>.tar.gz`` of ``root_dir/base_dir`` in GNU tar format.
  25. Writes to a sibling temp file and renames onto the archive path only after it is fully written.
  26. """
  27. archive_path = f"{base}.tar.gz"
  28. dest_dir = os.path.dirname(archive_path) or "."
  29. fd, tmp_path = tempfile.mkstemp(dir=dest_dir, prefix=".archive_", suffix=".tar.gz.tmp")
  30. try:
  31. with os.fdopen(fd, "wb") as f, \
  32. tarfile.open(fileobj=f, mode="w:gz", format=tarfile.GNU_FORMAT) as tf:
  33. tf.add(str(Path(root_dir) / base_dir), arcname=base_dir)
  34. os.replace(tmp_path, archive_path)
  35. except BaseException:
  36. with suppress(OSError):
  37. os.unlink(tmp_path)
  38. raise
  39. return archive_path
  40. def safe_extract_targz(archive: Path, destination: Path) -> None:
  41. """Extract ``archive`` into ``destination`` without path escapes or links.
  42. Only directories and regular files are extracted; symlinks, hardlinks, and device nodes raise
  43. rather than being silently skipped, so a tampered archive fails the import instead of landing a
  44. partial tree.
  45. """
  46. with tarfile.open(archive, "r:gz") as tf:
  47. for member in tf.getmembers():
  48. target = destination.joinpath(*normalize_archive_parts(member.name))
  49. if member.isdir():
  50. target.mkdir(parents=True, exist_ok=True)
  51. continue
  52. if not member.isfile():
  53. raise ValueError(f"Unsupported archive member type: {member.name}")
  54. target.parent.mkdir(parents=True, exist_ok=True)
  55. extracted = tf.extractfile(member)
  56. if extracted is None:
  57. raise ValueError(f"Cannot read archive member: {member.name}")
  58. with extracted, open(target, "wb") as dst:
  59. shutil.copyfileobj(extracted, dst)
  60. with suppress(OSError):
  61. os.chmod(target, member.mode & 0o777)
  62. def archive_root_dirs(archive: Path) -> set[str]:
  63. """Return the archive's top-level directory names.
  64. Transfer archives carry exactly one root directory, which names the thing being imported.
  65. Inspecting before extraction lets the caller resolve the target name (and refuse a malformed
  66. archive) without first mutating a live tree.
  67. """
  68. with tarfile.open(archive, "r:gz") as tf:
  69. return {parts[0] for member in tf.getmembers()
  70. for parts in [normalize_archive_parts(member.name)]
  71. if len(parts) > 1 or member.isdir()}
  72. def copy_regular_files(src: Path, dst: Path) -> int:
  73. """Copy the regular files under ``src`` into ``dst``, skipping symlinks; return the count.
  74. Used on the *export* side so a symlink planted in an attachments or logs tree can't pull an
  75. arbitrary file into the archive. A missing ``src`` copies nothing.
  76. """
  77. if not src.is_dir():
  78. return 0
  79. copied = 0
  80. for entry in sorted(src.rglob("*")):
  81. if entry.is_symlink() or not entry.is_file():
  82. continue
  83. target = dst / entry.relative_to(src)
  84. target.parent.mkdir(parents=True, exist_ok=True)
  85. shutil.copyfile(entry, target)
  86. copied += 1
  87. return copied