Spaces:
Build error
Build error
File size: 1,023 Bytes
d6703a1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
"""Validation utilities for user-supplied input."""
# Known static asset paths used as default profile images
_ALLOWED_STATIC_PATHS = (
"/user.png",
"/static/favicon.png",
)
def validate_profile_image_url(url: str) -> str:
"""
Pydantic-compatible validator for profile image URLs.
Allowed formats:
- Empty string (falls back to default avatar)
- data:image/* URIs (base64-encoded uploads from the frontend)
- Known static asset paths (/user.png, /static/favicon.png)
Returns the url unchanged if valid, raises ValueError otherwise.
"""
if not url:
return url
_ALLOWED_DATA_PREFIXES = (
"data:image/png",
"data:image/jpeg",
"data:image/gif",
"data:image/webp",
)
if any(url.startswith(prefix) for prefix in _ALLOWED_DATA_PREFIXES):
return url
if url in _ALLOWED_STATIC_PATHS:
return url
raise ValueError(
"Invalid profile image URL: only data URIs and default avatars are allowed."
)
|