Update security.py

This commit is contained in:
Saifeddine ALOUI 2024-09-12 09:08:44 +02:00 committed by GitHub
parent 39ca983490
commit 627f72832d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -9,6 +9,7 @@ import re
import platform
import string
from lollms.utilities import PackageManager
from starlette.middleware.base import BaseHTTPMiddleware
if not PackageManager.check_package_installed("lxml"):
PackageManager.install_package("lxml")
@ -334,3 +335,11 @@ if __name__=="__main__":
print(f"Original: {path}, Sanitized: {sanitized}")
except HTTPException as e:
print(f"Original: {path}, Exception: {e.detail}")
class MultipartBoundaryCheck(BaseHTTPMiddleware):
async def dispatch(self, 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"})
return await call_next(request)