forked from mtusi2026/mtusi2026lab5
130 lines
6.7 KiB
Python
130 lines
6.7 KiB
Python
"""Contract boundaries and object authorization; runs in Compose ops network."""
|
|
import hashlib
|
|
import http.client
|
|
import io
|
|
import json
|
|
from pathlib import Path
|
|
import socket
|
|
import struct
|
|
import sys
|
|
import time
|
|
from urllib.parse import quote, urlsplit
|
|
import uuid
|
|
import zlib
|
|
from PIL import Image
|
|
sys.path.insert(0,str(Path(__file__).resolve().parent))
|
|
sys.path.insert(0,str(Path(__file__).resolve().parents[1]))
|
|
from client import Client, check, run
|
|
|
|
|
|
def main():
|
|
c=Client('http://lb:8080',timeout=15)
|
|
alice,bob,eve,chat=c.bootstrap()
|
|
path='/api/v1/chats/'+chat['id']+'/attachments'
|
|
fixture=(Path(__file__).parent/'fixtures/sample.png').read_bytes()
|
|
cases=[]
|
|
def bad(label,status,data=fixture,mime='image/png',filename='test.png',actor=None):
|
|
c.call('POST',path+'?filename='+quote(filename,safe=''),status,actor=actor or alice,raw=data,headers={'Content-Type':mime})
|
|
cases.append({'case':label,'status':status})
|
|
bad('corrupt_png',415,b'not an image')
|
|
bad('truncated_png',415,fixture[:50])
|
|
bad('mime_spoof',415,mime='image/jpeg')
|
|
bad('unsupported_mime',415,mime='application/octet-stream')
|
|
bad('empty_payload',415,b'')
|
|
for filename in ('../secret.png','a\\b.png','x\x00.png','x\n.png','x\u202e.png','x'*256):
|
|
bad('unsafe_filename',400,filename=filename)
|
|
bad('foreign_chat_upload',404,actor=eve)
|
|
c.call('POST',path+'?filename=x.png',401,raw=fixture,headers={'Content-Type':'image/png'})
|
|
cases.append({'case':'unauthenticated_upload','status':401})
|
|
# Oversized dimensions are rejected using headers before a full raster decode.
|
|
data=bytearray(fixture)
|
|
data[16:24]=struct.pack('>II',10000,4001)
|
|
data[29:33]=struct.pack('>I',zlib.crc32(data[12:29])&0xffffffff)
|
|
bad('over_40_million_pixels',413,bytes(data))
|
|
# No large allocation or body transfer: reject declared size before reading.
|
|
parsed=urlsplit(c.base)
|
|
conn=http.client.HTTPConnection(parsed.hostname,parsed.port,timeout=10)
|
|
conn.putrequest('POST',path+'?filename=big.png')
|
|
for k,v in {'Authorization':'Bearer '+alice['token'],'Content-Type':'image/png','Content-Length':str(10485761)}.items():
|
|
conn.putheader(k,v)
|
|
conn.endheaders()
|
|
response=conn.getresponse()
|
|
check(response.status==413,'oversized content-length accepted')
|
|
response.read();conn.close()
|
|
cases.append({'case':'oversized_content_length_before_body','status':413})
|
|
# True streamed overflow, no Content-Length; response is read after the last
|
|
# chunk, even if the peer stopped accepting bytes once it knew the limit.
|
|
conn=http.client.HTTPConnection(parsed.hostname,parsed.port,timeout=15)
|
|
conn.putrequest('POST',path+'?filename=chunked.png')
|
|
conn.putheader('Authorization','Bearer '+alice['token'])
|
|
conn.putheader('Content-Type','image/png')
|
|
conn.putheader('Transfer-Encoding','chunked')
|
|
conn.endheaders()
|
|
try:
|
|
block=b'x'*65536
|
|
for _ in range(161):
|
|
conn.send(b'10000\r\n'+block+b'\r\n')
|
|
conn.send(b'0\r\n\r\n')
|
|
except (BrokenPipeError,ConnectionResetError):
|
|
pass
|
|
response=conn.getresponse()
|
|
check(response.status==413,'streamed byte limit not enforced')
|
|
response.read();conn.close()
|
|
cases.append({'case':'chunked_oversize','status':413})
|
|
att,content=c.attachment(alice,chat)
|
|
# Another member can reference an attachment; ownership is not required.
|
|
msg=c.message(bob,chat,'',attachments=[att['id']])['json']
|
|
check(msg['attachment_ids']==[att['id']],'member could not link attachment')
|
|
for suffix in ('','/content'):
|
|
c.call('GET','/api/v1/attachments/'+att['id']+suffix,404,actor=eve)
|
|
c.wait_ready(alice,att)
|
|
c.call('GET','/api/v1/attachments/'+att['id']+'/content?variant=unknown',404,actor=alice)
|
|
cases.append({'case':'foreign_attachment_metadata_and_bytes','status':404})
|
|
other=c.call('POST','/api/v1/chats',201,actor=alice,body={'title':'other','member_ids':[alice['id']]},schema='Chat')['json']
|
|
other_path='/api/v1/chats/'+other['id']+'/messages'
|
|
for aid in (att['id'],str(uuid.uuid4())):
|
|
c.call('POST',other_path,404,actor=alice,body={'text':'','attachment_ids':[aid]},headers={'Idempotency-Key':uuid.uuid4().hex})
|
|
cases.append({'case':'cross_chat_and_unknown_attachment','status':404})
|
|
message_path='/api/v1/chats/'+chat['id']+'/messages'
|
|
for ids in ([att['id'],att['id']],['bad-id'],[str(uuid.uuid4()) for _ in range(11)]):
|
|
c.call('POST',message_path,400,actor=alice,body={'text':'','attachment_ids':ids},headers={'Idempotency-Key':uuid.uuid4().hex})
|
|
key=uuid.uuid4().hex
|
|
first=c.message(alice,chat,'',key=key,attachments=[att['id']])['json']
|
|
second=c.message(alice,chat,'',key=key,attachments=[att['id']])['json']
|
|
check(first==second,'attachment message replay changed')
|
|
c.call('POST',message_path,409,actor=alice,body={'text':'different','attachment_ids':[att['id']]},headers={'Idempotency-Key':key})
|
|
cases.append({'case':'attachment_idempotency_and_conflict','status':'201/409'})
|
|
# Full JPEG validation, including truncated entropy-coded body.
|
|
stream=io.BytesIO()
|
|
Image.new('RGB',(320,200),'blue').save(stream,'JPEG')
|
|
jpeg=stream.getvalue()
|
|
uploaded=c.call('POST',path+'?filename=photo.jpg',202,actor=alice,raw=jpeg,headers={'Content-Type':'image/jpeg'},schema='Attachment')['json']
|
|
c.wait_ready(alice,uploaded)
|
|
downloaded=c.call('GET','/api/v1/attachments/'+uploaded['id']+'/content',actor=bob)
|
|
check(downloaded['bytes']==jpeg,'JPEG original changed')
|
|
bad('truncated_jpeg',415,jpeg[:-20],mime='image/jpeg')
|
|
# A disconnect before receiving all bytes creates neither intent nor object.
|
|
from app import store,object_store
|
|
store.pool.open(wait=True,timeout=15)
|
|
try:
|
|
def counts():
|
|
with store.connect() as db:
|
|
n=db.execute('SELECT count(*) FROM attachments').fetchone()[0]
|
|
return n,len(object_store.client.list_objects_v2(Bucket=object_store.bucket,Prefix='staging/').get('Contents',[]))
|
|
before=counts()
|
|
conn=http.client.HTTPConnection(parsed.hostname,parsed.port,timeout=5)
|
|
conn.putrequest('POST',path+'?filename=interrupted.png')
|
|
conn.putheader('Authorization','Bearer '+alice['token'])
|
|
conn.putheader('Content-Type','image/png')
|
|
conn.putheader('Content-Length',str(len(fixture)))
|
|
conn.endheaders();conn.send(fixture[:50]);conn.close()
|
|
time.sleep(1)
|
|
check(counts()==before,'aborted upload persisted partial data')
|
|
cases.append({'case':'interrupted_http_body','metadata_or_object_added':False})
|
|
finally:
|
|
store.pool.close();object_store.close()
|
|
print(json.dumps({'status':'PASS','cases':cases,'count':len(cases)},ensure_ascii=False,indent=2))
|
|
|
|
if __name__=='__main__':
|
|
run(main)
|