fix: address review 414 write consent and native ownership boundaries
This commit is contained in:
1 parent
1572e2d8ca
commit
fee4a67ff1
19 files changed
+1008
-64
No files matched your search
@@ -0,0 +1,69 @@
|
||||
"""Batch-local user confirmation for native clients with unknown target paths."""
|
||||
from pathlib import Path
|
||||
import time
|
||||
from uuid import uuid4
|
||||
|
||||
from .pcb_open_consent import OpenChoice
|
||||
from .session_record import process_identity
|
||||
|
||||
|
||||
class WriteOwnerChoice(OpenChoice):
|
||||
def __init__(self, block, library, owners):
|
||||
super().__init__(block, library)
|
||||
self.owners=tuple(dict(row) for row in owners)
|
||||
|
||||
|
||||
class WriteAdmissionDeclined(ValueError):
|
||||
def __init__(self, decision):
|
||||
self.decision=decision
|
||||
super().__init__('已取消本批次,未提交后续写入。' if decision=='cancel' else '用户跳过本项,未确认未知程序与目标库无关。')
|
||||
|
||||
|
||||
class WriteAdmission:
|
||||
def __init__(self, *, prompt=None, stop=None):
|
||||
self.prompt,self.stop=prompt,stop
|
||||
self._grants={}
|
||||
|
||||
def _identities(self, owners):
|
||||
rows=[]
|
||||
for row in owners:
|
||||
try:identity=process_identity(row['pid'])
|
||||
except Exception as error:
|
||||
if getattr(error,'winerror',None)==87:continue
|
||||
raise
|
||||
if Path(identity['image']).name.casefold()!=row['name'].casefold():
|
||||
raise ValueError('TARGET_NATIVE_OWNER_UNCONFIRMED: PID 已被其他程序复用,请重新检查。')
|
||||
if row.get('executable') and Path(row['executable']).resolve()!=Path(identity['image']).resolve():
|
||||
raise ValueError('TARGET_NATIVE_OWNER_UNCONFIRMED: 原生程序身份已变化,请重新检查。')
|
||||
rows.append({**identity,'name':row['name']})
|
||||
return tuple(sorted((row['pid'],row['created_handle'],row['image'],row['owner_sid'],row['session_id'])
|
||||
for row in rows)),rows
|
||||
|
||||
def confirmed(self, library, owners):
|
||||
key=str(Path(library).resolve()).casefold()
|
||||
identities,_=self._identities(owners)
|
||||
return set(identities).issubset(self._grants.get(key,set()))
|
||||
|
||||
def confirm(self, library, owners, *, evidence, block):
|
||||
key=str(Path(library).resolve()).casefold()
|
||||
identities,rows=self._identities(owners)
|
||||
if set(identities).issubset(self._grants.get(key,set())):return
|
||||
if self.prompt is None:
|
||||
raise ValueError('TARGET_NATIVE_OWNER_UNCONFIRMED: 无法确认原生程序是否使用目标库,请通过界面核对后选择。')
|
||||
choice=WriteOwnerChoice(block,str(Path(library)),rows)
|
||||
self.prompt(choice)
|
||||
while choice.decision is None and not (self.stop is not None and self.stop.is_set()):time.sleep(.05)
|
||||
decision=choice.decision or 'cancel'
|
||||
if decision!='continue':
|
||||
if decision=='cancel' and self.stop is not None:self.stop.set()
|
||||
raise WriteAdmissionDeclined(decision)
|
||||
current,_=self._identities(owners)
|
||||
if not set(current).issubset(identities):
|
||||
raise ValueError('TARGET_NATIVE_OWNER_UNCONFIRMED: 确认期间进程身份已变化,请重新检查。')
|
||||
from .management import _write_json
|
||||
root=Path(evidence);root.mkdir(parents=True,exist_ok=True)
|
||||
_write_json(root/('write-owner-consent-'+uuid4().hex+'.json'),dict(
|
||||
intent='user_confirmed_unknown_clients_do_not_use_target_library',library=str(Path(library).resolve()),
|
||||
block=block,scope='current_batch_library',owners=rows,choice='continue',
|
||||
proves_native_ownership=False,requires_fresh_resource_checks=True))
|
||||
self._grants.setdefault(key,set()).update(current)
|
||||
Reference in new issue
Block a user