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:
parent
dd3a3db95d
commit
9dda742fef
1 changed files with 5 additions and 2 deletions
|
|
@ -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()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue