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(api): require exact match for CSRF whitelist paths (#42118)

Soroush Ahmadia425a127f5ce
diff --git a/api/libs/token.py b/api/libs/token.py
index 1b28b60a538..9fd684fe305 100644
--- a/api/libs/token.py
+++ b/api/libs/token.py
@@ -204,7 +204,7 @@ def check_csrf_token(request: Request, user_id: str):
raise Unauthorized("CSRF token is missing or invalid.")
for pattern in CSRF_WHITE_LIST:
- if pattern.match(request.path):
+ if pattern.fullmatch(request.path):
return
csrf_token = extract_csrf_token(request)
diff --git a/api/tests/unit_tests/libs/test_token.py b/api/tests/unit_tests/libs/test_token.py
index 09e213651d4..b8c737bc11a 100644
--- a/api/tests/unit_tests/libs/test_token.py
+++ b/api/tests/unit_tests/libs/test_token.py
@@ -100,6 +100,38 @@ def test_non_whitelisted_path_requires_csrf():
token.check_csrf_token(request, "account-1")
+def test_exact_workflow_draft_path_bypasses_csrf():
+ request = cast(
+ Request,
+ MockRequest(
+ headers={},
+ cookies={},
+ args={},
+ path="/console/api/apps/5923ce20-2914-44af-45f0-580fb49f1cc9/workflows/draft",
+ ),
+ )
+
+ token.check_csrf_token(request, "account-1")
+
+
+ "path",
+ [
+ "/console/api/apps/5923ce20-2914-44af-45f0-580fb49f1cc9/workflows/draft/variables",
+ "/console/api/apps/5923ce20-2914-44af-45f0-580fb49f1cc9/workflows/draft/nodes/node-1/variables",
+ "/console/api/apps/5923ce20-2914-44af-45f0-580fb49f1cc9/workflows/draft/conversation-variables",
+ "/console/api/apps/5923ce20-2914-44af-45f0-580fb49f1cc9/workflows/draft/iteration/nodes/node-1/run",
+ "/console/api/apps/5923ce20-2914-44af-45f0-580fb49f1cc9/workflows/draft/loop/nodes/node-1/run",
+ "/console/api/workflow-run-archives/downloads/5923ce20291444af45f0580fb49f1cc9/file/extra",
+ ],
+)
+def test_whitelist_prefix_paths_still_require_csrf(path: str):
+ request = cast(Request, MockRequest(headers={}, cookies={}, args={}, path=path))
+
+ with pytest.raises(Unauthorized):
+ token.check_csrf_token(request, "account-1")
+
+
def test_admin_api_key_header_bypasses_csrf_when_console_cookie_is_present(config_overrides):
config_overrides(
ADMIN_API_KEY_ENABLE=True,