63 lines
3.1 KiB
Python
63 lines
3.1 KiB
Python
"""Adversarial M6 checks, no real platform operations."""
|
|
import hashlib
|
|
import json
|
|
import pytest
|
|
from test_credential_api import env,wire
|
|
from test_approval_integration import approval_env,pair,pc_errors
|
|
from relay.approval_store import ApprovalStore,ApprovalError
|
|
from relay.approval_policy import describe_action
|
|
from relay.crypto import canonical
|
|
|
|
|
|
@pytest.mark.parametrize('choice',[None,'deny','allow'])
|
|
async def test_same_intent_cannot_get_new_card_by_changing_operation_id(pair,choice):
|
|
p=pair;job=p['launch']();row=await p['card']()
|
|
if choice:
|
|
await p['click'](choice)
|
|
if choice=='deny':
|
|
with pytest.raises(pc_errors.WorkError):await job
|
|
else:await job
|
|
e=p['env'];ledger=ApprovalStore(e['store'].lifecycle,e['api'].broker.store.keyring)
|
|
action=json.loads(ledger.keyring.decrypt(row['key_id'],row['ciphertext'],ledger._aad(row)))['action']
|
|
other='new-operation-id'
|
|
action['params']['idempotency_key']=hashlib.sha256(canonical(['ou_a',other])).hexdigest()
|
|
path='/work/v2/approvals/create'
|
|
response=await e['client'].post(path,json=wire(e,path,task_id='task-approval',operation_id=other,action=action,ttl=120))
|
|
assert response.status_code==409
|
|
assert e['store'].db.execute('SELECT COUNT(*) n FROM work_approvals_v3').fetchone()['n']==1
|
|
assert len(p['calls'])==(1 if choice=='allow'else 0)
|
|
if not choice:
|
|
p['stop'].set()
|
|
with pytest.raises(pc_errors.WorkError):await job
|
|
|
|
|
|
def test_confirmation_identifies_actual_calendar_not_just_events_suffix():
|
|
def action(calendar):return {'kind':'work-api-v1','method':'POST',
|
|
'path':'/open-apis/calendar/v4/calendars/'+calendar+'/events','params':{},'body':{'summary':'同一标题'}}
|
|
first=describe_action(action('calendar_a'))
|
|
second=describe_action(action('calendar_b'))
|
|
assert 'calendar_a'in first[1]and 'calendar_b'in second[1]and first!=second
|
|
|
|
|
|
def test_long_target_is_rejected_instead_of_silently_truncated():
|
|
with pytest.raises(ApprovalError):describe_action({'kind':'work-api-v1','method':'DELETE',
|
|
'path':'/open-apis/drive/v1/files/'+'x'*400,'params':{},'body':{}})
|
|
|
|
|
|
async def test_changed_action_same_id_rejected_but_distinct_new_action_can_confirm(pair):
|
|
p=pair;job=p['launch']();row=await p['card']();await p['click']('deny')
|
|
with pytest.raises(pc_errors.WorkError):await job
|
|
e=p['env'];ledger=ApprovalStore(e['store'].lifecycle,e['api'].broker.store.keyring)
|
|
action=json.loads(ledger.keyring.decrypt(row['key_id'],row['ciphertext'],ledger._aad(row)))['action']
|
|
action['body']['summary']='真正不同的合成动作'
|
|
path='/work/v2/approvals/create'
|
|
same=await e['client'].post(path,json=wire(e,path,task_id='task-approval',
|
|
operation_id='operation-synthetic',action=action,ttl=120))
|
|
assert same.status_code==409
|
|
other='distinct-operation'
|
|
action['params']['idempotency_key']=hashlib.sha256(canonical(['ou_a',other])).hexdigest()
|
|
distinct=await e['client'].post(path,json=wire(e,path,task_id='task-approval',
|
|
operation_id=other,action=action,ttl=120))
|
|
assert distinct.status_code==200 and distinct.json()['state']=='pending'
|
|
assert not p['calls']
|