#!/usr/bin/env python3 """Lab 4 negative upload, chat-boundary, and attachment-id checks.""" import uuid from client import Client, arguments, check, run def main(): args=arguments('Lab 4 attachment security checks') c=Client(args.base_url,args.ca_file) alice,bob,eve,chat=c.bootstrap() url='/api/v1/chats/'+chat['id']+'/attachments?filename=test.png' fixture=(c.__class__.__module__ and __import__('pathlib').Path(__file__).parent/'fixtures'/'sample.png').read_bytes() c.call('POST',url,413,actor=alice,raw=b'x'*(10*1024*1024+1),headers={'Content-Type':'image/png'}) c.call('POST',url,415,actor=alice,raw=b'broken image',headers={'Content-Type':'image/png'}) c.call('POST',url,415,actor=alice,raw=fixture,headers={'Content-Type':'image/jpeg'}) c.call('POST','/api/v1/chats/'+chat['id']+'/attachments?filename=../secret.png',400,actor=alice,raw=fixture,headers={'Content-Type':'image/png'}) other=c.call('POST','/api/v1/chats',201,actor=alice,body={'title':'Другой чат','member_ids':[alice['id']]},schema='Chat')['json'] attachment=c.call('POST','/api/v1/chats/'+other['id']+'/attachments?filename=other.png',201,actor=alice,raw=fixture,headers={'Content-Type':'image/png'},schema='Attachment')['json'] key=uuid.uuid4().hex c.call('POST','/api/v1/chats/'+chat['id']+'/messages',404,actor=alice,body={'text':'cross-chat', 'attachment_ids':[attachment['id']]},headers={'Idempotency-Key':key}) c.call('POST','/api/v1/chats/'+chat['id']+'/messages',404,actor=eve,body={'text':'foreign attachment','attachment_ids':[attachment['id']]},headers={'Idempotency-Key':uuid.uuid4().hex}) print('PASS: oversize=413, corrupt=415, MIME spoof=415, unsafe filename=400, cross-chat/outsider attachment IDs=404') if __name__=='__main__': run(main)