Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix: maintenance update (#43897)

-LAN-8ac58b0e405c
diff --git a/api/.env.example b/api/.env.example
index a85466da2e8..75f0c8fdbfb 100644
--- a/api/.env.example
+++ b/api/.env.example
@@ -573,7 +573,8 @@ HTTP_REQUEST_NODE_SSL_VERIFY=True
# Webhook request configuration
WEBHOOK_REQUEST_BODY_MAX_SIZE=10485760
-# Respect X-* headers to redirect clients
+# Enable only behind a trusted proxy that overwrites X-Forwarded-For with the verified client IP.
+# Restrict direct API access to trusted callers; see docker/README.md#client-ip-and-trusted-proxies.
RESPECT_XFORWARD_HEADERS_ENABLED=false
# Log file path
diff --git a/api/configs/feature/__init__.py b/api/configs/feature/__init__.py
index 99818967812..80e26142786 100644
--- a/api/configs/feature/__init__.py
+++ b/api/configs/feature/__init__.py
@@ -765,7 +765,8 @@ class HttpConfig(BaseSettings):
RESPECT_XFORWARD_HEADERS_ENABLED: bool = Field(
description="Enable handling of X-Forwarded-For, X-Forwarded-Proto, and X-Forwarded-Port headers"
- " when the app is behind a single trusted reverse proxy.",
+ " when the app is behind a single trusted reverse proxy. Restrict API access to trusted callers"
+ " and have the proxy overwrite X-Forwarded-For with the verified client IP.",
default=False,
)
diff --git a/api/extensions/ext_proxy_fix.py b/api/extensions/ext_proxy_fix.py
index fe6685f633b..585e6f69de1 100644
--- a/api/extensions/ext_proxy_fix.py
+++ b/api/extensions/ext_proxy_fix.py
@@ -6,4 +6,4 @@ def init_app(app: DifyApp):
if dify_config.RESPECT_XFORWARD_HEADERS_ENABLED:
from werkzeug.middleware.proxy_fix import ProxyFix
- app.wsgi_app = ProxyFix(app.wsgi_app, x_port=1) # type: ignore[method-assign]
+ app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1, x_port=1) # type: ignore[method-assign]
diff --git a/api/libs/helper.py b/api/libs/helper.py
index 858f78d0281..613db1f074f 100644
--- a/api/libs/helper.py
+++ b/api/libs/helper.py
@@ -401,12 +401,8 @@ def generate_string(n):
def extract_remote_ip(request: Request) -> str:
- if request.headers.get("CF-Connecting-IP"):
- return cast(str, request.headers.get("CF-Connecting-IP"))
- elif request.headers.getlist("X-Forwarded-For"):
- return request.headers.getlist("X-Forwarded-For")[0]
- else:
- return cast(str, request.remote_addr)
+ """Use the WSGI address, normalized by opt-in trusted proxy middleware."""
+ return cast(str, request.remote_addr)
def generate_text_hash(text: str) -> str:
diff --git a/api/tests/test_containers_integration_tests/controllers/console/test_setup.py b/api/tests/test_containers_integration_tests/controllers/console/test_setup.py
index a38c1becd6f..f488a8860cd 100644
--- a/api/tests/test_containers_integration_tests/controllers/console/test_setup.py
+++ b/api/tests/test_containers_integration_tests/controllers/console/test_setup.py
@@ -55,7 +55,8 @@ def test_setup_endpoint_persists_bootstrap_state_and_rejects_repeat(
response = test_client_with_containers.post(
"/console/api/setup",
json=_setup_payload(email="[email protected]", password=password),
- headers={"CF-Connecting-IP": "203.0.113.7"},
+ headers={"CF-Connecting-IP": "198.51.100.7"},
+ environ_overrides={"REMOTE_ADDR": "203.0.113.7"},
)
assert response.status_code == 201
diff --git a/api/tests/unit_tests/controllers/console/test_fastopenapi_setup.py b/api/tests/unit_tests/controllers/console/test_fastopenapi_setup.py
index e88463ccac0..a0f0527fd9c 100644
--- a/api/tests/unit_tests/controllers/console/test_fastopenapi_setup.py
+++ b/api/tests/unit_tests/controllers/console/test_fastopenapi_setup.py
@@ -89,7 +89,8 @@ def test_console_setup_fastopenapi_post_success(
response = app.test_client().post(
"/console/api/setup",
json=payload,
- headers={"CF-Connecting-IP": "203.0.113.7"},
+ headers={"CF-Connecting-IP": "198.51.100.7"},
+ environ_overrides={"REMOTE_ADDR": "203.0.113.7"},
)
assert response.status_code == 201
diff --git a/api/tests/unit_tests/libs/test_helper.py b/api/tests/unit_tests/libs/test_helper.py
index d5f78e67abc..d8287a203bd 100644
--- a/api/tests/unit_tests/libs/test_helper.py
+++ b/api/tests/unit_tests/libs/test_helper.py
@@ -1,12 +1,100 @@
-from datetime import datetime
+from collections.abc import Callable
+from datetime import datetime, timedelta
+from unittest.mock import call, create_autospec
import pytest
-
-from libs.helper import OptionalTimestampField, alphanumeric, email, escape_like_pattern, extract_tenant_id
+from flask import request
+
+from dify_app import DifyApp
+from extensions import ext_proxy_fix
+from libs import rate_limit as rate_limit_module
+from libs.helper import (
+ OptionalTimestampField,
+ RateLimiter,
+ alphanumeric,
+ email,
+ escape_like_pattern,
+ extract_remote_ip,
+ extract_tenant_id,
+)
from models.account import Account
from models.model import EndUser
+ ("trust_proxy", "headers", "expected_ips", "expected_statuses"),
+ [
+ pytest.param(
+ False,
+ [
+ {},
+ {"CF-Connecting-IP": "203.0.113.1"},
+ {"X-Forwarded-For": "203.0.113.2, 192.0.2.42"},
+ ],
+ ["198.51.100.7"] * 3,
+ [200, 429, 429],
+ id="direct-client-ignores-untrusted-headers",
+ ),
+ pytest.param(
+ True,
+ [
+ {"CF-Connecting-IP": "203.0.113.1", "X-Forwarded-For": "192.0.2.42"},
+ {"CF-Connecting-IP": "203.0.113.2", "X-Forwarded-For": "203.0.113.3, 192.0.2.42"},
+ ],
+ ["192.0.2.42"] * 2,
+ [200, 429],
+ id="spoofed-headers-cannot-change-trusted-ip-bucket",
+ ),
+ pytest.param(
+ True,
+ [{"X-Forwarded-For": "192.0.2.42"}, {"X-Forwarded-For": "192.0.2.43"}],
+ ["192.0.2.42", "192.0.2.43"],
+ [200, 200],
+ id="canonical-visitors-have-distinct-buckets",
+ ),
+ pytest.param(
+ True,
+ [{"CF-Connecting-IP": "203.0.113.1"}, {"CF-Connecting-IP": "203.0.113.2"}],
+ ["198.51.100.7"] * 2,
+ [200, 429],
+ id="cloudflare-header-alone-is-not-trusted",
+ ),
+ ],
+)
+def test_extract_remote_ip_preserves_trusted_rate_limit_buckets(
+ config_overrides: Callable[..., None],
+ monkeypatch: pytest.MonkeyPatch,
+ trust_proxy: bool,
+ headers: list[dict[str, str]],
+ expected_ips: list[str],
+ expected_statuses: list[int],
+) -> None:
+ config_overrides(RESPECT_XFORWARD_HEADERS_ENABLED=trust_proxy)
+ app = DifyApp(__name__)
+ ext_proxy_fix.init_app(app)
+ used_keys: set[str] = set()
+ limiter = create_autospec(RateLimiter, instance=True, spec_set=True)
+ limiter.is_rate_limited.side_effect = lambda key: key in used_keys
+ limiter.increment_rate_limit.side_effect = used_keys.add
+ monkeypatch.setattr(rate_limit_module, "_build_limiter", lambda _spec: limiter)
+
+ @app.get("/")
+ @rate_limit_module.rate_limit(
+ rate_limit_module.RateLimit(limit=1, window=timedelta(minutes=1), scopes=(rate_limit_module.RateLimitScope.IP,))
+ )
+ def remote_ip() -> str:
+ return extract_remote_ip(request)
+
+ client = app.test_client()
+ for request_headers, expected_ip, expected_status in zip(headers, expected_ips, expected_statuses, strict=True):
+ response = client.get("/", headers=request_headers, environ_overrides={"REMOTE_ADDR": "198.51.100.7"})
+ assert response.status_code == expected_status
+ if expected_status == 200:
+ assert response.get_data(as_text=True) == expected_ip
+
+ assert limiter.is_rate_limited.call_args_list == [call(f"ip:{ip}") for ip in expected_ips]
+
+
class TestExtractTenantId:
"""Test cases for the extract_tenant_id utility function."""
diff --git a/docker/README.md b/docker/README.md
index 3f208427428..3576a82de0b 100644
--- a/docker/README.md
+++ b/docker/README.md
@@ -26,6 +26,120 @@ Run Dify with Docker Compose. Configuration templates live in `.env.example` and
- Copy `envs/core-services/shared.env.example` to `envs/core-services/shared.env`.
- Set `ENABLE_OTEL=true` and configure `OTLP_BASE_ENDPOINT`. Tune the other `OTEL_*` knobs in the same file if needed.
+## Client IP and trusted proxies
+
+Dify uses the request's resolved client IP for rate limits and audit records. The
+bundled Nginx overwrites `X-Forwarded-For` with `$remote_addr`, and Compose enables
+`RESPECT_XFORWARD_HEADERS_ENABLED` for the API and WebSocket API services. Dify's
+proxy middleware trusts one forwarded value, so different visitors keep separate
+IP buckets while caller-supplied IP headers cannot override Nginx's address.
+
+The middleware does not authenticate the proxy. Keep the API port private and
+restrict direct API connections to trusted callers. Compose does not publish that
+port, but services on its shared Docker networks can still connect directly,
+including the code sandbox on `ssrf_proxy_network`. Those connections bypass
+Nginx's header replacement. If those workloads are untrusted, enforce network or
+firewall isolation and route their API calls through a sanitizing gateway before
+enabling forwarded-header trust. Merely setting a Docker network to `internal`
+does not isolate containers from each other.
+
+For a directly exposed API, leave `RESPECT_XFORWARD_HEADERS_ENABLED=false`.
+The Python configuration and `api/.env.example` retain that default. To override
+the Compose default, set the variable in `docker/.env`, the shell used by Compose,
+or a Compose override. The API services' explicit `environment` entry takes
+precedence over `envs/core-services/shared.env`.
+Existing `false` overrides in `docker/.env`, the invoking shell, or a Compose
+override remain in effect during upgrades. Move settings previously supplied only
+by `shared.env` to one of those override locations if you need to preserve them.
+After establishing the trusted ingress boundary, enable the setting to retain
+visitor IPs.
+
+### Cloudflare or another load balancer before Nginx
+
+With an upstream proxy, configure Nginx to resolve the visitor address before
+forwarding it. Otherwise `$remote_addr` is the upstream proxy's address, and its
+visitors share a rate-limit bucket. Add an operator-managed
+`docker/nginx/conf.d/real-ip.conf`; the existing mount and Nginx include load this
+file without changes to the entrypoint.
+
+For a load balancer that supplies a correctly maintained `X-Forwarded-For` chain:
+
+```nginx
+# Replace this documentation address with the actual trusted proxy address/CIDR.
+set_real_ip_from 192.0.2.10/32;
+real_ip_header X-Forwarded-For;
+real_ip_recursive on;
+```
+
+For Cloudflare connecting directly to Nginx, use `real_ip_header CF-Connecting-IP`
+and `set_real_ip_from` entries for Cloudflare's current published IPv4 and IPv6
+ranges. Restrict origin access to the intended Cloudflare path; a Tunnel or
+Authenticated Origin Pulls with your own certificate can authenticate your zone.
+Cloudflare IP ranges alone identify its network, not your zone. If another load
+balancer sits between Cloudflare and Nginx, validate the source and sanitize
+headers at that load balancer too. Do not trust an arbitrary Cloudflare header
+merely because an internal load balancer forwarded it.
+
+Never use `0.0.0.0/0`, `::/0`, or an entire untrusted workload network as the trusted
+proxy range. Reload Nginx after updating its configuration. See the
+[Nginx real-IP module](https://nginx.org/en/docs/http/ngx_http_realip_module.html),
+[Cloudflare IP ranges](https://www.cloudflare.com/ips/), and
+[Cloudflare origin protection](https://developers.cloudflare.com/fundamentals/security/protect-your-origin-server/).
+
+### Kubernetes
+
+Use stable workload selectors to restrict API access; individual ingress pod IPs
+do not need to be configured in Dify. The ingress must resolve and overwrite the
+forwarded client IP as above. Keep the API Service as `ClusterIP`, set
+`RESPECT_XFORWARD_HEADERS_ENABLED=true` on its Deployment, and enforce a
+NetworkPolicy on the API pods. `ClusterIP` alone does not restrict other pods.
+
+This example must be adapted to your namespaces, pod labels, and API port:
+
+```yaml
+apiVersion: networking.k8s.io/v1
+kind: NetworkPolicy
+metadata:
+ name: api-from-ingress
+ namespace: dify
+spec:
+ podSelector:
+ matchLabels:
+ app: dify-api
+ policyTypes:
+ - Ingress
+ ingress:
+ - from:
+ - namespaceSelector:
+ matchLabels:
+ kubernetes.io/metadata.name: ingress-system
+ podSelector:
+ matchLabels:
+ app: ingress-controller
+ ports:
+ - protocol: TCP
+ port: 5001
+```
+
+Both selectors belong to the same `from` entry, so the namespace **and** pod labels
+must match. Keep that namespace restricted to trusted workloads. Confirm that
+your CNI enforces NetworkPolicy, no other policy broadly allows API access, and
+any `hostNetwork` ingress is covered by your CNI's policy behavior. Allow rules
+from multiple policies are additive. See the
+[Kubernetes NetworkPolicy documentation](https://kubernetes.io/docs/concepts/services-networking/network-policies/).
+
+Inventory internal API callers before applying the policy. Route untrusted
+callers through the same sanitizing ingress; directly allowed callers can supply
+forwarded headers too. Internal requests without a verified visitor address are
+attributed to the calling service, not an end user. Preserve required internal
+service connectivity while enforcing this boundary.
+
+Before rollout, verify that two distinct visitor IPs use different limit buckets,
+changing client-supplied IP headers does not change the resolved address, and an
+untrusted pod cannot connect directly to the API. IP limits still need independent
+account/email limits because NAT can share public addresses and attackers can
+use multiple real addresses.
+
## How to Deploy Middleware for Developing Dify
1. **Middleware Setup**:
diff --git a/docker/docker-compose-template.yaml b/docker/docker-compose-template.yaml
index ca15364a483..2007761efca 100644
--- a/docker/docker-compose-template.yaml
+++ b/docker/docker-compose-template.yaml
@@ -223,6 +223,8 @@ services:
image: langgenius/dify-api:1.17.1
environment:
MODE: api
+ # The bundled Nginx supplies a verified client IP; keep direct API callers trusted.
+ RESPECT_XFORWARD_HEADERS_ENABLED: ${RESPECT_XFORWARD_HEADERS_ENABLED:-true}
SENTRY_DSN: ${API_SENTRY_DSN:-}
SENTRY_TRACES_SAMPLE_RATE: ${API_SENTRY_TRACES_SAMPLE_RATE:-1.0}
SENTRY_PROFILES_SAMPLE_RATE: ${API_SENTRY_PROFILES_SAMPLE_RATE:-1.0}
@@ -273,6 +275,7 @@ services:
- collaboration
environment:
MODE: api
+ RESPECT_XFORWARD_HEADERS_ENABLED: ${RESPECT_XFORWARD_HEADERS_ENABLED:-true}
SERVER_WORKER_AMOUNT: ${API_WEBSOCKET_WORKER_AMOUNT:-1}
SERVER_WORKER_CLASS: ${API_WEBSOCKET_WORKER_CLASS:-geventwebsocket.gunicorn.workers.GeventWebSocketWorker}
SERVER_WORKER_CONNECTIONS: ${API_WEBSOCKET_WORKER_CONNECTIONS:-1000}
diff --git a/docker/docker-compose.yaml b/docker/docker-compose.yaml
index f8124db0114..99e89d317a5 100644
--- a/docker/docker-compose.yaml
+++ b/docker/docker-compose.yaml
@@ -229,6 +229,8 @@ services:
image: langgenius/dify-api:1.17.1
environment:
MODE: api
+ # The bundled Nginx supplies a verified client IP; keep direct API callers trusted.
+ RESPECT_XFORWARD_HEADERS_ENABLED: ${RESPECT_XFORWARD_HEADERS_ENABLED:-true}
SENTRY_DSN: ${API_SENTRY_DSN:-}
SENTRY_TRACES_SAMPLE_RATE: ${API_SENTRY_TRACES_SAMPLE_RATE:-1.0}
SENTRY_PROFILES_SAMPLE_RATE: ${API_SENTRY_PROFILES_SAMPLE_RATE:-1.0}
@@ -279,6 +281,7 @@ services:
- collaboration
environment:
MODE: api
+ RESPECT_XFORWARD_HEADERS_ENABLED: ${RESPECT_XFORWARD_HEADERS_ENABLED:-true}
SERVER_WORKER_AMOUNT: ${API_WEBSOCKET_WORKER_AMOUNT:-1}
SERVER_WORKER_CLASS: ${API_WEBSOCKET_WORKER_CLASS:-geventwebsocket.gunicorn.workers.GeventWebSocketWorker}
SERVER_WORKER_CONNECTIONS: ${API_WEBSOCKET_WORKER_CONNECTIONS:-1000}
diff --git a/docker/envs/core-services/shared.env.example b/docker/envs/core-services/shared.env.example
index 8e07ebef499..bbb56f547fa 100644
--- a/docker/envs/core-services/shared.env.example
+++ b/docker/envs/core-services/shared.env.example
@@ -242,6 +242,9 @@ GRAPH_ENGINE_SCALE_DOWN_IDLE_TIME=5.0
ALIYUN_SLS_ACCESS_KEY_ID=
ALIYUN_SLS_ACCESS_KEY_SECRET=
WEBHOOK_REQUEST_BODY_MAX_SIZE=10485760
+# Bare API default. Compose enables this for its API services behind Nginx.
+# Override Compose through docker/.env, the shell, or a Compose override, not this env_file.
+# See docker/README.md#client-ip-and-trusted-proxies before exposing the API directly.
RESPECT_XFORWARD_HEADERS_ENABLED=false
SSRF_HTTP_PORT=3128
SSRF_COREDUMP_DIR=/var/spool/squid
diff --git a/docker/nginx/proxy.conf.template b/docker/nginx/proxy.conf.template
index 117f8061464..e30c8ece8a9 100644
--- a/docker/nginx/proxy.conf.template
+++ b/docker/nginx/proxy.conf.template
@@ -1,7 +1,8 @@
# Please do not directly edit this file. Instead, modify the .env variables related to NGINX configuration.
proxy_set_header Host $host;
-proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+# real_ip configuration must validate upstream proxies before replacing $remote_addr.
+proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port $server_port;
proxy_http_version 1.1;