#!/usr/bin/env python3 import base64 import uuid from client import Client, arguments, check, run def main(): args = arguments("Login rate limit security test") client = Client(args.base_url, args.ca_file) username = "rate_" + uuid.uuid4().hex[:12] password = "Correct-" + uuid.uuid4().hex client.call( "POST", "/api/v1/users", 201, body={ "username": username, "display_name": "Rate limit test", "password": password, }, schema="User", ) wrong = base64.b64encode( f"{username}:Wrong-{uuid.uuid4().hex}".encode() ).decode() for attempt in range(5): client.call( "POST", "/api/v1/auth/sessions", 401, headers={"Authorization": "Basic " + wrong}, ) blocked = client.call( "POST", "/api/v1/auth/sessions", 429, headers={"Authorization": "Basic " + wrong}, ) retry_after = blocked["headers"].get("Retry-After") check(retry_after is not None, "429 response has no Retry-After") check(int(retry_after) > 0, "Retry-After must be positive") print("PASS: login rate limit returns 429 and Retry-After") if __name__ == "__main__": run(main)