Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed invalid api key format raising 500 #1079

Merged
merged 6 commits into from
Jan 28, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions backend/api_v2/api_key_validator.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import logging
import uuid
from functools import wraps
from typing import Any

from account_v2.exceptions import BadRequestException
from api_v2.exceptions import Forbidden
from rest_framework.request import Request

Expand Down Expand Up @@ -39,6 +41,8 @@ def wrapper(self: Any, request: Request, *args: Any, **kwargs: Any) -> Any:
api_key = authorization_header.split(" ")[1]
if not api_key:
raise Forbidden("Missing api key")
if not cls.is_valid_uuid(api_key):
raise BadRequestException("Invalid API key format. Expected a UUID.")
pk-zipstack marked this conversation as resolved.
Show resolved Hide resolved
cls.validate_parameters(request, **kwargs)
return cls.validate_and_process(
self, request, func, *args, **kwargs, api_key=api_key
Expand All @@ -58,3 +62,19 @@ def validate_and_process(
"""Process and validate API key with specific logic required by
subclasses."""
pass

@staticmethod
def is_valid_uuid(api_key: str) -> bool:
"""Check if a given string is a valid UUID.

Args:
api_key (str): The API key to validate

Returns:
bool: True if valid UUID, False otherwise
"""
try:
uuid.UUID(api_key)
return True
except ValueError:
return False
Loading