Implement Lab 7 security and infrastructure
Skeleton contracts / contracts (pull_request) Canceled after 0s
Skeleton contracts / contracts (pull_request) Canceled after 0s
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
|
||||
#!/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)
|
||||
Reference in New Issue
Block a user