71 lines
3.5 KiB
Python
71 lines
3.5 KiB
Python
"""Host business security, limits and a real two-second session expiry."""
|
|
import base64
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import ssl
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
import urllib.request
|
|
import uuid
|
|
|
|
ROOT=Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0,str(ROOT/'tests'))
|
|
sys.path.insert(0,str(ROOT/'scripts'))
|
|
from client import Client, check
|
|
from manage import env,ready
|
|
|
|
|
|
def main():
|
|
c=Client('https://localhost:'+env()['HTTPS_PORT'],str(ROOT/'secrets/ca.crt'))
|
|
alice,bob,eve,chat=c.bootstrap()
|
|
attachment,original=c.attachment(alice,chat)
|
|
attachment=c.wait_ready(alice,attachment)
|
|
other=c.call('POST','/api/v1/chats',201,actor=eve,body={'title':'Other','member_ids':[eve['id']]},schema='Chat')['json']
|
|
c.call('POST','/api/v1/chats/'+other['id']+'/messages',404,actor=eve,
|
|
body={'text':'','attachment_ids':[attachment['id']]},headers={'Idempotency-Key':uuid.uuid4().hex})
|
|
for suffix in ('','/content','/content?variant=thumbnail'):
|
|
c.call('GET','/api/v1/attachments/'+attachment['id']+suffix,404,actor=eve)
|
|
me=c.call('GET','/api/v1/users/me',actor=alice,headers={'X-User-Id':eve['id']},schema='User')['json']
|
|
assert me['id']==alice['id']
|
|
c.call('DELETE','/api/v1/auth/sessions/current',204,actor=alice)
|
|
c.call('GET','/api/v1/users/me',401,actor=alice)
|
|
print('PASS attachment ACL, foreign-chat attach=404, spoofed identity ignored, logout=401')
|
|
name='limited_'+uuid.uuid4().hex[:12]
|
|
bad='Basic '+base64.b64encode((name+':synthetic-invalid').encode()).decode()
|
|
for _ in range(10):c.call('POST','/api/v1/auth/sessions',401,headers={'Authorization':bad})
|
|
response=c.call('POST','/api/v1/auth/sessions',429,headers={'Authorization':bad})
|
|
assert int(response['headers']['Retry-After'])>0
|
|
print('PASS login limit: 10 allowed attempts, 11th=429 with Retry-After')
|
|
c.call('POST','/api/v1/users',413,raw=b'x'*65537,headers={'Content-Type':'application/json'})
|
|
c.call('POST','/api/v1/chats/'+chat['id']+'/attachments?filename=large.png',413,actor=bob,
|
|
raw=b'x'*(10485760+1),headers={'Content-Type':'image/png'})
|
|
# urllib uses chunked transfer for an iterable without Content-Length.
|
|
c.call('POST','/api/v1/users',413,raw=iter([b'x'*40000,b'x'*30000]),headers={'Content-Type':'application/json'})
|
|
print('PASS JSON 64KiB, upload 10MiB and actual chunked stream limits=413')
|
|
ctx=ssl.create_default_context(cafile=str(ROOT/'secrets/ca.crt'))
|
|
import socket
|
|
with socket.create_connection(('127.0.0.1',int(env()['HTTPS_PORT'])),timeout=3) as raw:
|
|
try:ctx.wrap_socket(raw,server_hostname='wrong.invalid')
|
|
except ssl.SSLCertVerificationError:pass
|
|
else:raise AssertionError('external wrong hostname accepted')
|
|
print('PASS external wrong hostname rejected by certificate verifier')
|
|
temporary=dict(os.environ,SESSION_TTL_SECONDS='2')
|
|
try:
|
|
subprocess.run(['docker','compose','up','-d','api1','api2'],cwd=ROOT,env=temporary,check=True,stdout=subprocess.DEVNULL)
|
|
ready()
|
|
# Ensure BOTH new replicas are ready and serving before making a session.
|
|
time.sleep(3)
|
|
actor=c.user('expiry')
|
|
c.call('GET','/api/v1/users/me',actor=actor,schema='User')
|
|
time.sleep(3)
|
|
c.call('GET','/api/v1/users/me',401,actor=actor)
|
|
print('PASS configured SESSION_TTL_SECONDS=2: valid before expiry, 401 after expiry')
|
|
finally:
|
|
subprocess.run(['docker','compose','up','-d','api1','api2'],cwd=ROOT,check=True,stdout=subprocess.DEVNULL)
|
|
ready()
|
|
|
|
|
|
if __name__=='__main__':main()
|