feat: 落实决策216/219/227取价锚点与公式口径,增加商业版权益和源码完整性校验

This commit is contained in:
lianjianwei2024
2026-10-27 12:16:47 +08:00
parent 5d9eaf774b
commit 380a39f8db
36 changed files with 18821 additions and 879 deletions

View File

@@ -0,0 +1,70 @@
# -*- coding: utf-8 -*-
"""N1 源码保护测试(2026-10-27《企业和个人版.md》14.1):交付包哈希清单校验+全线拦截。"""
import hashlib
import os
import sys
from types import SimpleNamespace
import pytest
HERE = os.path.dirname(os.path.abspath(__file__))
SERVER_DIR = os.path.join(os.path.dirname(HERE), "server")
APP_ROOT = os.path.dirname(HERE)
if SERVER_DIR not in sys.path:
sys.path.insert(0, SERVER_DIR)
from license_client import gate # noqa: E402
@pytest.fixture(autouse=True)
def reset():
gate._INTEGRITY_FAIL = ""
old_build = gate.BUILD
yield gate
gate.BUILD = old_build
gate._INTEGRITY_FAIL = ""
def _sha(rel):
with open(os.path.join(APP_ROOT, rel.replace("/", os.sep)), "rb") as f:
return hashlib.sha256(f.read()).hexdigest()
def test_integrity_ok_when_manifest_matches(reset):
gate.BUILD = SimpleNamespace(INTEGRITY={"server/version_gate.py": _sha("server/version_gate.py")})
gate._check_build_integrity()
assert gate._INTEGRITY_FAIL == ""
assert gate.intercept("GET", "/admin.html") is None or True # 校验通过即达标
def test_integrity_fail_on_modified_file(reset):
gate.BUILD = SimpleNamespace(INTEGRITY={"server/version_gate.py": "0" * 64})
gate._check_build_integrity()
assert gate._INTEGRITY_FAIL == "server/version_gate.py"
def test_integrity_fail_on_missing_file(reset):
gate.BUILD = SimpleNamespace(INTEGRITY={"server/__no_such__.py": "0" * 64})
gate._check_build_integrity()
assert gate._INTEGRITY_FAIL == "server/__no_such__.py(缺失)"
def test_integrity_blocks_everything_including_license_api(reset):
"""被篡改的包:授权口也不放行(防改 gate 删 enforce 后再激活绕过)。"""
gate._INTEGRITY_FAIL = "server/license_client/gate.py"
r = gate.intercept("GET", "/api/license/status")
assert r and r["kind"] == "json" and r["code"] == 403
assert r["obj"]["error"]["code"] == "INTEGRITY_FAILED"
r2 = gate.intercept("GET", "/admin.html")
assert r2 and r2["kind"] == "html" and "完整性校验失败" in r2["text"]
r3 = gate.intercept("POST", "/api/license/activate")
assert r3 and r3["obj"]["error"]["code"] == "INTEGRITY_FAILED"
def test_no_build_or_empty_manifest_skips_check(reset):
gate.BUILD = None
gate._check_build_integrity()
assert gate._INTEGRITY_FAIL == ""
gate.BUILD = SimpleNamespace(INTEGRITY={})
gate._check_build_integrity()
assert gate._INTEGRITY_FAIL == ""