langgenius / langgenius/dify

[Refactor/Chore] Serve stored SVG/XML file responses inert (nosniff + sandbox + attachment)

Open
#41,034 1 comment 1 reaction 1 assignee Claimed by @kah-ja View on GitHub
Dominant language
TypeScript
Stars
156k
Forks
24.6k
Avg merge
22h 9m
Merged PRs (30d)
610

Description

### 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 refactors or chores; 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.

### Description

Dify serves stored user files with their original `Content-Type`. HTML is already forced to download, but `image/svg+xml` and XML files are still served inline, so a browser can execute scripts embedded in an SVG in the app origin instead of treating the file as an inert image.

Proposed change: a `harden_served_file` helper in `api/controllers/common/file_response.py`, applied across the file-serving endpoints. It always sets `X-Content-Type-Options: nosniff`; for SVG/XML it keeps the `Content-Type` (so `` embedding still renders) and adds `Content-Disposition: attachment` plus `Content-Security-Policy: default-src 'none'; style-src 'unsafe-inline'; sandbox`. `` rendering is unchanged; direct navigation downloads the file instead of executing it.

Context: this is a security hardening, tracked privately as GHSA-6w5x-w2p4-7837 (https://github.com/langgenius/dify/security/advisories/GHSA-6w5x-w2p4-7837, visible to maintainers). Fix PR #39834 was closed on 2026-08-14 for lacking a linked issue (CONTRIBUTING.md). This issue exists to satisfy that so the PR can be re-opened and linked (`Fixes #`). Per this repo's issue config, exploit specifics are kept out of this public issue and remain in the advisory. Research and patch by turingpoint (https://www.turingpoint.de).

Endpoints touched: `image_preview`, `file_preview` (service_api), `tool_files`, `agent_drive_archive`, and the console model/tool/plugin provider icons. Unit tests added for the helper and image-preview.

### Motivation

_No response_

### Additional Context

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.