am-freescan/scripts/verify_scanner_history_readonly.py

47 lines
2.9 KiB
Python
Raw Normal View History

"""Audit existing execution history and result ownership without scanner control or business writes."""
import argparse
import json
from verify_cmvr_native import Client, require
def audit(client, task_id):
detail = client.request('GET', '/admin-api/lab/scan-task/get', query={'taskId': task_id})['data']
checked = []
for unit in detail['units']:
attempts = {str(row['id']): row for row in unit.get('attempts', [])}
if unit.get('currentExecutionId'):
require(str(unit['currentExecutionId']) in attempts, 'Current execution is missing from history')
owned = {}
for execution_id, attempt in attempts.items():
require(str(attempt['sampleUnitId']) == str(unit['id']), 'Execution belongs to another sample')
require(str(attempt['taskId']) == str(task_id), 'Execution belongs to another task')
if attempt.get('executionType') == 'SUPPLEMENT':
require(str(attempt.get('parentExecutionId')) in attempts, 'Supplement source is missing')
files = client.request('GET', '/admin-api/lab/execution/scanner-view/assets',
query={'executionId': execution_id})['data']
ids = [file['fileId'] for file in files]
require(len(ids) == len(set(ids)), 'Duplicate result IDs')
require(all('binding' not in file and 'sha256' not in file for file in files), 'Private metadata leaked')
owned[execution_id] = set(ids)
checked.append({'executionId': execution_id, 'executionNo': attempt['executionNo'],
'type': attempt.get('executionType'), 'status': attempt['status'],
'files': [{'fileId': f['fileId'], 'name': f['name'], 'bytes': f['bytes'],
'archived': f.get('archived', False)} for f in files]})
# A real file from another attempt must never be served under this execution ID.
for execution_id, ids in owned.items():
foreign = next((file for other, values in owned.items() if other != execution_id
for file in values if file not in ids), None)
if foreign:
result = client.request('GET', '/admin-api/lab/execution/scanner-view/asset/' + foreign,
query={'executionId': execution_id}, expected=None)
require(result.get('code') not in (0, 200), 'Another execution can read this result')
return {'taskId': str(task_id), 'taskNo': detail['task']['taskNo'], 'attempts': checked}
if __name__ == '__main__':
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--task-id', action='append', required=True)
args = parser.parse_args()
client = Client().login()
print(json.dumps({'passed': True, 'checks': [audit(client, task) for task in args.task_id]}, ensure_ascii=False))