#!/usr/bin/env python3 """Additional grade-3 checks for login throttling and the request body limit.""" from __future__ import annotations import base64 import uuid from client import Client, arguments, check, run def main() -> None: args = arguments('Login rate-limit and request-size checks') client = Client(args.base_url, args.ca_file) username = 'lab7_limit_' + uuid.uuid4().hex[:16] credential = base64.b64encode((username + ':wrong-password').encode()).decode() for _ in range(10): client.call('POST', '/api/v1/auth/sessions', 401, headers={'Authorization': 'Basic ' + credential}) limited = client.call('POST', '/api/v1/auth/sessions', 429, headers={'Authorization': 'Basic ' + credential}) retry_after = limited['headers'].get('Retry-After') check(retry_after is not None and retry_after.isdigit() and int(retry_after) > 0, '429 lacks a positive Retry-After header') oversized = b'x' * (11 * 1024 * 1024 + 1) too_large = client.call('POST', '/api/v1/users', 413, raw=oversized, headers={'Content-Type': 'application/json'}) check(too_large['json']['error']['code'] == 'payload_too_large', 'oversized request had the wrong error code') print('PASS: shared login rate limit (429/Retry-After) and request-size limit (413).') if __name__ == '__main__': run(main)