lab4: наблюдаемые результаты в evidence

This commit is contained in:
Vladislav Kunetskiy
2026-10-21 23:25:46 +03:00
parent e9f0fde6f2
commit affe5ce1b5
4 changed files with 40 additions and 0 deletions
+12
View File
@@ -1,2 +1,14 @@
env -u HTTP_PROXY -u HTTPS_PROXY -u ALL_PROXY -u http_proxy -u https_proxy -u all_proxy NO_PROXY=localhost,127.0.0.1,::1 no_proxy=localhost,127.0.0.1,::1 python3 student-tests/attachments_negative.py --base-url "http://localhost:8080"
10 MiB + 1 body: 413
empty body: 415
corrupt PNG (IDAT CRC): 415
MIME spoof (PNG declared as JPEG): 415
dangerous filenames: 400
foreign attachment metadata/content: 404
empty text without attachments: 400
attachment from another chat: 404
11 attachment ids: 400
duplicate attachment ids: 400
attachment-only message: 201
anonymous direct S3 GET: 403
PASS: lab4 attachment negative scenarios
+8
View File
@@ -1,2 +1,10 @@
env -u HTTP_PROXY -u HTTPS_PROXY -u ALL_PROXY -u http_proxy -u https_proxy -u all_proxy NO_PROXY=localhost,127.0.0.1,::1 no_proxy=localhost,127.0.0.1,::1 python3 student-tests/presign_flow.py --base-url "http://localhost:8080"
happy path: direct PUT 200, complete ready, checksum/size match
message with attachment downloaded as the second chat member
repeated complete is idempotent
foreign complete: 404
complete before PUT: 409
wrong completion checksum: 409
oversized direct PUT: 403 from S3; complete: 409 (nothing stored)
same-size overwrite: last PUT wins, checksum verified
PASS: lab4 presigned upload flow
+12
View File
@@ -32,22 +32,27 @@ def main():
# Oversized body: rejected before any image parsing.
upload(client, alice, chat, b'\x00' * (10 * 1024 * 1024 + 1), expected=413)
print('10 MiB + 1 body: 413')
# Empty body is not a valid image.
upload(client, alice, chat, b'', expected=415)
print('empty body: 415')
# Corrupt PNG: flip one IDAT payload byte, CRC no longer matches.
corrupt = bytearray(fixture)
index = corrupt.index(b'IDAT') + 8
corrupt[index] ^= 0xFF
upload(client, alice, chat, bytes(corrupt), expected=415)
print('corrupt PNG (IDAT CRC): 415')
# Declared JPEG but the bytes are PNG.
upload(client, alice, chat, fixture, filename='photo.jpg', content_type='image/jpeg', expected=415)
print('MIME spoof (PNG declared as JPEG): 415')
# Dangerous display names.
for name in ('../x.png', 'a/b.png', 'a\\b.png', 'a\x00.png', 'a\n.png'):
upload(client, alice, chat, fixture, filename=name, expected=400)
print('dangerous filenames: 400')
# A member of one chat cannot read an attachment of another chat.
other = client.call(
@@ -57,17 +62,20 @@ def main():
foreign, foreign_content = client.attachment(eve, other)
client.call('GET', '/api/v1/attachments/' + foreign['id'], 404, actor=bob)
client.call('GET', '/api/v1/attachments/' + foreign['id'] + '/content', 404, actor=bob)
print('foreign attachment metadata/content: 404')
# Message rules: empty text without attachments, attachment from another chat.
client.call(
'POST', '/api/v1/chats/' + chat['id'] + '/messages', 400, actor=alice,
body={'text': ''}, headers={'Idempotency-Key': uuid.uuid4().hex},
)
print('empty text without attachments: 400')
client.call(
'POST', '/api/v1/chats/' + chat['id'] + '/messages', 404, actor=alice,
body={'text': 'чужое', 'attachment_ids': [foreign['id']]},
headers={'Idempotency-Key': uuid.uuid4().hex},
)
print('attachment from another chat: 404')
# Contract limits: 11 unique IDs and duplicate IDs are rejected by the schema.
client.call(
@@ -75,12 +83,14 @@ def main():
body={'text': 'много', 'attachment_ids': [str(uuid.uuid4()) for _ in range(11)]},
headers={'Idempotency-Key': uuid.uuid4().hex},
)
print('11 attachment ids: 400')
duplicate = str(uuid.uuid4())
client.call(
'POST', '/api/v1/chats/' + chat['id'] + '/messages', 400, actor=alice,
body={'text': 'дубликат', 'attachment_ids': [duplicate, duplicate]},
headers={'Idempotency-Key': uuid.uuid4().hex},
)
print('duplicate attachment ids: 400')
# The only variant in the lab4 contract is original; other values are 400.
own, content = client.attachment(alice, chat)
@@ -88,6 +98,7 @@ def main():
# Attachment-only message is valid.
client.message(alice, chat, '', attachments=[own['id']])
print('attachment-only message: 201')
# Object keys are server-generated; anonymous direct access must be denied.
key = 'attachments/' + chat['id'] + '/' + own['id']
@@ -99,6 +110,7 @@ def main():
else:
raise Failure('anonymous S3 GET unexpectedly succeeded')
print('anonymous direct S3 GET: 403')
check(hashlib.sha256(content).hexdigest() == own['sha256'], 'fixture checksum mismatch')
print('PASS: lab4 attachment negative scenarios')
+8
View File
@@ -73,33 +73,40 @@ def main():
check(put(presigned['upload_url'], content) == 200, 'direct PUT failed')
attachment = complete(client, alice, presigned['attachment_id'], digest)['json']
check(attachment['sha256'] == digest and attachment['size_bytes'] == len(content), 'completed metadata mismatch')
print('happy path: direct PUT 200, complete ready, checksum/size match')
client.message(alice, chat, '', attachments=[attachment['id']])
download = client.call('GET', '/api/v1/attachments/' + attachment['id'] + '/content', actor=bob)
check(download['bytes'] == content, 'download differs from the presigned original')
print('message with attachment downloaded as the second chat member')
# Completion is idempotent.
again = complete(client, alice, presigned['attachment_id'], digest)['json']
check(again['id'] == attachment['id'], 'repeated complete returned another attachment')
print('repeated complete is idempotent')
# A foreign user cannot complete someone else's pending upload.
pending = presign(client, alice, chat, content)
complete(client, bob, pending['attachment_id'], digest, expected=404)
print('foreign complete: 404')
# Incomplete upload: complete before PUT.
incomplete = presign(client, alice, chat, content)
complete(client, alice, incomplete['attachment_id'], digest, expected=409)
print('complete before PUT: 409')
# Wrong checksum: the object is rejected and removed.
mismatch = presign(client, alice, chat, content)
check(put(mismatch['upload_url'], content) == 200, 'direct PUT failed')
complete(client, alice, mismatch['attachment_id'], '0' * 64, expected=409)
print('wrong completion checksum: 409')
# Content-Length is signed, so S3 rejects a different number of bytes at
# write time instead of storing an oversized staging object.
oversized = presign(client, alice, chat, content)
check(put(oversized['upload_url'], content + b'x') == 403, 'oversized PUT must be rejected by S3')
complete(client, alice, oversized['attachment_id'], digest, expected=409)
print('oversized direct PUT: 403 from S3; complete: 409 (nothing stored)')
# Last PUT wins before completion; the declared checksum must describe it.
# The replacement is the same length because Content-Length is signed.
@@ -110,6 +117,7 @@ def main():
check(put(overwrite['upload_url'], content) == 200, 'second PUT failed')
overwritten = complete(client, alice, overwrite['attachment_id'], digest)['json']
check(overwritten['sha256'] == digest, 'last PUT was not the completed object')
print('same-size overwrite: last PUT wins, checksum verified')
# A user outside the chat cannot obtain a presigned URL.
other = client.call(