fix: bind persisted inventory cache coverage to full checksum
This commit is contained in:
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)
|
||||
|
||||
Reference in new issue
Block a user