Dify: Stored XSS via SVG Attachments in the image-preview Endpoint

An editor attaches an SVG containing a script to a knowledge base segment; when an owner opens the signed preview URL, the script creates an admin invitation with that session.

Advisory ID: TP-2026-167
Product: Dify (open-source platform for LLM applications and knowledge bases)
Vulnerability type: Stored cross-site scripting (CWE-79)
CVE: not requested
CVSS 3.1: 8.7 (High) · CVSS:3.1/AV:N/AC:L/PR:L/UI:R/S:C/C:H/I:H/A:N
Affected versions: before 1.20.0 (verified live: 1.14.1 and 1.16.1)
Fixed in: 1.20.0
Reported: 13 May 2026

Summary

Dify is an open-source platform for LLM applications whose knowledge base segments can carry image attachments. The endpoint /files/<id>/image-preview serves such attachments with the content type given at upload, without nosniff, without Content-Disposition and without a sandbox. A user with the editor or dataset_operator role uploads an SVG containing a script and attaches it to a segment. When another member opens the signed preview URL as a page of its own, the script runs in the origin of the Dify console with that member's session. With an owner's session the script creates an admin invitation and gives the attacker lasting admin access to the workspace.

Root cause

In version 1.14.1 (commit 3f6644a), to which all line numbers refer, ImagePreviewApi.get responds with Response(generator, mimetype=mimetype) and sets neither X-Content-Type-Options: nosniff nor Content-Disposition: attachment (api/controllers/files/image_preview.py:50-69).
The value of mimetype is upload_file.mime_type from the content type of the multipart part, and upload_file() stores the SVG bytes unchanged (api/services/file_service.py:50-79).
The protective function enforce_download_for_html covers only text/html and application/xhtml+xml and is not called by this endpoint (api/controllers/common/file_response.py:7).
get_sign_content and attachments sign image-preview URLs for segment attachments anew on every read (api/models/dataset.py:945-1032).
POST /console/api/files/upload checks no file extension without the parameter source=datasets, and the preview endpoint requires only the signature in the URL, no session.

Proof of Concept

POST /console/api/files/upload HTTP/1.1
Content-Type: multipart/form-data; boundary=x

--x
Content-Disposition: form-data; name="file"; filename="a.svg"
Content-Type: image/svg+xml

<svg xmlns="http://www.w3.org/2000/svg"><script>/* reads csrf_token and calls the console API */</script></svg>
--x--

GET /files/<id>/image-preview?timestamp=...&nonce=...&sign=... HTTP/1.1

HTTP/1.1 200
Content-Type: image/svg+xml

A segment created with the uploaded file in attachment_ids returns the signed address as attachments[0].source_url. Verified on Dify 1.16.1 with an unmodified .env.example: the endpoint answers without cookies with Content-Type: image/svg+xml and byte-identical content. In Chromium with an owner session the SVG renders as a page of its own in the application's origin, reads csrf_token, fetches the profile and the member list (HTTP 200) and creates an admin invitation (HTTP 201).

Impact

  • JavaScript execution in the origin of the Dify console with the session of the member who opens the preview URL.
  • With an owner session: reading the profile and member list and creating an admin invitation, which means lasting admin access to the workspace.
  • The editor or dataset_operator role is enough as a starting point.
  • The signature is valid for 300 seconds (FILES_ACCESS_TIMEOUT), but the application signs the address anew on every read of the segment.

References

Is Something Like This in Your Software?

Our team found this vulnerability during its work. Have your applications tested by the same specialists, with a penetration test from turingpoint.