Add User ID validation to ensure it is not an email or phone number

This commit is contained in:
raz-alon 2025-04-17 15:50:12 +03:00 committed by GitHub
parent 8be8022914
commit 57f98f2dc6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,5 +1,6 @@
import json
import time
import re
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Tuple, Union, cast
import httpx
@ -527,6 +528,7 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig):
_litellm_metadata
and isinstance(_litellm_metadata, dict)
and "user_id" in _litellm_metadata
and not _valid_user_id(_litellm_metadata.get("user_id", None))
):
optional_params["metadata"] = {"user_id": _litellm_metadata["user_id"]}
@ -784,3 +786,19 @@ class AnthropicConfig(AnthropicModelInfo, BaseConfig):
message=error_message,
headers=cast(httpx.Headers, headers),
)
def _valid_user_id(user_id: str) -> bool:
"""
Validate that user_id is not an email or phone number.
Returns: bool: True if valid (not email or phone), False otherwise
"""
email_pattern = r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"
phone_pattern = r"^\+?[\d\s\(\)-]{7,}$"
if re.match(email_pattern, user_id):
return False
if re.match(phone_pattern, user_id):
return False
return True