From 488c6416c24ffdafce9e729353ace2ae476dd5c2 Mon Sep 17 00:00:00 2001 From: shenlei Date: Thu, 26 Nov 2026 12:02:55 +0900 Subject: [PATCH] =?UTF-8?q?fix(build):=20IPA=20=E5=8A=A0=E5=9B=BA=E9=97=A8?= =?UTF-8?q?=E7=A6=81=E5=AE=A1=E8=AE=A1=E6=8C=89=E5=88=86=E6=94=AF=E8=84=9A?= =?UTF-8?q?=E6=9C=AC=E6=94=AF=E6=8C=81=E7=9A=84=E5=8F=82=E6=95=B0=E4=BC=A0?= =?UTF-8?q?=E5=8F=82=EF=BC=8C=E5=85=BC=E5=AE=B9=E6=97=A7=E5=88=86=E6=94=AF?= =?UTF-8?q?=E6=89=93=E5=8C=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 旧分支自带的 audit_exported_ipa.py 只支持 ipa / --bundle-id, 服务端传入 --archive、--report-out 等参数会被 argparse 拒绝导致打包失败。 现先解析分支脚本声明的参数,仅传入其支持的选项,旧脚本仍执行基础审计。 Co-Authored-By: Claude Opus 5.5 --- backend/services/build_service.py | 38 +++++++++++++++++++++++++++++++ tests/test_build_service.py | 32 ++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/backend/services/build_service.py b/backend/services/build_service.py index 0a171d9..8aa32c2 100644 --- a/backend/services/build_service.py +++ b/backend/services/build_service.py @@ -1285,6 +1285,34 @@ def persist_task_artifacts( return persisted_dsym, persisted_maps +_ARGPARSE_OPTION_RE = re.compile(r"""add_argument\(\s*['"](--[\w-]+)['"]""") + + +def _detect_audit_script_options(audit_script: Path) -> Optional[set]: + """从分支自带审计脚本源码中解析 argparse 声明的可选参数;无法判断时返回 None(按新版处理)。""" + try: + source = audit_script.read_text(encoding="utf-8", errors="replace") + except OSError: + return None + options = set(_ARGPARSE_OPTION_RE.findall(source)) + return options or None + + +def _filter_audit_cmd_options(cmd: list, supported: set) -> tuple: + """剔除旧版审计脚本不认识的「--选项 值」对,返回 (新命令, 被剔除的选项列表)。""" + filtered, dropped = [], [] + i = 0 + while i < len(cmd): + item = cmd[i] + if item.startswith("--") and item not in supported: + dropped.append(item) + i += 2 # 服务端追加的审计选项都带一个值 + continue + filtered.append(item) + i += 1 + return filtered, dropped + + async def _run_exported_ipa_audit( task_id: str, audit_script: Path, @@ -1324,6 +1352,16 @@ async def _run_exported_ipa_audit( chosen_sym = (exact or sym_maps)[-1] cmd.extend(["--symbol-map", str(chosen_sym)]) + # 兼容旧分支:其自带审计脚本可能只支持 ipa / --bundle-id,传入未知参数会被 argparse 直接拒绝 + supported = _detect_audit_script_options(audit_script) + if supported is not None: + cmd, dropped = _filter_audit_cmd_options(cmd, supported) + if dropped: + await log_streamer.emit( + task_id, + f"⚠️ 分支审计脚本为旧版,不支持 {', '.join(dropped)},按分支脚本能力执行基础审计", + ) + await log_streamer.emit(task_id, "开始 IPA 加固门禁审计...") process = await asyncio.create_subprocess_exec( *cmd, diff --git a/tests/test_build_service.py b/tests/test_build_service.py index 78b50a7..d81b78e 100644 --- a/tests/test_build_service.py +++ b/tests/test_build_service.py @@ -865,6 +865,38 @@ async def test_build_project_passes_dist_channel_and_blocks_on_audit_failure(tmp assert "--archive" in audit_cmd and "--bundle-id" in audit_cmd and "--report-out" in audit_cmd +async def test_exported_ipa_audit_compatible_with_legacy_branch_script(tmp_path, log_streamer): + """旧分支自带审计脚本只支持 ipa / --bundle-id 时,只传其支持的参数,仍执行审计。""" + from backend.services.build_service import _run_exported_ipa_audit + + build_dir = tmp_path / "snap" + audit_script = build_dir / "iOSGuardIntegration" / "toolchain" / "tools" / "xcode_archive" / "audit_exported_ipa.py" + audit_script.parent.mkdir(parents=True) + audit_script.write_text( + 'parser.add_argument("ipa", type=Path)\nparser.add_argument("--bundle-id")\n', encoding="utf-8" + ) + export_path = build_dir / "build" + maps_dir = build_dir / "obfuscation_maps" + maps_dir.mkdir(parents=True) + (maps_dir / "symbol_map_readoor31_1.json").write_text("{}", encoding="utf-8") + ipa_path = export_path / "readoor.ipa" + + with patch("asyncio.create_subprocess_exec", return_value=_make_mock_process(0)) as mock_exec: + await _run_exported_ipa_audit( + "t-legacy", + audit_script, + ipa_path, + export_path / "readoor31.xcarchive", + export_path, + {"SCHEME": "readoor31", "BUNDLE_ID": "com.test.app"}, + build_dir, + {}, + ) + + audit_cmd = list(mock_exec.call_args.args) + assert audit_cmd[1:] == [str(audit_script), str(ipa_path), "--bundle-id", "com.test.app"] + + def _dist_channel_snapshot(tmp_path): """构造接入了 anti_tamper 的源码快照(pbxproj 引用 IGAntiTamper.c)。""" build_dir = tmp_path / "snap"