fix: require obfuscation for release packaging
This commit is contained in:
1 parent
7890320ff7
commit
5ebca337eb
4 files changed
+91
-38
No files matched your search
+12
-6
@@ -19,23 +19,29 @@ powershell -NoProfile -ExecutionPolicy Bypass -File packaging\pyinstaller\build_
|
||||
powershell -NoProfile -ExecutionPolicy Bypass -File packaging\pyinstaller\build_windows.ps1 -SkipInstall
|
||||
```
|
||||
|
||||
如果要生成混淆后的分发包,加 `-Obfuscate`:
|
||||
正式打包默认会先通过 PyArmor 生成混淆 staging,再让 PyInstaller 从混淆目录打包:
|
||||
|
||||
```powershell
|
||||
powershell -NoProfile -ExecutionPolicy Bypass -File packaging\pyinstaller\build_windows.ps1 -SkipInstall -Obfuscate
|
||||
powershell -NoProfile -ExecutionPolicy Bypass -File packaging\pyinstaller\build_windows.ps1 -SkipInstall
|
||||
```
|
||||
|
||||
混淆流程会先生成 `build\obfuscated-src\`,再让 PyInstaller 从该目录打包。该目录是中间产物,不要直接分发;最终仍然只分发 `dist\DouyinGuaji-<version>\` 或 Inno 生成的安装包。
|
||||
|
||||
注意:PyArmor 试用版会在混淆文件头部留下 trial 标记,正式分发前建议购买/配置正式授权后重新打包。
|
||||
注意:PyArmor 试用版会在混淆文件头部留下 trial 标记,正式分发前建议购买/配置正式授权后重新打包。若 PyArmor 授权不可用,正式打包会失败并停止。
|
||||
|
||||
仅本机开发调试需要生成明文包时,才显式加 `-NoObfuscateForDevOnly`:
|
||||
|
||||
```powershell
|
||||
powershell -NoProfile -ExecutionPolicy Bypass -File packaging\pyinstaller\build_windows.ps1 -SkipInstall -NoObfuscateForDevOnly
|
||||
```
|
||||
|
||||
如果要尝试对根目录 exe 做 UPX 加壳/压缩,可额外加 `-PackExe -UpxDir <UPX目录>`:
|
||||
|
||||
```powershell
|
||||
powershell -NoProfile -ExecutionPolicy Bypass -File packaging\pyinstaller\build_windows.ps1 -SkipInstall -Obfuscate -PackExe -UpxDir C:\path\to\upx
|
||||
powershell -NoProfile -ExecutionPolicy Bypass -File packaging\pyinstaller\build_windows.ps1 -SkipInstall -PackExe -UpxDir C:\path\to\upx
|
||||
```
|
||||
|
||||
当前 PyInstaller 生成的 exe 带 `GUARD_CF`,UPX 默认会拒绝压缩。脚本提供 `-PackExeForce` 仅用于实验;验证中它会导致 exe 启动失败并报 `Failed to load Python DLL`,脚本已加入加壳后 smoke 检查,失败会中止打包。正式分发目前建议使用 `-Obfuscate`,不要启用 `-PackExeForce`。
|
||||
当前 PyInstaller 生成的 exe 带 `GUARD_CF`,UPX 默认会拒绝压缩。脚本提供 `-PackExeForce` 仅用于实验;验证中它会导致 exe 启动失败并报 `Failed to load Python DLL`,脚本已加入加壳后 smoke 检查,失败会中止打包。正式分发不要启用 `-PackExeForce`。
|
||||
|
||||
如果要生成带本地授权的测试包,先生成当前机器的 `license.dat`:
|
||||
|
||||
@@ -87,4 +93,4 @@ dist\installer\DouyinGuaji-3.3.3-Setup.exe
|
||||
- 目标电脑需要能使用 ADB,或者把 Android platform-tools 加入 PATH。
|
||||
- 首次启动 PaddleOCR 相关组件可能较慢,杀毒软件也可能扫描 exe。
|
||||
- `config/config.json` 是本地运行配置,默认不随安装包带个人配置;让使用者首次打开后在 GUI 中保存配置。
|
||||
- 启用 `-Obfuscate` 后仍要以 `python -m tools.distribution_manifest dist\DouyinGuaji-<version>` 的结果为准;PASS 才能发给别人。
|
||||
- 默认混淆后仍要以 `python -m tools.distribution_manifest dist\DouyinGuaji-<version>` 的结果为准;PASS 才能发给别人。使用 `-NoObfuscateForDevOnly` 生成的明文包禁止分发。
|
||||
@@ -1,7 +1,7 @@
|
||||
param(
|
||||
[string]$Version = "",
|
||||
[switch]$SkipInstall,
|
||||
[switch]$Obfuscate,
|
||||
[switch]$NoObfuscateForDevOnly,
|
||||
[switch]$PackExe,
|
||||
[switch]$PackExeForce,
|
||||
[switch]$IncludeLicense,
|
||||
@@ -27,7 +27,7 @@ if (-not $SkipInstall) {
|
||||
python -m pip install --upgrade pip
|
||||
python -m pip install -r requirements.txt
|
||||
python -m pip install pyinstaller
|
||||
if ($Obfuscate) {
|
||||
if (-not $NoObfuscateForDevOnly) {
|
||||
python -m pip install pyarmor
|
||||
}
|
||||
}
|
||||
@@ -40,12 +40,14 @@ New-Item -ItemType Directory -Force $DistRoot | Out-Null
|
||||
|
||||
$BuildSourceRoot = $Root
|
||||
$HiddenImportsFile = Join-Path $Root "build\obfuscation-hidden-imports.json"
|
||||
if ($Obfuscate) {
|
||||
if (-not $NoObfuscateForDevOnly) {
|
||||
$BuildSourceRoot = Join-Path $Root "build\obfuscated-src"
|
||||
python -m tools.obfuscation_staging --source-root $Root --staging-root $BuildSourceRoot --hidden-imports-output $HiddenImportsFile
|
||||
if ($LASTEXITCODE -ne 0) {
|
||||
throw "PyArmor obfuscation failed with exit code $LASTEXITCODE"
|
||||
}
|
||||
} else {
|
||||
Write-Warning "Development plaintext build requested. Do not distribute this package."
|
||||
}
|
||||
|
||||
$CommonArgs = @(
|
||||
@@ -86,7 +88,7 @@ foreach ($name in $ExcludedOptionalModules) {
|
||||
$CommonArgs += @("--exclude-module", $name)
|
||||
}
|
||||
|
||||
if ($Obfuscate -and (Test-Path $HiddenImportsFile)) {
|
||||
if ((-not $NoObfuscateForDevOnly) -and (Test-Path $HiddenImportsFile)) {
|
||||
$HiddenImports = Get-Content -LiteralPath $HiddenImportsFile -Raw | ConvertFrom-Json
|
||||
$CliOnlyHiddenImportRoots = @("cv2", "numpy", "paddleocr", "paddle")
|
||||
foreach ($name in $HiddenImports) {
|
||||
|
||||
@@ -6,6 +6,12 @@ from pathlib import Path
|
||||
import pytest
|
||||
|
||||
|
||||
PYARMOR_STUB = (
|
||||
"from pyarmor_runtime_000000 import __pyarmor__\n"
|
||||
"__pyarmor__(__name__, __file__, b'PY000000')\n"
|
||||
)
|
||||
|
||||
|
||||
def _write(path: Path, text: str = "x = 1\n") -> None:
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.write_text(text, encoding="utf-8")
|
||||
@@ -41,14 +47,14 @@ def test_create_pyarmor_staging_uses_only_runtime_sources(tmp_path, monkeypatch)
|
||||
calls.append([str(x) for x in cmd])
|
||||
assert cwd == root
|
||||
assert check is True
|
||||
_write(staging / "douyin" / "__init__.py", "# obfuscated\n")
|
||||
_write(staging / "gui" / "gui_app.py", "# obfuscated\n")
|
||||
_write(staging / "gui" / "gui_app_entry.py", "# obfuscated\n")
|
||||
_write(staging / "gui" / "web_gui.py", "# obfuscated\n")
|
||||
_write(staging / "gui" / "web_gui_entry.py", "# obfuscated\n")
|
||||
_write(staging / "operations" / "__init__.py", "# obfuscated\n")
|
||||
_write(staging / "bin" / "run_cli.py", "# obfuscated\n")
|
||||
_write(staging / "bin" / "run_cli_entry.py", "# obfuscated\n")
|
||||
_write(staging / "douyin" / "__init__.py", PYARMOR_STUB)
|
||||
_write(staging / "gui" / "gui_app.py", PYARMOR_STUB)
|
||||
_write(staging / "gui" / "gui_app_entry.py", PYARMOR_STUB)
|
||||
_write(staging / "gui" / "web_gui.py", PYARMOR_STUB)
|
||||
_write(staging / "gui" / "web_gui_entry.py", PYARMOR_STUB)
|
||||
_write(staging / "operations" / "__init__.py", PYARMOR_STUB)
|
||||
_write(staging / "bin" / "run_cli.py", PYARMOR_STUB)
|
||||
_write(staging / "bin" / "run_cli_entry.py", PYARMOR_STUB)
|
||||
_write(staging / "pyarmor_runtime_000000" / "__init__.py", "# runtime\n")
|
||||
return subprocess.CompletedProcess(cmd, 0)
|
||||
|
||||
@@ -84,18 +90,36 @@ def test_validate_pyarmor_staging_requires_runtime_package(tmp_path):
|
||||
validate_pyarmor_staging(staging)
|
||||
|
||||
|
||||
def test_validate_pyarmor_staging_rejects_plain_runtime_source(tmp_path):
|
||||
from tools.obfuscation_staging import validate_pyarmor_staging
|
||||
|
||||
staging = tmp_path / "stage"
|
||||
_write(staging / "douyin" / "__init__.py", PYARMOR_STUB)
|
||||
_write(staging / "gui" / "gui_app.py", "def leaked():\n return 'plain source'\n")
|
||||
_write(staging / "gui" / "gui_app_entry.py", PYARMOR_STUB)
|
||||
_write(staging / "gui" / "web_gui.py", PYARMOR_STUB)
|
||||
_write(staging / "gui" / "web_gui_entry.py", PYARMOR_STUB)
|
||||
_write(staging / "operations" / "__init__.py", PYARMOR_STUB)
|
||||
_write(staging / "bin" / "run_cli.py", PYARMOR_STUB)
|
||||
_write(staging / "bin" / "run_cli_entry.py", PYARMOR_STUB)
|
||||
_write(staging / "pyarmor_runtime_000000" / "__init__.py")
|
||||
|
||||
with pytest.raises(RuntimeError, match="plain runtime source"):
|
||||
validate_pyarmor_staging(staging)
|
||||
|
||||
|
||||
def test_validate_pyarmor_staging_rejects_sensitive_tools(tmp_path):
|
||||
from tools.obfuscation_staging import validate_pyarmor_staging
|
||||
|
||||
staging = tmp_path / "stage"
|
||||
_write(staging / "douyin" / "__init__.py")
|
||||
_write(staging / "gui" / "gui_app.py")
|
||||
_write(staging / "gui" / "gui_app_entry.py")
|
||||
_write(staging / "gui" / "web_gui.py")
|
||||
_write(staging / "gui" / "web_gui_entry.py")
|
||||
_write(staging / "operations" / "__init__.py")
|
||||
_write(staging / "bin" / "run_cli.py")
|
||||
_write(staging / "bin" / "run_cli_entry.py")
|
||||
_write(staging / "douyin" / "__init__.py", PYARMOR_STUB)
|
||||
_write(staging / "gui" / "gui_app.py", PYARMOR_STUB)
|
||||
_write(staging / "gui" / "gui_app_entry.py", PYARMOR_STUB)
|
||||
_write(staging / "gui" / "web_gui.py", PYARMOR_STUB)
|
||||
_write(staging / "gui" / "web_gui_entry.py", PYARMOR_STUB)
|
||||
_write(staging / "operations" / "__init__.py", PYARMOR_STUB)
|
||||
_write(staging / "bin" / "run_cli.py", PYARMOR_STUB)
|
||||
_write(staging / "bin" / "run_cli_entry.py", PYARMOR_STUB)
|
||||
_write(staging / "pyarmor_runtime_000000" / "__init__.py")
|
||||
_write(staging / "tools" / "license_dev.py", "PRIVATE_KEY = 'leak'\n")
|
||||
|
||||
@@ -112,21 +136,21 @@ def test_create_pyarmor_staging_moves_flat_entry_script_back_to_bin(tmp_path, mo
|
||||
_make_minimal_project(root)
|
||||
|
||||
def fake_runner(cmd, *, cwd, check):
|
||||
_write(staging / "douyin" / "__init__.py", "# obfuscated\n")
|
||||
_write(staging / "gui" / "gui_app.py", "# obfuscated\n")
|
||||
_write(staging / "gui" / "gui_app_entry.py", "# obfuscated\n")
|
||||
_write(staging / "gui" / "web_gui.py", "# obfuscated\n")
|
||||
_write(staging / "gui" / "web_gui_entry.py", "# obfuscated\n")
|
||||
_write(staging / "operations" / "__init__.py", "# obfuscated\n")
|
||||
_write(staging / "run_cli.py", "# flat obfuscated entry\n")
|
||||
_write(staging / "run_cli_entry.py", "# flat obfuscated entry wrapper\n")
|
||||
_write(staging / "douyin" / "__init__.py", PYARMOR_STUB)
|
||||
_write(staging / "gui" / "gui_app.py", PYARMOR_STUB)
|
||||
_write(staging / "gui" / "gui_app_entry.py", PYARMOR_STUB)
|
||||
_write(staging / "gui" / "web_gui.py", PYARMOR_STUB)
|
||||
_write(staging / "gui" / "web_gui_entry.py", PYARMOR_STUB)
|
||||
_write(staging / "operations" / "__init__.py", PYARMOR_STUB)
|
||||
_write(staging / "run_cli.py", PYARMOR_STUB)
|
||||
_write(staging / "run_cli_entry.py", PYARMOR_STUB)
|
||||
_write(staging / "pyarmor_runtime_000000" / "__init__.py", "# runtime\n")
|
||||
return subprocess.CompletedProcess(cmd, 0)
|
||||
|
||||
create_pyarmor_staging(root, staging, runner=fake_runner)
|
||||
|
||||
assert (staging / "bin" / "run_cli.py").read_text(encoding="utf-8") == "# flat obfuscated entry\n"
|
||||
assert (staging / "bin" / "run_cli_entry.py").read_text(encoding="utf-8") == "# flat obfuscated entry wrapper\n"
|
||||
assert (staging / "bin" / "run_cli.py").read_text(encoding="utf-8") == PYARMOR_STUB
|
||||
assert (staging / "bin" / "run_cli_entry.py").read_text(encoding="utf-8") == PYARMOR_STUB
|
||||
assert not (staging / "run_cli.py").exists()
|
||||
assert not (staging / "run_cli_entry.py").exists()
|
||||
|
||||
@@ -186,10 +210,11 @@ def test_collect_runtime_hidden_imports_skips_optional_heavy_modules(tmp_path):
|
||||
assert "scipy" not in hidden_imports
|
||||
|
||||
|
||||
def test_windows_packaging_script_exposes_obfuscate_switch():
|
||||
def test_windows_packaging_script_obfuscates_by_default():
|
||||
script = Path("packaging/pyinstaller/build_windows.ps1").read_text(encoding="utf-8")
|
||||
|
||||
assert "[switch]$Obfuscate" in script
|
||||
assert "[switch]$NoObfuscateForDevOnly" in script
|
||||
assert "if (-not $NoObfuscateForDevOnly)" in script
|
||||
assert "tools.obfuscation_staging" in script
|
||||
assert '$env:DISABLE_MODEL_SOURCE_CHECK = "True"' in script
|
||||
assert '$env:PADDLE_PDX_DISABLE_MODEL_SOURCE_CHECK = "1"' in script
|
||||
@@ -209,6 +234,7 @@ def test_windows_packaging_script_exposes_obfuscate_switch():
|
||||
assert "python -m PyInstaller @CommonArgs @GuiArgs --name web_gui" in script
|
||||
assert "python -m PyInstaller @CommonArgs @CliArgs --name run_cli" in script
|
||||
assert "PyArmor obfuscation failed" in script
|
||||
assert "Development plaintext build requested" in script
|
||||
assert "--paths\", $BuildSourceRoot" in script
|
||||
common_block = script.split("$GuiArgs = @(", 1)[0]
|
||||
gui_block = script.split("$GuiArgs = @(", 1)[1].split("$CliArgs = @(", 1)[0]
|
||||
|
||||
@@ -19,6 +19,7 @@ RUNTIME_ENTRY_FILES = (
|
||||
)
|
||||
FORBIDDEN_STAGING_PREFIXES = ("tools/", "tests/", "artifacts/", "docs/", "packaging/")
|
||||
FORBIDDEN_STAGING_FILES = ("tools/license_dev.py",)
|
||||
PYARMOR_REQUIRED_MARKERS = ("pyarmor_runtime", "__pyarmor__", "PY000000")
|
||||
INTERNAL_IMPORT_TOP_LEVELS = {Path(item).parts[0] for item in RUNTIME_SOURCE_DIRS}
|
||||
OPTIONAL_OR_DEV_HIDDEN_IMPORTS = {
|
||||
"easyocr",
|
||||
@@ -139,6 +140,24 @@ def validate_pyarmor_staging(staging_root: str | Path) -> ObfuscationResult:
|
||||
if forbidden:
|
||||
raise RuntimeError(f"obfuscation staging contains forbidden entries: {', '.join(forbidden[:20])}")
|
||||
|
||||
plain_sources: list[str] = []
|
||||
for rel in [*RUNTIME_SOURCE_DIRS, *RUNTIME_ENTRY_FILES]:
|
||||
path = root / rel
|
||||
candidates = [path] if path.is_file() else list(path.rglob("*.py"))
|
||||
for candidate in candidates:
|
||||
try:
|
||||
text = candidate.read_text(encoding="utf-8", errors="ignore")
|
||||
except OSError:
|
||||
plain_sources.append(candidate.relative_to(root).as_posix())
|
||||
continue
|
||||
if not all(marker in text for marker in PYARMOR_REQUIRED_MARKERS):
|
||||
plain_sources.append(candidate.relative_to(root).as_posix())
|
||||
if plain_sources:
|
||||
raise RuntimeError(
|
||||
"obfuscation staging contains plain runtime source: "
|
||||
+ ", ".join(plain_sources[:20])
|
||||
)
|
||||
|
||||
return ObfuscationResult(staging_root=root, runtime_package=runtime_package)
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user