Skip to content

Commit 3e702c6

Browse files
fix: guard against missing/malformed Authorization header in apikey_required (#13860)
### What problem does this PR solve? Previously, `apikey_required` called `request.headers.get('Authorization').split()[1]` without checking for None or insufficient parts, causing an unhandled AttributeError or IndexError (500) instead of a proper 403 JSON response. This applies the same guarding pattern already used by `token_required` in the same file. ### Type of change - [x] Bug Fix (non-breaking change which fixes an issue) - [x] Refactoring
1 parent 4f27090 commit 3e702c6

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

api/utils/api_utils.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,13 @@ def get_json_result(code: RetCode = RetCode.SUCCESS, message="success", data=Non
252252
def apikey_required(func):
253253
@wraps(func)
254254
async def decorated_function(*args, **kwargs):
255-
token = request.headers.get("Authorization").split()[1]
255+
authorization = request.headers.get("Authorization")
256+
if not authorization:
257+
return build_error_result(message="Authorization header is missing!", code=RetCode.FORBIDDEN)
258+
parts = authorization.split()
259+
if len(parts) < 2:
260+
return build_error_result(message="Please check your authorization format.", code=RetCode.FORBIDDEN)
261+
token = parts[1]
256262
objs = APIToken.query(token=token)
257263
if not objs:
258264
return build_error_result(message="API-KEY is invalid!", code=RetCode.FORBIDDEN)

0 commit comments

Comments
 (0)