import socket import ssl import time import uuid from urllib.parse import urlsplit from client import Client, arguments, check, run def main(): def configure(p): p.add_argument('--expiry',action='store_true',help='Requires a stack with SESSION_TTL_SECONDS=5') args=arguments('Application security checks',configure) c=Client(args.base_url,args.ca_file) if args.expiry: actor=c.user('expiry') c.call('GET','/api/v1/users/me',actor=actor,schema='User') time.sleep(6) c.call('GET','/api/v1/users/me',401,actor=actor) print('PASS: a previously accepted bearer expired') return url=urlsplit(c.base) c.call('GET','/health/ready',schema='Health') ctx=ssl.create_default_context(cafile=args.ca_file) with socket.create_connection((url.hostname,url.port or 443),timeout=5) as raw: try: ctx.wrap_socket(raw,server_hostname='wrong.invalid') except ssl.SSLCertVerificationError as e: check(e.verify_code==62,'expected hostname mismatch') else: raise AssertionError('Wrong hostname accepted') alice,bob,eve,chat=c.bootstrap() att,_=c.attachment(alice,chat) other=c.call('POST','/api/v1/chats',201,actor=eve,body={'title':'Other','member_ids':[eve['id'],bob['id']]},schema='Chat')['json'] c.call('POST','/api/v1/chats/'+other['id']+'/messages',404,actor=eve, headers={'X-User-Id':alice['id'],'Idempotency-Key':uuid.uuid4().hex}, body={'text':'','attachment_ids':[att['id']]}) for _ in range(80): r=c.call('POST','/api/v1/auth/sessions',[401,429]) if r['status']==429: check(int(r['headers']['Retry-After'])>0,'missing Retry-After') break else: raise AssertionError('Login throttle did not activate') print('PASS: hostname, foreign attachment and ingress rate limit') if __name__=='__main__': run(main)