Files
mtusi2026lab7/student-tests/rate_limit.py
T

36 lines
1.3 KiB
Python

"""Verify repeatable login limiting over the HTTPS ingress."""
import argparse
import base64
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "tests"))
from client import Client, check
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--base-url", default="https://localhost:8443")
parser.add_argument("--ca-file", default=ROOT / "certs/private/ca.crt")
args = parser.parse_args()
c = Client(args.base_url, str(args.ca_file))
user = c.user("ratelimit")
name = user["registration"]["username"]
authorization = base64.b64encode((name + ":synthetic-wrong-password").encode()).decode()
outcomes = []
for _ in range(9):
reply = c.call("POST", "/api/v1/auth/sessions", (401, 429),
headers={"Authorization": "Basic " + authorization})
outcomes.append(reply["status"])
if reply["status"] == 429:
check(int(reply["headers"]["Retry-After"]) > 0, "missing retry interval")
check(outcomes[:7] == [401] * 7 and outcomes[7:] == [429, 429],
f"unexpected shared login limit: {outcomes}")
print("PASS: 401 for wrong passwords, then shared 429/Retry-After over HTTPS")
if __name__ == "__main__":
main()