fix(build): IPA 加固门禁审计按分支脚本支持的参数传参,兼容旧分支打包

旧分支自带的 audit_exported_ipa.py 只支持 ipa / --bundle-id,
服务端传入 --archive、--report-out 等参数会被 argparse 拒绝导致打包失败。
现先解析分支脚本声明的参数,仅传入其支持的选项,旧脚本仍执行基础审计。

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
shenlei
2026-11-26 12:02:55 +09:00
co-authored by Claude Opus 5.5
parent e9449155e4
commit 488c6416c2
2 changed files with 70 additions and 0 deletions
+38
View File
@@ -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,
+32
View File
@@ -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"