50 lines
2.1 KiB
Python
50 lines
2.1 KiB
Python
"""TLS hostname, cross-chat attachment ACL, and actual session expiry."""
|
|
import hashlib
|
|
from pathlib import Path
|
|
import socket
|
|
import ssl
|
|
import subprocess
|
|
import sys
|
|
import time
|
|
from urllib.parse import urlsplit
|
|
from uuid import uuid4
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT / 'tests'))
|
|
from client import Client, arguments, run
|
|
|
|
|
|
def main():
|
|
args = arguments(__doc__)
|
|
client = Client(args.base_url, args.ca_file)
|
|
target = urlsplit(client.base)
|
|
context = ssl.create_default_context(cafile=args.ca_file)
|
|
try:
|
|
with socket.create_connection((target.hostname, target.port or 443), timeout=5) as raw:
|
|
with context.wrap_socket(raw, server_hostname='wrong-host.invalid'):
|
|
pass
|
|
except ssl.SSLCertVerificationError:
|
|
pass
|
|
else:
|
|
raise AssertionError('TLS accepted wrong server hostname')
|
|
alice, bob, eve, chat = client.bootstrap()
|
|
attachment, _ = client.attachment(alice, chat)
|
|
other_chat = client.call('POST', '/api/v1/chats', 201, actor=eve,
|
|
body={'title': 'Other chat', 'member_ids': [eve['id']]}, schema='Chat')['json']
|
|
client.call('POST', '/api/v1/chats/' + other_chat['id'] + '/messages', 404, actor=eve,
|
|
body={'text': '', 'attachment_ids': [attachment['id']]},
|
|
headers={'Idempotency-Key': uuid4().hex})
|
|
client.call('GET', '/api/v1/users/me', actor=alice, schema='User')
|
|
digest = hashlib.sha256(alice['token'].encode('ascii')).hexdigest()
|
|
sql = "UPDATE sessions SET expires_at=now()+interval '2 seconds' WHERE token_hash='" + digest + "';\n"
|
|
result = subprocess.run(['docker', 'compose', 'exec', '-T', 'postgres', 'psql', '-U', 'messenger', '-d', 'messenger', '-q'],
|
|
cwd=ROOT, input=sql, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True)
|
|
del result
|
|
time.sleep(3)
|
|
client.call('GET', '/api/v1/users/me', 401, actor=alice)
|
|
print('PASS: wrong hostname rejected; cross-chat attachment denied; expired session rejected')
|
|
|
|
|
|
if __name__ == '__main__':
|
|
run(main)
|