fix(api): validate Bearer prefix before extracting API key

The replace("Bearer ", "") approach returned the full Authorization header value when the prefix was not present, incorrectly treating other auth schemes (e.g., "Basic xyz") as API keys.
This commit is contained in:
Andy
2026-01-23 17:20:22 -07:00
parent 6b90a19632
commit 0c7d20c943

View File

@@ -18,7 +18,15 @@ def get_api_key_from_request(request: web.Request) -> Optional[str]:
Returns:
API key string or None
"""
return request.headers.get("X-API-Key") or request.headers.get("Authorization", "").replace("Bearer ", "")
api_key = request.headers.get("X-API-Key")
if api_key:
return api_key
auth_header = request.headers.get("Authorization", "")
if auth_header.startswith("Bearer "):
return auth_header[7:] # len("Bearer ") == 7
return None
def get_api_key_config(app: web.Application, api_key: str) -> Optional[Dict[str, Any]]: