83 lines
4.0 KiB
Python
83 lines
4.0 KiB
Python
#!/usr/bin/env python3
|
|
"""Lab7 external TLS, login/session limits and user/attachment ACL checks."""
|
|
import base64
|
|
import ssl
|
|
import urllib.error
|
|
import urllib.request
|
|
from client import Client, LAB, NoRedirect, ROOT, arguments, check, run
|
|
|
|
|
|
def main():
|
|
args = arguments('Lab7 external TLS trust check; provide --ca-file for the positive probe')
|
|
check(LAB==7,'this check belongs to lab7')
|
|
c = Client(args.base_url,args.ca_file)
|
|
c.call('GET','/health/ready',schema='Health')
|
|
# An empty trust store deliberately trusts no CA, including public CAs.
|
|
untrusted = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
|
|
untrusted.verify_mode = ssl.CERT_REQUIRED
|
|
untrusted.check_hostname = True
|
|
opener = urllib.request.build_opener(NoRedirect(),urllib.request.HTTPSHandler(context=untrusted))
|
|
rejected = False
|
|
try:
|
|
with opener.open(c.base+'/health/ready',timeout=5) as response:
|
|
response.read(1024)
|
|
except urllib.error.URLError as exc:
|
|
check(isinstance(exc.reason,ssl.SSLCertVerificationError),'negative probe failed for a non-certificate reason')
|
|
rejected = True
|
|
check(rejected,'server unexpectedly passed validation with an empty trust store')
|
|
|
|
alice,bob,eve,chat = c.bootstrap()
|
|
chat_path = '/api/v1/chats/'+chat['id']
|
|
|
|
# Bearer identity wins over attacker-controlled compatibility headers.
|
|
forged = c.call('GET','/api/v1/users/me',actor=eve,
|
|
headers={'X-User-Id':alice['id']},schema='User')['json']
|
|
check(forged['id']==eve['id'],'X-User-Id overrode Bearer identity')
|
|
c.call('GET',chat_path,404,actor=eve)
|
|
c.call('GET',chat_path+'/messages',404,actor=eve)
|
|
c.call('POST',chat_path+'/messages',404,actor=eve,
|
|
headers={'Idempotency-Key':'acl-outsider-message'},body={'text':'forbidden'})
|
|
|
|
attachment,_ = c.attachment(alice,chat)
|
|
for suffix in ('','/content'):
|
|
c.call('GET','/api/v1/attachments/'+attachment['id']+suffix,404,actor=eve)
|
|
sample = (ROOT / 'tests/fixtures/sample.png').read_bytes()
|
|
c.call('POST',chat_path+'/attachments?filename=forbidden.png',404,actor=eve,
|
|
raw=sample,headers={'Content-Type':'image/png'})
|
|
|
|
second_chat = c.call('POST','/api/v1/chats',201,actor=alice,
|
|
body={'title':'Изолированный чат','member_ids':[alice['id'],bob['id']]},
|
|
schema='Chat')['json']
|
|
c.call('POST','/api/v1/chats/'+second_chat['id']+'/messages',404,actor=alice,
|
|
headers={'Idempotency-Key':'cross-chat-attachment'},
|
|
body={'text':'','attachment_ids':[attachment['id']]})
|
|
|
|
# The declared request size is rejected before image decoding/storage.
|
|
c.call('POST',chat_path+'/attachments?filename=oversized.png',413,actor=alice,
|
|
raw=b'x'*(10*1024*1024+1),headers={'Content-Type':'image/png'})
|
|
|
|
# At most three active sessions are retained. Five logins fit under the
|
|
# default per-username login window and must revoke the oldest token.
|
|
credential = base64.b64encode((alice['registration']['username']+':'
|
|
+alice['registration']['password']).encode()).decode()
|
|
for _ in range(4):
|
|
c.call('POST','/api/v1/auth/sessions',201,
|
|
headers={'Authorization':'Basic '+credential},schema='Session')
|
|
c.call('GET','/api/v1/users/me',401,actor=alice)
|
|
|
|
limited = c.user('rate_limit')
|
|
bad = base64.b64encode((limited['registration']['username']+':wrong-password').encode()).decode()
|
|
for _ in range(4):
|
|
c.call('POST','/api/v1/auth/sessions',401,
|
|
headers={'Authorization':'Basic '+bad})
|
|
blocked = c.call('POST','/api/v1/auth/sessions',429,
|
|
headers={'Authorization':'Basic '+bad})
|
|
check(blocked['json']['error']['code']=='rate_limited','wrong rate-limit error code')
|
|
retry_after = blocked['headers'].get('Retry-After','')
|
|
check(retry_after.isdigit() and int(retry_after)>=1,'missing positive Retry-After')
|
|
|
|
print('PASS: trusted HTTPS, ACL/attachment isolation, session cap, upload and login limits.')
|
|
|
|
if __name__=='__main__':
|
|
run(main)
|