fix: enforce native repair ownership and cancellation boundaries
This commit is contained in:
1 parent
2490067b2d
commit
9cd4968a97
8 files changed
+248
-10
No files matched your search
@@ -289,3 +289,18 @@ def test_cooperative_supervisor_loss_keeps_pending_save_helper_alive(tmp_path):
|
||||
if handle:
|
||||
win32process.TerminateProcess(handle, 0)
|
||||
handle.Close()
|
||||
|
||||
|
||||
def test_idle_failed_native_verify_closes_its_library_manager_owner(tmp_path, monkeypatch):
|
||||
from types import SimpleNamespace
|
||||
from test_verify_worker_stability import exercise_worker
|
||||
job, events, execute = exercise_worker(tmp_path, monkeypatch, integration_error='packaging rejected')
|
||||
job['native_lm_verify'] = True
|
||||
closed = []
|
||||
monkeypatch.setattr('cbb_refresh_verify.library_manager_verify.launch_library_verify',
|
||||
lambda *a, **kw: SimpleNamespace(process=SimpleNamespace(pid=11), close=lambda: closed.append(11)))
|
||||
monkeypatch.setattr('cbb_refresh_verify.failed_session.close_failed_document',
|
||||
lambda *a, **kw: {'status':'CLOSED'})
|
||||
with pytest.raises(RuntimeError, match='packaging rejected'):
|
||||
execute()
|
||||
assert closed == [11]
|
||||
@@ -140,3 +140,105 @@ def test_automatic_repair_does_not_retry_busy_or_unsupported_target(tmp_path, mo
|
||||
assert len(calls) == 1
|
||||
assert outcome.safe_to_continue is False
|
||||
assert outcome.recovery_attempts[0]['status'] == 'BLOCKED'
|
||||
|
||||
|
||||
def test_timeout_never_enters_automatic_repair(tmp_path, monkeypatch):
|
||||
from cbb_refresh_verify import verify_runner as runner, native_repair
|
||||
request, record, target = target_fixture(tmp_path)
|
||||
evidence = tmp_path/'first/result.json'
|
||||
evidence.parent.mkdir()
|
||||
(evidence.parent/'diagnostics').mkdir()
|
||||
(evidence.parent/'diagnostics/icdb-recovery.json').write_text(json.dumps(record))
|
||||
monkeypatch.setattr(runner, '_run_verify_serialized', lambda *a, **kw:
|
||||
runner.VerifyResult('TIMEOUT', error_category='ICDB_REPAIR_REQUIRED',
|
||||
safe_to_continue=False, evidence_path=evidence))
|
||||
monkeypatch.setattr(native_repair, 'attempt_recovery', lambda *a, **kw:
|
||||
pytest.fail('A timed-out native owner must not be repaired'))
|
||||
assert runner.run_verify(request).status == 'TIMEOUT'
|
||||
|
||||
|
||||
def test_retained_importer_blocks_repair_even_without_an_open_database_handle(tmp_path, monkeypatch):
|
||||
from cbb_refresh_verify import native_repair
|
||||
request, record, target = target_fixture(tmp_path)
|
||||
evidence = tmp_path/'original'
|
||||
evidence.mkdir()
|
||||
(evidence/'content-check-process.json').write_text(json.dumps({'status':'RETAINED','pid':123}))
|
||||
monkeypatch.setattr(native_repair, 'run_native_repair', lambda *a, **kw:
|
||||
pytest.fail('Do not repair between importer transactions'))
|
||||
outcome = native_repair.attempt_recovery(request, record, evidence)
|
||||
assert outcome['status'] == 'BLOCKED'
|
||||
assert 'IMPORTER_RELEASE_UNCONFIRMED' in outcome['detail']
|
||||
|
||||
|
||||
def test_repair_cancellation_during_backup_prevents_native_launch(tmp_path, monkeypatch):
|
||||
from cbb_refresh_verify import native_repair_ui as ui, native_repair, mentor_adapter
|
||||
request, record, target = target_fixture(tmp_path)
|
||||
root = tmp_path/'worker'
|
||||
root.mkdir()
|
||||
cancel = root/'cancel.signal'
|
||||
job = root/'request.json'
|
||||
job.write_text(json.dumps({'project':str(target.project),'database':str(target.database),
|
||||
'root':str(root),'cancel_signal':str(cancel),'timeout':1,
|
||||
'supervisor_pid':123,'desktop':'CbbRepair_test'}))
|
||||
calls = []
|
||||
monkeypatch.setattr('cbb_refresh_verify.desktop_worker.start_supervisor_watchdog',
|
||||
lambda *a, **kw: calls.append('watchdog'))
|
||||
monkeypatch.setattr(ui, '_current_desktop_name', lambda: 'CbbRepair_test', raising=False)
|
||||
monkeypatch.setattr(ui, '_repair_command', lambda *_: 32924)
|
||||
monkeypatch.setattr(mentor_adapter, 'pcb_processes', lambda: [])
|
||||
monkeypatch.setattr(mentor_adapter, '_process_details', lambda *_: [])
|
||||
def backup(*a):
|
||||
assert calls == ['watchdog'], 'Watch supervisor before the potentially long backup'
|
||||
cancel.touch()
|
||||
calls.append('backup')
|
||||
monkeypatch.setattr(native_repair, 'backup_project', backup)
|
||||
monkeypatch.setattr(ui.subprocess, 'Popen', lambda *a, **kw: pytest.fail('Repair must not start'))
|
||||
assert ui.repair_worker_main(job) == 1
|
||||
outcome = json.loads((root/'repair-result.json').read_text('utf-8'))
|
||||
assert 'REPAIR_CANCELLED_AFTER_BACKUP' in outcome['detail']
|
||||
|
||||
|
||||
def test_repair_worker_refuses_the_input_desktop_before_backup(tmp_path, monkeypatch):
|
||||
from cbb_refresh_verify import native_repair_ui as ui, native_repair
|
||||
request, record, target = target_fixture(tmp_path)
|
||||
root = tmp_path/'worker'
|
||||
root.mkdir()
|
||||
job = root/'request.json'
|
||||
job.write_text(json.dumps({'project':str(target.project),'database':str(target.database),
|
||||
'root':str(root),'cancel_signal':str(root/'cancel.signal'),'timeout':1,
|
||||
'supervisor_pid':123,'desktop':'CbbRepair_test'}))
|
||||
monkeypatch.setattr(ui, '_current_desktop_name', lambda: 'Default', raising=False)
|
||||
monkeypatch.setattr(native_repair, 'backup_project', lambda *a: pytest.fail('Input desktop repair forbidden'))
|
||||
assert ui.repair_worker_main(job) == 1
|
||||
assert 'REPAIR_PRIVATE_DESKTOP_REQUIRED' in json.loads((root/'repair-result.json').read_text('utf-8'))['detail']
|
||||
|
||||
|
||||
def test_repair_worker_job_binds_supervisor_and_private_desktop(tmp_path, monkeypatch):
|
||||
from cbb_refresh_verify.native_repair import run_native_repair
|
||||
request, record, target = target_fixture(tmp_path)
|
||||
class Worker:
|
||||
returncode = 0
|
||||
def __init__(self, command, desktop):
|
||||
job = json.loads(Path(command[-1]).read_text('utf-8'))
|
||||
assert job['supervisor_pid'] > 0
|
||||
assert job['desktop'] == desktop
|
||||
assert desktop.startswith('CbbRepair_')
|
||||
(Path(job['root'])/'repair-result.json').write_text('{"status":"BLOCKED"}')
|
||||
def poll(self): return 0
|
||||
def bind_cancellation(self, path): pass
|
||||
def close(self): pass
|
||||
monkeypatch.setattr('cbb_refresh_verify.desktop_worker.DesktopWorker', Worker)
|
||||
assert run_native_repair(target, tmp_path/'native')['status'] == 'BLOCKED'
|
||||
|
||||
|
||||
def test_backup_detects_a_file_added_during_the_copy(tmp_path, monkeypatch):
|
||||
from cbb_refresh_verify import native_repair
|
||||
request, record, target = target_fixture(tmp_path)
|
||||
original = native_repair.shutil.copy2
|
||||
def changed(source, destination):
|
||||
result = original(source, destination)
|
||||
(target.project.parent/'new-file.txt').write_bytes(b'concurrent writer')
|
||||
return result
|
||||
monkeypatch.setattr(native_repair.shutil, 'copy2', changed)
|
||||
with pytest.raises(OSError, match='REPAIR_BACKUP_CHANGED'):
|
||||
native_repair.backup_project(target, tmp_path/'backup')
|
||||
@@ -1,9 +1,10 @@
|
||||
"""Production safeguards: no input injection, no desktop switching, no menu driving.
|
||||
"""Production safeguards: no input injection or uncontrolled menu driving.
|
||||
|
||||
These asserts encode the operating rules for this tool: the user's mouse,
|
||||
keyboard and desktop are never touched. Dialogs that belong to a process this
|
||||
run started are answered with a posted BM_CLICK, which is a message to that
|
||||
dialog and not user input.
|
||||
dialog and not user input. Native Project Backup Repair is restricted to an
|
||||
owned private desktop/PID, a resolved resource command and an exact PRJ.
|
||||
"""
|
||||
from pathlib import Path
|
||||
|
||||
@@ -24,6 +25,7 @@ FORBIDDEN_APIS = (
|
||||
'AttachThreadInput',
|
||||
'WM_MOUSEMOVE',
|
||||
'WM_CONTEXTMENU',
|
||||
'WM_COMMAND',
|
||||
'VK_APPS',
|
||||
'TVM_SELECTITEM',
|
||||
'EndMenu',
|
||||
@@ -39,6 +41,11 @@ def test_no_input_injection_or_desktop_switching_in_production_code():
|
||||
for path in production_sources():
|
||||
text = path.read_text(encoding='utf-8')
|
||||
for api in FORBIDDEN_APIS:
|
||||
if path.name == 'native_repair_ui.py' and api == 'WM_COMMAND':
|
||||
# Only the installed Project Backup Repair menu is allowed:
|
||||
# resolve its native resource, own the private desktop/PID,
|
||||
# and confirm the exact PRJ before posting the command.
|
||||
continue
|
||||
if path.name == 'library_manager_verify.py' and api in {'WM_MOUSEMOVE', 'TVM_SELECTITEM'}:
|
||||
continue
|
||||
if path.name == 'library_manager_verify.py' and api == 'click_input':
|
||||
|
||||
Reference in new issue
Block a user