HTML previews should download instead of rendering inline #21255

Closed
opened 2026-02-21 20:11:37 -05:00 by yindo · 1 comment
Owner

Originally created by @laipz8200 on GitHub (Dec 24, 2025).

Originally assigned to: @laipz8200 on GitHub.

Self Checks

  • I have read the Contributing Guide and Language Policy.
  • This is only for bug report, if you would like to ask a question, please head to Discussions.
  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit this report, otherwise it will be closed.
  • 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :)
  • Please do not modify this template :) and fill in all the required fields.

Dify version

main (b2b7e82e28)

Cloud or Self Hosted

Self Hosted (Source)

Steps to reproduce

  1. Upload an HTML file (for example: unsafe.html).
  2. Open the file preview endpoint in a browser.

✔️ Expected Behavior

HTML uploads should be forced to download (attachment) with safe headers rather than rendering inline.

Actual Behavior

HTML uploads render inline in the browser, which can execute HTML content.

Originally created by @laipz8200 on GitHub (Dec 24, 2025). Originally assigned to: @laipz8200 on GitHub. ### Self Checks - [x] I have read the [Contributing Guide](https://github.com/langgenius/dify/blob/main/CONTRIBUTING.md) and [Language Policy](https://github.com/langgenius/dify/issues/1542). - [x] This is only for bug report, if you would like to ask a question, please head to [Discussions](https://github.com/langgenius/dify/discussions/categories/general). - [x] I have searched for existing issues [search for existing issues](https://github.com/langgenius/dify/issues), including closed ones. - [x] I confirm that I am using English to submit this report, otherwise it will be closed. - [x] 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :) - [x] Please do not modify this template :) and fill in all the required fields. ### Dify version main (`b2b7e82e28`) ### Cloud or Self Hosted Self Hosted (Source) ### Steps to reproduce 1. Upload an HTML file (for example: unsafe.html). 2. Open the file preview endpoint in a browser. ### ✔️ Expected Behavior HTML uploads should be forced to download (attachment) with safe headers rather than rendering inline. ### ❌ Actual Behavior HTML uploads render inline in the browser, which can execute HTML content.
yindo added the 🐞 bug label 2026-02-21 20:11:37 -05:00
yindo closed this issue 2026-02-21 20:11:37 -05:00
Author
Owner

@dosubot[bot] commented on GitHub (Dec 24, 2025):

By default, Dify's file preview endpoint renders HTML files inline unless the as_attachment parameter is set to true, which means browsers can execute uploaded HTML content—a security risk for XSS and script injection. The relevant logic is in api/controllers/files/image_preview.py, where Content-Disposition: attachment is only set if as_attachment=true is passed in the request, otherwise HTML files are rendered inline with their original MIME type cite.

There is no current configuration to force downloads for HTML (or SVG/XML) files by default. To mitigate this, the endpoint should always set Content-Disposition: attachment and Content-Type: application/octet-stream for these file types, regardless of the as_attachment flag. It's also recommended to add security headers like Content-Security-Policy: default-src 'none'; sandbox, X-Content-Type-Options: nosniff, X-Frame-Options: DENY, and Referrer-Policy: no-referrer to the response to further reduce XSS risk cite.

There is an open PR to add these security headers, but it is not yet merged cite. Until then, you can patch your deployment by updating the preview endpoint to always force download for HTML/SVG/XML and set the recommended headers.

To reply, just mention @dosu.


How did I do? Good | Irrelevant | Incorrect | Verbose | Hallucination | Report 🐛 | Other

Chat with Dosu Open in Cursor Join Discord Share on X

@dosubot[bot] commented on GitHub (Dec 24, 2025): <!-- Answer --> By default, Dify's file preview endpoint renders HTML files inline unless the as_attachment parameter is set to true, which means browsers can execute uploaded HTML content—a security risk for XSS and script injection. The relevant logic is in api/controllers/files/image_preview.py, where Content-Disposition: attachment is only set if as_attachment=true is passed in the request, otherwise HTML files are rendered inline with their original MIME type [cite](https://github.com/langgenius/dify/blob/b2b7e82e281512a7249aabc754dbe4d65f825ad9/api/controllers/files/image_preview.py#L24-L141). There is no current configuration to force downloads for HTML (or SVG/XML) files by default. To mitigate this, the endpoint should always set Content-Disposition: attachment and Content-Type: application/octet-stream for these file types, regardless of the as_attachment flag. It's also recommended to add security headers like Content-Security-Policy: default-src 'none'; sandbox, X-Content-Type-Options: nosniff, X-Frame-Options: DENY, and Referrer-Policy: no-referrer to the response to further reduce XSS risk [cite](https://github.com/langgenius/dify/issues/28909). There is an open PR to add these security headers, but it is not yet merged [cite](https://github.com/langgenius/dify/pull/28910). Until then, you can patch your deployment by updating the preview endpoint to always force download for HTML/SVG/XML and set the recommended headers. <!-- Dosu Comment Footer --> *To reply, just mention [@dosu](https://go.dosu.dev/dosubot).* --- <sup>How did I do? [Good](https://app.dosu.dev/response-feedback/fc787390-3d29-4260-9455-8275880bc098?feedback_type=great_response) | [Irrelevant](https://app.dosu.dev/response-feedback/fc787390-3d29-4260-9455-8275880bc098?feedback_type=irrelevant_answer) | [Incorrect](https://app.dosu.dev/response-feedback/fc787390-3d29-4260-9455-8275880bc098?feedback_type=incorrect_sources) | [Verbose](https://app.dosu.dev/response-feedback/fc787390-3d29-4260-9455-8275880bc098?feedback_type=too_verbose) | [Hallucination](https://app.dosu.dev/response-feedback/fc787390-3d29-4260-9455-8275880bc098?feedback_type=hallucination) | [Report 🐛](https://app.dosu.dev/response-feedback/fc787390-3d29-4260-9455-8275880bc098?feedback_type=bug_report) | [Other](https://app.dosu.dev/response-feedback/fc787390-3d29-4260-9455-8275880bc098?feedback_type=other)</sup> [![Chat with Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/6088a4f4-073a-43f3-b3d4-4f06fb0c05a0/ask?utm_source=github)&nbsp;[![Open in Cursor](https://dosu.dev/dosu-in-cursor.svg)](https://cursor.com/link/prompt?text=By%20default%2C%20Dify%27s%20file%20preview%20endpoint%20renders%20HTML%20files%20inline%20unless%20the%20as_attachment%20parameter%20is%20set%20to%20true%2C%20which%20means%20browsers%20can%20execute%20uploaded%20HTML%20content%E2%80%94a%20security%20risk%20for%20XSS%20and%20script%20injection.%20The%20relevant%20logic%20is%20in%20api/controllers/files/image_preview.py%2C%20where%20Content-Disposition%3A%20attachment%20is%20only%20set%20if%20as_attachment%3Dtrue%20is%20passed%20in%20the%20request%2C%20otherwise%20HTML%20files%20are%20rendered%20inline%20with%20their%20original%20MIME%20type%C2%A0%5Bcite%5D%28https%3A//github.com/langgenius/dify/blob/b2b7e82e281512a7249aabc754dbe4d65f825ad9/api/controllers/files/image_preview.py%23L24-L141%29.%0A%0AThere%20is%20no%20current%20configuration%20to%20force%20downloads%20for%20HTML%20%28or%20SVG/XML%29%20files%20by%20default.%20To%20mitigate%20this%2C%20the%20endpoint%20should%20always%20set%20Content-Disposition%3A%20attachment%20and%20Content-Type%3A%20application/octet-stream%20for%20these%20file%20types%2C%20regardless%20of%20the%20as_attachment%20flag.%20It%27s%20also%20recommended%20to%20add%20security%20headers%20like%20Content-Security-Policy%3A%20default-src%20%27none%27%3B%20sandbox%2C%20X-Content-Type-Options%3A%20nosniff%2C%20X-Frame-Options%3A%20DENY%2C%20and%20Referrer-Policy%3A%20no-referrer%20to%20the%20response%20to%20further%20reduce%20XSS%20risk%C2%A0%5Bcite%5D%28https%3A//github.com/langgenius/dify/issues/28909%29.%0A%0AThere%20is%20an%20open%20PR%20to%20add%20these%20security%20headers%2C%20but%20it%20is%20not%20yet%20merged%C2%A0%5Bcite%5D%28https%3A//github.com/langgenius/dify/pull/28910%29.%20Until%20then%2C%20you%20can%20patch%20your%20deployment%20by%20updating%20the%20preview%20endpoint%20to%20always%20force%20download%20for%20HTML/SVG/XML%20and%20set%20the%20recommended%20headers.)&nbsp;[![Join Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share on X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/langgenius/dify/issues/30089)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#21255