forked from mtusi2026/mtusi2026lab4
70 lines
3.1 KiB
Python
70 lines
3.1 KiB
Python
"""Execute with the isolated tools profile; inject faults only into new test IDs."""
|
|
import hashlib
|
|
import io
|
|
import json
|
|
from pathlib import Path
|
|
import socket
|
|
import sys
|
|
import urllib.error
|
|
import urllib.request
|
|
import uuid
|
|
|
|
sys.path[:0] = [str(Path.cwd() / 'tests'), '/app/src']
|
|
from botocore.exceptions import ClientError
|
|
from PIL import Image
|
|
from client import Client
|
|
from objects import Objects
|
|
from postgres import setting
|
|
|
|
|
|
def main():
|
|
c = Client()
|
|
alice, bob, eve, chat = c.bootstrap()
|
|
att, content = c.attachment(alice, chat)
|
|
api = Objects(setting)
|
|
admin = Objects(lambda key: Path('/run/secrets/s3_admin_' + ('key' if key.endswith('_ID') else 'secret')).read_text().strip())
|
|
key = 'originals/' + att['id']
|
|
assert admin.head(key)['ContentLength'] == len(content)
|
|
denied = []
|
|
for path in ('/' + api.bucket + '/' + key, '/' + api.bucket + '?list-type=2'):
|
|
try:
|
|
urllib.request.urlopen('http://s3:8333' + path, timeout=3)
|
|
raise AssertionError('Anonymous S3 access allowed')
|
|
except urllib.error.HTTPError as error:
|
|
assert error.code == 403
|
|
denied.append(error.code)
|
|
# API credentials cannot create a bucket, read another bucket or change policy.
|
|
other = 'test-' + uuid.uuid4().hex
|
|
admin.client.create_bucket(Bucket=other)
|
|
rejected = []
|
|
try:
|
|
actions = [lambda: api.client.create_bucket(Bucket='forbidden-' + uuid.uuid4().hex), lambda: api.client.list_objects_v2(Bucket=other), lambda: api.client.put_bucket_policy(Bucket=api.bucket, Policy=json.dumps({'Version': '2012-10-17', 'Statement': []}))]
|
|
for action in actions:
|
|
try:
|
|
action()
|
|
raise AssertionError('API credentials have excessive permissions')
|
|
except ClientError as error:
|
|
assert error.response['ResponseMetadata']['HTTPStatusCode'] == 403
|
|
rejected.append(403)
|
|
finally:
|
|
admin.client.delete_bucket(Bucket=other)
|
|
for host, port in [('s3-store', 8888), ('s3', 12535)]:
|
|
try:
|
|
connection = socket.create_connection((host, port), timeout=1)
|
|
connection.close()
|
|
raise AssertionError('Administrative/internal endpoint is exposed')
|
|
except OSError:
|
|
pass
|
|
image = io.BytesIO()
|
|
Image.new('RGB', (320, 200), (20, 150, 210)).save(image, format='JPEG')
|
|
jpg = image.getvalue()
|
|
uploaded = c.call('POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=photo.jpg', 201, actor=alice, raw=jpg, headers={'Content-Type': 'image/jpeg'}, schema='Attachment')['json']
|
|
downloaded = c.call('GET', '/api/v1/attachments/' + uploaded['id'] + '/content', actor=bob)
|
|
assert downloaded['bytes'] == jpg and uploaded['sha256'] == hashlib.sha256(jpg).hexdigest()
|
|
c.call('POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=bad.jpg', 415, actor=alice, raw=jpg[:200], headers={'Content-Type': 'image/jpeg'})
|
|
print(json.dumps({'anonymous_statuses': denied, 'api_credentials_denied': rejected, 'internal_filer_and_admin_inaccessible': True, 'jpeg_byte_roundtrip': True, 'damaged_jpeg_415': True}, indent=2))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|