fix: bind persisted inventory cache coverage to full checksum

This commit is contained in:
阿白叔 committed 2026-10-23 16:10:00 +08:00
1 parent c787ef325b
commit 9837ca581b
2 files changed
+32 -3

No files matched your search

@@ -148,8 +148,15 @@ class DiscoveryCache:
if path.stat().st_size > 32 * 1024 * 1024:
raise ValueError('inventory cache exceeds size limit')
record = json.loads(path.read_text(encoding='utf-8'))
checksum = record.pop('record_sha256')
if (record.get('version') != 2 or not isinstance(checksum, str)
or checksum != hashlib.sha256(json.dumps(record, ensure_ascii=True, sort_keys=True).encode()).hexdigest()
or type(record.get('collect_components')) is not bool
or not isinstance(record.get('parts'), list)
or not all(isinstance(part, str) for part in record['parts'])):
raise ValueError('invalid discovery cache coverage or identity')
payload = json.dumps(record['discovery'], ensure_ascii=True, sort_keys=True).encode('utf-8')
if record['version'] != 1 or record['payload_sha256'] != hashlib.sha256(payload).hexdigest():
if record['payload_sha256'] != hashlib.sha256(payload).hexdigest():
raise ValueError('invalid discovery cache')
if record['project'] != _key(project) or record['fingerprint'] != hashlib.sha256(repr(stamp).encode()).hexdigest():
return None
@@ -172,10 +179,12 @@ class DiscoveryCache:
from .supervisor_protocol import encode
try:
payload = encode(discovery)
write_record(self._disk_path(project), {'version': 1, 'project': _key(project),
record = {'version': 2, 'project': _key(project),
'fingerprint': hashlib.sha256(repr(stamp).encode()).hexdigest(), 'parts': sorted(requested),
'collect_components': components, 'discovery': payload,
'payload_sha256': hashlib.sha256(json.dumps(payload, ensure_ascii=True, sort_keys=True).encode()).hexdigest()})
'payload_sha256': hashlib.sha256(json.dumps(payload, ensure_ascii=True, sort_keys=True).encode()).hexdigest()}
record['record_sha256'] = hashlib.sha256(json.dumps(record, ensure_ascii=True, sort_keys=True).encode()).hexdigest()
write_record(self._disk_path(project), record)
files = sorted(self._disk_path(project).parent.glob('*.json'), key=lambda path: path.stat().st_mtime_ns, reverse=True)
for stale in files[self.capacity:]:
stale.unlink(missing_ok=True)
@@ -39,3 +39,23 @@ def test_corrupted_disk_cache_is_quarantined_and_read_once(tmp_path):
assert DiscoveryCache(root).discover(project,('OLD',),False,loader)==discovery
assert calls==[1]
assert list((root/'discovery-cache').glob('*.bad-*'))
def test_corrupted_cache_coverage_cannot_authorize_incomplete_reuse(tmp_path):
from dx_productivity.trial_task_center.discovery_cache import DiscoveryCache
from dx_productivity.trial_task_center.quick_run_protocol import ProjectDiscovery
project = tmp_path/'p/demo.prj'; project.parent.mkdir(); project.write_text('project')
root = tmp_path/'data'
discovery = ProjectDiscovery((('main','1'),), {('main','1'): frozenset({'OLD'})})
DiscoveryCache(root).discover(project, ('OLD',), False, lambda: discovery)
path = next((root/'discovery-cache').glob('*.json'))
value = json.loads(path.read_text())
value['parts'].append('other')
path.write_text(json.dumps(value))
calls = []
def loader():
calls.append(1)
return discovery
DiscoveryCache(root).discover(project, ('OTHER',), False, loader)
assert calls == [1]
assert list((root/'discovery-cache').glob('*.bad-*'))