diff --git a/src/launchpad/artifacts/apple/zipped_xcarchive.py b/src/launchpad/artifacts/apple/zipped_xcarchive.py index af33bf54..36961d7c 100644 --- a/src/launchpad/artifacts/apple/zipped_xcarchive.py +++ b/src/launchpad/artifacts/apple/zipped_xcarchive.py @@ -1,6 +1,7 @@ import json import plistlib import shutil +import stat import subprocess import tempfile import uuid @@ -8,6 +9,7 @@ from dataclasses import dataclass from pathlib import Path from typing import Any, List, NamedTuple +from zipfile import ZipFile import lief import sentry_sdk.traces @@ -229,8 +231,23 @@ def generate_ipa(self, output_path: Path): payload_dir.mkdir() dest_app_path = payload_dir / app_bundle_path.name - # Copy the .app bundle into Payload (preserve symlinks, permissions, etc.) - shutil.copytree(app_bundle_path, dest_app_path, symlinks=True) + # ZipFile.extract creates regular files with default permissions. Carry + # only the original execute bits onto the copy made for distribution. + # Member names are untrusted, so key them by where they were extracted. + with ZipFile(self.path) as archive: + members = {self._extract_dir.resolve(entry.filename): entry for entry in archive.infolist()} + + def copy_app_file(src: str, dst: str) -> str: + copied = shutil.copy2(src, dst) + member = members.get(Path(src)) + if member is not None and member.create_system == 3: + mode = member.external_attr >> 16 + if stat.S_IFMT(mode) in (0, stat.S_IFREG) and mode & 0o111: + path = Path(copied) + path.chmod(path.stat().st_mode | (mode & 0o111)) + return copied + + shutil.copytree(app_bundle_path, dest_app_path, symlinks=True, copy_function=copy_app_file) # Create the IPA file using zip to preserve symlinks and metadata try: diff --git a/tests/integration/test_ipa.py b/tests/integration/test_ipa.py index 6138ed4d..0ce4785e 100644 --- a/tests/integration/test_ipa.py +++ b/tests/integration/test_ipa.py @@ -1,22 +1,21 @@ import subprocess -import tempfile from pathlib import Path +from zipfile import ZipFile from launchpad.artifacts.artifact_factory import ArtifactFactory class TestIPA: - def test_ipa_generation(self, hackernews_xcarchive: Path): + def test_ipa_generation(self, hackernews_xcarchive: Path, tmp_path: Path): artifact = ArtifactFactory.from_path(hackernews_xcarchive) - temp_dir = Path(tempfile.mkdtemp()) - ipa_path = temp_dir / "HackerNews.ipa" + ipa_path = tmp_path / "HackerNews.ipa" artifact.generate_ipa(ipa_path) assert ipa_path.exists() - extract_dir = temp_dir / "extracted_ipa" + extract_dir = tmp_path / "extracted_ipa" extract_dir.mkdir() try: @@ -31,3 +30,13 @@ def test_ipa_generation(self, hackernews_xcarchive: Path): payload_dir = extract_dir / "Payload" app_bundles = list(payload_dir.glob("*.app")) assert len(app_bundles) == 1, f"Expected exactly one .app bundle, found {len(app_bundles)}" + + app = app_bundles[0].relative_to(extract_dir) + with ZipFile(ipa_path) as ipa: + for executable in ( + app / "HackerNews", + app / "Frameworks/Sentry.framework/Sentry", + app / "PlugIns/HackerNewsHomeWidgetExtension.appex/HackerNewsHomeWidgetExtension", + ): + assert (ipa.getinfo(executable.as_posix()).external_attr >> 16) & 0o111 == 0o111 + assert (ipa.getinfo((app / "Info.plist").as_posix()).external_attr >> 16) & 0o111 == 0