mirror of
https://github.com/ParisNeo/lollms.git
synced 2024-12-18 20:27:58 +00:00
Fixed DOS vulenerability on boudaries in multipart chunks
This commit is contained in:
parent
269c640dbc
commit
b5dc256323
@ -11,6 +11,8 @@ import string
|
||||
from lollms.utilities import PackageManager
|
||||
from starlette.middleware.base import BaseHTTPMiddleware
|
||||
from fastapi.responses import JSONResponse
|
||||
from fastapi import Request
|
||||
|
||||
|
||||
if not PackageManager.check_package_installed("lxml"):
|
||||
PackageManager.install_package("lxml")
|
||||
@ -338,9 +340,21 @@ if __name__=="__main__":
|
||||
print(f"Original: {path}, Exception: {e.detail}")
|
||||
|
||||
class MultipartBoundaryCheck(BaseHTTPMiddleware):
|
||||
async def dispatch(self, request, call_next):
|
||||
async def dispatch(self, request: Request, call_next):
|
||||
if request.headers.get("content-type", "").startswith("multipart/form-data"):
|
||||
boundary = request.headers.get("content-type").split("boundary=")[-1]
|
||||
if len(boundary) > 70: # Adjust this limit as needed
|
||||
return JSONResponse(status_code=400, content={"detail": "Invalid boundary length"})
|
||||
if len(boundary) > 70: # Check header boundary
|
||||
return JSONResponse(status_code=400, content={"detail": "Invalid boundary length in header"})
|
||||
|
||||
# Read and check the request body
|
||||
body = await request.body()
|
||||
body_str = body.decode()
|
||||
|
||||
# Check for excessively long boundaries in the body
|
||||
pattern = re.escape(f"--{boundary}") + r"-*"
|
||||
matches = re.findall(pattern, body_str)
|
||||
for match in matches:
|
||||
if len(match) > 74: # 70 + 4 for '--' prefix and '--' suffix
|
||||
return JSONResponse(status_code=400, content={"detail": "Invalid boundary length in body"})
|
||||
|
||||
return await call_next(request)
|
||||
|
Loading…
Reference in New Issue
Block a user