cap request body to prevent memory DoS

Both search handlers trusted the client-supplied Content-Length and
passed it straight to `rfile.read()`, so a single request with a
forged `Content-Length: 1073741824` would allocate gigabytes. Clamp
to 64 KiB, well above any legitimate search payload.
This commit is contained in:
authentik Default Admin 2026-05-09 18:29:49 +00:00
parent dd3a3db95d
commit 9dda742fef

View file

@ -40,6 +40,9 @@ _db_path = os.path.join(tempfile.gettempdir(), "eirescope_investigations.db")
db = Database(_db_path)
engine = InvestigationEngine()
# Cap request body to prevent memory DoS from forged Content-Length.
MAX_REQUEST_BODY = 64 * 1024 # 64 KiB
class EireScopeHandler(BaseHTTPRequestHandler):
"""HTTP Request handler for EireScope web interface."""
@ -103,7 +106,7 @@ class EireScopeHandler(BaseHTTPRequestHandler):
def _handle_search_form(self):
"""Handle form-based search submission."""
content_length = int(self.headers.get("Content-Length", 0))
content_length = min(int(self.headers.get("Content-Length", 0)), MAX_REQUEST_BODY)
body = self.rfile.read(content_length).decode("utf-8")
params = urllib.parse.parse_qs(body)
@ -134,7 +137,7 @@ class EireScopeHandler(BaseHTTPRequestHandler):
def _handle_search(self):
"""Handle API search (JSON)."""
content_length = int(self.headers.get("Content-Length", 0))
content_length = min(int(self.headers.get("Content-Length", 0)), MAX_REQUEST_BODY)
body = json.loads(self.rfile.read(content_length).decode("utf-8"))
query = body.get("query", "").strip()