mirror of
https://git.openapi.site/https://github.com/desirecore/market.git
synced 2026-12-05 22:19:36 +08:00
feat(market): 专项复核支持合集成员与根 Notice 的精确接受哈希 (#307)
为 `special-review` v1 增加两个可选接受字段,使 Class C 专项复核能在**精确匹配**时接受合集成员变化和根目录 LICENSE/NOTICE/COPYING 变化。这不是无条件绕过硬门槛:哈希或路径集合不对、不完整,审计仍然失败。 本 PR **只改审计工具、schema 和单元测试**,不更新任何技能 pin,也不把失败草稿标成 Ready。 ## 新字段 - `acceptedRootEvidenceSha256`:根目录 LICENSE/NOTICE/COPYING 路径 → 新快照文件字节的 SHA-256。当根证据有变化时,该映射必须**恰好等于**新增或变更路径及其新哈希。删除这些文件仍一律阻止,即使复核记录省略了被删路径。`excludePaths` 仍不能用来隐藏许可、Notice 或依赖清单。若声明的 `licenseEvidencePath` / `noticePath` / `license.evidencePath` 同时变化,也必须被同一接受哈希覆盖,否则该检查仍失败。安装范围内的许可/Notice 变化仍是硬门槛,本次不加豁免。 - `acceptedCollectionPaths`:必须恰好等于新快照 `discover_children` 的成员路径集合。接受后用发现结果作为审核树、预算和后续检查的路径,并据此重建 `entry.children` 与 sidecar `spec.collection.children`(写入 id、path,以及 frontmatter 归一化后的 version)。新成员必须同时提供 `acceptedFrontmatterSha256`,且**不会自动编造翻译**。已有成员的 frontmatter 规则和版本回退保护保持不变。没有该字段时,新增成员仍会以「合集成员」被阻止。 ## 本地验证 在当前 `origin/main`(`d463565237fab79b9e6eb5e944246a4e14dd625e`)上: - `scripts/catalog/test_audit_skill_update.py`:22/22 - `scripts/catalog/test_validate_catalog_metadata.py`:62/62 - `scripts/catalog/test_propose_upstream_updates.py`:9/9 本 PR 不代表运行或安全认证,也不自动合并。
This commit is contained in:
1 parent
d463565237
commit
b329b8cc30
3 files changed
+175
-9
No files matched your search
@@ -31,6 +31,17 @@
|
||||
"type": "object",
|
||||
"description": "Map of install path (empty string for a repo-root skill) to SHA-256 of stable_frontmatter(new SKILL.md).",
|
||||
"additionalProperties": { "type": "string", "pattern": "^[a-f0-9]{64}$" }
|
||||
},
|
||||
"acceptedRootEvidenceSha256": {
|
||||
"type": "object",
|
||||
"description": "Map of repo-root LICENSE/NOTICE/COPYING path to SHA-256 of the *new* snapshot file bytes. When root evidence changes, this map must exactly equal the added or changed paths; deletions of LICENSE/NOTICE/COPYING are never accepted.",
|
||||
"additionalProperties": { "type": "string", "pattern": "^[a-f0-9]{64}$" }
|
||||
},
|
||||
"acceptedCollectionPaths": {
|
||||
"type": "array",
|
||||
"description": "Exact set of new collection membership install paths when discovered children differ from the old entry. Must equal the discovered path set. Newly added members also require acceptedFrontmatterSha256 for that path.",
|
||||
"items": { "type": "string", "minLength": 1 },
|
||||
"uniqueItems": true
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -188,15 +188,44 @@ def audit_snapshots(repo: Path, entry: dict, sidecar: dict, result: dict,
|
||||
paths = [safe_path(child['path']) for child in entry.get('children', [])]
|
||||
if not paths:
|
||||
paths = [safe_path(entry['source'].get('path', ''))]
|
||||
old_member_paths = set(paths) if entry.get('children') else set()
|
||||
discovered_children = []
|
||||
if entry.get('children'):
|
||||
discovered_children = GENERATOR.discover_children(repo, new)
|
||||
discovered = {safe_path(child['path']) for child in discovered_children}
|
||||
if discovered != set(paths):
|
||||
accepted_paths = review.get('acceptedCollectionPaths') or []
|
||||
accepted = None
|
||||
if isinstance(accepted_paths, list) and all(isinstance(item, str) for item in accepted_paths):
|
||||
try:
|
||||
accepted = {safe_path(item) for item in accepted_paths}
|
||||
except AuditBlocked:
|
||||
accepted = None
|
||||
if accepted is None or len(accepted_paths) != len(accepted) or accepted != discovered:
|
||||
raise AuditBlocked('合集成员集合变化,需核对新增/移除成员及翻译')
|
||||
paths = [safe_path(child['path']) for child in discovered_children]
|
||||
old_files, new_files = {}, {}
|
||||
for path in paths:
|
||||
old_files.update(tree(repo, old, path, exclude_paths, budget_exempt_paths))
|
||||
new_files.update(tree(repo, new, path, exclude_paths, budget_exempt_paths))
|
||||
if not new_files or len(new_files) > MAX_FILES or sum(item[2] for item in new_files.values()) > MAX_CONTENT_BYTES:
|
||||
raise AuditBlocked('新快照为空或超过总预算')
|
||||
licenses = root_evidence(repo, old)
|
||||
if licenses != root_evidence(repo, new):
|
||||
old_root = root_evidence(repo, old)
|
||||
new_root = root_evidence(repo, new)
|
||||
if old_root != new_root:
|
||||
accepted_root = review.get('acceptedRootEvidenceSha256') or {}
|
||||
if set(old_root) - set(new_root):
|
||||
raise AuditBlocked('根许可证或 Notice 有变化,需专项复核')
|
||||
expected_root = {
|
||||
path: digest
|
||||
for path, digest in new_root.items()
|
||||
if old_root.get(path) != digest
|
||||
}
|
||||
if (not isinstance(accepted_root, dict)
|
||||
or any(not isinstance(key, str) or not isinstance(value, str) for key, value in accepted_root.items())
|
||||
or accepted_root != expected_root):
|
||||
raise AuditBlocked('根许可证或 Notice 有变化,需专项复核')
|
||||
licenses = new_root
|
||||
# 安装范围内的嵌套许可同样影响再分发,不能只核对根许可证。
|
||||
def scoped_evidence(ref, files):
|
||||
return {path: hashlib.sha256(blob(repo, ref, path)).hexdigest()
|
||||
@@ -205,10 +234,14 @@ def audit_snapshots(repo: Path, entry: dict, sidecar: dict, result: dict,
|
||||
if scoped_licenses != scoped_evidence(new, new_files):
|
||||
raise AuditBlocked('安装范围内的许可证或 Notice 有变化,需专项复核')
|
||||
licenses.update(scoped_licenses)
|
||||
accepted_root = review.get('acceptedRootEvidenceSha256') or {}
|
||||
for path in {compliance.get('licenseEvidencePath'), compliance.get('noticePath'),
|
||||
governance.get('license', {}).get('evidencePath')} - {None}:
|
||||
old_bytes, new_bytes = blob(repo, old, path), blob(repo, new, path)
|
||||
if not old_bytes or old_bytes != new_bytes:
|
||||
digest = hashlib.sha256(new_bytes).hexdigest() if new_bytes else None
|
||||
if (not old_bytes or not new_bytes or not isinstance(accepted_root, dict)
|
||||
or accepted_root.get(path) != digest):
|
||||
raise AuditBlocked('声明的许可证或 Notice 变化,需专项复核')
|
||||
licenses[path] = hashlib.sha256(new_bytes).hexdigest()
|
||||
if not licenses:
|
||||
@@ -240,14 +273,21 @@ def audit_snapshots(repo: Path, entry: dict, sidecar: dict, result: dict,
|
||||
if accepted != expected:
|
||||
raise AuditBlocked('依赖清单有变化,无法自动确认新的前置条件')
|
||||
versions = {}
|
||||
accepted_fm = review.get('acceptedFrontmatterSha256') or {}
|
||||
for path in paths:
|
||||
marker = f'{path}/SKILL.md' if path else 'SKILL.md'
|
||||
old_fm = GENERATOR.skill_frontmatter(repo, marker, old)
|
||||
new_fm = GENERATOR.skill_frontmatter(repo, marker, new)
|
||||
if not isinstance(old_fm, dict) or not isinstance(new_fm, dict):
|
||||
if not isinstance(new_fm, dict):
|
||||
raise AuditBlocked('技能 frontmatter 必须是对象')
|
||||
is_new_member = bool(entry.get('children')) and path not in old_member_paths
|
||||
if is_new_member:
|
||||
if not isinstance(accepted_fm, dict) or accepted_fm.get(path) != frontmatter_identity(new_fm):
|
||||
raise AuditBlocked('名称、简介、依赖、兼容性或其他结构化声明有变化,需同步审核元数据和翻译')
|
||||
else:
|
||||
old_fm = GENERATOR.skill_frontmatter(repo, marker, old)
|
||||
if not isinstance(old_fm, dict):
|
||||
raise AuditBlocked('技能 frontmatter 必须是对象')
|
||||
if stable_frontmatter(old_fm) != stable_frontmatter(new_fm):
|
||||
accepted_fm = review.get('acceptedFrontmatterSha256') or {}
|
||||
if not isinstance(accepted_fm, dict) or accepted_fm.get(path) != frontmatter_identity(new_fm):
|
||||
raise AuditBlocked('名称、简介、依赖、兼容性或其他结构化声明有变化,需同步审核元数据和翻译')
|
||||
raw = new_fm.get('version')
|
||||
@@ -265,10 +305,37 @@ def audit_snapshots(repo: Path, entry: dict, sidecar: dict, result: dict,
|
||||
updated['source']['ref'] = new
|
||||
metadata['provenance']['content']['ref'] = new
|
||||
if updated.get('children'):
|
||||
# 新增/移除成员及翻译需要单独处理;不能把一个未审成员混入整包审核。
|
||||
discovered = {child['path'] for child in GENERATOR.discover_children(repo, new)}
|
||||
if discovered != set(paths):
|
||||
raise AuditBlocked('合集成员集合变化,需核对新增/移除成员及翻译')
|
||||
# 新增/移除成员及翻译需要单独处理;接受后按发现结果重建成员列表,不为新成员编造翻译。
|
||||
if {child['path'] for child in updated['children']} != {child['path'] for child in discovered_children}:
|
||||
existing = {child['path']: child for child in updated['children']}
|
||||
catalog_existing = {
|
||||
child.get('identity', {}).get('id'): child
|
||||
for child in metadata.get('spec', {}).get('collection', {}).get('children', [])
|
||||
}
|
||||
rebuilt, rebuilt_catalog = [], []
|
||||
for discovered_child in discovered_children:
|
||||
path = discovered_child['path']
|
||||
if path in existing:
|
||||
child = existing[path]
|
||||
catalog_child = catalog_existing.get(child['id'])
|
||||
else:
|
||||
child = {'id': discovered_child['id'], 'path': path}
|
||||
catalog_child = {'identity': {'id': child['id']}, 'path': path}
|
||||
if path in versions:
|
||||
version = versions[path]
|
||||
previous = GENERATOR.normalize_version(child.get('version'))
|
||||
if previous and tuple(map(int, version.split('.'))) < tuple(map(int, previous.split('.'))):
|
||||
raise AuditBlocked('拒绝自动回退合集成员版本')
|
||||
child['version'] = version
|
||||
if catalog_child is not None:
|
||||
catalog_child['release'] = {'state': 'known', 'version': version, 'versionScheme': 'semver'}
|
||||
rebuilt.append(child)
|
||||
if catalog_child is not None:
|
||||
rebuilt_catalog.append(catalog_child)
|
||||
updated['children'] = rebuilt
|
||||
if metadata.get('spec', {}).get('collection') is not None:
|
||||
metadata['spec']['collection']['children'] = rebuilt_catalog
|
||||
else:
|
||||
for child in updated['children']:
|
||||
if child['path'] in versions:
|
||||
version = versions[child['path']]
|
||||
@@ -311,6 +378,8 @@ def audit_snapshots(repo: Path, entry: dict, sidecar: dict, result: dict,
|
||||
'budgetExemptPaths': list(budget_exempt_paths),
|
||||
}
|
||||
report['limitations'].append('依赖清单或结构化简介如有变化,已按专项复核记录中的接受哈希核对,不代表这些字段与旧快照逐字节相同。')
|
||||
if review.get('acceptedRootEvidenceSha256') or review.get('acceptedCollectionPaths'):
|
||||
report['limitations'].append('根许可证/Notice 或合集成员如有变化,已按专项复核记录中的接受哈希或路径集合核对,不代表与旧快照逐字节或成员集合相同。')
|
||||
if exclude_paths:
|
||||
report['limitations'].append('excludePaths 只从本仓库的自动审核树遍历中省略路径;DesireCore 安装器在支持该字段前仍会从上游 pin 安装这些文件。')
|
||||
errors = validate_record(metadata, report)
|
||||
|
||||
@@ -234,6 +234,92 @@ class AuditTests(unittest.TestCase):
|
||||
self.assertEqual(metadata['governance']['compliance']['reviewedBy'], REVIEWER)
|
||||
self.assertEqual(report['specialReview']['reviewedBy'] if report.get('specialReview') else None, None)
|
||||
|
||||
def test_special_review_accepts_new_collection_member(self):
|
||||
from audit_skill_update import frontmatter_identity
|
||||
from check_upstream_health import GENERATOR
|
||||
self.entry['children'] = [{'id': 'sample', 'path': 'skills/sample', 'version': '1.0.0', 'i18n': {'zh-CN': {'name': '示例'}}}]
|
||||
self.metadata['spec'] = {'collection': {'children': [{'identity': {'id':'sample'}, 'path':'skills/sample', 'release': {'state':'known','version':'1.0.0','versionScheme':'semver'}}]}}
|
||||
self.write('skills/another/SKILL.md', '---\nname: another\ndescription: New member\nversion: 0.1.0\n---\nBody\n')
|
||||
new = self.commit()
|
||||
fm = GENERATOR.skill_frontmatter(self.repo, 'skills/another/SKILL.md', new)
|
||||
discovered = [child['path'] for child in GENERATOR.discover_children(self.repo, new)]
|
||||
with self.assertRaisesRegex(AuditBlocked, '合集成员'):
|
||||
audit_snapshots(self.repo, self.entry, self.metadata, {'ref': self.old, 'head': new}, '2026-10-31T01:02:03Z')
|
||||
review = {
|
||||
'schemaVersion': 1,
|
||||
'acceptedCollectionPaths': discovered,
|
||||
'acceptedFrontmatterSha256': {'skills/another': frontmatter_identity(fm)},
|
||||
}
|
||||
entry, metadata, report = audit_snapshots(
|
||||
self.repo, self.entry, self.metadata, {'ref': self.old, 'head': new}, '2026-10-31T01:02:03Z', special_review=review)
|
||||
ids = [child['id'] for child in entry['children']]
|
||||
self.assertEqual(set(ids), {'sample', 'another'})
|
||||
another = next(child for child in entry['children'] if child['id'] == 'another')
|
||||
self.assertEqual(another['path'], 'skills/another')
|
||||
self.assertEqual(another['version'], '0.1.0')
|
||||
self.assertNotIn('i18n', another)
|
||||
sample = next(child for child in entry['children'] if child['id'] == 'sample')
|
||||
self.assertEqual(sample['i18n'], self.entry['children'][0]['i18n'])
|
||||
catalog_ids = [child['identity']['id'] for child in metadata['spec']['collection']['children']]
|
||||
self.assertEqual(set(catalog_ids), {'sample', 'another'})
|
||||
catalog_another = next(child for child in metadata['spec']['collection']['children'] if child['identity']['id'] == 'another')
|
||||
self.assertNotIn('i18n', catalog_another)
|
||||
self.assertEqual(catalog_another['release']['version'], '0.1.0')
|
||||
self.assertEqual(report['source']['paths'], sorted(['skills/sample', 'skills/another']))
|
||||
|
||||
def test_special_review_rejects_wrong_collection_or_frontmatter_acceptance(self):
|
||||
from audit_skill_update import frontmatter_identity
|
||||
from check_upstream_health import GENERATOR
|
||||
self.entry['children'] = [{'id': 'sample', 'path': 'skills/sample', 'version': '1.0.0'}]
|
||||
self.metadata['spec'] = {'collection': {'children': [{'identity': {'id':'sample'}, 'path':'skills/sample'}]}}
|
||||
self.write('skills/another/SKILL.md', '---\nname: another\ndescription: New member\nversion: 0.1.0\n---\n')
|
||||
new = self.commit()
|
||||
fm = GENERATOR.skill_frontmatter(self.repo, 'skills/another/SKILL.md', new)
|
||||
discovered = [child['path'] for child in GENERATOR.discover_children(self.repo, new)]
|
||||
digest = frontmatter_identity(fm)
|
||||
cases = [
|
||||
{'schemaVersion': 1, 'acceptedCollectionPaths': ['skills/sample'], 'acceptedFrontmatterSha256': {'skills/another': digest}},
|
||||
{'schemaVersion': 1, 'acceptedCollectionPaths': discovered + ['skills/extra'], 'acceptedFrontmatterSha256': {'skills/another': digest}},
|
||||
{'schemaVersion': 1, 'acceptedCollectionPaths': discovered, 'acceptedFrontmatterSha256': {'skills/another': 'a' * 64}},
|
||||
{'schemaVersion': 1, 'acceptedCollectionPaths': discovered},
|
||||
]
|
||||
for review in cases:
|
||||
with self.subTest(review=review):
|
||||
with self.assertRaisesRegex(AuditBlocked, '合集成员|结构化声明'):
|
||||
audit_snapshots(self.repo, self.entry, self.metadata, {'ref': self.old, 'head': new}, '2026-10-31T01:02:03Z', special_review=review)
|
||||
|
||||
def test_special_review_accepts_root_notice_change(self):
|
||||
from audit_skill_update import root_evidence
|
||||
self.write('NOTICE', 'old notice\n')
|
||||
self.old = self.commit()
|
||||
self.entry['source']['ref'] = self.old
|
||||
self.metadata['provenance']['content']['ref'] = self.old
|
||||
self.metadata['governance']['compliance']['reviewedRef'] = self.old
|
||||
self.write('NOTICE', 'new notice documenting a font license\n')
|
||||
new = self.commit()
|
||||
with self.assertRaisesRegex(AuditBlocked, '许可证|Notice'):
|
||||
audit_snapshots(self.repo, self.entry, self.metadata, {'ref': self.old, 'head': new}, '2026-10-31T01:02:03Z')
|
||||
digest = root_evidence(self.repo, new)['NOTICE']
|
||||
with self.assertRaisesRegex(AuditBlocked, '许可证|Notice'):
|
||||
audit_snapshots(self.repo, self.entry, self.metadata, {'ref': self.old, 'head': new}, '2026-10-31T01:02:03Z',
|
||||
special_review={'schemaVersion': 1, 'acceptedRootEvidenceSha256': {'NOTICE': 'b' * 64}})
|
||||
_, _, report = audit_snapshots(
|
||||
self.repo, self.entry, self.metadata, {'ref': self.old, 'head': new}, '2026-10-31T01:02:03Z',
|
||||
special_review={'schemaVersion': 1, 'acceptedRootEvidenceSha256': {'NOTICE': digest}})
|
||||
self.assertEqual(report['licenseEvidenceSha256']['NOTICE'], digest)
|
||||
|
||||
def test_deleted_root_license_is_not_accepted(self):
|
||||
(self.repo / 'LICENSE').unlink()
|
||||
new = self.commit()
|
||||
reviews = [
|
||||
{'schemaVersion': 1, 'acceptedRootEvidenceSha256': {}},
|
||||
{'schemaVersion': 1},
|
||||
]
|
||||
for review in reviews:
|
||||
with self.subTest(review=review):
|
||||
with self.assertRaisesRegex(AuditBlocked, '许可证|Notice'):
|
||||
audit_snapshots(self.repo, self.entry, self.metadata, {'ref': self.old, 'head': new}, '2026-10-31T01:02:03Z', special_review=review)
|
||||
|
||||
def test_disallows_non_github_fetch_targets(self):
|
||||
self.entry['source']['repoUrl'] = 'file:///private/source'
|
||||
with self.assertRaisesRegex(AuditBlocked, 'HTTPS'): audit_update(self.entry, self.metadata, {})
|
||||
|
||||
Reference in new issue
Block a user