forked from mtusi2026/mtusi2026lab7
Complete lab 7 security infrastructure
This commit is contained in:
+1
-1
@@ -36,7 +36,7 @@ def main():
|
||||
script = Path(args.action_script).expanduser().resolve()
|
||||
check(script.is_file() and os.access(script,os.X_OK),'action script must exist and be executable (chmod +x)')
|
||||
check('.example.' not in script.name,'copy and implement the example hook first')
|
||||
c = Client(args.base_url,args.ca_file,timeout=2)
|
||||
c = Client(args.base_url,args.ca_file,timeout=5)
|
||||
alice,bob,eve,chat = c.bootstrap()
|
||||
sent = c.message(alice,chat,'Сохранить при отказе')
|
||||
original = sent['json']
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
#!/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)
|
||||
@@ -0,0 +1,42 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Verify that a bearer session is rejected after its configured Redis TTL."""
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
import time
|
||||
from client import Client, arguments, check, run
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def configured_ttl() -> int:
|
||||
env_file = ROOT / '.env'
|
||||
check(env_file.is_file(), '.env is missing')
|
||||
for raw in env_file.read_text(encoding='utf-8').splitlines():
|
||||
line = raw.strip()
|
||||
if line.startswith('SESSION_TTL_SECONDS='):
|
||||
return int(line.split('=', 1)[1].strip().strip('"\''))
|
||||
raise SystemExit('SESSION_TTL_SECONDS is missing from .env')
|
||||
|
||||
|
||||
def main() -> None:
|
||||
def configure(parser):
|
||||
parser.add_argument('--wait-seconds', type=int, default=5,
|
||||
help='expected SESSION_TTL_SECONDS configured on the running API')
|
||||
args = arguments('Session expiry check; run against an isolated short-TTL test configuration', configure)
|
||||
check(args.wait_seconds >= 1, '--wait-seconds must be positive')
|
||||
check(configured_ttl() == args.wait_seconds,
|
||||
'set SESSION_TTL_SECONDS in .env to --wait-seconds and recreate both API replicas first')
|
||||
|
||||
client = Client(args.base_url, args.ca_file)
|
||||
actor = client.user('expiry')
|
||||
fresh = client.call('GET', '/api/v1/users/me', actor=actor, schema='User')['json']
|
||||
check(fresh == actor['user'], 'fresh session was not accepted')
|
||||
# Add a small scheduling margin without exposing or storing the bearer token.
|
||||
time.sleep(args.wait_seconds + 1)
|
||||
client.call('GET', '/api/v1/users/me', expected=401, actor=actor)
|
||||
print('PASS: bearer session was accepted before expiry and rejected after the configured TTL.')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
run(main)
|
||||
Reference in New Issue
Block a user