"""Real Nginx/API rejection of oversized Content-Length and chunked bodies.""" import json import subprocess import sys from direct_client import ROOT, Failure, check, fixture, verify def main(): report = {'status': 'failed', 'attempts': []} try: with fixture() as (client, owner, reader, chat, _): payload = b'x' * (10485760 + 1) path = f"/api/v1/chats/{chat['id']}/attachments?filename=oversize.png" for index in range(16): mode = 'content-length' if index % 2 == 0 else 'chunked' body = payload if mode == 'content-length' else ( payload[offset:offset + 65536] for offset in range(0, len(payload), 65536)) result = client.call('POST', path, 413, actor=owner, raw=body, headers={'Content-Type': 'image/png'}) check(result['json']['error']['code'] == 'payload_too_large', 'Incorrect error code') report['attempts'].append({'mode': mode, 'status': result['status'], 'instance': result['headers']['X-Instance-Id']}) check(len({row['instance'] for row in report['attempts']}) == 2, 'Oversize requests missed a replica') image = (ROOT / 'tests/fixtures/sample.png').read_bytes() attachment = client.call('POST', path.replace('oversize.png', 'valid.png'), 201, actor=owner, raw=image, headers={'Content-Type': 'image/png'}, schema='Attachment')['json'] verify(client, reader, attachment, image) report['fixturesRemoved'] = True report['status'] = 'passed' print('PASS: 16 oversized Content-Length/chunked uploads through Nginx returned canonical 413; both APIs covered; subsequent upload/download verified') except BaseException as error: report['errorType'] = type(error).__name__ if isinstance(error, Failure): report['failure'] = str(error) raise finally: (ROOT / 'evidence/oversized-uploads.json').write_text(json.dumps(report, indent=2) + '\n', encoding='utf-8') if __name__ == '__main__': try: main() except (Failure, ValueError, OSError, subprocess.SubprocessError) as error: print('FAIL: ' + (str(error) if isinstance(error, Failure) else type(error).__name__), file=sys.stderr) sys.exit(1)