101 Commits

  • perf: large profile pictures and knowledge exports load much faster (#34598)
    Large profile pictures of users, models and channel webhooks (animated ones, or ones set through the API) were sent out in thousands of small pieces, each handled separately, and the knowledge base export sent its zip file the same way. A 2 MB picture took around 200 to 400 ms to load, 20 loads of it at once, as on a page full of avatars, took 9 to 15 seconds, and an 8 MB export spent close to a second just sending the file. Both are now sent as one complete response with the same content type and headers, plus a size header in place of chunked sending.
    
    | Picture | One load before | One load after | 20 at once before | 20 at once after |
    | --- | --- | --- | --- | --- |
    | Model, 2 MB | 207 ms | 15 ms | 9.3 s | 0.48 s |
    | User, 2 MB | 379 ms | 15 ms | 15.0 s | 0.34 s |
    | Model, 5 MB | 743 ms | 39 ms | 33.6 s | 0.63 s |
    | User, 5 MB | 627 ms | 25 ms | 30.7 s | 0.54 s |
    
    Fixes #34597
  • fix: model import silently skips a model whose ID is already taken (#32866)
    Importing a model JSON on the Workspace Models page as a regular user skipped any model whose ID already belonged to another user's model they cannot edit, or to a model from one of the instance's connections such as OpenAI or Ollama. There was no message, so the model just never showed up. The import now stops at that model with the error "Uh-oh! This model id is already registered. Please choose another model id string."; the models before it in the file are imported and the ones after it are not. Admins, and users who can edit the existing model, are unaffected and still overwrite it.
  • Gate the model profile image endpoint on model read access (#29700)
    Any authenticated user could fetch the profile image of a model they have no access to, and could tell an existing model id from an unknown one by whether the response carried the image or the default logo.
    
    The endpoint now serves an image only to callers who can see the model itself: the owner, an admin under the admin bypass, or the holder of a read grant, with the same rule applied to arena models defined in config. Everyone else gets the default logo, byte for byte the response an unknown id already returned, so ids can no longer be probed. BYPASS_MODEL_ACCESS_CONTROL is honoured here because it is what decides which models reach a user's model list to begin with.
    
    Avatars now fall back to the default logo wherever a viewer meets a model id without holding a grant on it: a model reply in a channel shown to the other members, and the admin analytics and evaluation pages when the admin bypass is switched off.
  • perf: filter workspace models by access in SQL instead of loading every model (#28795)
    Exporting workspace models loaded every model row, built a full response object with its owner for each, and only then dropped the ones the caller may not see. On a large model table that made the export endpoint slow in proportion to models the user cannot even access.
    
    The owner-or-grant check now happens in the query itself, reusing the permission filter this file already applies to the paginated list endpoint, so only visible rows are ever hydrated. The by-user wrapper had one caller left and is gone with it.
    
    Measured with 500 workspace models of which 3 are visible to the caller: 5 queries and ~12.7 ms before, 4 queries and ~2.8 ms after. The resulting set is unchanged for owner, public, direct-user, group and multi-grant entries, and base model entries stay excluded as before.
  • chore: remove dead json imports (#27815)
    Fourteen modules import `json` without using it. Ruff flags every one with F401, and a word-boundary search for `json` in each file matches only the import line itself, including inside strings, comments and annotations.
    
    Two exclusions, both deliberate. Migration files are left alone: the import is equally dead there, but those files are frozen history and not worth the churn. `models/chats.py` has the same dead import and is handled in its own change, so it is skipped here to avoid two changes touching the same line.
    
    No behaviour change.
  • fix: strip model params for read-only callers in the model list endpoint (#27004)
    The per-id model endpoint (GET /api/v1/models/model) strips params, the system
    prompt and other curated model config, for callers who only have read access.
    The list endpoint (GET /api/v1/models/list) did not: it returned each
    read-accessible model's full params, so a read-shared model exposed its
    params.system to non-owner read-grant holders.
    
    Mirror the per-id behaviour: compute write_access per item and drop params
    before serialising when the caller lacks write access (not the owner, not an
    admin under BYPASS_ADMIN_ACCESS_CONTROL and holding no write grant). The
    model-card list UI does not render params, so this does not change
    functionality.
    
    Co-authored-by: bogdancherniy11-sudo <229690748+bogdancherniy11-sudo@users.noreply.github.com>
  • Fail closed when the proxy/redirect path decode cap is exceeded (#26050)
    The decode-until-stable loops in _sanitize_proxy_path (terminals.py, cap 8) and
    _safe_static_redirect_path (models.py, cap 2) proceed with whatever remains after the
    cap instead of rejecting it. A path encoded more times than the cap therefore exits the
    loop still percent-encoded, passes the literal '..' / prefix checks (the dots are still
    %2E, not '..'), and is forwarded to the upstream terminal server, or emitted as a
    redirect Location, which then decodes it once more and resolves the traversal. This is
    the residual of the decode-until-stable hardening added for CVE-2026-54017: the cap is a
    fixed depth, not a true stability guarantee.
    
    Reject when the value is still not stable after the cap (unquote(x) != x), so anything
    encoded more deeply than the cap fails closed rather than being forwarded. Legitimate
    paths stabilize within a pass or two and are unaffected.
    
    Co-authored-by: DavidCarliez <271374756+DavidCarliez@users.noreply.github.com>
    Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
  • fix: harden model profile image against SVG stored XSS (#25060)
    ModelMeta.profile_image_url now runs validate_profile_image_url, rejecting SVG/script data URIs (matching UserUpdateForm and ChannelWebhookForm). The /model/profile/image endpoint enforces the PROFILE_IMAGE_ALLOWED_MIME_TYPES allowlist and sets X-Content-Type-Options: nosniff, so an SVG data URI can no longer be served inline on-origin. Closes the fourth profile-image XSS sink missed by the user and webhook fixes.
    
    Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
  • fix: strip model params for read-only callers on per-id endpoint (#24525)
    GET /api/v1/models/model?id=<model_id> at routers/models.py:412
    returned the full model.model_dump() to any caller with read access,
    including the params dict that holds the admin-curated system prompt
    and other behavior config. The user-facing /api/models endpoint
    already strips this via utils/models.py:170,210 with the comment
    "Remove params to avoid exposing sensitive info", and /api/v1/models/list
    gates by write permission so non-curators don't see the model in their
    workspace listing at all. The per-id endpoint missed the same gate, so
    a user with read-only access (e.g. granted access to use the model in
    chat) could open /workspace/models/edit?id=<not-mine> in the browser
    and read the system prompt verbatim from the network response, even
    though saving was correctly blocked.
    
    Compute write_access once at the top of the handler so it can serve
    both the response-shape decision and the response field. When the
    caller lacks write access, replace params with an empty dict in the
    serialised response. Owners, admins under BYPASS_ADMIN_ACCESS_CONTROL,
    and explicit write-grant holders still get the full payload so the
    workspace edit UI keeps working for users who legitimately curate the
    model.
    
    Read-permission users continue to receive everything else they need to
    chat with the model — the chat path resolves prompt/params server-side
    from the stored ModelModel and never echoes them back through this
    endpoint.
    
    Reported by destination-one in GHSA-h2cw-7qw9-56xr.
    
    Co-authored-by: destination-one <destination-one@users.noreply.github.com>
  • fix:image url validation and signout post (#24420)
    * refac(routers): reject external URLs in profile/model image handlers
    
    * refac(ui): centralize image URL validation in safeImageUrl helper
    
    * refac(auths): make signout POST-only
    
    * refac: gate external profile image redirect behind ENABLE_PROFILE_IMAGE_URL_FORWARDING
    
    Restore the 302 redirect for external http(s) profile image URLs in
    the user and model profile-image endpoints, but gate it behind a new
    ENABLE_PROFILE_IMAGE_URL_FORWARDING env flag (default: True).
    
    Existing deployments that rely on external profile image forwarding
    continue to work unchanged.  Operators who want to suppress the
    redirect (to prevent client-side IP/UA/Referer leaks) can set the
    flag to False.
  • perf: redirect default model profile image to canonical static URL (#24015)
    - Return 302 to /static/favicon.png instead of streaming the same PNG per
      model id so browsers can cache one asset for default avatars.
    - Validate stored /static/ paths with decode, normpath, and /static
      prefix checks; invalid paths fall back to favicon.
    
    Made-with: Cursor
  • perf: reuse request db session in get_model_profile_image (#23796)
    Pass the request-scoped AsyncSession into Models.get_model_by_id so the
    endpoint no longer opens a fresh DB session on every call, avoiding an
    extra connection acquisition per profile image request.
    
    Co-authored-by: Claude <noreply@anthropic.com>