Forward final lab3 session security and validation fixes

This commit is contained in:
Artyom
2026-11-06 12:07:00 +03:00
parent f0fc084fbb
commit 75b93bbba5
41 changed files with 2864 additions and 28 deletions
+43
View File
@@ -0,0 +1,43 @@
import http.client
import json
import sys
import time
status = int(sys.argv[1])
path = {502: "/bad", 503: "/none", 504: "/slow"}[status]
if status != 503:
for _ in range(60):
conn = http.client.HTTPConnection("lb", 8080, timeout=2)
conn.request("GET", "/health/ready")
response = conn.getresponse()
response.read()
conn.close()
if response.status == 200:
break
time.sleep(1)
else:
raise RuntimeError("Backend never became ready")
else:
time.sleep(5)
for value in ['a"b', "a\\b", "a\tb", "x" * 200]:
conn = http.client.HTTPConnection("lb", 8080, timeout=20)
start = time.monotonic()
conn.request("GET", path, headers={"X-Request-Id": value})
response = conn.getresponse()
payload = json.loads(response.read())
assert response.status == status, response.status
assert response.getheader("X-Request-Id") == value
assert payload["error"]["request_id"] == value
print(
json.dumps(
{
"status": status,
"request_id": value,
"json_valid": True,
"echo_matches": True,
"seconds": round(time.monotonic() - start, 3),
}
),
flush=True,
)
conn.close()
@@ -0,0 +1,25 @@
services:
lb:
image: haproxy:3.2.26-alpine@sha256:5893840576071e6217cd52ebdcc14f454c6f7a631056fa6f4e0f40d3cac4215b
volumes: [../../../infra/haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro]
init: true
deploy:
resources:
limits: {cpus: "0.5", memory: 128m}
api:
image: ${API_IMAGE:?Set the image built by the lab4 stack}
entrypoint: [python, /fixture/server.py]
volumes: [./:/fixture:ro]
init: true
deploy:
resources:
limits: {cpus: "0.5", memory: 128m}
probe:
image: ${API_IMAGE:?Set the image built by the lab4 stack}
entrypoint: [python, /fixture/client.py]
volumes: [./:/fixture:ro]
profiles: [probe]
init: true
deploy:
resources:
limits: {cpus: "0.5", memory: 128m}
+20
View File
@@ -0,0 +1,20 @@
import socketserver
import time
class Handler(socketserver.StreamRequestHandler):
def handle(self) -> None:
request = self.rfile.readline(8192)
while self.rfile.readline(8192) not in {b"\r\n", b""}:
pass
path = request.split(b" ")[1]
if path == b"/bad":
self.wfile.write(b"NOT-HTTP\r\n\r\n")
elif path == b"/slow":
time.sleep(12)
else:
self.wfile.write(b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\n\r\nOK")
with socketserver.ThreadingTCPServer(("0.0.0.0", 8080), Handler) as server:
server.serve_forever()