mirror of
https://github.com/langgenius/dify.git
synced 2026-01-19 11:45:05 +08:00
fix: allow unauthenticated CORS preflight for embedded bots (#30587)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
@ -6,6 +6,7 @@ BASE_CORS_HEADERS: tuple[str, ...] = ("Content-Type", HEADER_NAME_APP_CODE, HEAD
|
|||||||
SERVICE_API_HEADERS: tuple[str, ...] = (*BASE_CORS_HEADERS, "Authorization")
|
SERVICE_API_HEADERS: tuple[str, ...] = (*BASE_CORS_HEADERS, "Authorization")
|
||||||
AUTHENTICATED_HEADERS: tuple[str, ...] = (*SERVICE_API_HEADERS, HEADER_NAME_CSRF_TOKEN)
|
AUTHENTICATED_HEADERS: tuple[str, ...] = (*SERVICE_API_HEADERS, HEADER_NAME_CSRF_TOKEN)
|
||||||
FILES_HEADERS: tuple[str, ...] = (*BASE_CORS_HEADERS, HEADER_NAME_CSRF_TOKEN)
|
FILES_HEADERS: tuple[str, ...] = (*BASE_CORS_HEADERS, HEADER_NAME_CSRF_TOKEN)
|
||||||
|
EMBED_HEADERS: tuple[str, ...] = ("Content-Type", HEADER_NAME_APP_CODE)
|
||||||
EXPOSED_HEADERS: tuple[str, ...] = ("X-Version", "X-Env", "X-Trace-Id")
|
EXPOSED_HEADERS: tuple[str, ...] = ("X-Version", "X-Env", "X-Trace-Id")
|
||||||
|
|
||||||
|
|
||||||
@ -42,10 +43,28 @@ def init_app(app: DifyApp):
|
|||||||
|
|
||||||
_apply_cors_once(
|
_apply_cors_once(
|
||||||
web_bp,
|
web_bp,
|
||||||
resources={r"/*": {"origins": dify_config.WEB_API_CORS_ALLOW_ORIGINS}},
|
resources={
|
||||||
supports_credentials=True,
|
# Embedded bot endpoints (unauthenticated, cross-origin safe)
|
||||||
allow_headers=list(AUTHENTICATED_HEADERS),
|
r"^/chat-messages$": {
|
||||||
methods=["GET", "PUT", "POST", "DELETE", "OPTIONS", "PATCH"],
|
"origins": dify_config.WEB_API_CORS_ALLOW_ORIGINS,
|
||||||
|
"supports_credentials": False,
|
||||||
|
"allow_headers": list(EMBED_HEADERS),
|
||||||
|
"methods": ["GET", "POST", "OPTIONS"],
|
||||||
|
},
|
||||||
|
r"^/chat-messages/.*": {
|
||||||
|
"origins": dify_config.WEB_API_CORS_ALLOW_ORIGINS,
|
||||||
|
"supports_credentials": False,
|
||||||
|
"allow_headers": list(EMBED_HEADERS),
|
||||||
|
"methods": ["GET", "POST", "OPTIONS"],
|
||||||
|
},
|
||||||
|
# Default web application endpoints (authenticated)
|
||||||
|
r"/*": {
|
||||||
|
"origins": dify_config.WEB_API_CORS_ALLOW_ORIGINS,
|
||||||
|
"supports_credentials": True,
|
||||||
|
"allow_headers": list(AUTHENTICATED_HEADERS),
|
||||||
|
"methods": ["GET", "PUT", "POST", "DELETE", "OPTIONS", "PATCH"],
|
||||||
|
},
|
||||||
|
},
|
||||||
expose_headers=list(EXPOSED_HEADERS),
|
expose_headers=list(EXPOSED_HEADERS),
|
||||||
)
|
)
|
||||||
app.register_blueprint(web_bp)
|
app.register_blueprint(web_bp)
|
||||||
|
|||||||
Reference in New Issue
Block a user