diff --git a/backend/open_webui/utils/validate.py b/backend/open_webui/utils/validate.py index 98e03d9166..1482eeb4b2 100644 --- a/backend/open_webui/utils/validate.py +++ b/backend/open_webui/utils/validate.py @@ -10,6 +10,21 @@ _SAFE_DATA_URI_RE = re.compile( r'^data:image/(png|jpeg|gif|webp);base64,', re.IGNORECASE ) +# Relative path prefixes that are safe to accept as profile image URLs. +# These are the only patterns OWUI itself generates; arbitrary relative +# paths are rejected to prevent authenticated GET triggers against +# internal endpoints when other users view a profile. +_SAFE_RELATIVE_PATH_PREFIXES = ( + '/api/v1/users/', # OWUI-generated profile image routes + '/static/', # Static assets served by the frontend +) + +# Exact relative paths accepted (default avatars). +_SAFE_RELATIVE_PATHS = frozenset({ + '/user.png', + '/favicon.png', +}) + def validate_profile_image_url(url: str) -> str: """ @@ -17,21 +32,23 @@ def validate_profile_image_url(url: str) -> str: Allowed formats: - Empty string (falls back to default avatar) - - Relative paths starting with ``/`` (internal assets and API routes) + - Known-safe relative paths (default avatars, static assets, + and OWUI profile image API routes) - ``http://`` and ``https://`` URLs with a valid host - ``data:image/{png,jpeg,gif,webp};base64,...`` URIs All other schemes (javascript:, file:, ftp:, etc.) are rejected. SVG data URIs are rejected because SVG can contain embedded scripts. + Arbitrary relative paths are rejected to prevent authenticated GET + requests against internal endpoints. """ if not url: return url - # Relative paths: covers /user.png, /static/favicon.png, - # /api/v1/users/{id}/profile/image, and any future internal routes. - # Exclude scheme-relative URLs (//host/path) which browsers resolve - # against the current protocol. - if url.startswith('/') and not url.startswith('//'): + # Known-safe relative paths generated by OWUI itself. + if url in _SAFE_RELATIVE_PATHS: + return url + if any(url.startswith(prefix) for prefix in _SAFE_RELATIVE_PATH_PREFIXES): return url # urlparse normalises the scheme to lowercase, giving us @@ -55,7 +72,8 @@ def validate_profile_image_url(url: str) -> str: return url raise ValueError( - 'Invalid profile image URL: must be a relative path, an HTTP(S) URL ' - 'with a host, or a data:image URI (png/jpeg/gif/webp).' + 'Invalid profile image URL: must be a known internal path, ' + 'an HTTP(S) URL with a host, or a data:image URI (png/jpeg/gif/webp).' ) +