Skip to content

Commit 6702303

Browse files
authored
fix: replace brittle profile_image_url allowlist with safe-scheme validation (open-webui#23389)
* fix: replace brittle profile_image_url allowlist with safe-scheme validation The previous validation used a hardcoded allowlist of specific static paths and a single Gravatar prefix. This rejected OWUI's own internal API paths (e.g. /api/v1/users/{id}/profile/image) and external OAuth avatar URLs, making it impossible to save user profiles from the admin panel. Replace with scheme-based validation that allows relative paths, HTTP(S) URLs, and data:image URIs while blocking dangerous schemes like javascript:, file:, and ftp:. Fixes open-webui#23387 * fix: harden profile image URL validation per review feedback - Restrict data URIs to safe raster formats (png/jpeg/gif/webp); SVG is excluded because it can carry embedded scripts. - Block scheme-relative URLs (//host/path) which browsers resolve against the current protocol, bypassing the relative-path check. * fix: use structural validation instead of prefix checks - Use urlparse for HTTP(S) URLs: gives case-insensitive scheme matching and rejects bare schemes with no host (e.g. https://). - Use a compiled regex for data URIs: enforces the ;base64, boundary, restricts to safe raster formats, and is case-insensitive per spec. - Removes the startswith-based prefix tuple in favour of proper URL and data URI parsing. * fix: validate hostname not netloc, fix misleading comment - Use parsed.hostname instead of parsed.netloc so URLs like http://:80/path (non-empty netloc but no actual host) are rejected. - Update data URI comment to accurately state we validate MIME type and structure, not base64 payload integrity. * fix: constrain relative paths to known-safe prefixes Accepting any relative path starting with / allowed a user to set their profile_image_url to an arbitrary internal GET endpoint. When another user (e.g. an admin) views that profile, the browser fires the GET with the viewer's session cookies — an authenticated GET trigger surface. Constrain to known-safe prefixes (/api/v1/users/, /static/) and exact matches (/user.png, /favicon.png) which are the only relative paths OWUI itself generates. * fix: use exact matches and anchored regex, eliminate all prefix wildcarding Replace all startswith-based path checks with: - frozenset exact matches for static assets (/user.png, /favicon.png, /static/favicon.png) - Anchored regex for the OWUI profile image API route that accepts only /api/v1/users/{id}/profile/image (no trailing components, no path traversal across segments) This eliminates every prefix-based attack surface: - /api/v1/users/{id}/anything-else is rejected - /static/../../etc/passwd is rejected - /api/v1/users/../../admin/config is rejected - Arbitrary internal GET triggers are no longer possible * fix: exclude query/fragment delimiters from user-ID regex segment Change [^/]+ to [^/?#]+ so that inputs like /api/v1/users/alice?x=1/profile/image are rejected — the browser would interpret ? as the query string start, making the actual request target /api/v1/users/alice instead of the intended route.
1 parent e7ff476 commit 6702303

1 file changed

Lines changed: 61 additions & 20 deletions

File tree

Lines changed: 61 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
11
"""Validation utilities for user-supplied input."""
22

3-
# Known static asset paths used as default profile images
4-
_ALLOWED_STATIC_PATHS = (
5-
'/user.png',
6-
'/static/favicon.png',
3+
import re
4+
from urllib.parse import urlparse
5+
6+
# Matches the OWUI-generated profile image route. ``[^/?#]+`` accepts
7+
# any user-ID without allowing path-traversal or query/fragment injection,
8+
# and the ``$`` anchor rejects trailing path components.
9+
_USER_PROFILE_IMAGE_RE = re.compile(r'^/api/v1/users/[^/?#]+/profile/image$')
10+
11+
# Validates MIME type and structure of base64 data URIs. Only the prefix
12+
# is checked — validating the full base64 payload would mean running a
13+
# regex across megabytes of data on every Pydantic instantiation for zero
14+
# security benefit (corrupt base64 simply renders a broken image, same as
15+
# a 404 URL). SVG is intentionally excluded: it can carry embedded scripts.
16+
_SAFE_DATA_URI_RE = re.compile(
17+
r'^data:image/(png|jpeg|gif|webp);base64,', re.IGNORECASE
718
)
819

9-
# External URL prefixes that are explicitly trusted for profile images
10-
_ALLOWED_URL_PREFIXES = ('https://www.gravatar.com/avatar/',)
20+
# Exact relative paths accepted as profile images. These are the only
21+
# static-asset paths OWUI itself assigns; no prefix/wildcard matching is
22+
# used so that arbitrary relative paths cannot trigger authenticated GETs
23+
# against internal endpoints when rendered as ``<img>`` sources.
24+
_SAFE_STATIC_PATHS = frozenset({
25+
'/user.png',
26+
'/favicon.png',
27+
'/static/favicon.png',
28+
})
1129

1230

1331
def validate_profile_image_url(url: str) -> str:
@@ -16,28 +34,51 @@ def validate_profile_image_url(url: str) -> str:
1634
1735
Allowed formats:
1836
- Empty string (falls back to default avatar)
19-
- data:image/* URIs (base64-encoded uploads from the frontend)
20-
- Known static asset paths (/user.png, /static/favicon.png)
21-
- Trusted external URLs (e.g. Gravatar)
37+
- Known static-asset paths assigned by OWUI (exact match)
38+
- The OWUI profile-image API route ``/api/v1/users/{id}/profile/image``
39+
- ``http://`` and ``https://`` URLs with a valid hostname
40+
- ``data:image/{png,jpeg,gif,webp};base64,...`` URIs
2241
23-
Returns the url unchanged if valid, raises ValueError otherwise.
42+
Everything else is rejected, including:
43+
- Dangerous schemes (javascript:, file:, ftp:, …)
44+
- SVG data URIs (can contain embedded scripts)
45+
- Arbitrary relative paths (prevents authenticated GET triggers)
46+
- Scheme-relative URLs (``//host/path``)
2447
"""
2548
if not url:
2649
return url
2750

28-
_ALLOWED_DATA_PREFIXES = (
29-
'data:image/png',
30-
'data:image/jpeg',
31-
'data:image/gif',
32-
'data:image/webp',
33-
)
34-
if any(url.startswith(prefix) for prefix in _ALLOWED_DATA_PREFIXES):
51+
# --- Relative paths (exact match + anchored regex only) -----------
52+
53+
if url in _SAFE_STATIC_PATHS:
54+
return url
55+
56+
if _USER_PROFILE_IMAGE_RE.match(url):
3557
return url
3658

37-
if url in _ALLOWED_STATIC_PATHS:
59+
# --- Absolute URLs -------------------------------------------------
60+
61+
# urlparse normalises the scheme to lowercase, giving us
62+
# case-insensitive scheme matching for free.
63+
parsed = urlparse(url)
64+
65+
# External images served over HTTP(S), e.g. OAuth provider avatars.
66+
# Require a non-empty hostname (not just netloc, which can be ":80"
67+
# for a URL like http://:80/path with no actual host).
68+
if parsed.scheme in ('http', 'https'):
69+
if not parsed.hostname:
70+
raise ValueError(
71+
'Invalid profile image URL: HTTP(S) URLs must include a host.'
72+
)
3873
return url
3974

40-
if any(url.startswith(prefix) for prefix in _ALLOWED_URL_PREFIXES):
75+
# Base64-encoded raster images uploaded via the frontend.
76+
# The regex enforces the ;base64, boundary and is case-insensitive
77+
# per the data-URI / MIME-type specs.
78+
if _SAFE_DATA_URI_RE.match(url):
4179
return url
4280

43-
raise ValueError('Invalid profile image URL: only data URIs and default avatars are allowed.')
81+
raise ValueError(
82+
'Invalid profile image URL: must be a known internal path, '
83+
'an HTTP(S) URL with a host, or a data:image URI (png/jpeg/gif/webp).'
84+
)

0 commit comments

Comments
 (0)