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

@@ -9,6 +9,7 @@
- canonical_sha256 是规范化字节的 SHA-256 十六进制,仅辅助校验,不能代替公钥验签。
- 拒绝:未知版本、缺字段/多字段、非法类型、含 CR/LF/NUL 的字段、未知或已撤销
key_id、非 P-256 公钥、绑定到其他实例的信封、legacy v1。
(证书例外:OPTIONAL_CERT_FIELDS 两个可选权益字段可缺省,见下方注释。)
信任根:只信随软件/受控渠道固定分发的公钥(pinned_keys.json),绝不在线发现。
"""
@@ -33,6 +34,20 @@ FIELDSETS = {
LEASE_V2: ("version", "instance_id", "state", "server_time",
"entitlement_expires_at", "lease_expires_at", "key_id"),
}
# 商业版权益字段(2026-10-27,《企业和个人版.md》5.1):仅证书载荷**可选**携带;
# 出现 → 纳入 canonical(按本元组顺序追加在必填之后)并校验取值;
# 缺省 → edition=personal、max_seats=1(fail-closed 不放大权益;平台未升级协议仍能签发)。
OPTIONAL_CERT_FIELDS = ("edition", "max_seats")
EDITIONS = ("personal", "enterprise", "trial")
def entitlements(cert):
"""从证书载荷取商业版权益(规范化缺省)。门禁与状态视图共用唯一口径。"""
if not isinstance(cert, dict):
return {"edition": "personal", "max_seats": 1}
return {"edition": cert.get("edition") or "personal",
"max_seats": cert.get("max_seats") or 1}
UUID_RE = re.compile(r"^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$",
re.IGNORECASE)
RFC3339_RE = re.compile(r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-]\d{2}:\d{2})$")
@@ -145,6 +160,12 @@ def canonical_bytes(payload, version):
if name not in payload:
raise VerifyError("payload 缺字段 %s" % name)
out.append("%s=%s" % (name, _field_value(payload, name)))
if version == LICENSE_V2:
# 可选权益字段:签发方带了才进 canonical(顺序固定);取值合法性由 _check_payload 管
for name in OPTIONAL_CERT_FIELDS:
if name in payload:
v = payload[name]
out.append("%s=%s" % (name, str(v) if isinstance(v, int) else v))
return "\n".join(out).encode("utf-8")
@@ -153,7 +174,20 @@ def canonical_bytes(payload, version):
def _check_payload(payload, version, expected_instance):
if not isinstance(payload, dict):
raise VerifyError("payload 不是对象")
if set(payload.keys()) != set(FIELDSETS[version]):
keys = set(payload.keys())
required = set(FIELDSETS[version])
if version == LICENSE_V2:
# 必填全在;多出的键只能是可选权益字段(edition/max_seats),其余多字段仍拒
if (keys - required) - set(OPTIONAL_CERT_FIELDS) or required - keys:
raise VerifyError("payload 字段集不符(缺/多字段)")
ed = payload.get("edition")
if ed is not None and (not isinstance(ed, str) or ed not in EDITIONS
or any(c in ed for c in ("\r", "\n", "\x00"))):
raise VerifyError("edition 取值非法")
ms = payload.get("max_seats")
if ms is not None and (isinstance(ms, bool) or not isinstance(ms, int) or ms < 1):
raise VerifyError("max_seats 必须是正整数")
elif keys != required:
raise VerifyError("payload 字段集不符(缺/多字段)")
if payload["version"] != version:
raise VerifyError("payload.version=%r 不是 %s" % (payload["version"], version))