feat(catalog): 落地正式版根通知、精确提交例外与行为安全复审门控 (#388)

将正式版根通知、精确提交许可例外与行为安全复审门控写入策略文档、Schema 与审计脚本。
This commit is contained in:
mashagua authored and GitHub committed 2026-11-29 07:58:22 +08:00
1 parent d1123e67b5
commit 9fa7f88ef1
4 files changed
+202

No files matched your search

+64
View File
@@ -251,6 +251,7 @@ def accept_client_attached_notices(review: dict, repo: Path, new: str, install_s
source_ref = accepted.get('sourceRef')
client_commit = accepted.get('clientCommit')
client_tree = accepted.get('clientTree')
client_release_tag = accepted.get('clientReleaseTag')
observed_at = accepted.get('observedAt')
notice_path = accepted.get('noticeRootPath')
notice_sha = accepted.get('noticeSha256')
@@ -258,6 +259,11 @@ def accept_client_attached_notices(review: dict, repo: Path, new: str, install_s
raise AuditBlocked('客户端附加通知专项复核未绑定完整上游 pin')
if source_ref != new:
raise AuditBlocked('客户端附加通知专项复核未绑定当前固定快照')
if client_release_tag is not None and not (
isinstance(client_release_tag, str)
and re.fullmatch(r'v\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?', client_release_tag)
):
raise AuditBlocked('客户端附加通知专项复核的发行版标签格式不正确')
if not isinstance(client_commit, str) or not re.fullmatch(r'[0-9a-f]{40}', client_commit):
raise AuditBlocked('客户端附加通知专项复核的客户端提交无效')
implementations = (CLIENT_COLLECTION_NOTICE_IMPLEMENTATIONS if is_collection
@@ -332,6 +338,60 @@ def accept_client_attached_notices(review: dict, repo: Path, new: str, install_s
return seen
def accept_exact_pin_license_exception(review: dict, repo: Path, old: str, new: str) -> bool:
"""Validate an exact-pin human license exception (never inherited across pins)."""
accepted = review.get('acceptedExactPinLicenseException')
if not accepted:
return False
if not isinstance(accepted, dict):
raise AuditBlocked('精确固定提交许可例外记录无效')
if not all(isinstance(review.get(key), str) and review[key].strip()
for key in ('reviewedBy', 'reviewedAt', 'notes')):
raise AuditBlocked('精确固定提交许可例外必须包含审核人、日期和理由')
from_ref, to_ref = accepted.get('fromRef'), accepted.get('toRef')
if from_ref != old or to_ref != new:
raise AuditBlocked('精确固定提交许可例外未绑定当前旧/新快照,不可跨版本继承')
evidence_path = accepted.get('evidencePath')
evidence_sha = accepted.get('evidenceSha256')
review_doc = accepted.get('reviewDoc')
if not isinstance(evidence_path, str) or safe_path(evidence_path) != evidence_path or not evidence_path:
raise AuditBlocked('精确固定提交许可例外的证据路径无效')
if not _sha256_hex(evidence_sha):
raise AuditBlocked('精确固定提交许可例外的证据哈希无效')
actual_bytes = blob(repo, new, evidence_path)
if not actual_bytes or hashlib.sha256(actual_bytes).hexdigest() != evidence_sha:
raise AuditBlocked('精确固定提交许可例外的证据哈希与当前快照不一致')
if (not isinstance(review_doc, str) or safe_path(review_doc) != review_doc
or not review_doc.startswith('docs/')):
raise AuditBlocked('精确固定提交许可例外必须引用 docs/ 下的复审文档')
return True
def accept_behavioral_security_review(review: dict, old: str, new: str) -> bool:
"""Validate an exact-pin read-only behavioral security review record."""
accepted = review.get('acceptedBehavioralSecurityReview')
if not accepted:
return False
if not isinstance(accepted, dict):
raise AuditBlocked('安全与行为专项复核记录无效')
if not all(isinstance(review.get(key), str) and review[key].strip()
for key in ('reviewedBy', 'reviewedAt', 'notes')):
raise AuditBlocked('安全与行为专项复核必须包含审核人、日期和理由')
from_ref, to_ref = accepted.get('fromRef'), accepted.get('toRef')
if from_ref != old or to_ref != new:
raise AuditBlocked('安全与行为专项复核未绑定当前旧/新快照,不可跨版本继承')
review_doc = accepted.get('reviewDoc')
if (not isinstance(review_doc, str) or safe_path(review_doc) != review_doc
or not review_doc.startswith('docs/')):
raise AuditBlocked('安全与行为专项复核必须引用 docs/ 下的复审文档')
reviewed_paths = accepted.get('reviewedPaths')
if (not isinstance(reviewed_paths, list) or not reviewed_paths
or any(not isinstance(p, str) or not p or safe_path(p) != p for p in reviewed_paths)
or len(set(reviewed_paths)) != len(reviewed_paths)):
raise AuditBlocked('安全与行为专项复核的已审路径列表无效')
return True
LATIN_TOKEN = re.compile(r"[A-Za-z][A-Za-z0-9'-]{4,}")
@@ -662,6 +722,10 @@ def _audit_snapshots(repo: Path, entry: dict, sidecar: dict, result: dict,
is_collection=bool(entry.get('children')))
if review.get('acceptedDescriptionAndLocales'):
accept_description_and_locales(review, entry, sidecar, repo, old, new, paths)
if review.get('acceptedExactPinLicenseException'):
accept_exact_pin_license_exception(review, repo, old, new)
if review.get('acceptedBehavioralSecurityReview'):
accept_behavioral_security_review(review, old, new)
if old_root != new_root:
accepted_root = review.get('acceptedRootEvidenceSha256') or {}
if set(old_root) - set(new_root):
@@ -988,5 +988,72 @@ class AuditTests(unittest.TestCase):
self.assertEqual(entry['source']['ref'], new)
self.assertEqual(metadata['governance']['compliance']['reviewedRef'], new)
def test_client_attached_notices_validates_optional_release_tag(self):
from jsonschema import Draft7Validator
schema = json.loads((Path(__file__).resolve().parents[2] / 'schemas' / 'special-review.v1.schema.json').read_text())
(self.repo / 'skills/sample/LICENSE').unlink()
self._rebind_old(self.commit())
self.write('skills/sample/scripts/tool.py', 'print("release tag fixture")\n')
notice_sha = hashlib.sha256((self.repo / 'LICENSE').read_bytes()).hexdigest()
new = self.commit()
valid = self._client_attach_review(
new, ['skills/sample'], notice_sha,
acceptedClientAttachedNotices={'clientReleaseTag': 'v10.0.214'})
Draft7Validator(schema).validate(valid)
audit_snapshots(
self.repo, self.entry, self.metadata, {'ref': self.old, 'head': new},
'2026-10-31T01:02:03Z', special_review=valid)
invalid = self._client_attach_review(
new, ['skills/sample'], notice_sha,
acceptedClientAttachedNotices={'clientReleaseTag': '10.0.214'})
with self.assertRaisesRegex(AuditBlocked, '发行版标签格式不正确'):
audit_snapshots(
self.repo, self.entry, self.metadata, {'ref': self.old, 'head': new},
'2026-10-31T01:02:03Z', special_review=invalid)
def test_exact_pin_license_exception_and_behavioral_security_review(self):
from jsonschema import Draft7Validator
schema = json.loads((Path(__file__).resolve().parents[2] / 'schemas' / 'special-review.v1.schema.json').read_text())
self.write('skills/sample/scripts/tool.py', 'print("security review fixture")\n')
new = self.commit()
lic_sha = hashlib.sha256((self.repo / 'LICENSE').read_bytes()).hexdigest()
valid = {
'schemaVersion': 1,
'reviewedBy': 'mashagua',
'reviewedAt': '2026-11-29',
'notes': 'Exact-pin license exception and read-only behavioral review.',
'acceptedExactPinLicenseException': {
'fromRef': self.old,
'toRef': new,
'evidencePath': 'LICENSE',
'evidenceSha256': lic_sha,
'reviewDoc': 'docs/follow-builders-snapshot-review-20261128.md',
},
'acceptedBehavioralSecurityReview': {
'fromRef': self.old,
'toRef': new,
'reviewDoc': 'docs/reviews/last30days-pr360-7c2e1c2b099b.md',
'reviewedPaths': ['skills/sample/scripts/tool.py'],
},
}
Draft7Validator(schema).validate(valid)
audit_snapshots(
self.repo, self.entry, self.metadata, {'ref': self.old, 'head': new},
'2026-10-31T01:02:03Z', special_review=valid)
stale_exc = json.loads(json.dumps(valid))
stale_exc['acceptedExactPinLicenseException']['toRef'] = self.old
with self.assertRaisesRegex(AuditBlocked, '不可跨版本继承'):
audit_snapshots(
self.repo, self.entry, self.metadata, {'ref': self.old, 'head': new},
'2026-10-31T01:02:03Z', special_review=stale_exc)
bad_doc = json.loads(json.dumps(valid))
bad_doc['acceptedBehavioralSecurityReview']['reviewDoc'] = 'README.md'
with self.assertRaisesRegex(AuditBlocked, 'docs/ 下的复审文档'):
audit_snapshots(
self.repo, self.entry, self.metadata, {'ref': self.old, 'head': new},
'2026-10-31T01:02:03Z', special_review=bad_doc)
if __name__ == '__main__': unittest.main()