Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/launchpad/artifacts/apple/zipped_xcarchive.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import json
import plistlib
import shutil
import stat
import subprocess
import tempfile
import uuid

from dataclasses import dataclass
from pathlib import Path
from typing import Any, List, NamedTuple
from zipfile import ZipFile

import lief
import sentry_sdk.traces
Expand Down Expand Up @@ -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:
Expand Down
19 changes: 14 additions & 5 deletions tests/integration/test_ipa.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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
Loading