mirror of
https://github.com/open-webui/open-webui.git
synced 2025-12-16 20:07:49 +01:00
refac/fix
This commit is contained in:
@@ -1272,9 +1272,13 @@ app.add_middleware(SecurityHeadersMiddleware)
|
|||||||
class APIKeyRestrictionMiddleware(BaseHTTPMiddleware):
|
class APIKeyRestrictionMiddleware(BaseHTTPMiddleware):
|
||||||
async def dispatch(self, request: Request, call_next):
|
async def dispatch(self, request: Request, call_next):
|
||||||
auth_header = request.headers.get("Authorization")
|
auth_header = request.headers.get("Authorization")
|
||||||
|
token = None
|
||||||
|
|
||||||
|
if auth_header:
|
||||||
|
scheme, token = auth_header.split(" ")
|
||||||
|
|
||||||
# Only apply restrictions if an sk- API key is used
|
# Only apply restrictions if an sk- API key is used
|
||||||
if auth_header and auth_header.startswith("sk-"):
|
if token and token.startswith("sk-"):
|
||||||
# Check if restrictions are enabled
|
# Check if restrictions are enabled
|
||||||
if request.app.state.config.ENABLE_API_KEY_ENDPOINT_RESTRICTIONS:
|
if request.app.state.config.ENABLE_API_KEY_ENDPOINT_RESTRICTIONS:
|
||||||
allowed_paths = [
|
allowed_paths = [
|
||||||
@@ -1294,9 +1298,11 @@ class APIKeyRestrictionMiddleware(BaseHTTPMiddleware):
|
|||||||
)
|
)
|
||||||
|
|
||||||
if not is_allowed:
|
if not is_allowed:
|
||||||
raise HTTPException(
|
return JSONResponse(
|
||||||
status_code=status.HTTP_403_FORBIDDEN,
|
status_code=status.HTTP_403_FORBIDDEN,
|
||||||
detail="API key not allowed to access this endpoint.",
|
content={
|
||||||
|
"detail": "API key not allowed to access this endpoint."
|
||||||
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
response = await call_next(request)
|
response = await call_next(request)
|
||||||
|
|||||||
Reference in New Issue
Block a user